joshuarauh commited on
Commit
209e476
·
verified ·
1 Parent(s): 6b15b0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -17
app.py CHANGED
@@ -346,33 +346,60 @@ def extract_and_run_sympy_code(response_text):
346
  logger.debug(f"Code after removing imports:\n{sympy_code}")
347
 
348
  # Create globals dict with all SymPy symbols
 
349
  import sys
350
- globals_dict = {}
351
 
352
- # Add built-ins
353
- globals_dict['__builtins__'] = __builtins__
354
 
355
- # Add sympy itself by loading it from sys.modules
356
- globals_dict.update({name: value for name, value in globals().items() if name in dir(sys.modules['sympy'])})
 
 
 
 
 
 
 
 
 
 
 
 
 
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:
 
346
  logger.debug(f"Code after removing imports:\n{sympy_code}")
347
 
348
  # Create globals dict with all SymPy symbols
349
+ import io
350
  import sys
351
+ from contextlib import redirect_stdout
352
 
353
+ # Import SymPy first at the function level
354
+ import sympy
355
 
356
+ # Create output buffer
357
+ output_buffer = io.StringIO()
358
+
359
+ # Create globals dict with SymPy module
360
+ globals_dict = {
361
+ '__builtins__': __builtins__,
362
+ 'symbols': sympy.symbols,
363
+ 'diff': sympy.diff,
364
+ 'solve': sympy.solve,
365
+ 'integrate': sympy.integrate,
366
+ 'oo': sympy.oo,
367
+ 'Sum': sympy.Sum
368
+ }
369
+
370
+ logger.debug(f"Globals dictionary contains: {list(globals_dict.keys())}")
371
 
372
  # Create locals dict to store variables
373
  local_vars = {}
374
 
375
+ # Execute the code and capture its output
376
+ try:
377
+ with redirect_stdout(output_buffer):
378
+ exec(sympy_code, globals_dict, local_vars)
379
+ stdout_output = output_buffer.getvalue()
380
+ logger.debug(f"Code execution stdout: {stdout_output}")
381
+ except Exception as e:
382
+ logger.error(f"Error during code execution: {str(e)}")
383
+ return f"Error executing code: {str(e)}"
384
+
385
+ # Format the final output
386
+ output_lines = ["SymPy Calculation Results:", "-" * 25]
387
+
388
+ # Add stdout if there was any
389
+ if stdout_output.strip():
390
+ output_lines.append(f"Output:")
391
+ output_lines.append(stdout_output.strip())
392
 
393
+ # Add variables
394
+ output_lines.append("\nVariables:")
395
+ for name, value in local_vars.items():
396
+ if not name.startswith('__') and not hasattr(value, '__module__'):
397
+ output_lines.append(f"{name} = {value}")
398
 
399
+ # Join all lines
400
+ final_output = "\n".join(output_lines)
401
+ logger.debug(f"Final formatted output:\n{final_output}")
 
 
402
 
 
 
 
403
  return final_output
404
 
405
  except Exception as e: