Jeremy Live commited on
Commit
57d9a46
·
1 Parent(s): 1740ecf
Files changed (1) hide show
  1. app.py +16 -70
app.py CHANGED
@@ -176,76 +176,22 @@ def run_crewai_process(user_query, model, temperature):
176
  f.write(generated_code)
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:
206
- f.write(debug_script)
207
-
208
- # Execute the script
209
- process = subprocess.run(
210
- ["python3", temp_script_path],
211
- capture_output=True,
212
- text=True,
213
- check=False
214
- )
215
-
216
- # Capture both stdout and stderr
217
- execution_output = process.stdout
218
- if process.stderr:
219
- execution_output += "\n\n[ERROR] Script execution errors:\n" + process.stderr
220
-
221
- # Check for common issues in the output
222
- if "KeyError" in execution_output:
223
- execution_output += "\n\n[HELP] The script encountered a KeyError. This typically happens when trying to access a column that doesn't exist in the stock data.\n"
224
- execution_output += "Common causes:\n"
225
- execution_output += "1. The stock symbol might not be recognized by yfinance\n"
226
- execution_output += "2. The requested time period might not have data (e.g., weekends, holidays)\n"
227
- execution_output += "3. The data column names might be different than expected\n\n"
228
- execution_output += "Please try a different stock symbol or time period."
229
-
230
- if "No data" in execution_output or "not found" in execution_output.lower():
231
- execution_output += "\n\n[HELP] No data was returned for the specified stock symbol or time period.\n"
232
- execution_output += "Please check the stock symbol and try a different time period."
233
-
234
- if "Figure(" in execution_output and "plot.png" not in os.listdir():
235
- execution_output += "\n\n[HELP] A plot was created but not saved. Adding save command...\n"
236
- # Try to save the plot if it wasn't saved
237
- try:
238
- import matplotlib.pyplot as plt
239
- if plt.get_fignums():
240
- plt.savefig('plot.png')
241
- execution_output += "Successfully saved plot to plot.png"
242
- generated_plot_path = 'plot.png'
243
- plt.close('all')
244
- except Exception as e:
245
- execution_output += f"Failed to save plot: {str(e)}"
246
- except Exception as e:
247
- execution_output = f"Error during script execution: {str(e)}\n\n"
248
- execution_output += "Please check the generated code for issues or try a different query."
249
 
250
  # Check for the generated plot file
251
  if os.path.exists(plot_file_path):
 
176
  f.write(generated_code)
177
 
178
  # Execute the temporary script using subprocess
179
+ # Use python3 to ensure correct interpreter in Colab
180
+ process = subprocess.run(
181
+ ["python3", temp_script_path],
182
+ capture_output=True,
183
+ text=True, # Capture stdout and stderr as text
184
+ check=False # Don't raise exception for non-zero exit codes
185
+ )
186
+ execution_output = process.stdout + process.stderr
187
+
188
+ # Check for specific errors in execution output
189
+ if "KeyError" in execution_output:
190
+ execution_output += "\n\nPotential Issue: The generated script encountered a KeyError. This might mean the script tried to access a column or data point that wasn't available for the specified stock or timeframe. Please try a different query or timeframe."
191
+ elif "FileNotFoundError: [Errno 2] No such file or directory: 'plot.png'" in execution_output and "Figure(" in execution_output:
192
+ execution_output += "\n\nPlot Generation Issue: The script seems to have created a plot but did not save it to 'plot.png'. Please ensure the generated code includes `plt.savefig('plot.png')`."
193
+ elif "FileNotFoundError: [Errno 2] No such file or directory: 'plot.png'" in execution_output:
194
+ 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'."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
  # Check for the generated plot file
197
  if os.path.exists(plot_file_path):