Spaces:
Sleeping
Sleeping
Update stories.py
Browse files- stories.py +37 -33
stories.py
CHANGED
|
@@ -191,51 +191,55 @@ def transcribe_audio(audio_file):
|
|
| 191 |
# PandasAI Response for DataFrame (using SmartDataframe and ChatSambaNovaCloud)
|
| 192 |
# -----------------------
|
| 193 |
def generateResponse(prompt, df):
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
| 197 |
pandas_agent = SmartDataframe(
|
| 198 |
df,
|
| 199 |
config={
|
| 200 |
"llm": llm,
|
| 201 |
-
"response_parser": FlaskResponse,
|
| 202 |
"custom_whitelisted_dependencies": [
|
| 203 |
-
"os",
|
| 204 |
-
"
|
| 205 |
-
"sys",
|
| 206 |
-
"chr",
|
| 207 |
-
"glob",
|
| 208 |
-
"b64decoder",
|
| 209 |
-
"collections",
|
| 210 |
-
"geopy",
|
| 211 |
-
"geopandas",
|
| 212 |
-
"wordcloud",
|
| 213 |
-
"builtins"
|
| 214 |
],
|
| 215 |
-
"security": "none",
|
|
|
|
|
|
|
|
|
|
| 216 |
}
|
| 217 |
)
|
| 218 |
|
| 219 |
-
|
| 220 |
-
answer = pandas_agent.chat(user_question)
|
| 221 |
|
| 222 |
-
#
|
| 223 |
-
formatted_answer = None
|
| 224 |
if isinstance(answer, pd.DataFrame):
|
| 225 |
-
|
| 226 |
-
elif isinstance(answer, plt.Figure):
|
| 227 |
-
buf = io.BytesIO()
|
| 228 |
-
answer.savefig(buf, format="png")
|
| 229 |
-
buf.seek(0)
|
| 230 |
-
image_base64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 231 |
-
formatted_answer = f"data:image/png;base64,{image_base64}"
|
| 232 |
-
elif isinstance(answer, (int, float)):
|
| 233 |
-
formatted_answer = str(answer)
|
| 234 |
-
else:
|
| 235 |
-
formatted_answer = str(answer)
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
# -----------------------
|
| 241 |
# DataFrame-Based Story Generation (for CSV/Excel files)
|
|
|
|
| 191 |
# PandasAI Response for DataFrame (using SmartDataframe and ChatSambaNovaCloud)
|
| 192 |
# -----------------------
|
| 193 |
def generateResponse(prompt, df):
|
| 194 |
+
"""
|
| 195 |
+
Return either a base64-encoded PNG string like 'data:image/png;base64,...'
|
| 196 |
+
if the answer is a chart, or a fallback string if the answer is something else.
|
| 197 |
+
"""
|
| 198 |
pandas_agent = SmartDataframe(
|
| 199 |
df,
|
| 200 |
config={
|
| 201 |
"llm": llm,
|
| 202 |
+
"response_parser": FlaskResponse, # You can still use it for internal logic
|
| 203 |
"custom_whitelisted_dependencies": [
|
| 204 |
+
"os", "io", "sys", "chr", "glob", "b64decoder",
|
| 205 |
+
"collections", "geopy", "geopandas", "wordcloud", "builtins"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
],
|
| 207 |
+
"security": "none",
|
| 208 |
+
"save_charts_path": user_defined_path,
|
| 209 |
+
"save_charts": False,
|
| 210 |
+
"enable_cache": False,
|
| 211 |
}
|
| 212 |
)
|
| 213 |
|
| 214 |
+
answer = pandas_agent.chat(prompt)
|
|
|
|
| 215 |
|
| 216 |
+
# Convert 'answer' into a base64 string or fallback
|
|
|
|
| 217 |
if isinstance(answer, pd.DataFrame):
|
| 218 |
+
return answer.to_html()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
elif hasattr(answer, "savefig"): # e.g. a Matplotlib figure
|
| 221 |
+
try:
|
| 222 |
+
buf = io.BytesIO()
|
| 223 |
+
answer.savefig(buf, format="png")
|
| 224 |
+
buf.seek(0)
|
| 225 |
+
image_base64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 226 |
+
return f"data:image/png;base64,{image_base64}"
|
| 227 |
+
except Exception as e:
|
| 228 |
+
print("Error processing figure:", e)
|
| 229 |
+
return None
|
| 230 |
+
|
| 231 |
+
elif isinstance(answer, str):
|
| 232 |
+
# Could be a file path or just a textual answer
|
| 233 |
+
if os.path.isfile(answer):
|
| 234 |
+
with open(answer, "rb") as f:
|
| 235 |
+
data = f.read()
|
| 236 |
+
b64 = base64.b64encode(data).decode("utf-8")
|
| 237 |
+
return f"data:image/png;base64,{b64}"
|
| 238 |
+
else:
|
| 239 |
+
return answer
|
| 240 |
+
else:
|
| 241 |
+
# fallback
|
| 242 |
+
return str(answer)
|
| 243 |
|
| 244 |
# -----------------------
|
| 245 |
# DataFrame-Based Story Generation (for CSV/Excel files)
|