Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -24,6 +24,11 @@ anthropic = Anthropic(
|
|
24 |
api_key=os.environ.get('ANTHROPIC_API_KEY')
|
25 |
)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
27 |
# Request tracking
|
28 |
MAX_REQUESTS_PER_DAY = 500
|
29 |
request_history = deque(maxlen=1000)
|
@@ -509,6 +514,136 @@ The computation question MAY NOT:
|
|
509 |
}
|
510 |
return problem_type_additions.get(question_type, "")
|
511 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
512 |
def generate_question(subject, difficulty, question_type, use_enhancement=False):
|
513 |
"""Generate a single math question with additional verification"""
|
514 |
try:
|
@@ -552,7 +687,8 @@ def generate_question(subject, difficulty, question_type, use_enhancement=False)
|
|
552 |
system_prompt = f"""You are an expert mathematics professor creating a {difficulty_params['description']} exam question.
|
553 |
STRICT REQUIREMENTS:
|
554 |
1. Write exactly 1 {question_type} question on {subject} covering {selected_topic} that can be solved analytically without numerical methods.
|
555 |
-
A question where any part of the solution must resort to numerical
|
|
|
556 |
2. Difficulty Level Guidelines:
|
557 |
{difficulty_params['description'].upper()}
|
558 |
Follow these specific constraints:
|
@@ -570,6 +706,7 @@ STRICT REQUIREMENTS:
|
|
570 |
- When writing questions involving currency expressed in dollars NEVER use the `$` symbol as it will be interepreted as math mode. ALWAYS write out the word dollars.
|
571 |
* Example: 1000 dollars
|
572 |
5. Include a detailed solution
|
|
|
573 |
- If the question involves geometry make sure to identify any general geometric formulas that apply, For example:
|
574 |
* Areas/volumes of common shapes and solids
|
575 |
* Cross-sectional areas of geometric figures
|
@@ -676,6 +813,14 @@ j. **Concluding and Intuitive Explanations**
|
|
676 |
final_verification = perform_final_verification(revised_solution, sympy_correct)
|
677 |
response_text += "\n\nFinal Expert Verification:\n" + final_verification
|
678 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
679 |
# Create LaTeX content
|
680 |
questions_latex = create_latex_document(response_text, questions_only=True)
|
681 |
full_latex = create_latex_document(response_text, questions_only=False)
|
|
|
24 |
api_key=os.environ.get('ANTHROPIC_API_KEY')
|
25 |
)
|
26 |
|
27 |
+
openai.api_key = os.getenv("My_MathTest_key")
|
28 |
+
|
29 |
+
if openai.api_key is None:
|
30 |
+
raise ValueError("OpenAI API key not found.")
|
31 |
+
|
32 |
# Request tracking
|
33 |
MAX_REQUESTS_PER_DAY = 500
|
34 |
request_history = deque(maxlen=1000)
|
|
|
514 |
}
|
515 |
return problem_type_additions.get(question_type, "")
|
516 |
|
517 |
+
def get_solution_for_verification(response_text, sympy_correct, final_verification=None):
|
518 |
+
"""
|
519 |
+
Extract the relevant parts of the solution for verification based on whether
|
520 |
+
the original solution was correct or not. Always preserves the original question.
|
521 |
+
"""
|
522 |
+
# Extract the question using the specific markers
|
523 |
+
question_start = "Here is a test question"
|
524 |
+
solution_start = "Here is a detailed solution to the test question"
|
525 |
+
|
526 |
+
# Find the question section
|
527 |
+
q_start = response_text.find(question_start)
|
528 |
+
q_end = response_text.find(solution_start)
|
529 |
+
|
530 |
+
if q_start == -1 or q_end == -1:
|
531 |
+
logger.error("Could not find question markers")
|
532 |
+
return response_text.strip()
|
533 |
+
|
534 |
+
question = response_text[q_start:q_end].strip()
|
535 |
+
|
536 |
+
# If no sympy verification was done, treat as correct and use original
|
537 |
+
if sympy_correct is None:
|
538 |
+
sympy_correct = True
|
539 |
+
|
540 |
+
if sympy_correct:
|
541 |
+
# Use original solution - need to extract before SymPy code
|
542 |
+
original_solution = parts[1]
|
543 |
+
sympy_start = original_solution.find('```python')
|
544 |
+
if sympy_start != -1:
|
545 |
+
solution = original_solution[:sympy_start].strip()
|
546 |
+
else:
|
547 |
+
solution = original_solution.strip()
|
548 |
+
else:
|
549 |
+
# Use final verified solution
|
550 |
+
if final_verification:
|
551 |
+
# Extract the solution after the marker
|
552 |
+
marker = "Here is the complete verified solution:"
|
553 |
+
if marker in final_verification:
|
554 |
+
solution = final_verification.split(marker)[1].strip()
|
555 |
+
else:
|
556 |
+
# Fallback to using the whole verification text
|
557 |
+
solution = final_verification.strip()
|
558 |
+
else:
|
559 |
+
# Fallback to original if no final verification
|
560 |
+
solution = parts[1].strip()
|
561 |
+
|
562 |
+
# Combine question and appropriate solution
|
563 |
+
complete_text = f"{question}\n\nSolution:\n{solution}"
|
564 |
+
return complete_text
|
565 |
+
|
566 |
+
def verify_with_chatgpt(question_and_solution):
|
567 |
+
"""
|
568 |
+
Send the solution to ChatGPT for verification and grading.
|
569 |
+
Returns the verification response.
|
570 |
+
"""
|
571 |
+
try:
|
572 |
+
# Construct the prompt for ChatGPT
|
573 |
+
verification_prompt = f"""As an expert mathematician, please verify and grade this mathematics solution.
|
574 |
+
|
575 |
+
Analyze the following aspects:
|
576 |
+
1. Mathematical Correctness (50 points):
|
577 |
+
- Are all calculations correct?
|
578 |
+
- Are proofs logically sound?
|
579 |
+
- Are all steps properly justified?
|
580 |
+
|
581 |
+
2. Completeness (20 points):
|
582 |
+
- Are all necessary cases considered?
|
583 |
+
- Are edge cases addressed?
|
584 |
+
- Are all required steps shown?
|
585 |
+
|
586 |
+
3. Clarity and Presentation (20 points):
|
587 |
+
- Is the solution well-organized?
|
588 |
+
- Are steps clearly explained?
|
589 |
+
- Is mathematical notation used correctly?
|
590 |
+
|
591 |
+
4. Mathematical Sophistication (10 points):
|
592 |
+
- Is the approach elegant?
|
593 |
+
- Are efficient methods used?
|
594 |
+
- Is mathematical insight demonstrated?
|
595 |
+
|
596 |
+
Question and Solution to Verify:
|
597 |
+
{question_and_solution}
|
598 |
+
|
599 |
+
Please provide:
|
600 |
+
1. A brief point-by-point analysis of the solution
|
601 |
+
2. Specific comments on any errors or oversights
|
602 |
+
3. Suggestions for improvement (if any)
|
603 |
+
4. A numerical score out of 100 based on the criteria above
|
604 |
+
|
605 |
+
Format your response with clear headers and bullet points."""
|
606 |
+
|
607 |
+
# Call OpenAI API
|
608 |
+
response = openai.ChatCompletion.create(
|
609 |
+
model="gpt-4o", # Using GPT-4o model
|
610 |
+
messages=[
|
611 |
+
{"role": "system", "content": "You are an expert mathematics professor grading student solutions."},
|
612 |
+
{"role": "user", "content": verification_prompt}
|
613 |
+
],
|
614 |
+
temperature=0.3
|
615 |
+
)
|
616 |
+
|
617 |
+
# Extract the verification text from the response
|
618 |
+
verification_text = response.choices[0].message.content
|
619 |
+
|
620 |
+
return verification_text
|
621 |
+
|
622 |
+
except Exception as e:
|
623 |
+
logger.error(f"Error in ChatGPT verification: {str(e)}")
|
624 |
+
return f"Error in ChatGPT verification: {str(e)}"
|
625 |
+
|
626 |
+
def append_chatgpt_verification(initial_response, sympy_correct, final_verification=None):
|
627 |
+
"""
|
628 |
+
Main function to handle the ChatGPT verification process.
|
629 |
+
Returns the original response with the ChatGPT verification appended.
|
630 |
+
"""
|
631 |
+
try:
|
632 |
+
# Get the appropriate solution text for verification
|
633 |
+
solution_text = get_solution_for_verification(initial_response, sympy_correct, final_verification)
|
634 |
+
|
635 |
+
# Get ChatGPT's verification
|
636 |
+
chatgpt_verification = verify_with_chatgpt(solution_text)
|
637 |
+
|
638 |
+
# Append verification to the response
|
639 |
+
full_response = f"{initial_response}\n\nChatGPT Verification and Grading:\n{chatgpt_verification}"
|
640 |
+
|
641 |
+
return full_response
|
642 |
+
|
643 |
+
except Exception as e:
|
644 |
+
logger.error(f"Error in verification process: {str(e)}")
|
645 |
+
return initial_response + f"\n\nError in ChatGPT verification: {str(e)}"
|
646 |
+
|
647 |
def generate_question(subject, difficulty, question_type, use_enhancement=False):
|
648 |
"""Generate a single math question with additional verification"""
|
649 |
try:
|
|
|
687 |
system_prompt = f"""You are an expert mathematics professor creating a {difficulty_params['description']} exam question.
|
688 |
STRICT REQUIREMENTS:
|
689 |
1. Write exactly 1 {question_type} question on {subject} covering {selected_topic} that can be solved analytically without numerical methods.
|
690 |
+
- A question where any part of the solution must resort to numerical methods is invalid.
|
691 |
+
- Begin the output with the text "Here is a test question that is a {question_type} question on {subject} covering {selected_topic}."
|
692 |
2. Difficulty Level Guidelines:
|
693 |
{difficulty_params['description'].upper()}
|
694 |
Follow these specific constraints:
|
|
|
706 |
- When writing questions involving currency expressed in dollars NEVER use the `$` symbol as it will be interepreted as math mode. ALWAYS write out the word dollars.
|
707 |
* Example: 1000 dollars
|
708 |
5. Include a detailed solution
|
709 |
+
- Begin the solution with "Here is a detailed solution to the test question."
|
710 |
- If the question involves geometry make sure to identify any general geometric formulas that apply, For example:
|
711 |
* Areas/volumes of common shapes and solids
|
712 |
* Cross-sectional areas of geometric figures
|
|
|
813 |
final_verification = perform_final_verification(revised_solution, sympy_correct)
|
814 |
response_text += "\n\nFinal Expert Verification:\n" + final_verification
|
815 |
|
816 |
+
# add the ChatGPT verification
|
817 |
+
if sympy_output:
|
818 |
+
response_text = append_chatgpt_verification(
|
819 |
+
response_text,
|
820 |
+
sympy_correct,
|
821 |
+
final_verification if has_discrepancy else None
|
822 |
+
)
|
823 |
+
|
824 |
# Create LaTeX content
|
825 |
questions_latex = create_latex_document(response_text, questions_only=True)
|
826 |
full_latex = create_latex_document(response_text, questions_only=False)
|