Jeremy Live
commited on
Commit
·
50d0005
1
Parent(s):
5034cbf
v113
Browse files
app.py
CHANGED
@@ -170,8 +170,15 @@ def run_crewai_process(user_query, model, temperature):
|
|
170 |
yield final_answer_chat, agent_thoughts, generated_code, execution_output, None, None
|
171 |
|
172 |
# --- Execute the generated code ---
|
173 |
-
# Check for
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
if generated_code:
|
177 |
try:
|
@@ -215,22 +222,44 @@ def run_crewai_process(user_query, model, temperature):
|
|
215 |
|
216 |
# Check for the generated plot file in all possible locations
|
217 |
generated_plot_path = None
|
|
|
|
|
218 |
for plot_file in plot_file_paths:
|
219 |
plot_abs_path = os.path.abspath(plot_file)
|
220 |
if os.path.exists(plot_abs_path):
|
221 |
print(f"Plot file found at: {plot_abs_path}")
|
222 |
generated_plot_path = plot_abs_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
break
|
224 |
|
225 |
-
if not
|
226 |
# If no plot file was found, check the current directory for any .png files
|
227 |
current_dir = os.path.abspath('.')
|
228 |
-
png_files = [f for f in os.listdir(current_dir) if f.endswith('.png')]
|
229 |
if png_files:
|
230 |
# Use the first .png file found
|
231 |
plot_abs_path = os.path.abspath(png_files[0])
|
232 |
generated_plot_path = plot_abs_path
|
233 |
print(f"Using plot file found at: {plot_abs_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
else:
|
235 |
print(f"No plot file found in {current_dir}")
|
236 |
execution_output += "\nNo plot file was found after execution.\n\nMake sure the generated code includes:\n1. `plt.savefig('plot.png')` to save the plot\n2. `plt.close()` to close the figure after saving"
|
@@ -250,14 +279,14 @@ def run_crewai_process(user_query, model, temperature):
|
|
250 |
# Update final answer chat to reflect execution attempt
|
251 |
execution_complete_msg = "Code execution finished. See 'Execution Output'."
|
252 |
if generated_plot_path:
|
253 |
-
plot_msg = "Plot generated successfully
|
254 |
final_answer_chat = [
|
255 |
{"role": "user", "content": str(user_query)},
|
256 |
{"role": "assistant", "content": execution_complete_msg},
|
257 |
{"role": "assistant", "content": plot_msg}
|
258 |
]
|
|
|
259 |
yield final_answer_chat, agent_thoughts, generated_code, execution_output, None, generated_plot_path
|
260 |
-
return
|
261 |
else:
|
262 |
no_plot_msg = "No plot was generated. Make sure your query includes a request for a visualization. Check the 'Execution Output' tab for any errors."
|
263 |
final_answer_chat = [
|
@@ -265,8 +294,8 @@ def run_crewai_process(user_query, model, temperature):
|
|
265 |
{"role": "assistant", "content": execution_complete_msg},
|
266 |
{"role": "assistant", "content": no_plot_msg}
|
267 |
]
|
|
|
268 |
yield final_answer_chat, agent_thoughts, generated_code, execution_output, None, None
|
269 |
-
return
|
270 |
|
271 |
yield agent_thoughts, final_answer_chat, generated_code, execution_output, generated_plot_path
|
272 |
|
|
|
170 |
yield final_answer_chat, agent_thoughts, generated_code, execution_output, None, None
|
171 |
|
172 |
# --- Execute the generated code ---
|
173 |
+
# Check for common plot filename patterns
|
174 |
+
# First check for symbol-specific plots (e.g., META_plot.png)
|
175 |
+
symbol_plot_patterns = ['META_plot.png', 'AAPL_plot.png', 'MSFT_plot.png', 'GOOG_plot.png', 'TSLA_plot.png']
|
176 |
+
|
177 |
+
# Also check for generic plot filenames
|
178 |
+
generic_plot_patterns = ['plot.png', 'output.png', 'figure.png']
|
179 |
+
|
180 |
+
# Combine all patterns to check
|
181 |
+
plot_file_paths = symbol_plot_patterns + generic_plot_patterns
|
182 |
|
183 |
if generated_code:
|
184 |
try:
|
|
|
222 |
|
223 |
# Check for the generated plot file in all possible locations
|
224 |
generated_plot_path = None
|
225 |
+
plot_found = False
|
226 |
+
|
227 |
for plot_file in plot_file_paths:
|
228 |
plot_abs_path = os.path.abspath(plot_file)
|
229 |
if os.path.exists(plot_abs_path):
|
230 |
print(f"Plot file found at: {plot_abs_path}")
|
231 |
generated_plot_path = plot_abs_path
|
232 |
+
plot_found = True
|
233 |
+
|
234 |
+
# Add the plot to the execution output
|
235 |
+
try:
|
236 |
+
import base64
|
237 |
+
with open(plot_abs_path, 'rb') as img_file:
|
238 |
+
img_str = base64.b64encode(img_file.read()).decode('utf-8')
|
239 |
+
execution_output += f"\n\n"
|
240 |
+
except Exception as e:
|
241 |
+
execution_output += f"\n\nNote: Could not embed plot in output: {str(e)}"
|
242 |
+
|
243 |
break
|
244 |
|
245 |
+
if not plot_found:
|
246 |
# If no plot file was found, check the current directory for any .png files
|
247 |
current_dir = os.path.abspath('.')
|
248 |
+
png_files = [f for f in os.listdir(current_dir) if f.endswith('.png') and not f.startswith('gradio_')]
|
249 |
if png_files:
|
250 |
# Use the first .png file found
|
251 |
plot_abs_path = os.path.abspath(png_files[0])
|
252 |
generated_plot_path = plot_abs_path
|
253 |
print(f"Using plot file found at: {plot_abs_path}")
|
254 |
+
|
255 |
+
# Add the plot to the execution output
|
256 |
+
try:
|
257 |
+
import base64
|
258 |
+
with open(plot_abs_path, 'rb') as img_file:
|
259 |
+
img_str = base64.b64encode(img_file.read()).decode('utf-8')
|
260 |
+
execution_output += f"\n\n"
|
261 |
+
except Exception as e:
|
262 |
+
execution_output += f"\n\nNote: Could not embed plot in output: {str(e)}"
|
263 |
else:
|
264 |
print(f"No plot file found in {current_dir}")
|
265 |
execution_output += "\nNo plot file was found after execution.\n\nMake sure the generated code includes:\n1. `plt.savefig('plot.png')` to save the plot\n2. `plt.close()` to close the figure after saving"
|
|
|
279 |
# Update final answer chat to reflect execution attempt
|
280 |
execution_complete_msg = "Code execution finished. See 'Execution Output'."
|
281 |
if generated_plot_path:
|
282 |
+
plot_msg = f"Plot generated successfully at: {generated_plot_path}"
|
283 |
final_answer_chat = [
|
284 |
{"role": "user", "content": str(user_query)},
|
285 |
{"role": "assistant", "content": execution_complete_msg},
|
286 |
{"role": "assistant", "content": plot_msg}
|
287 |
]
|
288 |
+
# Return the plot path to be displayed
|
289 |
yield final_answer_chat, agent_thoughts, generated_code, execution_output, None, generated_plot_path
|
|
|
290 |
else:
|
291 |
no_plot_msg = "No plot was generated. Make sure your query includes a request for a visualization. Check the 'Execution Output' tab for any errors."
|
292 |
final_answer_chat = [
|
|
|
294 |
{"role": "assistant", "content": execution_complete_msg},
|
295 |
{"role": "assistant", "content": no_plot_msg}
|
296 |
]
|
297 |
+
# Return None for plot path
|
298 |
yield final_answer_chat, agent_thoughts, generated_code, execution_output, None, None
|
|
|
299 |
|
300 |
yield agent_thoughts, final_answer_chat, generated_code, execution_output, generated_plot_path
|
301 |
|