Jeremy Live commited on
Commit
158f99d
·
1 Parent(s): dcfac75
Files changed (1) hide show
  1. app.py +13 -85
app.py CHANGED
@@ -177,101 +177,29 @@ def run_crewai_process(user_query, model, temperature):
177
 
178
  # Execute the temporary script using subprocess
179
  try:
180
- # Add debug info and ensure plot is saved with absolute path
181
  debug_script = f"""
182
  import traceback
183
  import sys
184
- import os
185
- import matplotlib
186
- # Use non-interactive backend to avoid display issues
187
- matplotlib.use('Agg')
188
- import matplotlib.pyplot as plt
189
-
190
- # Debug info
191
- print("="*80)
192
- print("STARTING SCRIPT EXECUTION")
193
- print("="*80)
194
- print(f"[DEBUG] Python version: {{sys.version}}")
195
- print(f"[DEBUG] Working directory: {{os.getcwd()}}")
196
- print("[DEBUG] Directory contents:")
197
- for f in os.listdir('.'):
198
- print(f" - {{f}}{' (dir)' if os.path.isdir(f) else ''}")
199
- print("\n" + "="*80 + "\n")
200
 
201
  try:
202
- # Create a test plot first to verify matplotlib is working
203
- test_fig, test_ax = plt.subplots()
204
- test_ax.plot([1, 2, 3], [1, 4, 9])
205
- test_ax.set_title('Test Plot - If you see this, matplotlib is working')
206
- test_plot_path = 'test_plot.png'
207
- test_fig.savefig(test_plot_path, bbox_inches='tight')
208
- print(f"[DEBUG] Test plot saved to: {{os.path.abspath(test_plot_path)}}")
209
- print(f"[DEBUG] Test plot size: {{os.path.getsize(test_plot_path)}} bytes")
210
-
211
- # Execute the original script
212
- print("\n" + "="*80)
213
- print("EXECUTING USER SCRIPT")
214
- print("="*80)
215
  {generated_code}
216
 
217
- # Ensure any pending plots are drawn
218
- plt.ioff()
219
-
220
- # Save any open figures
221
- print("\n" + "="*80)
222
- print("SAVING PLOTS")
223
- print("="*80)
224
-
225
- # Get list of all figure numbers
226
- fig_nums = plt.get_fignums()
227
- print(f"[DEBUG] Found {{len(fig_nums)}} open figures")
228
-
229
- if fig_nums:
230
- # Save the last figure as plot.png
231
- last_fig = plt.figure(fig_nums[-1])
232
- plot_path = os.path.abspath('plot.png')
233
- last_fig.savefig(plot_path, bbox_inches='tight', dpi=100)
234
- print(f"[DEBUG] Saved plot to: {{plot_path}}")
235
- print(f"[DEBUG] Plot file size: {{os.path.getsize(plot_path)}} bytes")
236
-
237
- # Save all figures with unique names
238
- for i, num in enumerate(fig_nums, 1):
239
- fig = plt.figure(num)
240
- fig_path = os.path.abspath(f'plot_{{i}}.png')
241
- fig.savefig(fig_path, bbox_inches='tight', dpi=100)
242
- print(f"[DEBUG] Saved additional plot to: {{fig_path}} ({{os.path.getsize(fig_path)}} bytes)")
243
  else:
244
- print("[WARNING] No figures were created in the script")
245
-
246
- # Print final directory contents
247
- print("\n" + "="*80)
248
- print("FINAL DIRECTORY CONTENTS")
249
- print("="*80)
250
- for f in os.listdir('.'):
251
- fpath = os.path.join('.', f)
252
- if os.path.isfile(fpath):
253
- print(f" - {{f}} ({{os.path.getsize(fpath)}} bytes)")
254
- else:
255
- print(f" - {{f}}/ (dir)")
256
-
257
- print("\n" + "="*80)
258
- print("SCRIPT EXECUTION COMPLETE")
259
- print("="*80)
260
-
261
  except Exception as e:
262
- print("\n" + "!"*80)
263
- print("ERROR DURING EXECUTION")
264
- print("!"*80)
265
- print(f"Error type: {{type(e).__name__}}")
266
- print(f"Error message: {{str(e)}}")
267
- print("\nTraceback:")
268
  traceback.print_exc()
269
- print("\n" + "!"*80 + "\n")
270
- raise
271
-
272
- finally:
273
- # Always close all figures to free memory
274
- plt.close('all')
275
  """
276
  # Write the debug script to a temporary file
277
  with open(temp_script_path, "w") as f:
 
177
 
178
  # Execute the temporary script using subprocess
179
  try:
180
+ # Add debug info to the script
181
  debug_script = f"""
182
  import traceback
183
  import sys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  try:
186
+ # Original script
 
 
 
 
 
 
 
 
 
 
 
 
187
  {generated_code}
188
 
189
+ # Ensure the plot is saved
190
+ import matplotlib.pyplot as plt
191
+ if plt.get_fignums():
192
+ plt.savefig('plot.png')
193
+ print("\n[DEBUG] Plot saved successfully to plot.png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  else:
195
+ print("\n[DEBUG] No figures were created in the script")
196
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  except Exception as e:
198
+ print(f"\n[DEBUG] Error during script execution: {str(e)}")
199
+ print(f"[DEBUG] Error type: {type(e).__name__}")
200
+ print("\n[DEBUG] Traceback:")
 
 
 
201
  traceback.print_exc()
202
+ raise # Re-raise the exception to be caught by the outer try-except
 
 
 
 
 
203
  """
204
  # Write the debug script to a temporary file
205
  with open(temp_script_path, "w") as f: