Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -7,22 +7,16 @@ from io import StringIO
|
|
7 |
import os
|
8 |
import uuid,requests
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
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 +27,21 @@ 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("/
|
66 |
-
async def
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
llm = OpenAI(api_token=secret,save_charts=True)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
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 |
+
GOOD_BOY_URL = (
|
11 |
+
"https://images.unsplash.com/photo-1518717758536-85ae29035b6d?ixlib=rb-1.2.1"
|
12 |
+
"&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
|
13 |
+
)
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
|
|
15 |
|
16 |
+
from fastapi import FastAPI, Request, HTTPException
|
17 |
+
from fastapi.responses import PlainTextResponse
|
18 |
+
from twilio.twiml.messaging_response import MessagingResponse
|
19 |
|
|
|
20 |
|
21 |
app = FastAPI()
|
22 |
app.add_middleware(
|
|
|
27 |
allow_headers=["*"],
|
28 |
)
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
@app.post("/whatsapp")
|
33 |
+
async def reply_whatsapp(request: Request):
|
34 |
+
form_data = await request.form()
|
35 |
+
try:
|
36 |
+
num_media = int(form_data.get("NumMedia", 0))
|
37 |
+
except (ValueError, TypeError):
|
38 |
+
raise HTTPException(status_code=400, detail="Invalid request: invalid or missing NumMedia parameter")
|
|
|
39 |
|
40 |
+
response = MessagingResponse()
|
41 |
+
if not num_media:
|
42 |
+
msg = response.message("Send us an image!")
|
43 |
+
else:
|
44 |
+
msg = response.message("Thanks for the image. Here's one for you!")
|
45 |
+
msg.media(GOOD_BOY_URL)
|
46 |
+
return PlainTextResponse(str(response))
|
|
|
|
|
|
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|