Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,403 +1,345 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
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 |
-
"generated_at": timestamp,
|
346 |
-
"questions": latest_questions
|
347 |
-
}
|
348 |
-
|
349 |
-
# Create a temporary file
|
350 |
-
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as temp_file:
|
351 |
-
json.dump(json_data, temp_file, indent=2)
|
352 |
-
temp_file_path = temp_file.name
|
353 |
-
|
354 |
-
return temp_file_path
|
355 |
-
|
356 |
-
# Create the Gradio interface
|
357 |
-
with gr.Blocks(title="Math Mento - Calculus Questions Generator") as demo:
|
358 |
-
gr.Markdown("# Math Mento - Calculus Questions Generator")
|
359 |
-
gr.Markdown("Generate LaTeX-formatted calculus practice questions with solutions based on James Stewart's calculus textbook")
|
360 |
-
|
361 |
-
with gr.Row():
|
362 |
-
with gr.Column():
|
363 |
-
topic = gr.Dropdown(
|
364 |
-
choices=list(TOPICS.keys()),
|
365 |
-
label="Calculus Topic",
|
366 |
-
value="Limits and Continuity"
|
367 |
-
)
|
368 |
-
difficulty = gr.Radio(
|
369 |
-
choices=["easy", "hard"],
|
370 |
-
label="Difficulty",
|
371 |
-
value="easy"
|
372 |
-
)
|
373 |
-
count = gr.Slider(
|
374 |
-
minimum=1,
|
375 |
-
maximum=10,
|
376 |
-
step=1,
|
377 |
-
value=3,
|
378 |
-
label="Number of Questions"
|
379 |
-
)
|
380 |
-
generate_button = gr.Button("Generate Questions")
|
381 |
-
export_button = gr.Button("Export to JSON")
|
382 |
-
|
383 |
-
with gr.Column():
|
384 |
-
output = gr.Markdown()
|
385 |
-
json_file = gr.File(label="Exported JSON")
|
386 |
-
|
387 |
-
generate_button.click(
|
388 |
-
generate_calculus_questions,
|
389 |
-
inputs=[topic, difficulty, count],
|
390 |
-
outputs=output
|
391 |
-
)
|
392 |
-
|
393 |
-
export_button.click(
|
394 |
-
export_to_json,
|
395 |
-
inputs=[],
|
396 |
-
outputs=json_file
|
397 |
-
)
|
398 |
-
|
399 |
-
gr.Markdown("### Created by Kamogelo Mosia | Math Mento 漏 2025")
|
400 |
-
|
401 |
-
# Launch the app
|
402 |
-
if __name__ == "__main__":
|
403 |
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
|
5 |
+
# Define calculus topics and subtopics
|
6 |
+
CALCULUS_TOPICS = {
|
7 |
+
"Limits": [
|
8 |
+
"Finding limits algebraically",
|
9 |
+
"One-sided limits",
|
10 |
+
"Limits at infinity",
|
11 |
+
"Limits that don't exist",
|
12 |
+
"Epsilon-delta proofs"
|
13 |
+
],
|
14 |
+
"Derivatives": [
|
15 |
+
"Basic differentiation rules",
|
16 |
+
"Power rule",
|
17 |
+
"Product rule",
|
18 |
+
"Quotient rule",
|
19 |
+
"Chain rule",
|
20 |
+
"Implicit differentiation",
|
21 |
+
"Logarithmic differentiation",
|
22 |
+
"Higher-order derivatives"
|
23 |
+
],
|
24 |
+
"Applications of Derivatives": [
|
25 |
+
"Related rates",
|
26 |
+
"Mean value theorem",
|
27 |
+
"Critical points",
|
28 |
+
"Curve sketching",
|
29 |
+
"Optimization problems",
|
30 |
+
"Linear approximation",
|
31 |
+
"Newton's method"
|
32 |
+
],
|
33 |
+
"Integrals": [
|
34 |
+
"Indefinite integrals",
|
35 |
+
"Basic integration rules",
|
36 |
+
"Substitution method",
|
37 |
+
"Integration by parts",
|
38 |
+
"Partial fractions",
|
39 |
+
"Trigonometric integrals",
|
40 |
+
"Improper integrals"
|
41 |
+
],
|
42 |
+
"Applications of Integrals": [
|
43 |
+
"Area between curves",
|
44 |
+
"Volume of solids of revolution",
|
45 |
+
"Arc length",
|
46 |
+
"Surface area",
|
47 |
+
"Work and energy problems",
|
48 |
+
"Center of mass"
|
49 |
+
],
|
50 |
+
"Sequences and Series": [
|
51 |
+
"Convergence tests",
|
52 |
+
"Power series",
|
53 |
+
"Taylor and Maclaurin series",
|
54 |
+
"Radius of convergence"
|
55 |
+
],
|
56 |
+
"Multivariable Calculus": [
|
57 |
+
"Partial derivatives",
|
58 |
+
"Directional derivatives",
|
59 |
+
"Gradient",
|
60 |
+
"Multiple integrals",
|
61 |
+
"Line integrals",
|
62 |
+
"Surface integrals"
|
63 |
+
],
|
64 |
+
"Differential Equations": [
|
65 |
+
"First-order differential equations",
|
66 |
+
"Separable equations",
|
67 |
+
"Second-order differential equations",
|
68 |
+
"Systems of differential equations"
|
69 |
+
]
|
70 |
+
}
|
71 |
+
|
72 |
+
# Initialize DeepSeek client
|
73 |
+
def get_deepseek_client():
|
74 |
+
# You'll need to set up your HuggingFace token as an environment variable
|
75 |
+
client = InferenceClient(
|
76 |
+
model="deepseek-ai/deepseek-math-7b-instruct",
|
77 |
+
token=os.getenv("HUGGINGFACE_TOKEN")
|
78 |
+
)
|
79 |
+
return client
|
80 |
+
|
81 |
+
def generate_math_problems(topic, subtopic, difficulty, num_problems=3):
|
82 |
+
"""Generate math problems with step-by-step solutions and explanations"""
|
83 |
+
client = get_deepseek_client()
|
84 |
+
|
85 |
+
# Create a specific prompt with the selected topic, subtopic and difficulty
|
86 |
+
prompt = f"""
|
87 |
+
Create {num_problems} {difficulty} difficulty calculus problems about '{subtopic}' (under the general topic of '{topic}').
|
88 |
+
|
89 |
+
For each problem:
|
90 |
+
1. Provide a clear and concise question.
|
91 |
+
2. Provide a detailed step-by-step solution that a student can follow.
|
92 |
+
3. Include explanations for each step.
|
93 |
+
4. Mark the final answer clearly.
|
94 |
+
|
95 |
+
Format:
|
96 |
+
## Problem N:
|
97 |
+
[Problem description]
|
98 |
+
|
99 |
+
### Solution:
|
100 |
+
[Step-by-step solution]
|
101 |
+
|
102 |
+
### Explanation:
|
103 |
+
[Explanation for the solution steps]
|
104 |
+
|
105 |
+
### Answer:
|
106 |
+
[Final answer]
|
107 |
+
"""
|
108 |
+
|
109 |
+
try:
|
110 |
+
response = client.text_generation(
|
111 |
+
prompt=prompt,
|
112 |
+
max_new_tokens=2048,
|
113 |
+
temperature=0.7,
|
114 |
+
repetition_penalty=1.2,
|
115 |
+
do_sample=True
|
116 |
+
)
|
117 |
+
return response
|
118 |
+
except Exception as e:
|
119 |
+
return f"Error generating problems: {str(e)}"
|
120 |
+
|
121 |
+
# Function to update subtopics based on main topic selection
|
122 |
+
def update_subtopics(topic):
|
123 |
+
return gr.Dropdown(choices=CALCULUS_TOPICS.get(topic, []), value=CALCULUS_TOPICS.get(topic, [""])[0])
|
124 |
+
|
125 |
+
# Function to handle Gradio input and output
|
126 |
+
def create_problems(topic, subtopic, difficulty, num_problems):
|
127 |
+
if not topic or not subtopic:
|
128 |
+
return "Please select both a topic and subtopic to generate problems."
|
129 |
+
|
130 |
+
try:
|
131 |
+
num_problems = int(num_problems)
|
132 |
+
except ValueError:
|
133 |
+
num_problems = 3 # Default to 3 problems if input is invalid
|
134 |
+
|
135 |
+
return generate_math_problems(topic, subtopic, difficulty, num_problems)
|
136 |
+
|
137 |
+
# Custom CSS with Google Fonts integration
|
138 |
+
custom_css = """
|
139 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500&family=JetBrains+Mono&display=swap');
|
140 |
+
|
141 |
+
:root {
|
142 |
+
--border-color: #e0e0e0;
|
143 |
+
--bg-color: #ffffff;
|
144 |
+
--text-color: #333333;
|
145 |
+
--hover-color: #f0f0f0;
|
146 |
+
}
|
147 |
+
|
148 |
+
body, .gradio-container {
|
149 |
+
font-family: 'Inter', sans-serif;
|
150 |
+
color: var(--text-color);
|
151 |
+
}
|
152 |
+
|
153 |
+
.header {
|
154 |
+
text-align: center;
|
155 |
+
padding: 1.5rem 0;
|
156 |
+
margin-bottom: 1rem;
|
157 |
+
border-bottom: 1px solid var(--border-color);
|
158 |
+
}
|
159 |
+
|
160 |
+
.header h1 {
|
161 |
+
font-weight: 500;
|
162 |
+
margin-bottom: 0.5rem;
|
163 |
+
}
|
164 |
+
|
165 |
+
.header p {
|
166 |
+
font-weight: 300;
|
167 |
+
opacity: 0.8;
|
168 |
+
}
|
169 |
+
|
170 |
+
.card {
|
171 |
+
border: 1px solid var(--border-color);
|
172 |
+
border-radius: 4px;
|
173 |
+
padding: 1.25rem;
|
174 |
+
margin-bottom: 1rem;
|
175 |
+
background-color: var(--bg-color);
|
176 |
+
}
|
177 |
+
|
178 |
+
.card h3 {
|
179 |
+
font-weight: 500;
|
180 |
+
margin-bottom: 1rem;
|
181 |
+
font-size: 1.1rem;
|
182 |
+
}
|
183 |
+
|
184 |
+
button.generate-btn {
|
185 |
+
width: 100%;
|
186 |
+
background-color: var(--bg-color);
|
187 |
+
border: 1px solid var(--border-color);
|
188 |
+
border-radius: 4px;
|
189 |
+
padding: 0.5rem;
|
190 |
+
cursor: pointer;
|
191 |
+
transition: background-color 0.2s ease;
|
192 |
+
font-family: 'Inter', sans-serif;
|
193 |
+
font-weight: 500;
|
194 |
+
}
|
195 |
+
|
196 |
+
button.generate-btn:hover {
|
197 |
+
background-color: var(--hover-color);
|
198 |
+
}
|
199 |
+
|
200 |
+
.output-box {
|
201 |
+
font-family: 'JetBrains Mono', monospace;
|
202 |
+
border: 1px solid var(--border-color);
|
203 |
+
border-radius: 4px;
|
204 |
+
padding: 1rem;
|
205 |
+
white-space: pre-wrap;
|
206 |
+
line-height: 1.5;
|
207 |
+
}
|
208 |
+
|
209 |
+
.output-box h2 {
|
210 |
+
font-weight: 500;
|
211 |
+
margin-top: 1.5rem;
|
212 |
+
padding-top: 1rem;
|
213 |
+
border-top: 1px solid var(--border-color);
|
214 |
+
}
|
215 |
+
|
216 |
+
.output-box h2:first-child {
|
217 |
+
margin-top: 0;
|
218 |
+
padding-top: 0;
|
219 |
+
border-top: none;
|
220 |
+
}
|
221 |
+
|
222 |
+
.output-box h3 {
|
223 |
+
font-weight: 500;
|
224 |
+
margin-top: 1rem;
|
225 |
+
}
|
226 |
+
|
227 |
+
/* Customizing Gradio elements */
|
228 |
+
.gr-form, .gr-panel {
|
229 |
+
border: none !important;
|
230 |
+
box-shadow: none !important;
|
231 |
+
}
|
232 |
+
|
233 |
+
.gr-input, .gr-dropdown, .gr-radio {
|
234 |
+
border: 1px solid var(--border-color) !important;
|
235 |
+
border-radius: 4px !important;
|
236 |
+
}
|
237 |
+
|
238 |
+
.gr-input:focus, .gr-dropdown:focus {
|
239 |
+
border-color: #999 !important;
|
240 |
+
box-shadow: none !important;
|
241 |
+
}
|
242 |
+
|
243 |
+
.gr-slider-thumb {
|
244 |
+
background-color: #999 !important;
|
245 |
+
}
|
246 |
+
|
247 |
+
.footer {
|
248 |
+
text-align: center;
|
249 |
+
font-size: 0.8rem;
|
250 |
+
padding: 1rem 0;
|
251 |
+
border-top: 1px solid var(--border-color);
|
252 |
+
margin-top: 2rem;
|
253 |
+
opacity: 0.7;
|
254 |
+
}
|
255 |
+
"""
|
256 |
+
|
257 |
+
# Define the Gradio interface with custom styling
|
258 |
+
with gr.Blocks(css=custom_css) as demo:
|
259 |
+
# Header
|
260 |
+
gr.HTML(
|
261 |
+
"""
|
262 |
+
<div class="header">
|
263 |
+
<h1>Calculus Problem Generator</h1>
|
264 |
+
<p>Generate problems with step-by-step solutions</p>
|
265 |
+
</div>
|
266 |
+
"""
|
267 |
+
)
|
268 |
+
|
269 |
+
# Main content area
|
270 |
+
with gr.Row():
|
271 |
+
# Left panel - Problem configuration
|
272 |
+
with gr.Column(scale=1):
|
273 |
+
with gr.Box(elem_classes="card"):
|
274 |
+
gr.Markdown("### Configure Problems")
|
275 |
+
|
276 |
+
# Topic selection
|
277 |
+
topic_dropdown = gr.Dropdown(
|
278 |
+
choices=list(CALCULUS_TOPICS.keys()),
|
279 |
+
label="Topic",
|
280 |
+
value=list(CALCULUS_TOPICS.keys())[0]
|
281 |
+
)
|
282 |
+
|
283 |
+
# Subtopic selection (dynamically updated)
|
284 |
+
subtopic_dropdown = gr.Dropdown(
|
285 |
+
choices=CALCULUS_TOPICS[list(CALCULUS_TOPICS.keys())[0]],
|
286 |
+
label="Subtopic",
|
287 |
+
value=CALCULUS_TOPICS[list(CALCULUS_TOPICS.keys())[0]][0]
|
288 |
+
)
|
289 |
+
|
290 |
+
# Update subtopics when topic changes
|
291 |
+
topic_dropdown.change(
|
292 |
+
fn=update_subtopics,
|
293 |
+
inputs=topic_dropdown,
|
294 |
+
outputs=subtopic_dropdown
|
295 |
+
)
|
296 |
+
|
297 |
+
# Difficulty selection
|
298 |
+
difficulty_radio = gr.Radio(
|
299 |
+
choices=["Basic", "Intermediate", "Advanced"],
|
300 |
+
label="Difficulty",
|
301 |
+
value="Intermediate"
|
302 |
+
)
|
303 |
+
|
304 |
+
# Number of problems
|
305 |
+
num_problems_slider = gr.Slider(
|
306 |
+
minimum=1,
|
307 |
+
maximum=5,
|
308 |
+
value=3,
|
309 |
+
step=1,
|
310 |
+
label="Number of Problems"
|
311 |
+
)
|
312 |
+
|
313 |
+
# Generate button
|
314 |
+
generate_button = gr.Button("Generate Problems", elem_classes="generate-btn")
|
315 |
+
|
316 |
+
# Right panel - Output display
|
317 |
+
with gr.Column(scale=2):
|
318 |
+
with gr.Box(elem_classes="card"):
|
319 |
+
gr.Markdown("### Problems & Solutions")
|
320 |
+
|
321 |
+
# Output display
|
322 |
+
output_md = gr.Markdown(
|
323 |
+
elem_classes="output-box",
|
324 |
+
value="Select options and click 'Generate Problems' to start."
|
325 |
+
)
|
326 |
+
|
327 |
+
# Footer
|
328 |
+
gr.HTML(
|
329 |
+
"""
|
330 |
+
<div class="footer">
|
331 |
+
<p>Calculus Problem Generator | Educational Tool</p>
|
332 |
+
</div>
|
333 |
+
"""
|
334 |
+
)
|
335 |
+
|
336 |
+
# Connect the generate button to the function
|
337 |
+
generate_button.click(
|
338 |
+
fn=create_problems,
|
339 |
+
inputs=[topic_dropdown, subtopic_dropdown, difficulty_radio, num_problems_slider],
|
340 |
+
outputs=output_md
|
341 |
+
)
|
342 |
+
|
343 |
+
# Launch the app
|
344 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
345 |
demo.launch()
|