Arafath10 commited on
Commit
6122003
1 Parent(s): 7dff946

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -1
main.py CHANGED
@@ -6,6 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware
6
  import pandas as pd
7
  from io import StringIO
8
  import os
 
9
 
10
  from pandasai import SmartDataframe
11
  import pandas as pd
@@ -23,17 +24,32 @@ app.add_middleware(
23
  allow_headers=["*"],
24
  )
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  @app.post("/get_image_for_text")
27
  async def get_image_for_text(email,query,file: UploadFile = File(...)):
28
  print(file.filename)
29
  with open(email+".csv", "wb") as file_object:
30
  file_object.write(file.file.read())
 
31
  llm = OpenAI(api_token=secret,save_charts=True)
32
  try:
33
  df = pd.read_csv(email+".csv")
34
  sdf = SmartDataframe(df, config={"llm": llm})
35
  sdf.chat(query)
36
  image_path = "exports/charts/temp_chart.png" # Replace with your image's path
37
- return FileResponse(image_path)
 
38
  except:
39
  return "try again"
 
6
  import pandas as pd
7
  from io import StringIO
8
  import os
9
+ import uuid
10
 
11
  from pandasai import SmartDataframe
12
  import pandas as pd
 
24
  allow_headers=["*"],
25
  )
26
 
27
+ import base64
28
+ from PIL import Image
29
+ from io import BytesIO
30
+
31
+ def convert_image_to_base64(image_path):
32
+ with Image.open(image_path) as image:
33
+ buffered = BytesIO()
34
+ image.save(buffered, format="PNG")
35
+ img_bytes = buffered.getvalue()
36
+ img_base64 = base64.b64encode(img_bytes)
37
+ img_base64_string = img_base64.decode("utf-8")
38
+ return img_base64_string
39
+
40
  @app.post("/get_image_for_text")
41
  async def get_image_for_text(email,query,file: UploadFile = File(...)):
42
  print(file.filename)
43
  with open(email+".csv", "wb") as file_object:
44
  file_object.write(file.file.read())
45
+ uuid1 = uuid.uuid1()
46
  llm = OpenAI(api_token=secret,save_charts=True)
47
  try:
48
  df = pd.read_csv(email+".csv")
49
  sdf = SmartDataframe(df, config={"llm": llm})
50
  sdf.chat(query)
51
  image_path = "exports/charts/temp_chart.png" # Replace with your image's path
52
+ base64str = convert_image_to_base64(image_path)
53
+ return {"id":str(uuid1),"image":base64str}
54
  except:
55
  return "try again"