Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -12,7 +12,8 @@ import pandas as pd
|
|
12 |
from datetime import datetime
|
13 |
import os
|
14 |
from pandasai.llm import GoogleGemini
|
15 |
-
from pandasai import SmartDataframe
|
|
|
16 |
from pandasai.responses.response_parser import ResponseParser
|
17 |
import matplotlib.pyplot as plt
|
18 |
from wordcloud import WordCloud
|
@@ -30,7 +31,7 @@ load_dotenv()
|
|
30 |
app = Flask(__name__)
|
31 |
cors = CORS(app)
|
32 |
|
33 |
-
class
|
34 |
def __init__(self,context) -> None:
|
35 |
super().__init__(context)
|
36 |
def format_dataframe(self,result):
|
@@ -51,12 +52,9 @@ def home():
|
|
51 |
return "Hello Qx!"
|
52 |
|
53 |
|
54 |
-
def generateResponse(dataFrame,prompt):
|
55 |
-
llm = GoogleGemini(api_key=gemini_api_key)
|
56 |
-
pandas_agent = SmartDataframe(dataFrame,config={"llm":llm, "response_parser":StreamLitResponse})
|
57 |
-
answer = pandas_agent.chat(prompt)
|
58 |
-
return answer
|
59 |
|
|
|
|
|
60 |
# Initialize Firebase app
|
61 |
if not firebase_admin._apps:
|
62 |
|
@@ -65,43 +63,41 @@ if not firebase_admin._apps:
|
|
65 |
|
66 |
db = firestore.client()
|
67 |
|
68 |
-
inventory_ref = db.collection('inventory')
|
69 |
|
70 |
|
71 |
-
sales_ref = db.collection('sales')
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
inventory_list.append(a)
|
77 |
|
78 |
-
sales_list = []
|
79 |
-
for doc in sales_ref.stream():
|
80 |
-
a = doc.to_dict()
|
81 |
-
sales_list.append(a)
|
82 |
|
83 |
-
|
84 |
-
|
|
|
|
|
85 |
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
@app.route("/predict", methods=["POST"])
|
89 |
-
@cross_origin()
|
90 |
-
def bot():
|
91 |
-
load_dotenv()
|
92 |
-
#
|
93 |
-
json_table = request.json.get("json_table")
|
94 |
-
user_question = request.json.get("user_question")
|
95 |
-
#data = request.get_json(force=True)TRye
|
96 |
-
#print(req_body)
|
97 |
-
#data = eval(req_body)
|
98 |
-
#json_table = data["json_table"]
|
99 |
-
#user_question = data["user_question"]
|
100 |
-
#print(json_table)
|
101 |
print(user_question)
|
102 |
-
|
103 |
-
|
104 |
-
print(list(df))
|
105 |
return jsonify(response)
|
106 |
|
107 |
|
|
|
12 |
from datetime import datetime
|
13 |
import os
|
14 |
from pandasai.llm import GoogleGemini
|
15 |
+
from pandasai import SmartDataframe, SmartDatalake
|
16 |
+
|
17 |
from pandasai.responses.response_parser import ResponseParser
|
18 |
import matplotlib.pyplot as plt
|
19 |
from wordcloud import WordCloud
|
|
|
31 |
app = Flask(__name__)
|
32 |
cors = CORS(app)
|
33 |
|
34 |
+
class FlaskResponse(ResponseParser):
|
35 |
def __init__(self,context) -> None:
|
36 |
super().__init__(context)
|
37 |
def format_dataframe(self,result):
|
|
|
52 |
return "Hello Qx!"
|
53 |
|
54 |
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
llm = GoogleGemini(api_key=gemini_api_key)
|
57 |
+
|
58 |
# Initialize Firebase app
|
59 |
if not firebase_admin._apps:
|
60 |
|
|
|
63 |
|
64 |
db = firestore.client()
|
65 |
|
|
|
66 |
|
67 |
|
|
|
68 |
|
69 |
+
@app.route("/predict", methods=["POST"])
|
70 |
+
@cross_origin()
|
71 |
+
def bot():
|
|
|
72 |
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
user_id = request.json.get("user_id")
|
75 |
+
user_question = request.json.get("user_question")
|
76 |
+
load_dotenv()
|
77 |
+
inventory_ref = db.collection("users").document(user_id).collection('inventory')
|
78 |
|
79 |
|
80 |
+
sales_ref = db.collection("users").document(user_id).collection('sales')
|
81 |
+
|
82 |
+
inventory_list = []
|
83 |
+
for doc in inventory_ref.stream():
|
84 |
+
a = doc.to_dict()
|
85 |
+
inventory_list.append(a)
|
86 |
+
|
87 |
+
sales_list = []
|
88 |
+
for doc in sales_ref.stream():
|
89 |
+
a = doc.to_dict()
|
90 |
+
sales_list.append(a)
|
91 |
+
|
92 |
+
inventory_df = pd.DataFrame(inventory_list)
|
93 |
+
sales_df = pd.DataFrame(sales_list)
|
94 |
+
|
95 |
+
lake = SmartDatalake([inventory_df, sales_df], config={"llm":llm, "response_parser":FlaskResponse})
|
96 |
+
response = lake.chat(user_question)
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
print(user_question)
|
99 |
+
|
100 |
+
|
|
|
101 |
return jsonify(response)
|
102 |
|
103 |
|