Spaces:
Running
Running
Update app.py
Browse filesFixed "query" not being assigned in some cases before used in the error handling
app.py
CHANGED
@@ -60,6 +60,9 @@ def parse_questions(content):
|
|
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 |
|
@@ -71,7 +74,18 @@ def verify_solution(problem, answer):
|
|
71 |
lower, upper, integrand = integrand_match.groups()
|
72 |
# Format the query properly
|
73 |
query = f"integrate {integrand} from {lower} to {upper}"
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
# Case 2: Implicit Differentiation
|
77 |
elif 'dy/dx' in clean_problem or 'dy' in clean_problem:
|
@@ -80,7 +94,12 @@ def verify_solution(problem, answer):
|
|
80 |
if equation_match:
|
81 |
equation = equation_match.group(1)
|
82 |
query = f"d/dx of {equation}"
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# Case 3: Mean Value Theorem
|
86 |
elif 'Mean Value Theorem' in clean_problem:
|
@@ -90,17 +109,28 @@ def verify_solution(problem, answer):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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
|
103 |
-
print(f"Final query to Wolfram Alpha: {query}")
|
104 |
|
105 |
result = wolfram_client.query(query)
|
106 |
|
@@ -145,11 +175,16 @@ def verify_solution(problem, answer):
|
|
145 |
'wolfram_solution': None,
|
146 |
'error': "No suitable solution found in Wolfram Alpha response"
|
147 |
}
|
|
|
148 |
except Exception as e:
|
|
|
|
|
|
|
|
|
149 |
return {
|
150 |
'verified': False,
|
151 |
'wolfram_solution': None,
|
152 |
-
'error':
|
153 |
}
|
154 |
|
155 |
def generate_test(subject):
|
|
|
60 |
def verify_solution(problem, answer):
|
61 |
"""Verify a mathematical solution using Wolfram Alpha"""
|
62 |
try:
|
63 |
+
# Initialize query variable
|
64 |
+
query = ""
|
65 |
+
|
66 |
# Clean the problem text first
|
67 |
clean_problem = problem.replace('$$', '').replace('$', '').strip()
|
68 |
|
|
|
74 |
lower, upper, integrand = integrand_match.groups()
|
75 |
# Format the query properly
|
76 |
query = f"integrate {integrand} from {lower} to {upper}"
|
77 |
+
else:
|
78 |
+
# Try alternative pattern for integral
|
79 |
+
integrand_match = re.search(r'\int.*?(\d+).*?(\d+).*?\((.*?)\)\s*dx', clean_problem)
|
80 |
+
if integrand_match:
|
81 |
+
lower, upper, integrand = integrand_match.groups()
|
82 |
+
query = f"integrate {integrand} from {lower} to {upper}"
|
83 |
+
else:
|
84 |
+
return {
|
85 |
+
'verified': False,
|
86 |
+
'wolfram_solution': None,
|
87 |
+
'error': "Could not parse integral expression"
|
88 |
+
}
|
89 |
|
90 |
# Case 2: Implicit Differentiation
|
91 |
elif 'dy/dx' in clean_problem or 'dy' in clean_problem:
|
|
|
94 |
if equation_match:
|
95 |
equation = equation_match.group(1)
|
96 |
query = f"d/dx of {equation}"
|
97 |
+
else:
|
98 |
+
return {
|
99 |
+
'verified': False,
|
100 |
+
'wolfram_solution': None,
|
101 |
+
'error': "Could not parse differential equation"
|
102 |
+
}
|
103 |
|
104 |
# Case 3: Mean Value Theorem
|
105 |
elif 'Mean Value Theorem' in clean_problem:
|
|
|
109 |
if func_match and interval_match:
|
110 |
func = func_match.group(1)
|
111 |
a, b = interval_match.groups()
|
|
|
112 |
query = f"solve (d/dx({func})) = ({func.replace('x', str(b))} - {func.replace('x', str(a))})/{b} - {a}"
|
113 |
+
else:
|
114 |
+
return {
|
115 |
+
'verified': False,
|
116 |
+
'wolfram_solution': None,
|
117 |
+
'error': "Could not parse Mean Value Theorem problem"
|
118 |
+
}
|
119 |
|
120 |
# Default case
|
121 |
else:
|
122 |
query = clean_problem
|
123 |
query = re.sub(r'(?i)find|calculate|solve|evaluate|determine', '', query)
|
124 |
+
|
125 |
+
# Ensure query is not empty
|
126 |
+
if not query.strip():
|
127 |
+
return {
|
128 |
+
'verified': False,
|
129 |
+
'wolfram_solution': None,
|
130 |
+
'error': "Could not generate valid query"
|
131 |
+
}
|
132 |
|
133 |
+
print(f"Sending query to Wolfram Alpha: {query}")
|
|
|
134 |
|
135 |
result = wolfram_client.query(query)
|
136 |
|
|
|
175 |
'wolfram_solution': None,
|
176 |
'error': "No suitable solution found in Wolfram Alpha response"
|
177 |
}
|
178 |
+
|
179 |
except Exception as e:
|
180 |
+
# Now query will always be defined when we get here
|
181 |
+
error_msg = f"Error during verification: {str(e)}"
|
182 |
+
if query:
|
183 |
+
error_msg += f"\nQuery attempted: {query}"
|
184 |
return {
|
185 |
'verified': False,
|
186 |
'wolfram_solution': None,
|
187 |
+
'error': error_msg
|
188 |
}
|
189 |
|
190 |
def generate_test(subject):
|