Jeremy Live
commited on
Commit
·
d21581b
1
Parent(s):
499b4d5
v116
Browse files
app.py
CHANGED
@@ -200,6 +200,9 @@ def run_crewai_process(user_query, model, temperature):
|
|
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
|
@@ -221,13 +224,22 @@ def safe_get_column(df, column):
|
|
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=
|
225 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
# Ensure the directory exists
|
227 |
-
os.makedirs(os.path.dirname(
|
228 |
-
|
|
|
|
|
229 |
plt.close()
|
230 |
-
print(f"Plot saved
|
|
|
231 |
|
232 |
# Monkey patch DataFrame to add safe column access
|
233 |
import pandas as pd
|
@@ -255,6 +267,10 @@ pd.DataFrame.safe_get = safe_get_column
|
|
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)
|
@@ -281,12 +297,31 @@ pd.DataFrame.safe_get = safe_get_column
|
|
281 |
generated_plot_path = None
|
282 |
plot_found = False
|
283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
for plot_file in plot_file_paths:
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
|
|
|
|
|
|
290 |
|
291 |
# Add the plot to the execution output
|
292 |
try:
|
|
|
200 |
|
201 |
# Add helper functions at the beginning of the script
|
202 |
helpers = """
|
203 |
+
# Standard plot filename to use
|
204 |
+
PLOT_FILENAME = 'generated_plot.png'
|
205 |
+
|
206 |
# Helper functions for data processing
|
207 |
def safe_get_column(df, column):
|
208 |
# Try exact match first
|
|
|
224 |
# If still not found, raise a helpful error
|
225 |
raise KeyError(f"Column '{column}' not found in DataFrame. Available columns: {list(df.columns)}")
|
226 |
|
227 |
+
def plot_and_save(plt, filename=None):
|
228 |
import os
|
229 |
+
if filename is None:
|
230 |
+
filename = PLOT_FILENAME
|
231 |
+
|
232 |
+
# Convert to absolute path
|
233 |
+
filename = os.path.abspath(filename)
|
234 |
+
|
235 |
# Ensure the directory exists
|
236 |
+
os.makedirs(os.path.dirname(filename) or '.', exist_ok=True)
|
237 |
+
|
238 |
+
# Save the plot
|
239 |
+
plt.savefig(filename, bbox_inches='tight', dpi=100)
|
240 |
plt.close()
|
241 |
+
print(f"[DEBUG] Plot saved to: {filename}")
|
242 |
+
return filename
|
243 |
|
244 |
# Monkey patch DataFrame to add safe column access
|
245 |
import pandas as pd
|
|
|
267 |
script_content
|
268 |
)
|
269 |
|
270 |
+
# If no savefig call is found, add one at the end of the script
|
271 |
+
if 'plt.savefig(' not in script_content and 'plot_and_save(' not in script_content:
|
272 |
+
script_content += "\n# Ensure plot is saved\nif 'plt' in locals() and len(plt.get_fignums()) > 0:\n plot_and_save(plt, PLOT_FILENAME)\n"
|
273 |
+
|
274 |
# Write the updated script back
|
275 |
with open(temp_script_path, 'w') as f:
|
276 |
f.write(script_content)
|
|
|
297 |
generated_plot_path = None
|
298 |
plot_found = False
|
299 |
|
300 |
+
# First check the standard plot filename
|
301 |
+
plot_file_paths = ['generated_plot.png', 'plot.png', 'META_plot.png', 'AAPL_plot.png', 'MSFT_plot.png', 'output.png']
|
302 |
+
|
303 |
+
# Also check for any .png files in the current directory
|
304 |
+
current_dir = os.path.abspath('.')
|
305 |
+
png_files = [f for f in os.listdir(current_dir)
|
306 |
+
if f.endswith('.png') and not f.startswith('gradio_')]
|
307 |
+
|
308 |
+
# Add any found .png files to our search paths
|
309 |
+
plot_file_paths.extend(png_files)
|
310 |
+
|
311 |
+
# Make paths absolute and remove duplicates
|
312 |
+
plot_file_paths = list(dict.fromkeys([os.path.abspath(f) for f in plot_file_paths]))
|
313 |
+
|
314 |
+
print(f"[DEBUG] Looking for plot files in: {plot_file_paths}")
|
315 |
+
|
316 |
for plot_file in plot_file_paths:
|
317 |
+
try:
|
318 |
+
if os.path.exists(plot_file) and os.path.getsize(plot_file) > 0:
|
319 |
+
print(f"[DEBUG] Found plot file: {plot_file}")
|
320 |
+
generated_plot_path = plot_file
|
321 |
+
plot_found = True
|
322 |
+
break
|
323 |
+
except Exception as e:
|
324 |
+
print(f"[DEBUG] Error checking plot file {plot_file}: {e}")
|
325 |
|
326 |
# Add the plot to the execution output
|
327 |
try:
|