Spaces:
Sleeping
Sleeping
File size: 3,356 Bytes
d92c861 6122003 31ba98a 005c659 d92c861 6122003 005c659 6122003 d2e31be 098faa2 4b6b0a9 07fe890 4b6b0a9 07fe890 d92c861 fed63e7 d92c861 6122003 d92c861 fed63e7 e782b03 e5e2728 e782b03 e5e2728 d45bf32 e5e2728 e782b03 a152f92 7dff946 a152f92 e782b03 a152f92 e782b03 6122003 2e1190c a940aa8 7dff946 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.responses import StreamingResponse
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from io import StringIO
import os
import uuid
# from apscheduler.schedulers.background import BackgroundScheduler
# import googletrans
# from googletrans import Translator
# translator = Translator()
# lan = googletrans.LANGUAGES
# #print(lan)
# keys = list(lan.keys())
# vals = list(lan.values())
from pandasai import SmartDataframe
import pandas as pd
from pandasai.llm import OpenAI
secret = os.environ["key"]
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
import base64
from PIL import Image
from io import BytesIO
@app.post("/translator")
async def tra(sentence,lang):
lang = lang.lower()
return translator.translate(sentence,dest=keys[vals.index(lang)]).text
def convert_image_to_base64(image_path):
with Image.open(image_path) as image:
buffered = BytesIO()
image.save(buffered, format="PNG")
img_bytes = buffered.getvalue()
img_base64 = base64.b64encode(img_bytes)
img_base64_string = img_base64.decode("utf-8")
return img_base64_string
# # Function to call the endpoint
# def call_my_endpoint():
# #response = requests.get("http://127.0.0.1:8000/my-endpoint")
# print(f"Endpoint response: {response.json()}")
# # Configure the scheduler
# scheduler = BackgroundScheduler()
# scheduler.add_job(call_my_endpoint, 'interval', seconds=15) # Call every 30 seconds
# scheduler.start()
@app.post("/get_image_for_text")
async def get_image_for_text(email,query,file: UploadFile = File(...)):
print(file.filename)
file_name = file.filename
with open(email+file_name, "wb") as file_object:
file_object.write(file.file.read())
uuid1 = uuid.uuid1()
llm = OpenAI(api_token=secret,save_charts=True)
# Determine the file type and read accordingly
if file_name.endswith('.csv'):
df = pd.read_csv(email+file_name)
elif file_name.endswith('.xls') or file_name.endswith('.xlsx'):
df = pd.read_excel(email+file_name)
else:
return {"error": "Unsupported file type"}
sdf = SmartDataframe(df, config={"llm": llm})
sdf.chat(query)
code_to_exec = "import matplotlib.pyplot as plt\nimport seaborn as sns\n"
code_to_exec = code_to_exec + sdf.last_code_generated.replace("dfs[0]","dfs")
code_to_exec = code_to_exec.replace("exports/charts/temp_chart.png",email+file_name+".png")
code_to_exec = code_to_exec+f"\nplt.savefig('{email+file_name}.png')"
print(code_to_exec)
local_vars = {'dfs': df}
try:
exec(code_to_exec, globals(), local_vars)
print(email+file_name+".png",df.head())
#return FileResponse(email+file_name+".png")
base64str = convert_image_to_base64(email+file_name+".png")
return {"id":str(uuid1),"image":base64str}
except Exception as e:
print(str(e))
return "try again"
|