Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -345,55 +345,35 @@ def extract_and_run_sympy_code(response_text):
|
|
| 345 |
sympy_code = '\n'.join(filtered_lines)
|
| 346 |
logger.debug(f"Code after removing imports:\n{sympy_code}")
|
| 347 |
|
| 348 |
-
# Create globals dict
|
| 349 |
-
import sys
|
| 350 |
-
module = sys.modules[__name__]
|
| 351 |
globals_dict = {
|
| 352 |
'__builtins__': __builtins__,
|
|
|
|
| 353 |
}
|
| 354 |
|
| 355 |
-
# Add all
|
| 356 |
-
for name in dir(
|
| 357 |
-
|
| 358 |
-
globals_dict[name] = getattr(module, name)
|
| 359 |
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
# Create locals dict and output buffer
|
| 363 |
local_vars = {}
|
| 364 |
-
import io
|
| 365 |
-
from contextlib import redirect_stdout
|
| 366 |
-
output_buffer = io.StringIO()
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
for var_name, value in local_vars.items():
|
| 380 |
-
if (not var_name.startswith('__') and
|
| 381 |
-
not hasattr(value, '__module__')):
|
| 382 |
-
print(f"{var_name} = {value}")
|
| 383 |
-
|
| 384 |
-
except NameError as e:
|
| 385 |
-
logger.error(f"NameError during execution: {str(e)}")
|
| 386 |
-
logger.debug(f"Available names: {list(globals_dict.keys())}")
|
| 387 |
-
import traceback
|
| 388 |
-
logger.error(f"Full traceback:\n{traceback.format_exc()}")
|
| 389 |
-
return f"Error executing SymPy code: {str(e)}"
|
| 390 |
-
except Exception as e:
|
| 391 |
-
logger.error(f"Other error during execution: {str(e)}")
|
| 392 |
-
return f"Error executing SymPy code: {str(e)}"
|
| 393 |
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
|
|
|
| 397 |
|
| 398 |
except Exception as e:
|
| 399 |
logger.error(f"Top-level error in extract_and_run_sympy_code: {str(e)}")
|
|
|
|
| 345 |
sympy_code = '\n'.join(filtered_lines)
|
| 346 |
logger.debug(f"Code after removing imports:\n{sympy_code}")
|
| 347 |
|
| 348 |
+
# Create globals dict with all SymPy symbols
|
|
|
|
|
|
|
| 349 |
globals_dict = {
|
| 350 |
'__builtins__': __builtins__,
|
| 351 |
+
'sympy': sympy, # Add the sympy module itself
|
| 352 |
}
|
| 353 |
|
| 354 |
+
# Add all sympy attributes to globals
|
| 355 |
+
for name in dir(sympy):
|
| 356 |
+
globals_dict[name] = getattr(sympy, name)
|
|
|
|
| 357 |
|
| 358 |
+
# Create locals dict to store variables
|
|
|
|
|
|
|
| 359 |
local_vars = {}
|
|
|
|
|
|
|
|
|
|
| 360 |
|
| 361 |
+
# Execute the code
|
| 362 |
+
exec(sympy_code, globals_dict, local_vars)
|
| 363 |
+
|
| 364 |
+
# Format output
|
| 365 |
+
output = ["SymPy Calculation Results:", "-" * 25, ""]
|
| 366 |
+
|
| 367 |
+
# Add all created variables and their values
|
| 368 |
+
for var_name, value in local_vars.items():
|
| 369 |
+
if (not var_name.startswith('__') and
|
| 370 |
+
not hasattr(value, '__module__')):
|
| 371 |
+
output.append(f"{var_name} = {value}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
|
| 373 |
+
# Join all output lines with newlines
|
| 374 |
+
final_output = "\n".join(output)
|
| 375 |
+
logger.debug(f"Final output:\n{final_output}")
|
| 376 |
+
return final_output
|
| 377 |
|
| 378 |
except Exception as e:
|
| 379 |
logger.error(f"Top-level error in extract_and_run_sympy_code: {str(e)}")
|