Sinaptik-AI / pandas-ai

Chat with your database (SQL, CSV, pandas, polars, mongodb, noSQL, etc). PandasAI makes data analysis conversational using LLMs (GPT 3.5 / 4, Anthropic, VertexAI) and RAG.
https://pandas-ai.com
Other
13.61k stars 1.32k forks source link

error: [Errno 35] Resource temporarily unavailable on import pandasai running with streamlit on macOS #291

Closed kneelesh48 closed 1 year ago

kneelesh48 commented 1 year ago

🐛 Describe the bug

import pandas as pd
import streamlit as st
from pandasai import PandasAI
streamlit run pandas_ai_streamlit.py

This returns the following error error: [Errno 35] Resource temporarily unavailable: '/opt/homebrew/lib/python3.10/site-packages/cache/cache'

Trackback:

File "/opt/homebrew/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "/Users/neeleshkumar/Library/CloudStorage/OneDrive-Personal/Code/Python/PandasAI/pandas_ai_streamlit.py", line 5, in <module>
    from pandasai import PandasAI
File "/opt/homebrew/lib/python3.10/site-packages/pandasai/__init__.py", line 68, in <module>
    class PandasAI:
File "/opt/homebrew/lib/python3.10/site-packages/pandasai/__init__.py", line 118, in PandasAI
    _cache: Cache = Cache()
File "/opt/homebrew/lib/python3.10/site-packages/pandasai/helpers/cache.py", line 24, in __init__
    self.cache = shelve.open(self.filepath)
File "/opt/homebrew/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shelve.py", line 243, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
File "/opt/homebrew/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shelve.py", line 227, in __init__
    Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
File "/opt/homebrew/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dbm/__init__.py", line 95, in open
    return mod.open(file, flag, mode)

Expected behaviour: This should not happen. I tested it on Windows and this error doesn't happen on Windows

kneelesh48 commented 1 year ago

It happens even when I set enable_cache=False

gventuri commented 1 year ago

@kneelesh48 please check it out, should have been fixed with version 0.5.5. Feel free to reopen if the problem persists!

kneelesh48 commented 1 year ago

Hey, that's great, everything works as expected, even in streamlit. Thank you so much for your work.

There is still a small bug while running in streamlit on macOS I still get the cache error when I set enable_cache=True when I enter the prompt the second time, the first time it works fine. I don't get any error if I set enable_cache=False

Here's the entire streamlit code

import os

import pandas as pd
import streamlit as st
from pandasai import PandasAI
from pandasai.middlewares.streamlit import StreamlitMiddleware
from dotenv import load_dotenv

load_dotenv()

from pandasai.llm.openai import OpenAI

llm = OpenAI(api_token=os.environ["OPENAI_API_TOKEN"])

# pandas_ai = PandasAI(llm, verbose=True, enable_cache=True)
pandas_ai = PandasAI(llm, verbose=True, enable_cache=False, middlewares=[StreamlitMiddleware()])

uploaded_file = st.file_uploader("Upload a file", type=['csv', 'xlsx'])
if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
else:
    df = pd.DataFrame({
        "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
        "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
        "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
    })

with st.expander("Show DataFrame"):
    st.dataframe(df.head())

prompt = 'Which are the 5 happiest countries?'
# prompt = 'Plot a bar chart of gdp and happines index of each country'
# prompt = "Plot the histogram of countries showing for each the gdp, using different colors for each bar"
prompt = st.text_input("Enter prompt", value=prompt)

response = pandas_ai(df, prompt=prompt)
if response is not None:
    st.write(response)

And here's the error error: [Errno 35] Resource temporarily unavailable: '/Users/neeleshkumar/Library/CloudStorage/OneDrive-Personal/Code/Python/PandasAI/cache/cache'

Traceback

File "/opt/homebrew/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "/Users/neeleshkumar/Library/CloudStorage/OneDrive-Personal/Code/Python/PandasAI/pandas_ai_streamlit.py", line 24, in <module>
    pandas_ai = PandasAI(llm, verbose=True, enable_cache=True, middlewares=[StreamlitMiddleware()])
File "/opt/homebrew/lib/python3.10/site-packages/pandasai/__init__.py", line 192, in __init__
    self._cache = Cache()
File "/opt/homebrew/lib/python3.10/site-packages/pandasai/helpers/cache.py", line 22, in __init__
    self.cache = shelve.open(self.filepath)
File "/opt/homebrew/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shelve.py", line 243, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
File "/opt/homebrew/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shelve.py", line 227, in __init__
    Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
File "/opt/homebrew/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dbm/__init__.py", line 95, in open
    return mod.open(file, flag, mode)
felipmorais commented 1 year ago

Any updates on that? I am having the same error as @kneelesh48 .

gventuri commented 1 year ago

@kneelesh48 @felipmorais I think this could be related to the permissions of the cache folder. Are you running it inside docker? One suggestion that I can give you is to create a /cache folder yourself with the right permissions.

Let me know!

noamsiegel commented 1 year ago

@gventuri I'm having the same issue running it in a docker container -- what do you mean create my own cache folder with the right permissions? The cache folder seems to be automatically generated.

This is my code:

pandas_ai = PandasAI(
    llm,
    enable_cache=False,
    enforce_privacy=True,
    conversational=True,
    middlewares=[StreamlitMiddleware()],
)
argbbai commented 1 year ago

Also having this problem in a docker container.

karkeranikitha commented 1 year ago

Same problem is happening in Docker container. Its working now after setting enable_cache=False But the plots are not getting displayed in the streamlit application via docker.

Same thing works when executed outside docker but not in docker

RoyKulik commented 1 year ago

I keep getting this error (simply running as .py script). Tried to create the directory myself and changing directory and file permissions and the problem persists.

aiakubovich commented 1 year ago

same problem

Shudh commented 1 year ago

Yes same problem.. on ubuntu desktop _gdbm.error: [Errno 11] Resource temporarily unavailable: '/home/shudh/PycharmProjects/pythonProject/pandas-ai/cache/cache' .. gave permissions and retried but still the same issue .. removed cache manually and re ran ..but still same issue..

Shudh commented 1 year ago

Ok I can confirm that it does not happen when I disable cache..but I don't want to disable cache as I am running low on OPENAI credits :) ... image