Spaces:
Sleeping
Sleeping
File size: 14,973 Bytes
bfc1cf6 2f3d61a 6439bc9 64d6a1c 6439bc9 64d6a1c 6439bc9 bfc1cf6 2f3d61a 516fc47 2f3d61a 516fc47 2841710 9682d40 6439bc9 6b1a461 6439bc9 6b1a461 6439bc9 9682d40 64d6a1c 1871af1 2841710 bfc1cf6 1871af1 6439bc9 1871af1 6439bc9 6b1a461 e6857f6 6439bc9 bfc1cf6 2f3d61a 9682d40 2f3d61a 6439bc9 1871af1 2841710 2f3d61a 2841710 2f3d61a 6439bc9 bfc1cf6 2841710 615dd4c 6b1a461 bfc1cf6 516fc47 bfc1cf6 6439bc9 9814205 6439bc9 64d6a1c 9814205 64d6a1c 9814205 64d6a1c bfc1cf6 6439bc9 6b1a461 9682d40 91aaf8c 9682d40 2841710 9682d40 1871af1 2841710 1871af1 9682d40 6439bc9 91aaf8c 9682d40 6439bc9 9682d40 64d6a1c 1871af1 6b1a461 9682d40 91aaf8c 6439bc9 2f3d61a |
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
import os
import gradio as gr
from anthropic import Anthropic
from datetime import datetime, timedelta
from collections import deque
import random
import logging
import tempfile
from pathlib import Path
# Set up logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Initialize Anthropic client
anthropic = Anthropic(
api_key=os.environ.get('ANTHROPIC_API_KEY')
)
# Request tracking
MAX_REQUESTS_PER_DAY = 25
request_history = deque(maxlen=1000)
def get_difficulty_parameters(difficulty_level):
"""Return specific parameters and constraints based on difficulty level"""
parameters = {
1: { # Very Easy
"description": "very easy, suitable for beginners",
"constraints": [
"Use only basic concepts and straightforward calculations",
"Break complex problems into smaller, guided steps",
"Provide hints within the question when needed",
"Use simple numbers and avoid complex algebraic expressions"
],
"example_style": "Similar to standard homework problems",
"model": "claude-3-sonnet-20240229"
},
2: { # Easy
"description": "easy, but requiring some thought",
"constraints": [
"Use basic concepts with minor complications",
"Include two-step problems",
"Minimal guidance provided",
"Use moderately complex numbers or expressions"
],
"example_style": "Similar to quiz questions",
"model": "claude-3-sonnet-20240229"
},
3: { # Intermediate
"description": "intermediate difficulty, testing deeper understanding",
"constraints": [
"Combine 2-3 related concepts",
"Include some non-obvious solution paths",
"Require multi-step reasoning",
"Use moderate algebraic complexity"
],
"example_style": "Similar to midterm exam questions",
"model": "claude-3-sonnet-20240229"
},
4: { # Difficult
"description": "challenging, requiring strong mathematical maturity",
"constraints": [
"Combine multiple concepts creatively",
"Require insight and deep understanding",
"Include non-standard approaches",
"Use sophisticated mathematical reasoning"
],
"example_style": "Similar to final exam questions",
"model": "claude-3-opus-20240229"
},
5: { # Very Difficult
"description": "very challenging, testing mastery and creativity at a graduate level",
"constraints": [
"Create novel applications of theoretical concepts",
"Require graduate-level mathematical reasoning",
"Combine multiple advanced topics in unexpected ways",
"Demand creative problem-solving approaches",
"Include rigorous proof construction",
"Require synthesis across mathematical domains",
"Test deep theoretical understanding"
],
"example_style": "Similar to graduate qualifying exams or advanced competition problems",
"model": "claude-3-opus-20240229"
}
}
return parameters.get(difficulty_level)
def create_latex_document(content, questions_only=False):
"""Create a complete LaTeX document"""
try:
latex_header = r"""\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[margin=1in]{geometry}
\begin{document}
\title{Mathematics Test}
\maketitle
"""
latex_footer = r"\end{document}"
if questions_only:
processed_content = []
current_question = []
for line in content.split('\n'):
if 'Solution:' in line:
processed_content.append('\n'.join(current_question))
current_question = []
continue
if any(line.startswith(f"{i})") for i in range(1, 4)):
if current_question:
processed_content.append('\n'.join(current_question))
current_question = [line]
elif current_question:
current_question.append(line)
if current_question:
processed_content.append('\n'.join(current_question))
content = '\n\n'.join(processed_content)
full_document = f"{latex_header}\n{content}\n{latex_footer}"
logger.debug(f"Created {'questions-only' if questions_only else 'full'} LaTeX document")
return full_document
except Exception as e:
logger.error(f"Error creating LaTeX document: {str(e)}")
raise
def save_to_temp_file(content, filename):
"""Save content to a temporary file and return the path"""
try:
# Create a temporary directory that persists
temp_dir = Path(tempfile.gettempdir()) / "math_test_files"
temp_dir.mkdir(exist_ok=True)
# Create the file path
file_path = temp_dir / filename
# Write the content
file_path.write_text(content, encoding='utf-8')
logger.debug(f"Saved content to temporary file: {file_path}")
return str(file_path)
except Exception as e:
logger.error(f"Error saving temporary file: {str(e)}")
raise
def validate_question_counts(computation, proof, application):
"""Validate that question counts sum to 3"""
try:
comp = int(computation)
prf = int(proof)
app = int(application)
if comp + prf + app != 3:
return False, "Total number of questions must equal 3"
if any(x < 0 for x in [comp, prf, app]):
return False, "Question counts cannot be negative"
return True, ""
except ValueError:
return False, "Please enter valid numbers"
def generate_test(subject, difficulty, computation_count, proof_count, application_count):
"""Generate a math test with enhanced difficulty scaling"""
try:
# Validate inputs
is_valid, error_message = validate_question_counts(
computation_count, proof_count, application_count
)
if not is_valid:
return error_message, None, None
if not os.environ.get('ANTHROPIC_API_KEY'):
logger.error("Anthropic API key not found")
return "Error: Anthropic API key not configured", None, None
logger.debug(f"Generating test for subject: {subject} at difficulty level: {difficulty}")
# Check rate limit
now = datetime.now()
while request_history and (now - request_history[0]) > timedelta(days=1):
request_history.popleft()
if len(request_history) >= MAX_REQUESTS_PER_DAY:
return "Daily request limit reached. Please try again tomorrow.", None, None
request_history.append(now)
topics = {
"Single Variable Calculus": ["limits", "derivatives", "integrals", "series", "applications"],
"Multivariable Calculus": ["partial derivatives", "multiple integrals", "vector fields", "optimization"],
"Linear Algebra": ["matrices", "vector spaces", "eigenvalues", "linear transformations"],
}
selected_topics = random.sample(topics.get(subject, ["general"]), min(3, len(topics.get(subject, ["general"]))))
logger.debug(f"Selected topics: {selected_topics}")
# Create question type list based on user input
question_types = (
["computation"] * int(computation_count) +
["proof"] * int(proof_count) +
["application"] * int(application_count)
)
difficulty_params = get_difficulty_parameters(difficulty)
# Enhanced prompt for difficulty level 5
if difficulty == 5:
system_prompt = f"""You are an expert mathematics professor creating graduate-level exam questions.
STRICT REQUIREMENTS:
1. Write exactly 3 graduate-level questions on {subject} with these specific characteristics:
- Question Types (in order): {', '.join(question_types)}
- Topics to cover: {', '.join(selected_topics)}
2. Advanced Difficulty Requirements:
These questions must be suitable for PhD qualifying exams or advanced competitions.
MUST include:
- Novel applications of theoretical concepts
- Graduate-level mathematical reasoning
- Unexpected connections between different areas of {subject}
- Creative problem-solving approaches
- Rigorous proof requirements where applicable
Follow these specific constraints:
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
3. Style Reference:
Questions should be {difficulty_params['example_style']}
4. Each question MUST:
- Bridge multiple mathematical domains
- Require deep theoretical understanding
- Test mastery of advanced concepts
- Demand innovative solution approaches
5. For LaTeX formatting:
- Use $ for inline math
- Use $$ on separate lines for equations and solutions
- Put each solution step on its own line in $$ $$
- DO NOT use \\begin{{aligned}} or similar environments
6. Number questions as 1), 2), 3)
7. Include detailed solutions with thorough explanations of advanced concepts used
8. Maintain clear, precise formatting"""
else:
system_prompt = f"""You are an expert mathematics professor creating {difficulty_params['description']} exam questions.
STRICT REQUIREMENTS:
1. Write exactly 3 university-level questions on {subject} with these specific characteristics:
- Question Types (in order): {', '.join(question_types)}
- Topics to cover: {', '.join(selected_topics)}
2. Difficulty Level Guidelines:
{difficulty_params['description'].upper()}
Follow these specific constraints:
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
3. Style Reference:
Questions should be {difficulty_params['example_style']}
4. For LaTeX formatting:
- Use $ for inline math
- Use $$ on separate lines for equations and solutions
- Put each solution step on its own line in $$ $$
- DO NOT use \\begin{{aligned}} or similar environments
5. Number questions as 1), 2), 3)
6. Include detailed solutions
7. Maintain clear formatting"""
logger.debug("Sending request to Anthropic API")
message = anthropic.messages.create(
model=difficulty_params['model'],
max_tokens=2999,
temperature=0.7,
messages=[{
"role": "user",
"content": f"{system_prompt}\n\nWrite an exam for {subject}."
}]
)
if not hasattr(message, 'content') or not message.content:
logger.error("No content received from Anthropic API")
return "Error: No content received from API", None, None
response_text = message.content[0].text
logger.debug("Successfully received response from Anthropic API")
# Create LaTeX content
questions_latex = create_latex_document(response_text, questions_only=True)
full_latex = create_latex_document(response_text, questions_only=False)
# Save to temporary files
questions_path = save_to_temp_file(questions_latex, "questions.tex")
full_path = save_to_temp_file(full_latex, "full_test.tex")
logger.debug("Successfully created temporary files")
return response_text, questions_path, full_path
except Exception as e:
logger.error(f"Error generating test: {str(e)}")
return f"Error: {str(e)}", None, None
# Create Gradio interface
with gr.Blocks() as interface:
gr.Markdown("# Advanced Mathematics Test Generator")
gr.Markdown("""Generates unique university-level mathematics exam questions with solutions using Claude 3.
Each test features different topics and difficulty levels. Limited to 25 requests per day.""")
with gr.Row():
with gr.Column():
subject_dropdown = gr.Dropdown(
choices=[
"Single Variable Calculus",
"Multivariable Calculus",
"Linear Algebra",
"Differential Equations",
"Real Analysis",
"Complex Analysis",
"Abstract Algebra",
"Probability Theory",
"Numerical Analysis",
"Topology"
],
label="Select Mathematics Subject",
info="Choose a subject for the exam questions"
)
difficulty_slider = gr.Slider(
minimum=1,
maximum=5,
step=1,
value=3,
label="Difficulty Level",
info="1: Very Easy, 2: Easy, 3: Moderate, 4: Difficult, 5: Very Difficult"
)
with gr.Row():
with gr.Column():
computation_count = gr.Number(
value=1,
label="Number of Computation Questions",
info="Enter the number of computation questions (total must be 3)",
precision=0
)
proof_count = gr.Number(
value=1,
label="Number of Proof Questions",
info="Enter the number of proof questions (total must be 3)",
precision=0
)
application_count = gr.Number(
value=1,
label="Number of Application Questions",
info="Enter the number of application questions (total must be 3)",
precision=0
)
generate_btn = gr.Button("Generate Test")
output_text = gr.Markdown(
label="Generated Test Preview",
latex_delimiters=[
{"left": "$$", "right": "$$", "display": True},
{"left": "$", "right": "$", "display": False}
]
)
with gr.Row():
questions_file = gr.File(label="Questions Only (LaTeX)")
full_file = gr.File(label="Full Test with Solutions (LaTeX)")
generate_btn.click(
generate_test,
inputs=[
subject_dropdown,
difficulty_slider,
computation_count,
proof_count,
application_count
],
outputs=[output_text, questions_file, full_file]
)
if __name__ == "__main__":
logger.info("Starting application")
interface.launch() |