Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -24,20 +24,25 @@ cors = CORS(app)
|
|
| 24 |
|
| 25 |
|
| 26 |
class FlaskResponse(ResponseParser):
|
| 27 |
-
def __init__(self, context)
|
| 28 |
super().__init__(context)
|
| 29 |
|
| 30 |
def format_dataframe(self, result):
|
| 31 |
return result['value'].to_html()
|
| 32 |
|
| 33 |
def format_plot(self, result):
|
|
|
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def format_other(self, result):
|
| 43 |
return str(result['value'])
|
|
@@ -59,40 +64,65 @@ model = genai.GenerativeModel(
|
|
| 59 |
model_name="gemini-2.0-flash-thinking-exp",
|
| 60 |
generation_config=generation_config,
|
| 61 |
)
|
|
|
|
|
|
|
| 62 |
# Endpoint for chat
|
| 63 |
@app.route("/chat", methods=["POST"])
|
| 64 |
@cross_origin()
|
| 65 |
def bot():
|
| 66 |
-
|
| 67 |
json_table = request.json.get("json_table")
|
| 68 |
user_question = request.json.get("user_question")
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
#data
|
| 72 |
-
#json_table = data["json_table"]
|
| 73 |
-
#user_question = data["user_question"]
|
| 74 |
-
#print(json_table)
|
| 75 |
-
print(user_question)
|
| 76 |
data = eval(str(json_table))
|
| 77 |
df = pd.DataFrame(data)
|
| 78 |
-
print(list(df))
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
answer = pandas_agent.chat(user_question)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Reports endpoint
|
| 98 |
@app.route("/report", methods=["POST"])
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
class FlaskResponse(ResponseParser):
|
| 27 |
+
def __init__(self, context):
|
| 28 |
super().__init__(context)
|
| 29 |
|
| 30 |
def format_dataframe(self, result):
|
| 31 |
return result['value'].to_html()
|
| 32 |
|
| 33 |
def format_plot(self, result):
|
| 34 |
+
# Here we assume that result['value'] is a matplotlib Figure.
|
| 35 |
try:
|
| 36 |
+
fig = result['value']
|
| 37 |
+
buf = io.BytesIO()
|
| 38 |
+
fig.savefig(buf, format="png")
|
| 39 |
+
buf.seek(0)
|
| 40 |
+
image_base64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 41 |
+
# Return a data URL that can be rendered in an <img> tag.
|
| 42 |
+
return f"data:image/png;base64,{image_base64}"
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print("Error processing plot:", e)
|
| 45 |
+
return str(result['value'])
|
| 46 |
|
| 47 |
def format_other(self, result):
|
| 48 |
return str(result['value'])
|
|
|
|
| 64 |
model_name="gemini-2.0-flash-thinking-exp",
|
| 65 |
generation_config=generation_config,
|
| 66 |
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
# Endpoint for chat
|
| 70 |
@app.route("/chat", methods=["POST"])
|
| 71 |
@cross_origin()
|
| 72 |
def bot():
|
| 73 |
+
# Retrieve parameters from the request
|
| 74 |
json_table = request.json.get("json_table")
|
| 75 |
user_question = request.json.get("user_question")
|
| 76 |
+
print("User question:", user_question)
|
| 77 |
+
|
| 78 |
+
# Convert the table data into a dataframe
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
data = eval(str(json_table))
|
| 80 |
df = pd.DataFrame(data)
|
| 81 |
+
print("Columns in dataframe:", list(df.columns))
|
| 82 |
+
|
| 83 |
+
# Create a SmartDataframe instance using your configuration.
|
| 84 |
+
pandas_agent = SmartDataframe(
|
| 85 |
+
df,
|
| 86 |
+
config={
|
| 87 |
+
"llm": llm,
|
| 88 |
+
"response_parser": FlaskResponse,
|
| 89 |
+
"custom_whitelisted_dependencies": [
|
| 90 |
+
"os",
|
| 91 |
+
"io",
|
| 92 |
+
"sys",
|
| 93 |
+
"chr",
|
| 94 |
+
"glob",
|
| 95 |
+
"b64decoder",
|
| 96 |
+
"collections",
|
| 97 |
+
"geopy",
|
| 98 |
+
"geopandas",
|
| 99 |
+
"wordcloud",
|
| 100 |
+
"builtins"
|
| 101 |
+
],
|
| 102 |
+
"security": "none"
|
| 103 |
+
}
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Get the answer from the agent
|
| 107 |
answer = pandas_agent.chat(user_question)
|
| 108 |
+
|
| 109 |
+
# Process the answer based on its type
|
| 110 |
+
formatted_answer = None
|
| 111 |
+
if isinstance(answer, pd.DataFrame):
|
| 112 |
+
formatted_answer = answer.to_html()
|
| 113 |
+
elif isinstance(answer, plt.Figure):
|
| 114 |
+
buf = io.BytesIO()
|
| 115 |
+
answer.savefig(buf, format="png")
|
| 116 |
+
buf.seek(0)
|
| 117 |
+
image_base64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 118 |
+
formatted_answer = f"data:image/png;base64,{image_base64}"
|
| 119 |
+
elif isinstance(answer, (int, float)):
|
| 120 |
+
formatted_answer = str(answer)
|
| 121 |
+
else:
|
| 122 |
+
formatted_answer = str(answer)
|
| 123 |
+
|
| 124 |
+
# Return the formatted answer as JSON.
|
| 125 |
+
return jsonify({"answer": formatted_answer})
|
| 126 |
|
| 127 |
# Reports endpoint
|
| 128 |
@app.route("/report", methods=["POST"])
|