joshuarauh commited on
Commit
02551fc
·
verified ·
1 Parent(s): 71d66d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -9
app.py CHANGED
@@ -408,7 +408,7 @@ The computation question MAY NOT:
408
  return problem_type_additions.get(question_type, "")
409
 
410
  def generate_question(subject, difficulty, question_type):
411
- """Generate a single math question"""
412
  try:
413
  if not os.environ.get('ANTHROPIC_API_KEY'):
414
  logger.error("Anthropic API key not found")
@@ -508,10 +508,15 @@ STRICT REQUIREMENTS:
508
  if sympy_output:
509
  # Check if SymPy ran successfully
510
  if "Error" not in sympy_output:
511
- resolution = check_and_resolve_discrepancy(response_text, sympy_output)
512
- response_text = f"{response_text}\n\nSymPy Verification Results:\n```\n{sympy_output}\n```\n\nVerification Analysis:\n{resolution}"
 
 
 
 
 
 
513
  else:
514
- # Just append SymPy results if there was an error
515
  response_text += f"\n\nSymPy Verification Results:\n```\n{sympy_output}\n```"
516
 
517
  # Create LaTeX content
@@ -528,8 +533,8 @@ STRICT REQUIREMENTS:
528
 
529
  except Exception as e:
530
  logger.error(f"Error generating question: {str(e)}")
531
- return f"Error: {str(e)}", None, Non
532
-
533
  def extract_and_run_sympy_code_simple(response_text):
534
  """
535
  Extract SymPy code from the response and execute it.
@@ -587,6 +592,7 @@ def extract_and_run_sympy_code_simple(response_text):
587
  def check_and_resolve_discrepancy(initial_response, sympy_output):
588
  """
589
  Compare the SymPy output with the initial response and resolve any discrepancies.
 
590
  """
591
  try:
592
  resolution_prompt = f"""Here is a mathematics question with two answers.
@@ -599,7 +605,8 @@ def check_and_resolve_discrepancy(initial_response, sympy_output):
599
  If the two answers are inconsistent with each other then please:
600
  1. Identify which solution is correct
601
  2. Explain the error in the incorrect solution
602
- 3. Provide a revised complete solution that fixes any errors and does not refer to SymPy
 
603
 
604
  Original solution:
605
  {initial_response}
@@ -626,12 +633,76 @@ Please maintain the same LaTeX formatting as the original solution."""
626
  if "```python" in resolution_text:
627
  new_sympy_output = extract_and_run_sympy_code_simple(resolution_text)
628
  resolution_text += "\n\nNew SymPy Verification Results:\n```\n" + new_sympy_output + "\n```"
 
 
 
 
 
 
 
 
 
 
 
 
 
629
 
630
- return resolution_text
631
  except Exception as e:
632
  logger.error(f"Error in discrepancy resolution: {str(e)}")
633
- return f"Error in resolution: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
634
 
 
 
 
 
 
 
 
 
 
 
635
  # Create Gradio interface
636
  with gr.Blocks() as interface:
637
  gr.Markdown("# Advanced Mathematics Question Generator")
 
408
  return problem_type_additions.get(question_type, "")
409
 
410
  def generate_question(subject, difficulty, question_type):
411
+ """Generate a single math question with additional verification for difficulty 5"""
412
  try:
413
  if not os.environ.get('ANTHROPIC_API_KEY'):
414
  logger.error("Anthropic API key not found")
 
508
  if sympy_output:
509
  # Check if SymPy ran successfully
510
  if "Error" not in sympy_output:
511
+ resolution_text, has_discrepancy, revised_solution = check_and_resolve_discrepancy(response_text, sympy_output)
512
+ response_text = f"{response_text}\n\nSymPy Verification Results:\n```\n{sympy_output}\n```\n\nVerification Analysis:\n{resolution_text}"
513
+
514
+ # For difficulty level 5 AND when there's a discrepancy with a revised solution
515
+ if difficulty == 5 and has_discrepancy and revised_solution:
516
+ logger.debug("Performing final verification for difficulty 5 problem with discrepancy")
517
+ final_verification = perform_final_verification(revised_solution)
518
+ response_text += "\n\nFinal Expert Verification:\n" + final_verification
519
  else:
 
520
  response_text += f"\n\nSymPy Verification Results:\n```\n{sympy_output}\n```"
521
 
522
  # Create LaTeX content
 
533
 
534
  except Exception as e:
535
  logger.error(f"Error generating question: {str(e)}")
536
+ return f"Error: {str(e)}", None, None
537
+
538
  def extract_and_run_sympy_code_simple(response_text):
539
  """
540
  Extract SymPy code from the response and execute it.
 
592
  def check_and_resolve_discrepancy(initial_response, sympy_output):
593
  """
594
  Compare the SymPy output with the initial response and resolve any discrepancies.
595
+ Returns tuple of (resolution_text, has_discrepancy, revised_solution)
596
  """
597
  try:
598
  resolution_prompt = f"""Here is a mathematics question with two answers.
 
605
  If the two answers are inconsistent with each other then please:
606
  1. Identify which solution is correct
607
  2. Explain the error in the incorrect solution
608
+ 3. Provide a revised COMPLETE solution that fixes any errors, does not refer to SymPy,
609
+ and does not refer to the Original solution even if this means large parts of the Original solution are repeated.
610
 
611
  Original solution:
612
  {initial_response}
 
633
  if "```python" in resolution_text:
634
  new_sympy_output = extract_and_run_sympy_code_simple(resolution_text)
635
  resolution_text += "\n\nNew SymPy Verification Results:\n```\n" + new_sympy_output + "\n```"
636
+
637
+ # Determine if there was a discrepancy that required a revised solution
638
+ has_discrepancy = "revised complete solution" in resolution_text.lower()
639
+
640
+ # Extract revised solution if it exists
641
+ revised_solution = None
642
+ if has_discrepancy:
643
+ # Look for the revised solution after the explanation
644
+ solution_parts = resolution_text.split("revised complete solution", maxsplit=1)
645
+ if len(solution_parts) > 1:
646
+ revised_solution = solution_parts[1].strip()
647
+
648
+ return resolution_text, has_discrepancy, revised_solution
649
 
 
650
  except Exception as e:
651
  logger.error(f"Error in discrepancy resolution: {str(e)}")
652
+ return f"Error in resolution: {str(e)}", False, None
653
+
654
+ def perform_final_verification(revised_solution):
655
+ """
656
+ Perform a final verification of the revised solution for difficulty level 5 problems.
657
+ """
658
+ verification_prompt = f"""As an expert mathematician, please carefully verify this revised solution to an advanced (graduate-level) mathematics problem.
659
+
660
+ Revised Solution to Verify:
661
+ {revised_solution}
662
+
663
+ Please:
664
+ 1. Verify the mathematical correctness of the solution
665
+ 2. Check for any subtle errors or missing cases
666
+ 3. Verify that all assumptions are clearly stated
667
+ 4. Ensure the solution is complete and rigorous
668
+ 5. Check that any series convergence, existence conditions, or boundary cases are properly addressed
669
+
670
+ If you find any issues:
671
+ 1. Clearly explain what is incorrect or missing
672
+ 2. Provide a COMPLETE corrected solution
673
+ 3. Maintain the same LaTeX formatting as the original
674
+ 4. Include any missing assumptions or conditions
675
+ 5. If relevant, provide corrected SymPy code
676
+
677
+ If the solution is correct:
678
+ Briefly explain why the solution is mathematically sound and complete.
679
+
680
+ Please ensure any corrected solution maintains proper LaTeX formatting with $ for inline math and $$ on separate lines for displayed equations."""
681
+
682
+ try:
683
+ # Make API call for final verification
684
+ message = anthropic.messages.create(
685
+ model="claude-3-5-sonnet-20241022",
686
+ max_tokens=4096,
687
+ temperature=0.2,
688
+ messages=[{
689
+ "role": "user",
690
+ "content": verification_prompt
691
+ }]
692
+ )
693
+
694
+ verification_result = message.content[0].text
695
 
696
+ # If verification includes new SymPy code, run it
697
+ if "```python" in verification_result:
698
+ new_sympy_output = extract_and_run_sympy_code_simple(verification_result)
699
+ verification_result += "\n\nFinal SymPy Verification:\n```\n" + new_sympy_output + "\n```"
700
+
701
+ return verification_result
702
+ except Exception as e:
703
+ logger.error(f"Error in final verification: {str(e)}")
704
+ return f"Error in final verification: {str(e)}"
705
+
706
  # Create Gradio interface
707
  with gr.Blocks() as interface:
708
  gr.Markdown("# Advanced Mathematics Question Generator")