Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ import logging
|
|
8 |
import tempfile
|
9 |
from pathlib import Path
|
10 |
from sympy import *
|
|
|
|
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(
|
@@ -25,8 +27,64 @@ anthropic = Anthropic(
|
|
25 |
MAX_REQUESTS_PER_DAY = 25
|
26 |
request_history = deque(maxlen=1000)
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def load_proof_repository():
|
32 |
"""Load the proof repository from the repository file"""
|
@@ -332,64 +390,7 @@ def generate_question(subject, difficulty, question_type):
|
|
332 |
difficulty_params = get_difficulty_parameters(difficulty)
|
333 |
problem_type_addition = get_problem_type_addition(question_type)
|
334 |
|
335 |
-
|
336 |
-
system_prompt = f"""You are an expert mathematics professor creating a graduate-level exam question.
|
337 |
-
STRICT REQUIREMENTS:
|
338 |
-
1. Write exactly 1 graduate-level {question_type} question on {subject} covering {selected_topic}.
|
339 |
-
2. Advanced Difficulty Requirements:
|
340 |
-
This question must be suitable for PhD qualifying exams or advanced competitions.
|
341 |
-
MUST include:
|
342 |
-
- Novel applications of theoretical concepts
|
343 |
-
- Graduate-level mathematical reasoning
|
344 |
-
- Unexpected connections between different areas of {subject}
|
345 |
-
- Creative problem-solving approaches
|
346 |
-
- Rigorous proof requirements where applicable
|
347 |
-
|
348 |
-
Follow these specific constraints:
|
349 |
-
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
350 |
-
|
351 |
-
{problem_type_addition}
|
352 |
-
3. Style Reference:
|
353 |
-
Question should be {difficulty_params['example_style']}
|
354 |
-
4. The question MUST:
|
355 |
-
- Bridge multiple mathematical domains
|
356 |
-
- Require deep theoretical understanding
|
357 |
-
- Test mastery of advanced concepts
|
358 |
-
- Demand innovative solution approaches
|
359 |
-
5. For LaTeX formatting:
|
360 |
-
- Make sure that the question statement uses proper LaTeX math mode
|
361 |
-
- Use $ for inline math
|
362 |
-
- Use $$ on separate lines for equations and solutions
|
363 |
-
- Put each solution step on its own line in $$ $$
|
364 |
-
- DO NOT use \\begin{{aligned}} or similar environments
|
365 |
-
6. Include a detailed solution with thorough explanations of advanced concepts used
|
366 |
-
7. Maintain clear, precise formatting
|
367 |
-
8. At the end of the LaTeX solution output, print SymPy code that you would use to solve or verify the main equations in the question.
|
368 |
-
9. When writing SymPy code to verify solutions:
|
369 |
-
- Start with essential imports: from sympy import *
|
370 |
-
- Or use specific imports as needed: from sympy import Symbol, solve, limit, Abs, integrate, Function, diff, sin, cos
|
371 |
-
- DO NOT use NumPy, SciPy, or other libraries
|
372 |
-
- For numerical integration, use integrate() instead of scipy.integrate.quad()
|
373 |
-
- For any numerical computations, use native SymPy functions
|
374 |
-
- Always define symbolic variables using Symbol() before using them
|
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:
|
385 |
-
* Use Float() for decimal numbers
|
386 |
-
* Use float() for final printing of results
|
387 |
-
* Avoid evalf() as it may cause errors
|
388 |
-
- When working with series/sequences:
|
389 |
-
* Use Float() for sequence terms
|
390 |
-
* Convert sums to float() before printing"""
|
391 |
-
else:
|
392 |
-
system_prompt = f"""You are an expert mathematics professor creating a {difficulty_params['description']} exam question.
|
393 |
STRICT REQUIREMENTS:
|
394 |
1. Write exactly 1 {question_type} question on {subject} covering {selected_topic}.
|
395 |
2. Difficulty Level Guidelines:
|
@@ -416,38 +417,8 @@ STRICT REQUIREMENTS:
|
|
416 |
* ensure integration bounds align with the physical direction of the process being modeled
|
417 |
6. Maintain clear formatting
|
418 |
7. At the end of the LaTeX solution output, print SymPy code that you would use to solve or verify the main equations in the question.
|
419 |
-
8.
|
420 |
-
|
421 |
-
- For numerical integration, use SymPy's integrate() instead of scipy.integrate.quad()
|
422 |
-
- For any numerical computations, use SymPy's native functions
|
423 |
-
- Always define symbolic variables using Symbol() before using them
|
424 |
-
- Include print statements for ALL calculations and results
|
425 |
-
- Print intermediate steps and final answers
|
426 |
-
- Print variable values after they are computed
|
427 |
-
- Format numerical results using evalf() for decimal
|
428 |
-
9. When writing SymPy code to verify solutions:
|
429 |
-
- Start with essential imports: from sympy import *
|
430 |
-
- Or use specific imports as needed: from sympy import Symbol, symbols, solve, limit, Abs, integrate, Function, diff, sin, cos
|
431 |
-
- DO NOT use NumPy, SciPy, or other libraries
|
432 |
-
- For numerical integration, use integrate() instead of scipy.integrate.quad()
|
433 |
-
- For any numerical computations, use native SymPy functions
|
434 |
-
- Always define symbolic variables using Symbol() before using them
|
435 |
-
- Use Abs() for absolute value (not abs())
|
436 |
-
- Define functions using Function() class when needed
|
437 |
-
- Include print statements for ALL calculations and results
|
438 |
-
- Use simple print statements instead of f-strings for SymPy expressions
|
439 |
-
- Print intermediate steps and final answers
|
440 |
-
- Print variable values after they are computed
|
441 |
-
- Print expressions with labels on separate lines:
|
442 |
-
print("Expression label:")
|
443 |
-
print(expression)
|
444 |
-
- For numeric calculations:
|
445 |
-
* Use Float() for decimal numbers
|
446 |
-
* Use float() for final printing of results
|
447 |
-
* Avoid evalf() as it may cause errors
|
448 |
-
- When working with series/sequences:
|
449 |
-
* Use Float() for sequence terms
|
450 |
-
* Convert sums to float() before printing"""
|
451 |
|
452 |
#Consider
|
453 |
#When writing SymPy code:
|
@@ -508,8 +479,7 @@ STRICT REQUIREMENTS:
|
|
508 |
def extract_and_run_sympy_code_simple(response_text):
|
509 |
"""
|
510 |
Extract SymPy code from the response and execute it.
|
511 |
-
Handles SymPy expression printing safely
|
512 |
-
Returns the output exactly as SymPy would produce it.
|
513 |
"""
|
514 |
try:
|
515 |
# Extract code
|
@@ -524,22 +494,30 @@ def extract_and_run_sympy_code_simple(response_text):
|
|
524 |
|
525 |
sympy_code = response_text[code_start:code_end].strip()
|
526 |
|
527 |
-
#
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
538 |
"""
|
539 |
|
540 |
-
#
|
541 |
-
|
542 |
-
|
|
|
|
|
543 |
|
544 |
# Set up basic SymPy environment
|
545 |
import io
|
@@ -551,7 +529,10 @@ def safe_print_expr(label, expr):
|
|
551 |
"sympy": sympy,
|
552 |
"Symbol": sympy.Symbol,
|
553 |
"solve": sympy.solve,
|
|
|
|
|
554 |
"diff": sympy.diff,
|
|
|
555 |
"simplify": sympy.simplify,
|
556 |
"print": print,
|
557 |
"float": float
|
@@ -560,7 +541,7 @@ def safe_print_expr(label, expr):
|
|
560 |
# Capture output
|
561 |
output_buffer = io.StringIO()
|
562 |
with redirect_stdout(output_buffer):
|
563 |
-
exec(
|
564 |
|
565 |
return output_buffer.getvalue().strip() or "No output produced"
|
566 |
|
|
|
8 |
import tempfile
|
9 |
from pathlib import Path
|
10 |
from sympy import *
|
11 |
+
import json
|
12 |
+
from pathlib import Path
|
13 |
|
14 |
# Set up logging
|
15 |
logging.basicConfig(
|
|
|
27 |
MAX_REQUESTS_PER_DAY = 25
|
28 |
request_history = deque(maxlen=1000)
|
29 |
|
30 |
+
SYMPY_GUIDELINES = """
|
31 |
+
When writing SymPy code to verify solutions:
|
32 |
+
|
33 |
+
SYMPY_GUIDELINES = """
|
34 |
+
When writing SymPy code to verify solutions:
|
35 |
+
|
36 |
+
1. Imports and Library Usage:
|
37 |
+
- Start with: from sympy import *
|
38 |
+
- DO NOT use NumPy, SciPy, or other libraries
|
39 |
+
- For numerical integration, use integrate() instead of scipy.integrate.quad()
|
40 |
+
- For any numerical computations, use native SymPy functions only
|
41 |
+
|
42 |
+
2. Variable Declaration and Functions:
|
43 |
+
- Always define symbolic variables first, for example:
|
44 |
+
```python
|
45 |
+
x, y, z = symbols('x y z') # For multiple variables
|
46 |
+
t = Symbol('t') # For single variables
|
47 |
+
```
|
48 |
+
- Use Abs() for absolute value (not abs())
|
49 |
+
- Define functions using Function() class when needed
|
50 |
+
|
51 |
+
3. Solving and Computing:
|
52 |
+
- Never use strings in solve() or other SymPy functions:
|
53 |
+
CORRECT: solve(eq, x)
|
54 |
+
INCORRECT: solve(eq, 'x')
|
55 |
+
- Define equations symbolically:
|
56 |
+
CORRECT: eq = 2*sqrt(h) - sqrt(12) + 5*k
|
57 |
+
INCORRECT: eq = 2*sqrt('h') - sqrt(12) + 5*k
|
58 |
+
|
59 |
+
4. Printing and Output:
|
60 |
+
- Include print statements for ALL calculations and results
|
61 |
+
- Print intermediate steps and final answers
|
62 |
+
- Print variable values after they are computed
|
63 |
+
- Use simple print statements instead of f-strings for SymPy expressions
|
64 |
+
- Print expressions with labels on separate lines:
|
65 |
+
```python
|
66 |
+
print("Expression label:")
|
67 |
+
print(expression)
|
68 |
+
```
|
69 |
+
|
70 |
+
5. Numeric Calculations:
|
71 |
+
- Use Float() for decimal numbers in calculations
|
72 |
+
- Use float() for final printing of results
|
73 |
+
- Avoid evalf() as it may cause errors
|
74 |
+
- For numeric results:
|
75 |
+
```python
|
76 |
+
result = expression.evalf()
|
77 |
+
print("Result:")
|
78 |
+
print(float(result))
|
79 |
+
```
|
80 |
+
|
81 |
+
6. Working with Series and Sequences:
|
82 |
+
- Use Float() for sequence terms
|
83 |
+
- Convert sums to float() before printing
|
84 |
+
- For series calculations, print intermediate terms
|
85 |
+
"""
|
86 |
+
|
87 |
+
|
88 |
|
89 |
def load_proof_repository():
|
90 |
"""Load the proof repository from the repository file"""
|
|
|
390 |
difficulty_params = get_difficulty_parameters(difficulty)
|
391 |
problem_type_addition = get_problem_type_addition(question_type)
|
392 |
|
393 |
+
system_prompt = f"""You are an expert mathematics professor creating a {difficulty_params['description']} exam question.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
394 |
STRICT REQUIREMENTS:
|
395 |
1. Write exactly 1 {question_type} question on {subject} covering {selected_topic}.
|
396 |
2. Difficulty Level Guidelines:
|
|
|
417 |
* ensure integration bounds align with the physical direction of the process being modeled
|
418 |
6. Maintain clear formatting
|
419 |
7. At the end of the LaTeX solution output, print SymPy code that you would use to solve or verify the main equations in the question.
|
420 |
+
8. Observe the folloiwng SymPy Guidelines
|
421 |
+
{SYMPY_GUIDELINES}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
422 |
|
423 |
#Consider
|
424 |
#When writing SymPy code:
|
|
|
479 |
def extract_and_run_sympy_code_simple(response_text):
|
480 |
"""
|
481 |
Extract SymPy code from the response and execute it.
|
482 |
+
Handles SymPy expression printing safely and ensures proper symbol definition.
|
|
|
483 |
"""
|
484 |
try:
|
485 |
# Extract code
|
|
|
494 |
|
495 |
sympy_code = response_text[code_start:code_end].strip()
|
496 |
|
497 |
+
# Add wrapper function to ensure proper symbol definitions
|
498 |
+
wrapper_code = """
|
499 |
+
def run_sympy_verification():
|
500 |
+
# Import SymPy
|
501 |
+
from sympy import *
|
502 |
+
|
503 |
+
# Define standard helper functions
|
504 |
+
def safe_print_expr(label, expr):
|
505 |
+
print(label)
|
506 |
+
if expr is not None:
|
507 |
+
try:
|
508 |
+
float_val = float(expr)
|
509 |
+
print(float_val)
|
510 |
+
except:
|
511 |
+
print(expr)
|
512 |
+
|
513 |
+
# Start user code
|
514 |
"""
|
515 |
|
516 |
+
# Indent the original code
|
517 |
+
indented_code = '\n'.join(' ' + line for line in sympy_code.split('\n'))
|
518 |
+
|
519 |
+
# Add the execution call
|
520 |
+
full_code = wrapper_code + indented_code + "\n\n# Execute verification\nrun_sympy_verification()"
|
521 |
|
522 |
# Set up basic SymPy environment
|
523 |
import io
|
|
|
529 |
"sympy": sympy,
|
530 |
"Symbol": sympy.Symbol,
|
531 |
"solve": sympy.solve,
|
532 |
+
"sqrt": sympy.sqrt,
|
533 |
+
"pi": sympy.pi,
|
534 |
"diff": sympy.diff,
|
535 |
+
"integrate": sympy.integrate,
|
536 |
"simplify": sympy.simplify,
|
537 |
"print": print,
|
538 |
"float": float
|
|
|
541 |
# Capture output
|
542 |
output_buffer = io.StringIO()
|
543 |
with redirect_stdout(output_buffer):
|
544 |
+
exec(full_code, globals_dict)
|
545 |
|
546 |
return output_buffer.getvalue().strip() or "No output produced"
|
547 |
|