Spaces:
Running
Running
Update app.py
Browse filesfix the verification function to handle some specific types of calculus problems that come up in the single-variable case:
For definite integrals:
Fixed the regex pattern to properly handle the integral symbol ∫
Better extraction of integrand and limits
Properly formatted query for Wolfram Alpha
For implicit differentiation:
Added specific handling for dy/dx problems
Better extraction of the equation after the equals sign
Proper formatting for derivative queries
For Mean Value Theorem:
Added specific handling for MVT problems
Extracts function and interval
Formats the query to solve for the MVT value
app.py
CHANGED
@@ -60,32 +60,47 @@ def parse_questions(content):
|
|
60 |
def verify_solution(problem, answer):
|
61 |
"""Verify a mathematical solution using Wolfram Alpha"""
|
62 |
try:
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
# Extract the
|
79 |
-
|
80 |
-
if
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
else:
|
83 |
-
|
84 |
-
query = problem.replace('$$', '').replace('$', '')
|
85 |
query = re.sub(r'(?i)find|calculate|solve|evaluate|determine', '', query)
|
86 |
|
87 |
query = query.strip()
|
88 |
-
print(f"
|
89 |
|
90 |
result = wolfram_client.query(query)
|
91 |
|
@@ -93,17 +108,17 @@ def verify_solution(problem, answer):
|
|
93 |
return {
|
94 |
'verified': False,
|
95 |
'wolfram_solution': None,
|
96 |
-
'error': "Wolfram Alpha could not process
|
97 |
}
|
98 |
|
99 |
-
#
|
100 |
for pod in result.pods:
|
101 |
-
if pod.title in ['Result', 'Solution', 'Numerical result', 'Decimal approximation', '
|
102 |
wolfram_answer = pod.text
|
103 |
-
print(f"Wolfram
|
104 |
|
105 |
-
#
|
106 |
-
if answer
|
107 |
wolfram_nums = re.findall(r'[-+]?(?:\d*\.)?\d+', wolfram_answer)
|
108 |
if wolfram_nums:
|
109 |
wolfram_value = float(wolfram_nums[0])
|
@@ -114,11 +129,10 @@ def verify_solution(problem, answer):
|
|
114 |
'wolfram_solution': wolfram_answer,
|
115 |
'error': None
|
116 |
}
|
117 |
-
#
|
118 |
else:
|
119 |
-
# Clean up both answers for comparison
|
120 |
clean_wolfram = re.sub(r'\s+', '', wolfram_answer.lower())
|
121 |
-
clean_answer = re.sub(r'\s+', '', answer.lower())
|
122 |
is_verified = clean_wolfram == clean_answer
|
123 |
return {
|
124 |
'verified': is_verified,
|
@@ -135,10 +149,9 @@ def verify_solution(problem, answer):
|
|
135 |
return {
|
136 |
'verified': False,
|
137 |
'wolfram_solution': None,
|
138 |
-
'error': f"Error during verification: {str(e)}"
|
139 |
}
|
140 |
|
141 |
-
|
142 |
def generate_test(subject):
|
143 |
"""Generate and verify a math test"""
|
144 |
try:
|
|
|
60 |
def verify_solution(problem, answer):
|
61 |
"""Verify a mathematical solution using Wolfram Alpha"""
|
62 |
try:
|
63 |
+
# Clean the problem text first
|
64 |
+
clean_problem = problem.replace('$$', '').replace('$', '').strip()
|
65 |
+
|
66 |
+
# Case 1: Definite Integral
|
67 |
+
if '∫' in clean_problem or 'integral' in clean_problem.lower():
|
68 |
+
# Extract the integrand and limits using regex
|
69 |
+
integrand_match = re.search(r'\int_(\d+)\^(\d+)\s*\(?(.*?)\)?\s*dx', clean_problem)
|
70 |
+
if integrand_match:
|
71 |
+
lower, upper, integrand = integrand_match.groups()
|
72 |
+
# Format the query properly
|
73 |
+
query = f"integrate {integrand} from {lower} to {upper}"
|
74 |
+
print(f"Integral query: {query}")
|
75 |
+
|
76 |
+
# Case 2: Implicit Differentiation
|
77 |
+
elif 'dy/dx' in clean_problem or 'dy' in clean_problem:
|
78 |
+
# Extract the equation after the = sign
|
79 |
+
equation_match = re.search(r'=\s*(.*?)$', clean_problem)
|
80 |
+
if equation_match:
|
81 |
+
equation = equation_match.group(1)
|
82 |
+
query = f"d/dx of {equation}"
|
83 |
+
print(f"Differentiation query: {query}")
|
84 |
+
|
85 |
+
# Case 3: Mean Value Theorem
|
86 |
+
elif 'Mean Value Theorem' in clean_problem:
|
87 |
+
# Extract function and interval
|
88 |
+
func_match = re.search(r'f\(x\)\s*=\s*(.*?)\s+on', clean_problem)
|
89 |
+
interval_match = re.search(r'\[([\d.]+),\s*([\d.]+)\]', clean_problem)
|
90 |
+
if func_match and interval_match:
|
91 |
+
func = func_match.group(1)
|
92 |
+
a, b = interval_match.groups()
|
93 |
+
# Format query to find derivative and then solve f'(c) = [f(b)-f(a)]/(b-a)
|
94 |
+
query = f"solve (d/dx({func})) = ({func.replace('x', str(b))} - {func.replace('x', str(a))})/{b} - {a}"
|
95 |
+
print(f"MVT query: {query}")
|
96 |
+
|
97 |
+
# Default case
|
98 |
else:
|
99 |
+
query = clean_problem
|
|
|
100 |
query = re.sub(r'(?i)find|calculate|solve|evaluate|determine', '', query)
|
101 |
|
102 |
query = query.strip()
|
103 |
+
print(f"Final query to Wolfram Alpha: {query}")
|
104 |
|
105 |
result = wolfram_client.query(query)
|
106 |
|
|
|
108 |
return {
|
109 |
'verified': False,
|
110 |
'wolfram_solution': None,
|
111 |
+
'error': f"Wolfram Alpha could not process query: {query}"
|
112 |
}
|
113 |
|
114 |
+
# Process the result
|
115 |
for pod in result.pods:
|
116 |
+
if pod.title in ['Result', 'Solution', 'Numerical result', 'Decimal approximation', 'Definite integral']:
|
117 |
wolfram_answer = pod.text
|
118 |
+
print(f"Wolfram pod {pod.title}: {wolfram_answer}")
|
119 |
|
120 |
+
# Handle numerical answers
|
121 |
+
if str(answer).replace('.', '').isdigit():
|
122 |
wolfram_nums = re.findall(r'[-+]?(?:\d*\.)?\d+', wolfram_answer)
|
123 |
if wolfram_nums:
|
124 |
wolfram_value = float(wolfram_nums[0])
|
|
|
129 |
'wolfram_solution': wolfram_answer,
|
130 |
'error': None
|
131 |
}
|
132 |
+
# Handle symbolic answers
|
133 |
else:
|
|
|
134 |
clean_wolfram = re.sub(r'\s+', '', wolfram_answer.lower())
|
135 |
+
clean_answer = re.sub(r'\s+', '', str(answer).lower())
|
136 |
is_verified = clean_wolfram == clean_answer
|
137 |
return {
|
138 |
'verified': is_verified,
|
|
|
149 |
return {
|
150 |
'verified': False,
|
151 |
'wolfram_solution': None,
|
152 |
+
'error': f"Error during verification: {str(e)}\nQuery attempted: {query}"
|
153 |
}
|
154 |
|
|
|
155 |
def generate_test(subject):
|
156 |
"""Generate and verify a math test"""
|
157 |
try:
|