Commit
·
499b4d5
1
Parent(s):
6f1251a
v115
Browse files
app.py
CHANGED
|
@@ -187,7 +187,7 @@ def run_crewai_process(user_query, model, temperature):
|
|
| 187 |
with open(temp_script_path, "w") as f:
|
| 188 |
f.write(generated_code)
|
| 189 |
|
| 190 |
-
#
|
| 191 |
with open(temp_script_path, 'r') as f:
|
| 192 |
script_content = f.read()
|
| 193 |
|
|
@@ -198,6 +198,63 @@ def run_crewai_process(user_query, model, temperature):
|
|
| 198 |
script_content
|
| 199 |
)
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
# Write the updated script back
|
| 202 |
with open(temp_script_path, 'w') as f:
|
| 203 |
f.write(script_content)
|
|
|
|
| 187 |
with open(temp_script_path, "w") as f:
|
| 188 |
f.write(generated_code)
|
| 189 |
|
| 190 |
+
# Read the generated script
|
| 191 |
with open(temp_script_path, 'r') as f:
|
| 192 |
script_content = f.read()
|
| 193 |
|
|
|
|
| 198 |
script_content
|
| 199 |
)
|
| 200 |
|
| 201 |
+
# Add helper functions at the beginning of the script
|
| 202 |
+
helpers = """
|
| 203 |
+
# Helper functions for data processing
|
| 204 |
+
def safe_get_column(df, column):
|
| 205 |
+
# Try exact match first
|
| 206 |
+
if column in df.columns:
|
| 207 |
+
return df[column]
|
| 208 |
+
# Try case-insensitive match
|
| 209 |
+
col_lower = column.lower()
|
| 210 |
+
for col in df.columns:
|
| 211 |
+
if col.lower() == col_lower:
|
| 212 |
+
return df[col]
|
| 213 |
+
# If not found, try common variations
|
| 214 |
+
variations = {
|
| 215 |
+
'close': ['Close', 'Adj Close', 'close', 'adj close', 'CLOSE'],
|
| 216 |
+
'adj close': ['Adj Close', 'adj close', 'ADJ CLOSE', 'Close', 'close', 'CLOSE']
|
| 217 |
+
}
|
| 218 |
+
for var in variations.get(column.lower(), []):
|
| 219 |
+
if var in df.columns:
|
| 220 |
+
return df[var]
|
| 221 |
+
# If still not found, raise a helpful error
|
| 222 |
+
raise KeyError(f"Column '{column}' not found in DataFrame. Available columns: {list(df.columns)}")
|
| 223 |
+
|
| 224 |
+
def plot_and_save(plt, filename='plot.png'):
|
| 225 |
+
import os
|
| 226 |
+
# Ensure the directory exists
|
| 227 |
+
os.makedirs(os.path.dirname(os.path.abspath(filename)) or '.', exist_ok=True)
|
| 228 |
+
plt.savefig(filename)
|
| 229 |
+
plt.close()
|
| 230 |
+
print(f"Plot saved as {os.path.abspath(filename)}")
|
| 231 |
+
|
| 232 |
+
# Monkey patch DataFrame to add safe column access
|
| 233 |
+
import pandas as pd
|
| 234 |
+
pd.DataFrame.safe_get = safe_get_column
|
| 235 |
+
"""
|
| 236 |
+
# Insert the helper functions after imports
|
| 237 |
+
if 'import ' in script_content:
|
| 238 |
+
# Insert after the last import
|
| 239 |
+
last_import = script_content.rfind('import ')
|
| 240 |
+
insert_pos = script_content.find('\n', last_import) + 1
|
| 241 |
+
script_content = script_content[:insert_pos] + '\n' + helpers + script_content[insert_pos:]
|
| 242 |
+
else:
|
| 243 |
+
# Insert at the beginning if no imports found
|
| 244 |
+
script_content = helpers + '\n' + script_content
|
| 245 |
+
|
| 246 |
+
# Replace common column access patterns with our safe version
|
| 247 |
+
script_content = script_content.replace("['Adj Close']", ".safe_get('close')")
|
| 248 |
+
script_content = script_content.replace("['Close']", ".safe_get('close')")
|
| 249 |
+
script_content = script_content.replace("['close']", ".safe_get('close')")
|
| 250 |
+
|
| 251 |
+
# Replace plt.savefig() calls with our helper
|
| 252 |
+
script_content = re.sub(
|
| 253 |
+
r'plt\.savefig\(([^)]+)\)',
|
| 254 |
+
r'plot_and_save(plt, \1)',
|
| 255 |
+
script_content
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
# Write the updated script back
|
| 259 |
with open(temp_script_path, 'w') as f:
|
| 260 |
f.write(script_content)
|