pandas-ai icon indicating copy to clipboard operation
pandas-ai copied to clipboard

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

Open leungdunn opened this issue 11 months ago • 6 comments

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'

leungdunn avatar Apr 05 '24 17:04 leungdunn

Got the same issue, any help is appreciated @gventuri

Emad-Eldin-G avatar May 20 '24 22:05 Emad-Eldin-G

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

gventuri avatar May 23 '24 17:05 gventuri

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 avatar May 23 '24 17:05 Emad-Eldin-G

@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)

Emad-Eldin-G avatar May 23 '24 17:05 Emad-Eldin-G

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.

xuyuanme avatar Jun 07 '24 06:06 xuyuanme

Following, I am also facing this issue when using GPT3.5

Jamesetay1 avatar Jul 04 '24 09:07 Jamesetay1