joshuarauh commited on
Commit
bbf54a8
·
verified ·
1 Parent(s): 17cdf31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -7
app.py CHANGED
@@ -289,13 +289,15 @@ STRICT REQUIREMENTS:
289
 
290
  # Execute SymPy code and append results
291
  sympy_output = extract_and_run_sympy_code(response_text)
 
292
  if sympy_output:
293
- response_text += "\n\nSymPy Verification Results:\n```\n" + sympy_output + "```"
294
-
295
- # Then continue with the existing code:
296
- # Create LaTeX content
297
- questions_latex = create_latex_document(response_text, questions_only=True)
298
- full_latex = create_latex_document(response_text, questions_only=False)
 
299
 
300
  # Create LaTeX content
301
  questions_latex = create_latex_document(response_text, questions_only=True)
@@ -352,7 +354,10 @@ def extract_and_run_sympy_code(response_text):
352
  "exp": sympy.exp,
353
  "simplify": sympy.simplify,
354
  "expand": sympy.expand,
355
- "print": print # Allow print statements
 
 
 
356
  }
357
 
358
  # Create locals dict to capture new variables
@@ -401,7 +406,50 @@ def extract_and_run_sympy_code(response_text):
401
 
402
  except Exception as e:
403
  return f"Error executing SymPy code: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  # Create Gradio interface
406
  with gr.Blocks() as interface:
407
  gr.Markdown("# Advanced Mathematics Question Generator")
 
289
 
290
  # Execute SymPy code and append results
291
  sympy_output = extract_and_run_sympy_code(response_text)
292
+
293
  if sympy_output:
294
+ # Check if SymPy ran successfully
295
+ if "Error" not in sympy_output:
296
+ resolution = check_and_resolve_discrepancy(response_text, sympy_output)
297
+ response_text = f"{response_text}\n\nSymPy Verification Results:\n```\n{sympy_output}\n```\n\nVerification Analysis:\n{resolution}"
298
+ else:
299
+ # Just append SymPy results if there was an error
300
+ response_text += f"\n\nSymPy Verification Results:\n```\n{sympy_output}\n```"
301
 
302
  # Create LaTeX content
303
  questions_latex = create_latex_document(response_text, questions_only=True)
 
354
  "exp": sympy.exp,
355
  "simplify": sympy.simplify,
356
  "expand": sympy.expand,
357
+ "print": print, # Allow print statements
358
+ "pi": sympy.pi,
359
+ "N": sympy.N,
360
+ "evalf": lambda x: x.evalf()
361
  }
362
 
363
  # Create locals dict to capture new variables
 
406
 
407
  except Exception as e:
408
  return f"Error executing SymPy code: {str(e)}"
409
+
410
+ def check_and_resolve_discrepancy(initial_response, sympy_output):
411
+ """
412
+ Compare the SymPy output with the initial response and resolve any discrepancies
413
+ by making another API call to Claude.
414
+ """
415
+ try:
416
+ # Create prompt showing both solutions and asking for resolution
417
+ resolution_prompt = f"""Here is a mathematics question with two solutions.
418
+
419
+ If the two answers are consistent with each other
420
+ then please say that they are consistent and briefly explain why.
421
 
422
+ If the two answers are inconsistent with each other then please:
423
+
424
+ 1. Identify which solution is correct
425
+ 2. Explain the error in the incorrect solution
426
+ 3. Provide a revised complete solution that fixes any errors
427
+
428
+ Original solution:
429
+ {initial_response}
430
+
431
+ SymPy Verification Results:
432
+ {sympy_output}
433
+
434
+
435
+ Please maintain the same LaTeX formatting as the original solution."""
436
+
437
+ # Make API call for resolution
438
+ message = anthropic.messages.create(
439
+ model="claude-3-5-sonnet-20241022",
440
+ max_tokens=4096,
441
+ temperature=0.2, # Lower temperature for verification
442
+ messages=[{
443
+ "role": "user",
444
+ "content": resolution_prompt
445
+ }]
446
+ )
447
+
448
+ return message.content[0].text
449
+ except Exception as e:
450
+ logger.error(f"Error in discrepancy resolution: {str(e)}")
451
+ return initial_response # Return original response if resolution fails
452
+
453
  # Create Gradio interface
454
  with gr.Blocks() as interface:
455
  gr.Markdown("# Advanced Mathematics Question Generator")