joshuarauh commited on
Commit
543c39f
·
verified ·
1 Parent(s): 28c7324

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -53
app.py CHANGED
@@ -30,13 +30,7 @@ request_history = deque(maxlen=1000)
30
  SYMPY_GUIDELINES = """
31
  When writing SymPy code to verify solutions:
32
 
33
- 1. Imports and Library Usage:
34
- - Start with: from sympy import *
35
- - DO NOT use NumPy, SciPy, or other libraries
36
- - For numerical integration, use integrate() instead of scipy.integrate.quad()
37
- - For any numerical computations, use native SymPy functions only
38
-
39
- 2. Variable Declaration and Functions:
40
  - Always define symbolic variables first, for example:
41
  ```python
42
  x, y, z = symbols('x y z') # For multiple variables
@@ -45,7 +39,7 @@ When writing SymPy code to verify solutions:
45
  - Use Abs() for absolute value (not abs())
46
  - Define functions using Function() class when needed
47
 
48
- 3. Solving and Computing:
49
  - Never use strings in solve() or other SymPy functions:
50
  CORRECT: solve(eq, x)
51
  INCORRECT: solve(eq, 'x')
@@ -53,7 +47,7 @@ When writing SymPy code to verify solutions:
53
  CORRECT: eq = 2*sqrt(h) - sqrt(12) + 5*k
54
  INCORRECT: eq = 2*sqrt('h') - sqrt(12) + 5*k
55
 
56
- 4. Printing and Output:
57
  - Include print statements for ALL calculations and results
58
  - Print intermediate steps and final answers
59
  - Print variable values after they are computed
@@ -64,7 +58,7 @@ When writing SymPy code to verify solutions:
64
  print(expression)
65
  ```
66
 
67
- 5. Numeric Calculations:
68
  - Use Float() for decimal numbers in calculations
69
  - Use float() for final printing of results
70
  - Avoid evalf() as it may cause errors
@@ -75,10 +69,10 @@ When writing SymPy code to verify solutions:
75
  print(float(result))
76
  ```
77
 
78
- 6. Working with Series and Sequences:
79
  - Use Float() for sequence terms
80
  - Convert sums to float() before printing
81
- - For series calculations, print intermediate terms
82
  """
83
 
84
 
@@ -491,54 +485,37 @@ def extract_and_run_sympy_code_simple(response_text):
491
 
492
  sympy_code = response_text[code_start:code_end].strip()
493
 
494
- # Add wrapper function to ensure proper symbol definitions
495
- wrapper_code = """
496
- def run_sympy_verification():
497
- # Import SymPy
498
- from sympy import *
499
-
500
- # Define standard helper functions
501
- def safe_print_expr(label, expr):
502
- print(label)
503
- if expr is not None:
504
- try:
505
- float_val = float(expr)
506
- print(float_val)
507
- except:
508
- print(expr)
509
-
510
- # Start user code
511
- """
512
-
513
- # Indent the original code
514
- indented_code = '\n'.join(' ' + line for line in sympy_code.split('\n'))
515
 
516
- # Add the execution call
517
- full_code = wrapper_code + indented_code + "\n\n# Execute verification\nrun_sympy_verification()"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
 
519
- # Set up basic SymPy environment
520
  import io
521
- import sympy
522
  from contextlib import redirect_stdout
523
 
524
- # Create a dictionary of allowed names
525
- globals_dict = {
526
- "sympy": sympy,
527
- "Symbol": sympy.Symbol,
528
- "solve": sympy.solve,
529
- "sqrt": sympy.sqrt,
530
- "pi": sympy.pi,
531
- "diff": sympy.diff,
532
- "integrate": sympy.integrate,
533
- "simplify": sympy.simplify,
534
- "print": print,
535
- "float": float
536
- }
537
-
538
- # Capture output
539
  output_buffer = io.StringIO()
540
  with redirect_stdout(output_buffer):
541
- exec(full_code, globals_dict)
542
 
543
  return output_buffer.getvalue().strip() or "No output produced"
544
 
 
30
  SYMPY_GUIDELINES = """
31
  When writing SymPy code to verify solutions:
32
 
33
+ 1. Variable Declaration and Functions:
 
 
 
 
 
 
34
  - Always define symbolic variables first, for example:
35
  ```python
36
  x, y, z = symbols('x y z') # For multiple variables
 
39
  - Use Abs() for absolute value (not abs())
40
  - Define functions using Function() class when needed
41
 
42
+ 2. Solving and Computing:
43
  - Never use strings in solve() or other SymPy functions:
44
  CORRECT: solve(eq, x)
45
  INCORRECT: solve(eq, 'x')
 
47
  CORRECT: eq = 2*sqrt(h) - sqrt(12) + 5*k
48
  INCORRECT: eq = 2*sqrt('h') - sqrt(12) + 5*k
49
 
50
+ 3. Printing and Output:
51
  - Include print statements for ALL calculations and results
52
  - Print intermediate steps and final answers
53
  - Print variable values after they are computed
 
58
  print(expression)
59
  ```
60
 
61
+ 4. Numeric Calculations:
62
  - Use Float() for decimal numbers in calculations
63
  - Use float() for final printing of results
64
  - Avoid evalf() as it may cause errors
 
69
  print(float(result))
70
  ```
71
 
72
+ 5. Working with Series and Sequences:
73
  - Use Float() for sequence terms
74
  - Convert sums to float() before printing
75
+ - For series calculations, print intermediate terms
76
  """
77
 
78
 
 
485
 
486
  sympy_code = response_text[code_start:code_end].strip()
487
 
488
+ # Import SymPy at the module level
489
+ import sympy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490
 
491
+ # Create globals dict with all SymPy functions
492
+ globals_dict = {}
493
+ globals_dict.update(vars(sympy)) # This adds all SymPy functions
494
+ globals_dict.update({
495
+ 'print': print,
496
+ 'float': float,
497
+ 'Symbol': sympy.Symbol,
498
+ 'symbols': sympy.symbols,
499
+ 'solve': sympy.solve,
500
+ 'sqrt': sympy.sqrt,
501
+ 'pi': sympy.pi,
502
+ 'diff': sympy.diff,
503
+ 'integrate': sympy.integrate,
504
+ 'simplify': sympy.simplify,
505
+ })
506
+
507
+ # Remove the sympy import line from the code if present
508
+ lines = sympy_code.split('\n')
509
+ filtered_lines = [line for line in lines if not line.strip().startswith('from sympy import') and not line.strip().startswith('import sympy')]
510
+ modified_code = '\n'.join(filtered_lines)
511
 
512
+ # Capture output
513
  import io
 
514
  from contextlib import redirect_stdout
515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516
  output_buffer = io.StringIO()
517
  with redirect_stdout(output_buffer):
518
+ exec(modified_code, globals_dict)
519
 
520
  return output_buffer.getvalue().strip() or "No output produced"
521