joshuarauh commited on
Commit
2a40c5d
·
verified ·
1 Parent(s): 8545288

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -41
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 imported SymPy names to globals
356
- for name in dir(module):
357
- if not name.startswith('_'): # Skip private names
358
- globals_dict[name] = getattr(module, name)
359
 
360
- logger.debug(f"Global namespace keys: {list(globals_dict.keys())}")
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
- try:
369
- # Execute the code and capture all output in one redirect_stdout block
370
- with redirect_stdout(output_buffer):
371
- print("SymPy Calculation Results:")
372
- print("-" * 25)
373
-
374
- # Execute the code
375
- exec(sympy_code, globals_dict, local_vars)
376
-
377
- # Print all variables that were created
378
- print("\nFinal Values:")
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
- result = output_buffer.getvalue().strip()
395
- logger.debug(f"Final output:\n{result}")
396
- return result
 
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)}")