Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -14,6 +14,7 @@ from langchain.chains import LLMChain
|
|
| 14 |
from datetime import datetime
|
| 15 |
import matplotlib.pyplot as plt
|
| 16 |
import google.generativeai as genai
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
load_dotenv()
|
|
@@ -28,24 +29,36 @@ class FlaskResponse(ResponseParser):
|
|
| 28 |
super().__init__(context)
|
| 29 |
|
| 30 |
def format_dataframe(self, result):
|
| 31 |
-
return result[
|
| 32 |
|
| 33 |
def format_plot(self, result):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def format_other(self, result):
|
| 48 |
-
return
|
|
|
|
|
|
|
| 49 |
|
| 50 |
gemini_api_key = os.getenv('Gemini')
|
| 51 |
llm = ChatGoogleGenerativeAI(api_key=gemini_api_key, model='gemini-2.0-flash-thinking-exp', temperature=0.1)
|
|
@@ -66,6 +79,11 @@ model = genai.GenerativeModel(
|
|
| 66 |
)
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Endpoint for chat
|
| 70 |
@app.route("/chat", methods=["POST"])
|
| 71 |
@cross_origin()
|
|
@@ -99,7 +117,7 @@ def bot():
|
|
| 99 |
"wordcloud",
|
| 100 |
"builtins"
|
| 101 |
],
|
| 102 |
-
"security": "none"
|
| 103 |
}
|
| 104 |
)
|
| 105 |
|
|
|
|
| 14 |
from datetime import datetime
|
| 15 |
import matplotlib.pyplot as plt
|
| 16 |
import google.generativeai as genai
|
| 17 |
+
import uuid
|
| 18 |
|
| 19 |
|
| 20 |
load_dotenv()
|
|
|
|
| 29 |
super().__init__(context)
|
| 30 |
|
| 31 |
def format_dataframe(self, result):
|
| 32 |
+
return result["value"].to_html()
|
| 33 |
|
| 34 |
def format_plot(self, result):
|
| 35 |
+
val = result["value"]
|
| 36 |
+
# If val is a matplotlib figure, handle it accordingly.
|
| 37 |
+
if hasattr(val, "savefig"):
|
| 38 |
+
try:
|
| 39 |
+
buf = io.BytesIO()
|
| 40 |
+
val.savefig(buf, format="png")
|
| 41 |
+
buf.seek(0)
|
| 42 |
+
image_base64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 43 |
+
return f"data:image/png;base64,{image_base64}"
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print("Error processing figure:", e)
|
| 46 |
+
return str(val)
|
| 47 |
+
# If val is a string and is a valid file path, read and encode it.
|
| 48 |
+
if isinstance(val, str) and os.path.isfile(os.path.join(val)):
|
| 49 |
+
image_path = os.path.join(val)
|
| 50 |
+
print("My image path:", image_path)
|
| 51 |
+
with open(image_path, "rb") as file:
|
| 52 |
+
data = file.read()
|
| 53 |
+
base64_data = base64.b64encode(data).decode("utf-8")
|
| 54 |
+
return f"data:image/png;base64,{base64_data}"
|
| 55 |
+
# Fallback: return as a string.
|
| 56 |
+
return str(val)
|
| 57 |
|
| 58 |
def format_other(self, result):
|
| 59 |
+
# For non-image responses, simply return the value as a string.
|
| 60 |
+
return str(result["value"])
|
| 61 |
+
|
| 62 |
|
| 63 |
gemini_api_key = os.getenv('Gemini')
|
| 64 |
llm = ChatGoogleGenerativeAI(api_key=gemini_api_key, model='gemini-2.0-flash-thinking-exp', temperature=0.1)
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
|
| 82 |
+
guid = uuid.uuid4()
|
| 83 |
+
new_filename = f"{guid}"
|
| 84 |
+
user_defined_path = os.path.join("/exports/charts", new_filename)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
# Endpoint for chat
|
| 88 |
@app.route("/chat", methods=["POST"])
|
| 89 |
@cross_origin()
|
|
|
|
| 117 |
"wordcloud",
|
| 118 |
"builtins"
|
| 119 |
],
|
| 120 |
+
"security": "none", "save_charts_path": user_defined_path, "save_charts": False, , "enable_cache": False, "conversational":True
|
| 121 |
}
|
| 122 |
)
|
| 123 |
|