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
11.71k stars 1.09k forks source link

FileNotFoundError: [Errno 2] No such file or directory: 'path/to/project/exports/charts/temp_chart.png' #1092

Open leungdunn opened 2 months ago

leungdunn commented 2 months ago

System Info

OS version: macOS Sonoma 14.4.1 Python version: 3.10.12 pandasai version: 2.0.28

🐛 Describe the bug

It cannot go through when using the Agent to plot pictures. It is okay if the question response is supposed to be text.

Below is my code:

import os

from dotenv import load_dotenv
import pandas as pd
from pandasai import Agent

load_dotenv()

DEFAULT_PICTURE_FOLDER = "./exports/charts"

# create picture folder if necessary
if not os.path.isdir(DEFAULT_PICTURE_FOLDER):
    os.makedirs(DEFAULT_PICTURE_FOLDER)

df = pd.DataFrame({
    "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
    "sales": [5000, 3200, 2900, 4100, 2300, 2100, 2500, 2600, 4500, 7000]
})

agent = Agent(
    df,
    config={
        "verbose": True,
        "enforce_privacy": True,
        "enable_cache": True,
        "conversational": False,
        "save_charts": False,
        "open_charts": False,
        "save_charts_path": DEFAULT_PICTURE_FOLDER,
    },
)

qeury = "Plot the histogram of countries in Europe showing for each the gdp, using different colors for each bar"
response = agent.chat(qeury)

full traceback is as below

Traceback (most recent call last):
  File "/Users/liaden/miniconda3/envs/genai/lib/python3.10/site-packages/pandasai/pipelines/chat/generate_chat_pipeline.py", line 283, in run
    output = (self.code_generation_pipeline | self.code_execution_pipeline).run(
  File "/Users/liaden/miniconda3/envs/genai/lib/python3.10/site-packages/pandasai/pipelines/pipeline.py", line 137, in run
    raise e
  File "/Users/liaden/miniconda3/envs/genai/lib/python3.10/site-packages/pandasai/pipelines/pipeline.py", line 101, in run
    step_output = logic.execute(
  File "/Users/liaden/miniconda3/envs/genai/lib/python3.10/site-packages/pandasai/pipelines/chat/code_execution.py", line 134, in execute
    {"content_type": "response", "value": ResponseSerializer.serialize(result)},
  File "/Users/liaden/miniconda3/envs/genai/lib/python3.10/site-packages/pandasai/responses/response_serializer.py", line 29, in serialize
    with open(result["value"], "rb") as image_file:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/liaden/genai_streamlitapp/exports/charts/temp_chart.png'
Emad-Eldin-G commented 1 month ago

Got the same issue, any help is appreciated @gventuri

gventuri commented 1 month ago

Does it work if you remove the save_charts_path? Did you check whether the folder actually exists and is created?

Emad-Eldin-G commented 1 month ago

Does it work if you remove the save_charts_path? Did you check whether the folder actually exists and is created?

I did recreate @leungdunn 's code to try and fix the error on my side, and I can say that the issue persists with or without specifying save_charts_path.

The weird thing is that the directory or folder itself is created (exports/charts/), but the png file for the chart output itself is not created.

Emad-Eldin-G commented 1 month ago

@leungdunn try to set open_charts to True, this will create the plot png file, which can then be accessed via the returned path.

Note that turning it to True also opens the file automatically.

Also try setting save_charts to True, this should create the plot and save it without opening it (Although this creates some unexpected behaviour, @gventuri check issue #1175)

xuyuanme commented 3 weeks ago

The issue is because the original "save as png" prompt could be omitted by GPT3.5 or BambooLLM. Switching to GPT4 API would eliminate the issue.

Raised a PR https://github.com/Sinaptik-AI/pandas-ai/pull/1214 to fix this issue.