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
12.68k stars 1.23k forks source link

Unable to save chart image, or setting not to save chart will throw error "No such file or directory" #1223

Closed wuhuanyan closed 2 months ago

wuhuanyan commented 3 months ago

System Info

OS version: win 10 22h2 Python version: 3.12.3 The current version of pandasai being used: 2.1.1

🐛 Describe the bug

Originally, these codes were working properly. But since I specified running parameters such as "open_charts" and "save_charts"*, I found that the parameters seemed to be ineffective. I kept throwing errors afterwards, even deleting all the parameters and deleting the "charts" and "exports" folders(The folder will be rebuilt), but it still didn't work properly. car.csv

import pandas as pd
from pandasai import SmartDataframe
from os.path import dirname, join
from os import environ
from datetime import datetime

api_key = environ.get('PANDASAI_API_KEY')
if api_key is None:
    api_key = ''  # todo
    environ.setdefault('PANDASAI_API_KEY', api_key)

base_path = dirname(__file__)
files_path = join(base_path, "files")
csv_path = join(base_path, "car.csv")

data = pd.read_csv(csv_path)
ai_data = SmartDataframe(data)
print('ok')

respone = ai_data.chat('生成马力与油耗关系图?')  # Generate a graph of the relationship between horsepower and fuel consumption?
print(respone)
Traceback (most recent call last):
  File "D:\anaconda3\envs\zyzs-ai\Lib\site-packages\pandasai\pipelines\chat\generate_chat_pipeline.py", line 310, in run
    ).run(input)
      ^^^^^^^^^^
  File "D:\anaconda3\envs\zyzs-ai\Lib\site-packages\pandasai\pipelines\pipeline.py", line 137, in run
    raise e
  File "D:\anaconda3\envs\zyzs-ai\Lib\site-packages\pandasai\pipelines\pipeline.py", line 101, in run
    step_output = logic.execute(
                  ^^^^^^^^^^^^^^
  File "D:\anaconda3\envs\zyzs-ai\Lib\site-packages\pandasai\pipelines\chat\code_execution.py", line 133, in execute
    {"content_type": "response", "value": ResponseSerializer.serialize(result)},
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\anaconda3\envs\zyzs-ai\Lib\site-packages\pandasai\responses\response_serializer.py", line 35, in serialize
    with open(result["value"], "rb") as image_file:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'D:/Projects/zyzs-ai/exports/charts/temp_chart.png'
Unfortunately, I was not able to answer your question, because of the following error:

[Errno 2] No such file or directory: 'D:/Projects/zyzs-ai/exports/charts/temp_chart.png'

*Code marked with an asterisk:


ai_df = SmartDataframe(df, config={
                    'open_charts': True,  # or False
                    'save_charts': True,  # or False
                    'save_charts_path': join(project_path, 'exports', 'charts', datetime.now().strftime('%Y-%m-%d')),
                    'enable_cache': False
                })
dosubot[bot] commented 3 months ago

@wuhuanyan Hello there! I'm here to help you with any bugs, questions, or contributions. Let me know how I can assist you!

To resolve the "No such file or directory" error when trying to save or not save a chart image using pandasai, you can ensure that the directory exists before attempting to save the file. Here is an example of how you can modify your code:


import os
import pandas as pd
from pandasai import SmartDataframe
from os.path import dirname, join
from os import environ
from datetime import datetime

api_key = environ.get('PANDASAI_API_KEY')
if api_key is None:
    api_key = ''  # todo
    environ.setdefault('PANDASAI_API_KEY', api_key)

base_path = dirname(__file__)
files_path = join(base_path, "files")
csv_path = join(base_path, "car.csv")

data = pd.read_csv(csv_path)

# Define the path where you want to save the chart image
save_charts_path = join(base_path, 'exports', 'charts', datetime.now().strftime('%Y-%m-%d'))

# Ensure the directory exists
os.makedirs(save_charts_path, exist_ok=True)

ai_data = SmartDataframe(data, config={
    'open_charts': True,  # or False
    'save_char

<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
wuhuanyan commented 3 months ago

I have ensured that the saved directory has been created. Alternatively, if I set the "save_charts" parameter to "False", it should not be affected by the existence of the directory. But it will still throw an error stating that the directory does not exist.

image

image

import os
import pandas as pd
from pandasai import SmartDataframe
from os.path import dirname, join
from os import environ
from datetime import datetime

api_key = environ.get('PANDASAI_API_KEY')
if api_key is None:
    api_key = ''  # todo
    environ.setdefault('PANDASAI_API_KEY', api_key)

base_path = dirname(__file__)
files_path = join(base_path, "files")
csv_path = join(base_path, files_path, "car.csv")

data = pd.read_csv(csv_path)

# Define the path where you want to save the chart image
save_charts_path = join(base_path, 'exports', 'charts', datetime.now().strftime('%Y-%m-%d'))

# Ensure the directory exists
os.makedirs(save_charts_path, exist_ok=True)

ai_data = SmartDataframe(data)
respone = ai_data.chat('生成马力与油耗关系图?')
print(respone)
xuyuanme commented 3 months ago

This is related to issue #1092