Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -317,7 +317,6 @@ def extract_and_run_sympy_code(response_text):
|
|
317 |
"""
|
318 |
Extract SymPy code from the response and execute it.
|
319 |
Returns only the essential calculation results as a string.
|
320 |
-
Filters out debugging info and module imports.
|
321 |
"""
|
322 |
try:
|
323 |
# Find the SymPy code block
|
@@ -341,7 +340,7 @@ def extract_and_run_sympy_code(response_text):
|
|
341 |
# Create a string buffer to capture print output
|
342 |
output_buffer = io.StringIO()
|
343 |
|
344 |
-
# Create
|
345 |
sympy_globals = {
|
346 |
"Symbol": sympy.Symbol,
|
347 |
"symbols": sympy.symbols,
|
@@ -350,8 +349,10 @@ def extract_and_run_sympy_code(response_text):
|
|
350 |
"diff": sympy.diff,
|
351 |
"limit": sympy.limit,
|
352 |
"sqrt": sympy.sqrt,
|
|
|
353 |
"simplify": sympy.simplify,
|
354 |
-
"expand": sympy.expand
|
|
|
355 |
}
|
356 |
|
357 |
# Create locals dict to capture new variables
|
@@ -364,37 +365,39 @@ def extract_and_run_sympy_code(response_text):
|
|
364 |
# Get the printed output
|
365 |
printed_output = output_buffer.getvalue().strip()
|
366 |
|
367 |
-
#
|
368 |
-
|
369 |
-
calculation_results = {
|
370 |
-
name: value for name, value in local_vars.items()
|
371 |
-
if (
|
372 |
-
not name.startswith('__') and
|
373 |
-
not hasattr(value, '__call__') and # Exclude functions
|
374 |
-
not hasattr(value, '__module__') # Exclude modules
|
375 |
-
)
|
376 |
-
}
|
377 |
-
|
378 |
-
# Build the final output string
|
379 |
-
result_parts = []
|
380 |
|
381 |
-
# Add
|
382 |
-
|
383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
384 |
|
385 |
-
# Add
|
386 |
-
if
|
387 |
-
|
388 |
-
result_parts.append("\nCalculation Results:")
|
389 |
-
result_parts.append("-" * 20)
|
390 |
-
for var_name, value in calculation_results.items():
|
391 |
-
result_parts.append(f"{var_name} = {value}")
|
392 |
|
393 |
-
# If we have no results
|
394 |
-
if not
|
395 |
return "No calculation results produced."
|
396 |
|
397 |
-
return "\n".join(
|
398 |
|
399 |
except Exception as e:
|
400 |
return f"Error executing SymPy code: {str(e)}"
|
|
|
317 |
"""
|
318 |
Extract SymPy code from the response and execute it.
|
319 |
Returns only the essential calculation results as a string.
|
|
|
320 |
"""
|
321 |
try:
|
322 |
# Find the SymPy code block
|
|
|
340 |
# Create a string buffer to capture print output
|
341 |
output_buffer = io.StringIO()
|
342 |
|
343 |
+
# Create globals dict with minimal SymPy elements
|
344 |
sympy_globals = {
|
345 |
"Symbol": sympy.Symbol,
|
346 |
"symbols": sympy.symbols,
|
|
|
349 |
"diff": sympy.diff,
|
350 |
"limit": sympy.limit,
|
351 |
"sqrt": sympy.sqrt,
|
352 |
+
"exp": sympy.exp,
|
353 |
"simplify": sympy.simplify,
|
354 |
+
"expand": sympy.expand,
|
355 |
+
"print": print # Allow print statements
|
356 |
}
|
357 |
|
358 |
# Create locals dict to capture new variables
|
|
|
365 |
# Get the printed output
|
366 |
printed_output = output_buffer.getvalue().strip()
|
367 |
|
368 |
+
# Initialize result string
|
369 |
+
result = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
370 |
|
371 |
+
# Add numerical and symbolic calculation results
|
372 |
+
for name, value in local_vars.items():
|
373 |
+
# Skip private variables and functions
|
374 |
+
if name.startswith('__') or callable(value) or isinstance(value, type):
|
375 |
+
continue
|
376 |
+
|
377 |
+
# Skip module references
|
378 |
+
if hasattr(value, '__module__'):
|
379 |
+
continue
|
380 |
+
|
381 |
+
# Format the output
|
382 |
+
try:
|
383 |
+
# If it's a sympy expression or numerical result
|
384 |
+
if isinstance(value, (sympy.Basic, int, float)) or hasattr(value, 'free_symbols'):
|
385 |
+
result.append(f"{name} = {value}")
|
386 |
+
# If it's a list or tuple of sympy expressions
|
387 |
+
elif isinstance(value, (list, tuple)) and all(isinstance(x, (sympy.Basic, int, float)) for x in value):
|
388 |
+
result.append(f"{name} = {value}")
|
389 |
+
except:
|
390 |
+
continue
|
391 |
|
392 |
+
# Add printed output if it exists and isn't just whitespace
|
393 |
+
if printed_output and not printed_output.isspace():
|
394 |
+
result.insert(0, printed_output)
|
|
|
|
|
|
|
|
|
395 |
|
396 |
+
# If we have no results, provide a message
|
397 |
+
if not result:
|
398 |
return "No calculation results produced."
|
399 |
|
400 |
+
return "\n".join(result)
|
401 |
|
402 |
except Exception as e:
|
403 |
return f"Error executing SymPy code: {str(e)}"
|