Kamocodes commited on
Commit
0faa11e
verified
1 Parent(s): 55b48e8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +344 -402
app.py CHANGED
@@ -1,403 +1,345 @@
1
- import gradio as gr
2
- import random
3
- import json
4
- import tempfile
5
- import os
6
- from datetime import datetime
7
-
8
- # Define a comprehensive list of calculus topics based on James Stewart's textbook
9
- TOPICS = {
10
- "Limits and Continuity": {
11
- "formula": "For a function $f(x)$, $\\lim_{x \\to a} f(x) = L$",
12
- "functions": {
13
- "easy": [
14
- {"func": "$\\lim_{x \\to 2} (3x+4)$", "domain": ["$x \\to 2$"], "solution": "$10$"},
15
- {"func": "$\\lim_{x \\to 0} \\frac{\\sin(x)}{x}$", "domain": ["$x \\to 0$"], "solution": "$1$"},
16
- {"func": "$\\lim_{x \\to 3} (x^2-5x+2)$", "domain": ["$x \\to 3$"], "solution": "$-4$"},
17
- {"func": "$\\lim_{x \\to 1} \\frac{x^2-1}{x-1}$", "domain": ["$x \\to 1$"], "solution": "$2$"},
18
- {"func": "$\\lim_{x \\to \\infty} \\frac{2x^2+3x-5}{x^2}$", "domain": ["$x \\to \\infty$"], "solution": "$2$"}
19
- ],
20
- "hard": [
21
- {"func": "$\\lim_{x \\to 0} \\frac{1-\\cos(x)}{x^2}$", "domain": ["$x \\to 0$"], "solution": "$\\frac{1}{2}$"},
22
- {"func": "$\\lim_{x \\to 0} (\\frac{1}{x} - \\frac{1}{\\sin(x)})$", "domain": ["$x \\to 0$"], "solution": "$0$"},
23
- {"func": "$\\lim_{x \\to 0} \\frac{e^x-1-x}{x^2}$", "domain": ["$x \\to 0$"], "solution": "$\\frac{1}{2}$"},
24
- {"func": "$\\lim_{x \\to \\infty} (1 + \\frac{1}{x})^x$", "domain": ["$x \\to \\infty$"], "solution": "$e$"},
25
- {"func": "$\\lim_{x \\to 0^+} x^{\\alpha}\\ln(x)$ where $\\alpha > 0$", "domain": ["$x \\to 0^+$"], "solution": "$0$"}
26
- ]
27
- }
28
- },
29
- "Derivatives and Differentiation": {
30
- "formula": "$f'(x) = \\lim_{h \\to 0} \\frac{f(x+h) - f(x)}{h}$",
31
- "functions": {
32
- "easy": [
33
- {"func": "$f(x) = x^3 - 4x^2 + 7x - 2$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 3x^2 - 8x + 7$"},
34
- {"func": "$f(x) = \\sin(2x)$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 2\\cos(2x)$"},
35
- {"func": "$f(x) = e^{3x}$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 3e^{3x}$"},
36
- {"func": "$f(x) = \\ln(x^2 + 1)$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\frac{2x}{x^2+1}$"},
37
- {"func": "$f(x) = x^2 e^x$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = x^2 e^x + 2x e^x$"}
38
- ],
39
- "hard": [
40
- {"func": "$f(x) = \\frac{\\sin(x)}{\\cos(x) + 2}$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\frac{\\cos(x)(\\cos(x) + 2) + \\sin^2(x)}{(\\cos(x) + 2)^2}$"},
41
- {"func": "$f(x) = \\int_{0}^{x^2} \\sin(t^2) dt$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = 2x\\sin(x^4)$"},
42
- {"func": "$f(x) = \\arctan(e^x)$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\frac{e^x}{1 + e^{2x}}$"},
43
- {"func": "$f(x) = x^{\\sin(x)}$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = x^{\\sin(x)}(\\cos(x)\\ln(x) + \\frac{\\sin(x)}{x})$"},
44
- {"func": "$f(x) = \\ln(\\sin(x))$", "domain": ["Find $f'(x)$"], "solution": "$f'(x) = \\cot(x)$"}
45
- ]
46
- }
47
- },
48
- "Applications of Derivatives": {
49
- "formula": "Related Rates, Optimization, L'H么pital's Rule",
50
- "functions": {
51
- "easy": [
52
- {"func": "A particle moves according to $s(t) = t^3 - 6t^2 + 9t$. Find its velocity at $t = 2$", "domain": ["$t = 2$"], "solution": "$v(2) = -3$ units/sec"},
53
- {"func": "Find the critical points of $f(x) = x^3 - 3x^2 - 9x + 5$", "domain": ["$x \\in \\mathbb{R}$"], "solution": "$x = -1$ and $x = 3$"},
54
- {"func": "Find the absolute maximum and minimum of $f(x) = x^2 - 4x + 3$ on $[0, 3]$", "domain": ["$[0, 3]$"], "solution": "Maximum: $f(0) = 3$, Minimum: $f(2) = -1$"},
55
- {"func": "Use L'H么pital's Rule to evaluate $\\lim_{x \\to 0} \\frac{\\tan(3x)}{x}$", "domain": ["$x \\to 0$"], "solution": "$3$"},
56
- {"func": "Find the equation of the tangent line to $f(x) = x^2 + 2x - 3$ at $x = 1$", "domain": ["$x = 1$"], "solution": "$y = 4x - 2$"}
57
- ],
58
- "hard": [
59
- {"func": "A ladder 10 feet long leans against a wall. If the bottom slides away at 2 ft/s, how fast is the top sliding down when it's 6 feet above ground?", "domain": ["Rate problem"], "solution": "$\\frac{3}{2}$ ft/s"},
60
- {"func": "Find the dimensions of the rectangle with perimeter 100 m that has the maximum area", "domain": ["Optimization"], "solution": "25 m 脳 25 m square"},
61
- {"func": "Use Newton's method to approximate a root of $f(x) = x^3 - 2x - 5$ starting with $x_1 = 2$", "domain": ["Newton's Method"], "solution": "$x \\approx 2.0946$ after 3 iterations"},
62
- {"func": "Find the absolute extrema of $f(x) = xe^{-x^2}$ on $[0, \\infty)$", "domain": ["$[0, \\infty)$"], "solution": "Maximum: $f(\\frac{1}{\\sqrt{2}}) = \\frac{1}{\\sqrt{2e}}$, Minimum: $f(0) = f(\\infty) = 0$"},
63
- {"func": "Use implicit differentiation to find $\\frac{dy}{dx}$ for $x^3 + y^3 = 6xy$", "domain": ["Implicit"], "solution": "$\\frac{dy}{dx} = \\frac{6y - 3x^2}{3y^2 - 6x}$"}
64
- ]
65
- }
66
- },
67
- "Integration Techniques": {
68
- "formula": "$\\int f(x) dx$ using various methods",
69
- "functions": {
70
- "easy": [
71
- {"func": "$\\int x^3(x^2+1)^4 dx$", "domain": ["Use Substitution"], "solution": "$\\frac{1}{10}(x^2+1)^5 - \\frac{1}{6}(x^2+1)^3 + C$"},
72
- {"func": "$\\int \\frac{1}{x^2-4} dx$", "domain": ["Use Partial Fractions"], "solution": "$\\frac{1}{4}\\ln|\\frac{x-2}{x+2}| + C$"},
73
- {"func": "$\\int x\\sin(x) dx$", "domain": ["Use Integration by Parts"], "solution": "$\\sin(x) - x\\cos(x) + C$"},
74
- {"func": "$\\int \\sec^2(3x) dx$", "domain": ["Trigonometric"], "solution": "$\\frac{1}{3}\\tan(3x) + C$"},
75
- {"func": "$\\int \\frac{5}{3x+6} dx$", "domain": ["Substitution"], "solution": "$\\frac{5}{3}\\ln|3x+6| + C$"}
76
- ],
77
- "hard": [
78
- {"func": "$\\int \\frac{x^2}{\\sqrt{1-x^2}} dx$", "domain": ["Trigonometric Substitution"], "solution": "$-\\frac{x\\sqrt{1-x^2}}{2} - \\frac{\\arcsin(x)}{2} + C$"},
79
- {"func": "$\\int \\frac{\\ln(x)}{x^2} dx$", "domain": ["Integration by Parts"], "solution": "$-\\frac{\\ln(x)}{x} - \\frac{1}{x} + C$"},
80
- {"func": "$\\int e^x\\sin(x) dx$", "domain": ["Integration by Parts twice"], "solution": "$\\frac{e^x(\\sin(x)-\\cos(x))}{2} + C$"},
81
- {"func": "$\\int \\frac{1}{x^2-x-6} dx$", "domain": ["Partial Fractions"], "solution": "$\\frac{1}{5}\\ln|\\frac{x+2}{x-3}| + C$"},
82
- {"func": "$\\int \\frac{1}{\\sqrt{x^2-a^2}} dx$", "domain": ["$a > 0$"], "solution": "$\\ln|x + \\sqrt{x^2-a^2}| + C$"}
83
- ]
84
- }
85
- },
86
- "Average Value": {
87
- "formula": "$f_{avg} = \\frac{1}{b-a} \\int_{a}^{b} f(x) dx$",
88
- "functions": {
89
- "easy": [
90
- {"func": "$x^2$", "domain": [0, 2], "solution": "$\\frac{4}{3}$"},
91
- {"func": "$\\sin(x)$", "domain": [0, "蟺"], "solution": "$\\frac{2}{\\pi}$"},
92
- {"func": "$e^x$", "domain": [0, 1], "solution": "$(e-1)$"},
93
- {"func": "$x$", "domain": [1, 4], "solution": "$\\frac{5}{2}$"},
94
- {"func": "$x^3$", "domain": [0, 1], "solution": "$\\frac{1}{4}$"}
95
- ],
96
- "hard": [
97
- {"func": "$x\\sin(x)$", "domain": [0, "蟺"], "solution": "$\\frac{\\pi}{2}$"},
98
- {"func": "$\\ln(x)$", "domain": [1, "e"], "solution": "$1-\\frac{1}{e}$"},
99
- {"func": "$x^2e^x$", "domain": [0, 1], "solution": "$2e-2$"},
100
- {"func": "$\\frac{1}{1+x^2}$", "domain": [0, 1], "solution": "$\\frac{\\pi}{4}$"},
101
- {"func": "$\\sqrt{x}$", "domain": [0, 4], "solution": "$\\frac{4}{3}$"}
102
- ]
103
- }
104
- },
105
- "Arc Length": {
106
- "formula": "$L = \\int_{a}^{b} \\sqrt{1 + (f'(x))^2} dx$",
107
- "functions": {
108
- "easy": [
109
- {"func": "$x^2$", "domain": [0, 1], "solution": "$\\approx 1.4789$"},
110
- {"func": "$x^{3/2}$", "domain": [0, 1], "solution": "$\\approx 1.1919$"},
111
- {"func": "$2x+1$", "domain": [0, 2], "solution": "$2\\sqrt{5}$"},
112
- {"func": "$x^3$", "domain": [0, 1], "solution": "$\\approx 1.0801$"},
113
- {"func": "$\\sin(x)$", "domain": [0, "蟺/2"], "solution": "$\\approx 1.9118$"}
114
- ],
115
- "hard": [
116
- {"func": "$\\ln(x)$", "domain": [1, 3], "solution": "$\\approx 2.3861$"},
117
- {"func": "$e^x$", "domain": [0, 1], "solution": "$\\approx 1.1752$"},
118
- {"func": "$\\cosh(x)$", "domain": [0, 1], "solution": "$\\sinh(1)$"},
119
- {"func": "$x^2 - \\ln(x)$", "domain": [1, 2], "solution": "$\\approx 3.1623$"},
120
- {"func": "$x = \\cos(t)$, $y = \\sin(t)$ for $t\\in[0,\\pi]$", "domain": [0, "蟺"], "solution": "$\\pi$"}
121
- ]
122
- }
123
- },
124
- "Surface Area": {
125
- "formula": "$S = 2\\pi \\int_{a}^{b} f(x) \\sqrt{1 + (f'(x))^2} dx$",
126
- "functions": {
127
- "easy": [
128
- {"func": "$x$", "domain": [0, 3], "solution": "$2\\pi \\cdot 4.5$"},
129
- {"func": "$x^2$", "domain": [0, 1], "solution": "$\\approx 2\\pi \\cdot 0.7169$"},
130
- {"func": "$\\sqrt{x}$", "domain": [0, 4], "solution": "$\\approx 2\\pi \\cdot 4.5177$"},
131
- {"func": "$1$", "domain": [0, 2], "solution": "$2\\pi \\cdot 2$"},
132
- {"func": "$\\frac{x}{2}$", "domain": [0, 4], "solution": "$2\\pi \\cdot 4.1231$"}
133
- ],
134
- "hard": [
135
- {"func": "$x^3$", "domain": [0, 1], "solution": "$\\approx 2\\pi \\cdot 0.6004$"},
136
- {"func": "$e^x$", "domain": [0, 1], "solution": "$\\approx 2\\pi \\cdot 1.1793$"},
137
- {"func": "$\\sin(x)$", "domain": [0, "蟺/2"], "solution": "$\\approx 2\\pi \\cdot 0.6366$"},
138
- {"func": "$\\frac{1}{x}$", "domain": [1, 2], "solution": "$\\approx 2\\pi \\cdot 1.1478$"},
139
- {"func": "$\\ln(x)$", "domain": [1, 2], "solution": "$\\approx 2\\pi \\cdot 0.5593$"}
140
- ]
141
- }
142
- },
143
- "Differential Equations": {
144
- "formula": "Various types",
145
- "functions": {
146
- "easy": [
147
- {"func": "$\\frac{dy}{dx} = 2x$", "domain": ["$y(0)=1$"], "solution": "$y = x^2 + 1$"},
148
- {"func": "$\\frac{dy}{dx} = y$", "domain": ["$y(0)=1$"], "solution": "$y = e^x$"},
149
- {"func": "$\\frac{dy}{dx} = 3x^2$", "domain": ["$y(0)=2$"], "solution": "$y = x^3 + 2$"},
150
- {"func": "$\\frac{dy}{dx} = -y$", "domain": ["$y(0)=4$"], "solution": "$y = 4e^{-x}$"},
151
- {"func": "$\\frac{dy}{dx} = x+1$", "domain": ["$y(0)=-2$"], "solution": "$y = \\frac{x^2}{2} + x - 2$"}
152
- ],
153
- "hard": [
154
- {"func": "$y'' + 4y = 0$", "domain": ["$y(0)=1$, $y'(0)=0$"], "solution": "$y = \\cos(2x)$"},
155
- {"func": "$y'' - y = x$", "domain": ["$y(0)=0$, $y'(0)=1$"], "solution": "$y = \\frac{e^x}{2} - \\frac{e^{-x}}{2} - x$"},
156
- {"func": "$y' + y = e^x$", "domain": ["$y(0)=0$"], "solution": "$y = xe^x$"},
157
- {"func": "$y'' + 2y' + y = 0$", "domain": ["$y(0)=1$, $y'(0)=-1$"], "solution": "$y = (1-x)e^{-x}$"},
158
- {"func": "$y'' - 2y' + y = x^2$", "domain": ["$y(0)=1$, $y'(0)=1$"], "solution": "$y = \\frac{x^2}{2} + 2x + 1$"}
159
- ]
160
- }
161
- },
162
- "Area and Volume": {
163
- "formula": "$A = \\int_{a}^{b} f(x) dx$, $V = \\pi \\int_{a}^{b} [f(x)]^2 dx$",
164
- "functions": {
165
- "easy": [
166
- {"func": "$f(x) = x^2$, find area under the curve", "domain": [0, 3], "solution": "$9$"},
167
- {"func": "$f(x) = \\sin(x)$, find area under the curve", "domain": [0, "蟺"], "solution": "$2$"},
168
- {"func": "$f(x) = 4-x^2$, find area under the curve", "domain": [-2, 2], "solution": "$\\frac{16}{3}$"},
169
- {"func": "$f(x) = \\sqrt{x}$, find volume of revolution around x-axis", "domain": [0, 4], "solution": "$\\frac{16\\pi}{3}$"},
170
- {"func": "$f(x) = x$, find volume of revolution around x-axis", "domain": [0, 2], "solution": "$\\frac{8\\pi}{3}$"}
171
- ],
172
- "hard": [
173
- {"func": "Area between $f(x) = x^2$ and $g(x) = x^3$", "domain": [0, 1], "solution": "$\\frac{1}{12}$"},
174
- {"func": "Volume of solid bounded by $z = 4-x^2-y^2$ and $z = 0$", "domain": ["$x^2+y^2\\leq 4$"], "solution": "$8\\pi$"},
175
- {"func": "Volume of solid formed by rotating region bounded by $y = x^2$, $y = 0$, $x = 2$ around y-axis", "domain": [0, 2], "solution": "$\\frac{8\\pi}{5}$"},
176
- {"func": "Area between $f(x) = \\sin(x)$ and $g(x) = \\cos(x)$", "domain": [0, "蟺/4"], "solution": "$\\sqrt{2}-1$"},
177
- {"func": "Volume of solid formed by rotating region bounded by $y = e^x$, $y = 0$, $x = 0$, $x = 1$ around x-axis", "domain": [0, 1], "solution": "$\\frac{\\pi(e^2-1)}{2}$"}
178
- ]
179
- }
180
- },
181
- "Parametric Curves and Equations": {
182
- "formula": "$x = x(t)$, $y = y(t)$, Arc length = $\\int_{a}^{b} \\sqrt{(\\frac{dx}{dt})^2 + (\\frac{dy}{dt})^2} dt$",
183
- "functions": {
184
- "easy": [
185
- {"func": "$x = t$, $y = t^2$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = 2t$"},
186
- {"func": "$x = \\cos(t)$, $y = \\sin(t)$, find the arc length", "domain": [0, "蟺/2"], "solution": "$\\frac{\\pi}{2}$"},
187
- {"func": "$x = t^2$, $y = t^3$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\frac{3t}{2}$"},
188
- {"func": "$x = 2t$, $y = t^2$, find the area under the curve", "domain": [0, 2], "solution": "$\\frac{4}{3}$"},
189
- {"func": "$x = t$, $y = \\sin(t)$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\cos(t)$"}
190
- ],
191
- "hard": [
192
- {"func": "$x = e^t\\cos(t)$, $y = e^t\\sin(t)$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\tan(t) + 1$"},
193
- {"func": "$x = t-\\sin(t)$, $y = 1-\\cos(t)$, find the arc length", "domain": [0, "2蟺"], "solution": "$8$"},
194
- {"func": "$x = \\ln(\\sec(t))$, $y = \\tan(t)$, find $\\frac{dy}{dx}$", "domain": ["$t$"], "solution": "$\\frac{dy}{dx} = \\sec^2(t)$"},
195
- {"func": "$x = \\cos^3(t)$, $y = \\sin^3(t)$, find the area enclosed", "domain": [0, "2蟺"], "solution": "$\\frac{3\\pi}{8}$"},
196
- {"func": "$x = \\cos(t)+t\\sin(t)$, $y = \\sin(t)-t\\cos(t)$, find the arc length", "domain": [0, "2蟺"], "solution": "$2\\pi\\sqrt{1+4\\pi^2}$"}
197
- ]
198
- }
199
- },
200
- "Infinite Sequences and Series": {
201
- "formula": "$\\sum_{n=1}^{\\infty} a_n = a_1 + a_2 + a_3 + ...$",
202
- "functions": {
203
- "easy": [
204
- {"func": "Determine if the sequence $a_n = \\frac{n+3}{2n+1}$ converges and find its limit", "domain": ["$n \\to \\infty$"], "solution": "Converges to $\\frac{1}{2}$"},
205
- {"func": "Find the sum of the geometric series $\\sum_{n=0}^{\\infty} \\frac{1}{3^n}$", "domain": ["Geometric Series"], "solution": "$\\frac{3}{2}$"},
206
- {"func": "Determine if the series $\\sum_{n=1}^{\\infty} \\frac{1}{n^2}$ converges", "domain": ["p-series"], "solution": "Converges (p-series with $p=2 > 1$)"},
207
- {"func": "Find the first three non-zero terms in the Taylor series for $f(x) = e^x$ centered at $a = 0$", "domain": ["Taylor Series"], "solution": "$1 + x + \\frac{x^2}{2} + ...$"},
208
- {"func": "Find the radius of convergence of the power series $\\sum_{n=1}^{\\infty} \\frac{x^n}{n}$", "domain": ["Power Series"], "solution": "$R = 1$"}
209
- ],
210
- "hard": [
211
- {"func": "Test the convergence of the alternating series $\\sum_{n=1}^{\\infty} (-1)^{n+1}\\frac{\\ln(n)}{n}$", "domain": ["Alternating Series"], "solution": "Converges by the Alternating Series Test"},
212
- {"func": "Find the radius and interval of convergence for $\\sum_{n=1}^{\\infty} \\frac{n^2 x^n}{3^n}$", "domain": ["Power Series"], "solution": "$R = 3$, interval of convergence is $(-3, 3)$"},
213
- {"func": "Determine if the series $\\sum_{n=2}^{\\infty} \\frac{1}{n\\ln(n)}$ converges", "domain": ["Integral Test"], "solution": "Diverges by the Integral Test"},
214
- {"func": "Find the sum of the series $\\sum_{n=1}^{\\infty} \\frac{1}{n(n+1)}$", "domain": ["Telescoping Series"], "solution": "$1$"},
215
- {"func": "Find the Taylor series of $f(x) = \\ln(1+x)$ and its radius of convergence", "domain": ["Taylor Series"], "solution": "$\\sum_{n=1}^{\\infty} \\frac{(-1)^{n+1}x^n}{n}$, $R = 1$"}
216
- ]
217
- }
218
- },
219
- "Polar Coordinates": {
220
- "formula": "$x = r\\cos(\\theta)$, $y = r\\sin(\\theta)$, Area = $\\frac{1}{2}\\int_{\\alpha}^{\\beta} r^2 d\\theta$",
221
- "functions": {
222
- "easy": [
223
- {"func": "Convert the Cartesian point $(1, \\sqrt{3})$ to polar coordinates", "domain": ["Conversion"], "solution": "$r = 2$, $\\theta = \\frac{\\pi}{3}$"},
224
- {"func": "Find the area enclosed by the circle $r = 3\\sin(\\theta)$", "domain": ["Polar Area"], "solution": "$\\frac{9\\pi}{2}$"},
225
- {"func": "Convert the polar equation $r = 2$ to Cartesian form", "domain": ["Conversion"], "solution": "$x^2 + y^2 = 4$"},
226
- {"func": "Find the area enclosed by $r = 2\\cos(\\theta)$", "domain": ["Polar Area"], "solution": "$2\\pi$"},
227
- {"func": "Convert the Cartesian equation $x^2 + y^2 = 4y$ to polar form", "domain": ["Conversion"], "solution": "$r = 4\\sin(\\theta)$"}
228
- ],
229
- "hard": [
230
- {"func": "Find the area of the region enclosed by the lemniscate $r^2 = 4\\cos(2\\theta)$", "domain": ["Polar Area"], "solution": "$4$"},
231
- {"func": "Find the area of the region inside $r = 1 + \\cos(\\theta)$ and outside $r = 1$", "domain": ["Polar Area"], "solution": "$\\frac{\\pi}{2}$"},
232
- {"func": "Find the points of intersection of the polar curves $r = 1 + \\sin(\\theta)$ and $r = 1 - \\sin(\\theta)$", "domain": ["Polar Curves"], "solution": "$(0, 0)$ and $(1, \\frac{\\pi}{2})$, $(1, \\frac{3\\pi}{2})$"},
233
- {"func": "Find the area of the region enclosed by the cardioid $r = 1 + \\cos(\\theta)$", "domain": ["Polar Area"], "solution": "$\\frac{3\\pi}{2}$"},
234
- {"func": "Find the length of the spiral $r = \\theta$ for $0 \\leq \\theta \\leq 2\\pi$", "domain": ["Polar Arc Length"], "solution": "$\\frac{1}{2}\\sqrt{1+4\\pi^2} + \\frac{1}{2}\\ln(2\\pi + \\sqrt{1+4\\pi^2})$"}
235
- ]
236
- }
237
- },
238
- "Vector Calculus": {
239
- "formula": "$\\vec{r}(t) = x(t)\\vec{i} + y(t)\\vec{j} + z(t)\\vec{k}$, $\\vec{v}(t) = \\vec{r}'(t)$, $\\vec{a}(t) = \\vec{v}'(t)$",
240
- "functions": {
241
- "easy": [
242
- {"func": "Find the derivative of the vector function $\\vec{r}(t) = t^2\\vec{i} + \\sin(t)\\vec{j} + e^t\\vec{k}$", "domain": ["Vector Function"], "solution": "$\\vec{r}'(t) = 2t\\vec{i} + \\cos(t)\\vec{j} + e^t\\vec{k}$"},
243
- {"func": "Find the unit tangent vector of $\\vec{r}(t) = \\cos(t)\\vec{i} + \\sin(t)\\vec{j}$ at $t = 0$", "domain": ["$t = 0$"], "solution": "$\\vec{T}(0) = \\vec{j}$"},
244
- {"func": "Calculate $\\nabla f$ where $f(x,y,z) = x^2y + yz^2$", "domain": ["Gradient"], "solution": "$\\nabla f = 2xy\\vec{i} + (x^2 + z^2)\\vec{j} + 2yz\\vec{k}$"},
245
- {"func": "Find the divergence of the vector field $\\vec{F}(x,y,z) = x^2\\vec{i} + 2xy\\vec{j} + yz\\vec{k}$", "domain": ["Divergence"], "solution": "$\\nabla \\cdot \\vec{F} = 2x + 2x + z = 4x + z$"},
246
- {"func": "Find the curl of $\\vec{F}(x,y,z) = y\\vec{i} + z\\vec{j} + x\\vec{k}$", "domain": ["Curl"], "solution": "$\\nabla \\times \\vec{F} = (1-0)\\vec{i} + (1-0)\\vec{j} + (1-0)\\vec{k} = \\vec{i} + \\vec{j} + \\vec{k}$"}
247
- ],
248
- "hard": [
249
- {"func": "Find the curvature of $\\vec{r}(t) = t\\vec{i} + t^2\\vec{j} + t^3\\vec{k}$ at $t = 1$", "domain": ["Curvature at $t = 1$"], "solution": "$\\kappa = \\frac{2\\sqrt{37}}{49\\sqrt{3}}$"},
250
- {"func": "Verify Stokes' Theorem for $\\vec{F} = x^2\\vec{i} + xy\\vec{j} + z^2\\vec{k}$ on the hemisphere $z = \\sqrt{1-x^2-y^2}$, $z \\geq 0$", "domain": ["Stokes' Theorem"], "solution": "Both integrals equal $\\frac{\\pi}{2}$"},
251
- {"func": "Use the Divergence Theorem to evaluate $\\iint_S \\vec{F} \\cdot \\vec{n} \\, dS$ where $\\vec{F}(x,y,z) = x\\vec{i} + y\\vec{j} + z\\vec{k}$ and $S$ is the sphere $x^2+y^2+z^2=4$", "domain": ["Divergence Theorem"], "solution": "$\\iint_S \\vec{F} \\cdot \\vec{n} \\, dS = \\iiint_V 3 \\, dV = 3 \\cdot \\frac{4}{3}\\pi \\cdot 4^{3/2} = 16\\pi$"},
252
- {"func": "Find the potential function $f$ for the conservative vector field $\\vec{F} = (2x+y)\\vec{i} + (x+2z)\\vec{j} + (2y)\\vec{k}$", "domain": ["Potential Function"], "solution": "$f(x,y,z) = x^2 + xy + 2yz + C$"},
253
- {"func": "Use Green's Theorem to evaluate $\\oint_C (y^2\\,dx + x^2\\,dy)$ where $C$ is the boundary of the region enclosed by $y = x^2$ and $y = 4$", "domain": ["Green's Theorem"], "solution": "$\\frac{256}{15}$"}
254
- ]
255
- }
256
- },
257
- "Partial Derivatives and Multiple Integrals": {
258
- "formula": "$\\frac{\\partial f}{\\partial x}$, $\\frac{\\partial f}{\\partial y}$, $\\iint_D f(x,y) \\, dA$, $\\iiint_E f(x,y,z) \\, dV$",
259
- "functions": {
260
- "easy": [
261
- {"func": "Find $\\frac{\\partial z}{\\partial x}$ and $\\frac{\\partial z}{\\partial y}$ for $z = x^2 + 3xy - y^3$", "domain": ["Partial Derivatives"], "solution": "$\\frac{\\partial z}{\\partial x} = 2x + 3y$, $\\frac{\\partial z}{\\partial y} = 3x - 3y^2$"},
262
- {"func": "Evaluate $\\iint_D (x + y) \\, dA$ where $D = \\{(x, y) | 0 \\leq x \\leq 1, 0 \\leq y \\leq 2\\}$", "domain": ["Double Integral"], "solution": "$3$"},
263
- {"func": "Find all critical points of $f(x,y) = x^2 + y^2 - 4x - 6y + 12$", "domain": ["Critical Points"], "solution": "$(2, 3)$"},
264
- {"func": "Convert the double integral $\\iint_D x^2y \\, dA$ to polar coordinates where $D$ is the disc $x^2 + y^2 \\leq 4$", "domain": ["Change to Polar"], "solution": "$\\int_0^{2\\pi} \\int_0^2 r^3 \\cos^2(\\theta)\\sin(\\theta) \\, dr \\, d\\theta$"},
265
- {"func": "Evaluate $\\iint_D xy \\, dA$ where $D$ is the triangle with vertices $(0,0)$, $(1,0)$, and $(0,1)$", "domain": ["Double Integral"], "solution": "$\\frac{1}{24}$"}
266
- ],
267
- "hard": [
268
- {"func": "Find the absolute maximum and minimum values of $f(x,y) = 2x^2 + y^2 - 4x + 6y + 10$ on the closed disc $x^2 + y^2 \\leq 25$", "domain": ["Extrema on Region"], "solution": "Maximum: $135$ at $(5,0)$, Minimum: $1$ at $(1,-3)$"},
269
- {"func": "Evaluate $\\iiint_E xy^2z^3 \\, dV$ where $E$ is the solid bounded by $x=0$, $y=0$, $z=0$, $x+y+z=1$", "domain": ["Triple Integral"], "solution": "$\\frac{1}{840}$"},
270
- {"func": "Use Lagrange multipliers to find the maximum value of $f(x,y) = xy$ subject to the constraint $x^2 + y^2 = 8$", "domain": ["Lagrange Multipliers"], "solution": "$4$ at $(\\pm 2, \\pm 2)$"},
271
- {"func": "Change the order of integration in $\\int_0^1 \\int_y^1 e^{xy} \\, dx \\, dy$", "domain": ["Change of Order"], "solution": "$\\int_0^1 \\int_0^x e^{xy} \\, dy \\, dx$"},
272
- {"func": "Evaluate $\\iint_D \\frac{1}{1+x^2+y^2} \\, dA$ where $D = \\{(x,y) | x^2 + y^2 \\leq 4\\}$", "domain": ["Double Integral"], "solution": "$\\pi\\ln(5)$"}
273
- ]
274
- }
275
- }
276
- }
277
-
278
- # Function to generate a question for a given topic and difficulty
279
- def generate_question(topic_name, difficulty):
280
- topic_data = TOPICS[topic_name]
281
- formula = topic_data["formula"]
282
-
283
- # Select a random function from the available ones for this topic and difficulty
284
- function_data = random.choice(topic_data["functions"][difficulty])
285
- func = function_data["func"]
286
- domain = function_data["domain"]
287
- solution = function_data["solution"]
288
-
289
- # Format domain for display
290
- if isinstance(domain, list) and len(domain) == 2:
291
- domain_str = f"[{domain[0]}, {domain[1]}]"
292
- else:
293
- domain_str = str(domain)
294
-
295
- # Create question and solution based on difficulty
296
- if difficulty == "easy":
297
- question = f"Find the {topic_name.lower()} of {func} over the domain {domain_str}."
298
- solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n"
299
- solution_text += f"Step 2: Substitute $f(x) = {func}$ and evaluate over {domain_str}\n\n"
300
- solution_text += f"Step 3: Solve the resulting integral or calculation\n\n"
301
- solution_text += f"Final Answer: {solution}"
302
- else:
303
- question = f"Compute the {topic_name.lower()} for {func} over {domain_str}."
304
- solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n"
305
- solution_text += f"Step 2: For {func}, substitute into the formula and evaluate over {domain_str}\n\n"
306
- solution_text += f"Step 3: This requires advanced integration techniques or careful analysis\n\n"
307
- solution_text += f"Final Answer: {solution}"
308
-
309
- return {
310
- "topic": topic_name,
311
- "difficulty": difficulty,
312
- "question": question,
313
- "solution": solution_text
314
- }
315
-
316
- # Store the latest generated questions
317
- latest_questions = []
318
-
319
- # Generate questions function for Gradio
320
- def generate_calculus_questions(topic, difficulty, count):
321
- global latest_questions
322
- latest_questions = []
323
-
324
- for _ in range(int(count)):
325
- question_data = generate_question(topic, difficulty)
326
- latest_questions.append(question_data)
327
-
328
- # Format the output for display
329
- result = ""
330
- for i, q in enumerate(latest_questions):
331
- result += f"Question {i+1}: {q['question']}\n\n"
332
- result += f"Solution {i+1}: {q['solution']}\n\n"
333
- result += "-" * 40 + "\n\n"
334
-
335
- return result
336
-
337
- # Function to export questions to JSON
338
- def export_to_json():
339
- if not latest_questions:
340
- return None
341
-
342
- # Create a JSON file
343
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
344
- json_data = {
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()