Jeremy Live
commited on
Commit
·
5034cbf
1
Parent(s):
c26cd3f
v112
Browse files
app.py
CHANGED
@@ -170,7 +170,8 @@ 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 |
-
|
|
|
174 |
|
175 |
if generated_code:
|
176 |
try:
|
@@ -212,15 +213,27 @@ def run_crewai_process(user_query, model, temperature):
|
|
212 |
elif "FileNotFoundError: [Errno 2] No such file or directory: 'plot.png'" in execution_output:
|
213 |
execution_output += "\n\nPlot Generation Issue: The script ran, but the plot file was not created. Ensure the generated code includes commands to save the plot to 'plot.png'."
|
214 |
|
215 |
-
# Check for the generated plot file
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
except Exception as e:
|
226 |
traceback_str = traceback.format_exc()
|
@@ -357,15 +370,24 @@ def create_interface():
|
|
357 |
agent_thoughts,
|
358 |
generated_code,
|
359 |
execution_output,
|
360 |
-
plot_output
|
361 |
-
image_output
|
362 |
]
|
363 |
|
|
|
|
|
|
|
|
|
|
|
364 |
submit_btn.click(
|
365 |
fn=run_crewai_process,
|
366 |
inputs=inputs,
|
367 |
outputs=outputs,
|
368 |
api_name="analyze"
|
|
|
|
|
|
|
|
|
369 |
)
|
370 |
|
371 |
return interface
|
|
|
170 |
yield final_answer_chat, agent_thoughts, generated_code, execution_output, None, None
|
171 |
|
172 |
# --- Execute the generated code ---
|
173 |
+
# Check for both plot.png and {symbol}_plot.png
|
174 |
+
plot_file_paths = ['plot.png', 'META_plot.png', 'AAPL_plot.png', 'MSFT_plot.png'] # Common plot filenames
|
175 |
|
176 |
if generated_code:
|
177 |
try:
|
|
|
213 |
elif "FileNotFoundError: [Errno 2] No such file or directory: 'plot.png'" in execution_output:
|
214 |
execution_output += "\n\nPlot Generation Issue: The script ran, but the plot file was not created. Ensure the generated code includes commands to save the plot to 'plot.png'."
|
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 generated_plot_path:
|
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"
|
237 |
|
238 |
except Exception as e:
|
239 |
traceback_str = traceback.format_exc()
|
|
|
370 |
agent_thoughts,
|
371 |
generated_code,
|
372 |
execution_output,
|
373 |
+
None, # This is for plot_output (we'll handle it separately)
|
374 |
+
None # This is for image_output (we'll handle it separately)
|
375 |
]
|
376 |
|
377 |
+
def process_results(chat, thoughts, code, output, plot_path):
|
378 |
+
# This function will be called after run_crewai_process
|
379 |
+
# Show the image in the image_output component
|
380 |
+
return chat, thoughts, code, output, gr.update(visible=plot_path is not None), gr.update(value=plot_path, visible=plot_path is not None)
|
381 |
+
|
382 |
submit_btn.click(
|
383 |
fn=run_crewai_process,
|
384 |
inputs=inputs,
|
385 |
outputs=outputs,
|
386 |
api_name="analyze"
|
387 |
+
).then(
|
388 |
+
fn=process_results,
|
389 |
+
inputs=[final_answer_chat, agent_thoughts, generated_code, execution_output, image_output],
|
390 |
+
outputs=[final_answer_chat, agent_thoughts, generated_code, execution_output, plot_output, image_output]
|
391 |
)
|
392 |
|
393 |
return interface
|