Commit
·
76dd9b4
1
Parent(s):
9b08ac4
vv4
Browse files
app.py
CHANGED
|
@@ -176,22 +176,76 @@ def run_crewai_process(user_query, model, temperature):
|
|
| 176 |
f.write(generated_code)
|
| 177 |
|
| 178 |
# Execute the temporary script using subprocess
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# Check for the generated plot file
|
| 197 |
if os.path.exists(plot_file_path):
|
|
|
|
| 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):
|