Spaces:
Running
Running
added topics dictionary, added randomized difficulty distribution, made temperature dynamic
Browse files
app.py
CHANGED
@@ -1,17 +1,17 @@
|
|
1 |
-
# app.py
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
from anthropic import Anthropic
|
5 |
from datetime import datetime, timedelta
|
6 |
from collections import deque
|
|
|
7 |
|
8 |
-
# Initialize Anthropic client
|
9 |
anthropic = Anthropic(
|
10 |
api_key=os.environ.get('ANTHROPIC_API_KEY')
|
11 |
)
|
12 |
|
13 |
# Request tracking
|
14 |
-
MAX_REQUESTS_PER_DAY = 25
|
15 |
request_history = deque(maxlen=1000)
|
16 |
|
17 |
def check_api_key():
|
@@ -22,7 +22,6 @@ def check_api_key():
|
|
22 |
def check_rate_limit():
|
23 |
"""Check if we're within rate limits"""
|
24 |
now = datetime.now()
|
25 |
-
# Remove requests older than 24 hours
|
26 |
while request_history and (now - request_history[0]) > timedelta(days=1):
|
27 |
request_history.popleft()
|
28 |
return len(request_history) < MAX_REQUESTS_PER_DAY
|
@@ -33,33 +32,55 @@ def clean_latex(text):
|
|
33 |
return text
|
34 |
|
35 |
def generate_test(subject):
|
36 |
-
"""Generate a math test with
|
37 |
try:
|
38 |
-
# Check API key
|
39 |
check_api_key()
|
40 |
-
|
41 |
-
# Check rate limit
|
42 |
if not check_rate_limit():
|
43 |
return "Daily request limit reached. Please try again tomorrow."
|
44 |
|
45 |
-
# Record request
|
46 |
request_history.append(datetime.now())
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
- Use $ for simple inline math
|
52 |
- For equations and solution steps, use $$ on separate lines
|
53 |
- For multi-step solutions, put each step on its own line in $$ $$
|
54 |
-
- DO NOT use \\begin{aligned} or any other environments
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
message = anthropic.messages.create(
|
60 |
model="claude-3-opus-20240229",
|
61 |
max_tokens=1500,
|
62 |
-
temperature=
|
63 |
messages=[{
|
64 |
"role": "user",
|
65 |
"content": f"{system_prompt}\n\nWrite an exam for {subject}."
|
@@ -97,7 +118,7 @@ def generate_test(subject):
|
|
97 |
except Exception as e:
|
98 |
return f"Error: {str(e)}"
|
99 |
|
100 |
-
# Subject choices
|
101 |
subjects = [
|
102 |
"Single Variable Calculus",
|
103 |
"Multivariable Calculus",
|
@@ -127,12 +148,11 @@ interface = gr.Interface(
|
|
127 |
]
|
128 |
),
|
129 |
title="Advanced Mathematics Test Generator",
|
130 |
-
description="""Generates university-level mathematics exam questions with solutions using Claude 3 Opus.
|
131 |
-
Limited to 25 requests per day.
|
132 |
theme="default",
|
133 |
allow_flagging="never"
|
134 |
)
|
135 |
|
136 |
-
# Launch the interface
|
137 |
if __name__ == "__main__":
|
138 |
-
interface.launch()
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from anthropic import Anthropic
|
4 |
from datetime import datetime, timedelta
|
5 |
from collections import deque
|
6 |
+
import random
|
7 |
|
8 |
+
# Initialize Anthropic client
|
9 |
anthropic = Anthropic(
|
10 |
api_key=os.environ.get('ANTHROPIC_API_KEY')
|
11 |
)
|
12 |
|
13 |
# Request tracking
|
14 |
+
MAX_REQUESTS_PER_DAY = 25
|
15 |
request_history = deque(maxlen=1000)
|
16 |
|
17 |
def check_api_key():
|
|
|
22 |
def check_rate_limit():
|
23 |
"""Check if we're within rate limits"""
|
24 |
now = datetime.now()
|
|
|
25 |
while request_history and (now - request_history[0]) > timedelta(days=1):
|
26 |
request_history.popleft()
|
27 |
return len(request_history) < MAX_REQUESTS_PER_DAY
|
|
|
32 |
return text
|
33 |
|
34 |
def generate_test(subject):
|
35 |
+
"""Generate a math test with enhanced variety"""
|
36 |
try:
|
|
|
37 |
check_api_key()
|
|
|
|
|
38 |
if not check_rate_limit():
|
39 |
return "Daily request limit reached. Please try again tomorrow."
|
40 |
|
|
|
41 |
request_history.append(datetime.now())
|
42 |
|
43 |
+
# Randomly select focus areas and difficulty levels
|
44 |
+
topics = {
|
45 |
+
"Single Variable Calculus": ["limits", "derivatives", "integrals", "series", "applications"],
|
46 |
+
"Multivariable Calculus": ["partial derivatives", "multiple integrals", "vector fields", "optimization", "surface integrals"],
|
47 |
+
"Linear Algebra": ["matrices", "vector spaces", "eigenvalues", "linear transformations", "inner products"],
|
48 |
+
# ... add topics for other subjects
|
49 |
+
}
|
50 |
+
|
51 |
+
selected_topics = random.sample(topics.get(subject, ["general"]), min(3, len(topics.get(subject, ["general"]))))
|
52 |
+
difficulty_distribution = random.choice([
|
53 |
+
"one easy, one medium, one challenging",
|
54 |
+
"two medium, one very challenging",
|
55 |
+
"one medium, two challenging"
|
56 |
+
])
|
57 |
+
|
58 |
+
system_prompt = f"""You will write math exam questions. Follow these requirements EXACTLY:
|
59 |
+
1. Write exactly 3 university-level questions focusing on these specific topics: {', '.join(selected_topics)}
|
60 |
+
2. Make the questions {difficulty_distribution}
|
61 |
+
3. For each question, use a different type of mathematical thinking (e.g., computation, proof, application, conceptual understanding)
|
62 |
+
4. Ensure questions are unique and not commonly found in textbooks
|
63 |
+
5. For LaTeX math formatting:
|
64 |
- Use $ for simple inline math
|
65 |
- For equations and solution steps, use $$ on separate lines
|
66 |
- For multi-step solutions, put each step on its own line in $$ $$
|
67 |
+
- DO NOT use \\begin{{aligned}} or any other environments
|
68 |
+
6. Number each question as 1), 2), 3)
|
69 |
+
7. Include detailed solutions after each question
|
70 |
+
8. Keep formatting simple and clear
|
71 |
+
|
72 |
+
Important: Make these questions distinct from standard textbook problems. Incorporate real-world applications,
|
73 |
+
interdisciplinary connections, or creative scenarios where appropriate."""
|
74 |
+
|
75 |
+
# Vary temperature based on subject complexity
|
76 |
+
base_temperature = 0.7
|
77 |
+
complexity_adjustment = random.uniform(-0.1, 0.1)
|
78 |
+
temperature = min(1.0, max(0.5, base_temperature + complexity_adjustment))
|
79 |
|
80 |
message = anthropic.messages.create(
|
81 |
model="claude-3-opus-20240229",
|
82 |
max_tokens=1500,
|
83 |
+
temperature=temperature,
|
84 |
messages=[{
|
85 |
"role": "user",
|
86 |
"content": f"{system_prompt}\n\nWrite an exam for {subject}."
|
|
|
118 |
except Exception as e:
|
119 |
return f"Error: {str(e)}"
|
120 |
|
121 |
+
# Subject choices remain the same
|
122 |
subjects = [
|
123 |
"Single Variable Calculus",
|
124 |
"Multivariable Calculus",
|
|
|
148 |
]
|
149 |
),
|
150 |
title="Advanced Mathematics Test Generator",
|
151 |
+
description="""Generates unique university-level mathematics exam questions with solutions using Claude 3 Opus.
|
152 |
+
Each test features different topics and difficulty levels. Limited to 25 requests per day.""",
|
153 |
theme="default",
|
154 |
allow_flagging="never"
|
155 |
)
|
156 |
|
|
|
157 |
if __name__ == "__main__":
|
158 |
+
interface.launch()
|