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