joshuarauh commited on
Commit
fab49e1
·
verified ·
1 Parent(s): 8191966

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -2
app.py CHANGED
@@ -375,6 +375,10 @@ STRICT REQUIREMENTS:
375
  - Use Abs() for absolute value (not abs())
376
  - Define functions using Function() class when needed
377
  - Include print statements for ALL calculations and results
 
 
 
 
378
  - Print intermediate steps and final answers
379
  - Print variable values after they are computed
380
  - For numeric calculations:
@@ -433,6 +437,9 @@ STRICT REQUIREMENTS:
433
  - Include print statements for ALL calculations and results
434
  - Print intermediate steps and final answers
435
  - Print variable values after they are computed
 
 
 
436
  - For numeric calculations:
437
  * Use Float() for decimal numbers
438
  * Use float() for final printing of results
@@ -500,6 +507,7 @@ STRICT REQUIREMENTS:
500
  def extract_and_run_sympy_code_simple(response_text):
501
  """
502
  Extract SymPy code from the response and execute it.
 
503
  Returns the output exactly as SymPy would produce it.
504
  """
505
  try:
@@ -515,21 +523,49 @@ def extract_and_run_sympy_code_simple(response_text):
515
 
516
  sympy_code = response_text[code_start:code_end].strip()
517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  # Set up basic SymPy environment
519
  import io
520
  import sympy
521
  from contextlib import redirect_stdout
522
 
 
 
 
 
 
 
 
 
 
 
 
523
  # Capture output
524
  output_buffer = io.StringIO()
525
  with redirect_stdout(output_buffer):
526
- exec(sympy_code, {"sympy": sympy, "print": print})
527
 
528
  return output_buffer.getvalue().strip() or "No output produced"
529
 
530
  except Exception as e:
531
  return f"Error executing SymPy code: {str(e)}"
532
-
533
  def check_and_resolve_discrepancy(initial_response, sympy_output):
534
  """
535
  Compare the SymPy output with the initial response and resolve any discrepancies
 
375
  - Use Abs() for absolute value (not abs())
376
  - Define functions using Function() class when needed
377
  - Include print statements for ALL calculations and results
378
+ - Use simple print statements instead of f-strings for SymPy expressions
379
+ - Print expressions with labels on separate lines:
380
+ print("Expression label:")
381
+ print(expression)
382
  - Print intermediate steps and final answers
383
  - Print variable values after they are computed
384
  - For numeric calculations:
 
437
  - Include print statements for ALL calculations and results
438
  - Print intermediate steps and final answers
439
  - Print variable values after they are computed
440
+ - Print expressions with labels on separate lines:
441
+ print("Expression label:")
442
+ print(expression)
443
  - For numeric calculations:
444
  * Use Float() for decimal numbers
445
  * Use float() for final printing of results
 
507
  def extract_and_run_sympy_code_simple(response_text):
508
  """
509
  Extract SymPy code from the response and execute it.
510
+ Handles SymPy expression printing safely without f-strings.
511
  Returns the output exactly as SymPy would produce it.
512
  """
513
  try:
 
523
 
524
  sympy_code = response_text[code_start:code_end].strip()
525
 
526
+ # Replace problematic f-strings with safe print statements
527
+ sympy_code = sympy_code.replace('f"', '"')
528
+ sympy_code = sympy_code.replace('{', '{{')
529
+ sympy_code = sympy_code.replace('}', '}}')
530
+ sympy_code = sympy_code.replace('print(f"', 'print("')
531
+
532
+ # Add safe printing function
533
+ safe_print_setup = """
534
+ def safe_print_expr(label, expr):
535
+ print(label)
536
+ print(expr)
537
+ """
538
+
539
+ # Replace f-string prints with safe prints
540
+ sympy_code = safe_print_setup + sympy_code
541
+ sympy_code = sympy_code.replace('print(f"', 'safe_print_expr("')
542
+
543
  # Set up basic SymPy environment
544
  import io
545
  import sympy
546
  from contextlib import redirect_stdout
547
 
548
+ # Create a dictionary of allowed names
549
+ globals_dict = {
550
+ "sympy": sympy,
551
+ "Symbol": sympy.Symbol,
552
+ "solve": sympy.solve,
553
+ "diff": sympy.diff,
554
+ "simplify": sympy.simplify,
555
+ "print": print,
556
+ "float": float
557
+ }
558
+
559
  # Capture output
560
  output_buffer = io.StringIO()
561
  with redirect_stdout(output_buffer):
562
+ exec(sympy_code, globals_dict)
563
 
564
  return output_buffer.getvalue().strip() or "No output produced"
565
 
566
  except Exception as e:
567
  return f"Error executing SymPy code: {str(e)}"
568
+
569
  def check_and_resolve_discrepancy(initial_response, sympy_output):
570
  """
571
  Compare the SymPy output with the initial response and resolve any discrepancies