Spaces:
Sleeping
Sleeping
File size: 12,678 Bytes
bfc1cf6 ef21c2f bfc1cf6 109daa0 bfc1cf6 ef21c2f bfc1cf6 109daa0 e6857f6 109daa0 e6857f6 109daa0 e6857f6 fca8de1 e6857f6 109daa0 e6857f6 109daa0 fca8de1 ef21c2f bfc1cf6 379d2b4 bad6e28 ac3d1e8 bad6e28 ac3d1e8 bad6e28 ac3d1e8 379d2b4 ac3d1e8 379d2b4 ac3d1e8 bad6e28 ac3d1e8 bad6e28 ac3d1e8 bad6e28 ac3d1e8 bad6e28 ac3d1e8 379d2b4 ac3d1e8 379d2b4 1c1a2f5 ac3d1e8 ef21c2f bfc1cf6 e6857f6 bad6e28 e6857f6 bad6e28 fca8de1 ac3d1e8 fca8de1 bad6e28 1c1a2f5 ac3d1e8 bad6e28 1c1a2f5 bad6e28 1c1a2f5 bad6e28 1c1a2f5 fca8de1 e6857f6 fca8de1 bfc1cf6 ef21c2f fca8de1 1c1a2f5 ef21c2f 379d2b4 ef21c2f 379d2b4 ef21c2f fca8de1 379d2b4 ef21c2f fca8de1 ef21c2f fca8de1 e6857f6 bfc1cf6 fca8de1 bfc1cf6 fca8de1 ef21c2f fca8de1 ef21c2f fca8de1 ef21c2f bfc1cf6 fca8de1 e6857f6 ef21c2f e6857f6 ef21c2f bfc1cf6 ef21c2f bfc1cf6 ef21c2f bfc1cf6 fca8de1 bfc1cf6 91aaf8c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
import os
import gradio as gr
from anthropic import Anthropic
import wolframalpha
from datetime import datetime, timedelta
from collections import deque
import re
# Initialize clients
anthropic = Anthropic(api_key=os.environ.get('ANTHROPIC_API_KEY'))
wolfram_client = wolframalpha.Client(os.environ.get('WOLFRAM_APPID'))
def parse_questions(content):
"""Parse questions and their solutions from Claude's output"""
questions = []
current_question = {}
# Split content into lines for more reliable parsing
lines = content.split('\n')
for line in lines:
# Start of new question
if re.match(r'^\s*\d+\)', line):
if current_question:
questions.append(current_question)
current_question = {
'number': re.match(r'^\s*(\d+)\)', line).group(1),
'problem': line.split(')', 1)[1].strip(),
'solution': '',
'final_answer': None
}
# Solution marker
elif 'Solution:' in line and current_question:
current_question['problem'] = current_question['problem'].strip()
current_question['solution'] = line.split('Solution:', 1)[1].strip()
# Add to current problem or solution
elif current_question:
if current_question['solution']:
current_question['solution'] += '\n' + line
else:
current_question['problem'] += '\n' + line
# Extract final answer
if current_question and 'final answer' in line.lower():
matches = re.findall(r'[-+]?(?:\d*\.)?\d+', line)
if matches:
current_question['final_answer'] = matches[-1]
# Add last question
if current_question:
questions.append(current_question)
# Clean up questions
for q in questions:
q['problem'] = q['problem'].strip()
q['solution'] = q['solution'].strip()
return questions
def verify_solution(problem, answer):
"""Verify a mathematical solution using Wolfram Alpha"""
try:
# Initialize query variable
query = ""
# Clean the problem text first
clean_problem = problem.replace('$$', '').replace('$', '').strip()
# Case 1: Definite Integral
if 'integral' in clean_problem.lower() or '∫' in clean_problem or '\int' in clean_problem:
# Use raw string for regex to avoid escape issues
integrand_match = re.search(r'(?:\int|∫)_(\d+)\^(\d+)\s*\(?([\dx+\s]+)\)?\s*dx', clean_problem, re.UNICODE)
if integrand_match:
lower, upper, integrand = integrand_match.groups()
# Clean up the integrand
integrand = integrand.replace(' ', '')
query = f"integrate {integrand} from {lower} to {upper}"
print(f"Integral query: {query}")
else:
# Fallback for simpler pattern
integrand_match = re.search(r'(?:\int|∫).*?\(([\dx+\s]+)\)\s*dx', clean_problem, re.UNICODE)
if integrand_match:
integrand = integrand_match.group(1).replace(' ', '')
query = f"integrate {integrand} from 0 to 1" # Common default bounds
print(f"Fallback integral query: {query}")
# Case 2: Simple Differentiation
elif 'derivative' in clean_problem.lower() or 'd/dx' in clean_problem:
# Look for function after equals sign or f(x) =
func_match = re.search(r'[f\(x\)\s*=\s*](.*?)$', clean_problem)
if func_match:
func = func_match.group(1).strip()
query = f"derivative of {func}"
print(f"Derivative query: {query}")
# Case 3: Mean Value Theorem
elif 'Mean Value Theorem' in clean_problem:
func_match = re.search(r'f\(x\)\s*=\s*(.*?)\s+on', clean_problem)
interval_match = re.search(r'\[(\d+),\s*(\d+)\]', clean_problem)
if func_match and interval_match:
func = func_match.group(1).strip()
a, b = interval_match.groups()
# Calculate f'(x) first
derivative_query = f"derivative of {func}"
print(f"MVT derivative query: {derivative_query}")
derivative_result = wolfram_client.query(derivative_query)
if derivative_result.success:
for pod in derivative_result.pods:
if pod.title in ['Derivative']:
derivative = pod.text
# Now calculate [f(b) - f(a)]/(b-a)
query = f"solve {derivative} = ({func.replace('x', b)} - {func.replace('x', a)})/({b} - {a})"
print(f"MVT final query: {query}")
break
# Ensure query is not empty
if not query.strip():
return {
'verified': False,
'wolfram_solution': None,
'error': "Could not generate valid query from problem"
}
print(f"Final query to Wolfram Alpha: {query}")
result = wolfram_client.query(query)
if not result.success:
return {
'verified': False,
'wolfram_solution': None,
'error': f"Wolfram Alpha could not process query: {query}"
}
# Process the result
for pod in result.pods:
if pod.title in ['Result', 'Solution', 'Numerical result', 'Decimal approximation', 'Definite integral', 'Solutions']:
wolfram_answer = pod.text
print(f"Wolfram pod {pod.title}: {wolfram_answer}")
# For MVT problems, handle sqrt expressions
if 'Mean Value Theorem' in clean_problem:
# Convert both answers to decimal for comparison
if 'sqrt' in str(answer).lower():
# Convert sqrt expression to decimal
sqrt_match = re.search(r'sqrt\((\d+)/(\d+)\)', str(answer))
if sqrt_match:
num, denom = map(float, sqrt_match.groups())
user_value = (num/denom)**0.5
# Look for decimal in Wolfram result
wolfram_nums = re.findall(r'[-+]?(?:\d*\.)?\d+', wolfram_answer)
if wolfram_nums:
wolfram_value = float(wolfram_nums[0])
is_verified = abs(wolfram_value - user_value) < 0.01
return {
'verified': is_verified,
'wolfram_solution': wolfram_answer,
'error': None
}
# Handle numerical answers
if str(answer).replace('.', '').isdigit():
wolfram_nums = re.findall(r'[-+]?(?:\d*\.)?\d+', wolfram_answer)
if wolfram_nums:
wolfram_value = float(wolfram_nums[0])
user_value = float(answer)
is_verified = abs(wolfram_value - user_value) < 0.01
return {
'verified': is_verified,
'wolfram_solution': wolfram_answer,
'error': None
}
# Handle symbolic answers
else:
clean_wolfram = re.sub(r'\s+', '', wolfram_answer.lower())
clean_answer = re.sub(r'\s+', '', str(answer).lower())
is_verified = clean_wolfram == clean_answer
return {
'verified': is_verified,
'wolfram_solution': wolfram_answer,
'error': None
}
return {
'verified': False,
'wolfram_solution': None,
'error': "No suitable solution found in Wolfram Alpha response"
}
except Exception as e:
error_msg = f"Error during verification: {str(e)}"
if query:
error_msg += f"\nQuery attempted: {query}"
return {
'verified': False,
'wolfram_solution': None,
'error': error_msg
}
def generate_test(subject):
"""Generate and verify a math test"""
try:
system_prompt = """Generate 3 university-level math questions that can be verified numerically.
For each question:
1. Number the question as 1), 2), 3)
2. State the problem clearly using simple $$ for displayed math
3. Include "Solution:" before the solution
4. Show step-by-step work
5. End each solution with "Final answer = [number]"
6. Keep problems relatively simple (basic calculus, algebra, etc.)
7. Make sure problems have clear numerical answers
8. Avoid word problems - focus on pure mathematical expressions"""
message = anthropic.messages.create(
model="claude-3-opus-20240229",
max_tokens=1500,
temperature=0.7,
messages=[{
"role": "user",
"content": f"{system_prompt}\n\nWrite an exam for {subject} with simple numerical answers."
}]
)
# Get the content and parse questions
content = message.content[0].text
questions = parse_questions(content)
# Add verification results
verification_note = "\n\n---\n## Solution Verification:\n"
verification_results = []
for q in questions:
if q['final_answer'] is not None:
result = verify_solution(q['problem'], q['final_answer'])
verification_results.append(result)
verification_note += f"\nQuestion {q['number']}:\n"
if result['verified']:
verification_note += "✅ Solution verified by Wolfram Alpha\n"
else:
verification_note += "⚠️ Solution needs verification\n"
if result['wolfram_solution']:
verification_note += f"Wolfram Alpha result: {result['wolfram_solution']}\n"
if result['error']:
verification_note += f"Note: {result['error']}\n"
else:
verification_note += f"\nQuestion {q['number']}:\n⚠️ Could not extract final answer\n"
# Add usage statistics
usage_stats = f"""
\n---\nUsage Statistics:
• Input Tokens: {message.usage.input_tokens:,}
• Output Tokens: {message.usage.output_tokens:,}
• Wolfram Alpha calls: {len(verification_results)}
Cost Breakdown:
• Claude Cost: ${((message.usage.input_tokens / 1000) * 0.015) + ((message.usage.output_tokens / 1000) * 0.075):.4f}
• Wolfram API calls: {len(verification_results)}
"""
# Combine everything with proper spacing
final_output = content + "\n\n" + verification_note + usage_stats
return final_output
except Exception as e:
return f"Error: {str(e)}"
subjects = [
"Single Variable Calculus",
"Multivariable Calculus",
"Linear Algebra",
"Differential Equations",
"Real Analysis",
"Complex Analysis",
"Abstract Algebra",
"Probability Theory",
"Numerical Analysis",
"Topology"
]
# Create Gradio interface
interface = gr.Interface(
fn=generate_test,
inputs=gr.Dropdown(
choices=subjects,
label="Select Mathematics Subject",
info="Choose a subject for the exam questions"
),
outputs=gr.Markdown(
label="Generated Test",
latex_delimiters=[
{"left": "$$", "right": "$$", "display": True},
{"left": "$", "right": "$", "display": False}
]
),
title="Advanced Mathematics Test Generator",
description="""Generates university-level mathematics exam questions with solutions using Claude 3 Opus.
Limited to 25 requests per day. Please use responsibly.""",
theme="default",
allow_flagging="never"
)
# Launch the interface
if __name__ == "__main__":
interface.launch() |