Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -24,6 +24,71 @@ anthropic = Anthropic(
|
|
24 |
MAX_REQUESTS_PER_DAY = 25
|
25 |
request_history = deque(maxlen=1000)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def create_latex_document(content, questions_only=False):
|
28 |
"""Create a complete LaTeX document"""
|
29 |
try:
|
@@ -96,7 +161,7 @@ def validate_question_counts(computation, proof, application):
|
|
96 |
return False, "Please enter valid numbers"
|
97 |
|
98 |
def generate_test(subject, difficulty, computation_count, proof_count, application_count):
|
99 |
-
"""Generate a math test"""
|
100 |
try:
|
101 |
# Validate inputs
|
102 |
is_valid, error_message = validate_question_counts(
|
@@ -136,31 +201,76 @@ def generate_test(subject, difficulty, computation_count, proof_count, applicati
|
|
136 |
["application"] * int(application_count)
|
137 |
)
|
138 |
|
139 |
-
|
140 |
-
1: "very easy",
|
141 |
-
2: "easy",
|
142 |
-
3: "intermediate difficulty",
|
143 |
-
4: "difficult",
|
144 |
-
5: "very difficult"
|
145 |
-
}
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
logger.debug("Sending request to Anthropic API")
|
162 |
message = anthropic.messages.create(
|
163 |
-
model=
|
164 |
max_tokens=1500,
|
165 |
temperature=0.7,
|
166 |
messages=[{
|
@@ -195,7 +305,7 @@ def generate_test(subject, difficulty, computation_count, proof_count, applicati
|
|
195 |
# Create Gradio interface
|
196 |
with gr.Blocks() as interface:
|
197 |
gr.Markdown("# Advanced Mathematics Test Generator")
|
198 |
-
gr.Markdown("""Generates unique university-level mathematics exam questions with solutions using Claude 3
|
199 |
Each test features different topics and difficulty levels. Limited to 25 requests per day.""")
|
200 |
|
201 |
with gr.Row():
|
@@ -223,7 +333,7 @@ with gr.Blocks() as interface:
|
|
223 |
step=1,
|
224 |
value=3,
|
225 |
label="Difficulty Level",
|
226 |
-
info="1: Easy, 3: Moderate, 5: Difficult"
|
227 |
)
|
228 |
|
229 |
with gr.Row():
|
|
|
24 |
MAX_REQUESTS_PER_DAY = 25
|
25 |
request_history = deque(maxlen=1000)
|
26 |
|
27 |
+
def get_difficulty_parameters(difficulty_level):
|
28 |
+
"""Return specific parameters and constraints based on difficulty level"""
|
29 |
+
parameters = {
|
30 |
+
1: { # Very Easy
|
31 |
+
"description": "very easy, suitable for beginners",
|
32 |
+
"constraints": [
|
33 |
+
"Use only basic concepts and straightforward calculations",
|
34 |
+
"Break complex problems into smaller, guided steps",
|
35 |
+
"Provide hints within the question when needed",
|
36 |
+
"Use simple numbers and avoid complex algebraic expressions"
|
37 |
+
],
|
38 |
+
"example_style": "Similar to standard homework problems",
|
39 |
+
"model": "claude-3-sonnet-20240229"
|
40 |
+
},
|
41 |
+
2: { # Easy
|
42 |
+
"description": "easy, but requiring some thought",
|
43 |
+
"constraints": [
|
44 |
+
"Use basic concepts with minor complications",
|
45 |
+
"Include two-step problems",
|
46 |
+
"Minimal guidance provided",
|
47 |
+
"Use moderately complex numbers or expressions"
|
48 |
+
],
|
49 |
+
"example_style": "Similar to quiz questions",
|
50 |
+
"model": "claude-3-sonnet-20240229"
|
51 |
+
},
|
52 |
+
3: { # Intermediate
|
53 |
+
"description": "intermediate difficulty, testing deeper understanding",
|
54 |
+
"constraints": [
|
55 |
+
"Combine 2-3 related concepts",
|
56 |
+
"Include some non-obvious solution paths",
|
57 |
+
"Require multi-step reasoning",
|
58 |
+
"Use moderate algebraic complexity"
|
59 |
+
],
|
60 |
+
"example_style": "Similar to midterm exam questions",
|
61 |
+
"model": "claude-3-sonnet-20240229"
|
62 |
+
},
|
63 |
+
4: { # Difficult
|
64 |
+
"description": "challenging, requiring strong mathematical maturity",
|
65 |
+
"constraints": [
|
66 |
+
"Combine multiple concepts creatively",
|
67 |
+
"Require insight and deep understanding",
|
68 |
+
"Include non-standard approaches",
|
69 |
+
"Use sophisticated mathematical reasoning"
|
70 |
+
],
|
71 |
+
"example_style": "Similar to final exam questions",
|
72 |
+
"model": "claude-3-opus-20240229"
|
73 |
+
},
|
74 |
+
5: { # Very Difficult
|
75 |
+
"description": "very challenging, testing mastery and creativity at a graduate level",
|
76 |
+
"constraints": [
|
77 |
+
"Create novel applications of theoretical concepts",
|
78 |
+
"Require graduate-level mathematical reasoning",
|
79 |
+
"Combine multiple advanced topics in unexpected ways",
|
80 |
+
"Demand creative problem-solving approaches",
|
81 |
+
"Include rigorous proof construction",
|
82 |
+
"Require synthesis across mathematical domains",
|
83 |
+
"Test deep theoretical understanding"
|
84 |
+
],
|
85 |
+
"example_style": "Similar to graduate qualifying exams or advanced competition problems",
|
86 |
+
"model": "claude-3-opus-20240229"
|
87 |
+
}
|
88 |
+
}
|
89 |
+
|
90 |
+
return parameters.get(difficulty_level)
|
91 |
+
|
92 |
def create_latex_document(content, questions_only=False):
|
93 |
"""Create a complete LaTeX document"""
|
94 |
try:
|
|
|
161 |
return False, "Please enter valid numbers"
|
162 |
|
163 |
def generate_test(subject, difficulty, computation_count, proof_count, application_count):
|
164 |
+
"""Generate a math test with enhanced difficulty scaling"""
|
165 |
try:
|
166 |
# Validate inputs
|
167 |
is_valid, error_message = validate_question_counts(
|
|
|
201 |
["application"] * int(application_count)
|
202 |
)
|
203 |
|
204 |
+
difficulty_params = get_difficulty_parameters(difficulty)
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
+
# Enhanced prompt for difficulty level 5
|
207 |
+
if difficulty == 5:
|
208 |
+
system_prompt = f"""You are an expert mathematics professor creating graduate-level exam questions.
|
209 |
+
|
210 |
+
STRICT REQUIREMENTS:
|
211 |
+
1. Write exactly 3 graduate-level questions on {subject} with these specific characteristics:
|
212 |
+
- Question Types (in order): {', '.join(question_types)}
|
213 |
+
- Topics to cover: {', '.join(selected_topics)}
|
214 |
+
|
215 |
+
2. Advanced Difficulty Requirements:
|
216 |
+
These questions must be suitable for PhD qualifying exams or advanced competitions.
|
217 |
+
MUST include:
|
218 |
+
- Novel applications of theoretical concepts
|
219 |
+
- Graduate-level mathematical reasoning
|
220 |
+
- Unexpected connections between different areas of {subject}
|
221 |
+
- Creative problem-solving approaches
|
222 |
+
- Rigorous proof requirements where applicable
|
223 |
+
|
224 |
+
Follow these specific constraints:
|
225 |
+
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
226 |
+
|
227 |
+
3. Style Reference:
|
228 |
+
Questions should be {difficulty_params['example_style']}
|
229 |
+
|
230 |
+
4. Each question MUST:
|
231 |
+
- Bridge multiple mathematical domains
|
232 |
+
- Require deep theoretical understanding
|
233 |
+
- Test mastery of advanced concepts
|
234 |
+
- Demand innovative solution approaches
|
235 |
+
|
236 |
+
5. For LaTeX formatting:
|
237 |
+
- Use $ for inline math
|
238 |
+
- Use $$ on separate lines for equations and solutions
|
239 |
+
- Put each solution step on its own line in $$ $$
|
240 |
+
- DO NOT use \\begin{{aligned}} or similar environments
|
241 |
+
|
242 |
+
6. Number questions as 1), 2), 3)
|
243 |
+
7. Include detailed solutions with thorough explanations of advanced concepts used
|
244 |
+
8. Maintain clear, precise formatting"""
|
245 |
+
else:
|
246 |
+
system_prompt = f"""You are an expert mathematics professor creating {difficulty_params['description']} exam questions.
|
247 |
+
|
248 |
+
STRICT REQUIREMENTS:
|
249 |
+
1. Write exactly 3 university-level questions on {subject} with these specific characteristics:
|
250 |
+
- Question Types (in order): {', '.join(question_types)}
|
251 |
+
- Topics to cover: {', '.join(selected_topics)}
|
252 |
+
|
253 |
+
2. Difficulty Level Guidelines:
|
254 |
+
{difficulty_params['description'].upper()}
|
255 |
+
Follow these specific constraints:
|
256 |
+
{chr(10).join(f' - {c}' for c in difficulty_params['constraints'])}
|
257 |
+
|
258 |
+
3. Style Reference:
|
259 |
+
Questions should be {difficulty_params['example_style']}
|
260 |
+
|
261 |
+
4. For LaTeX formatting:
|
262 |
+
- Use $ for inline math
|
263 |
+
- Use $$ on separate lines for equations and solutions
|
264 |
+
- Put each solution step on its own line in $$ $$
|
265 |
+
- DO NOT use \\begin{{aligned}} or similar environments
|
266 |
+
|
267 |
+
5. Number questions as 1), 2), 3)
|
268 |
+
6. Include detailed solutions
|
269 |
+
7. Maintain clear formatting"""
|
270 |
|
271 |
logger.debug("Sending request to Anthropic API")
|
272 |
message = anthropic.messages.create(
|
273 |
+
model=difficulty_params['model'],
|
274 |
max_tokens=1500,
|
275 |
temperature=0.7,
|
276 |
messages=[{
|
|
|
305 |
# Create Gradio interface
|
306 |
with gr.Blocks() as interface:
|
307 |
gr.Markdown("# Advanced Mathematics Test Generator")
|
308 |
+
gr.Markdown("""Generates unique university-level mathematics exam questions with solutions using Claude 3.
|
309 |
Each test features different topics and difficulty levels. Limited to 25 requests per day.""")
|
310 |
|
311 |
with gr.Row():
|
|
|
333 |
step=1,
|
334 |
value=3,
|
335 |
label="Difficulty Level",
|
336 |
+
info="1: Very Easy, 2: Easy, 3: Moderate, 4: Difficult, 5: Very Difficult"
|
337 |
)
|
338 |
|
339 |
with gr.Row():
|