Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
import math
|
5 |
+
|
6 |
+
# Define a list of topics with specific formulas and question templates
|
7 |
+
TOPICS = {
|
8 |
+
"Average Value": {
|
9 |
+
"formula": "f_avg = (1/(b-a)) * ∫[a,b] f(x) dx",
|
10 |
+
"functions": {
|
11 |
+
"easy": [
|
12 |
+
{"func": "x^2", "domain": [0, 2], "solution": "4/3"},
|
13 |
+
{"func": "sin(x)", "domain": [0, "π"], "solution": "2/π"},
|
14 |
+
{"func": "e^x", "domain": [0, 1], "solution": "(e-1)"},
|
15 |
+
{"func": "x", "domain": [1, 4], "solution": "5/2"},
|
16 |
+
{"func": "x^3", "domain": [0, 1], "solution": "1/4"}
|
17 |
+
],
|
18 |
+
"hard": [
|
19 |
+
{"func": "x*sin(x)", "domain": [0, "π"], "solution": "π/2"},
|
20 |
+
{"func": "ln(x)", "domain": [1, "e"], "solution": "1-1/e"},
|
21 |
+
{"func": "x^2*e^x", "domain": [0, 1], "solution": "2e-2"},
|
22 |
+
{"func": "1/(1+x^2)", "domain": [0, 1], "solution": "π/4"},
|
23 |
+
{"func": "sqrt(x)", "domain": [0, 4], "solution": "4/3"}
|
24 |
+
]
|
25 |
+
}
|
26 |
+
},
|
27 |
+
"Arc Length": {
|
28 |
+
"formula": "L = ∫[a,b] sqrt(1 + (f'(x))^2) dx",
|
29 |
+
"functions": {
|
30 |
+
"easy": [
|
31 |
+
{"func": "x^2", "domain": [0, 1], "solution": "approx. 1.4789"},
|
32 |
+
{"func": "x^(3/2)", "domain": [0, 1], "solution": "approx. 1.1919"},
|
33 |
+
{"func": "2x+1", "domain": [0, 2], "solution": "sqrt(5)*2"},
|
34 |
+
{"func": "x^3", "domain": [0, 1], "solution": "approx. 1.0801"},
|
35 |
+
{"func": "sin(x)", "domain": [0, "π/2"], "solution": "approx. 1.9118"}
|
36 |
+
],
|
37 |
+
"hard": [
|
38 |
+
{"func": "ln(x)", "domain": [1, 3], "solution": "approx. 2.3861"},
|
39 |
+
{"func": "e^x", "domain": [0, 1], "solution": "approx. 1.1752"},
|
40 |
+
{"func": "cosh(x)", "domain": [0, 1], "solution": "sinh(1)"},
|
41 |
+
{"func": "x^2 - ln(x)", "domain": [1, 2], "solution": "approx. 3.1623"},
|
42 |
+
{"func": "parametric: x=cos(t), y=sin(t) for t∈[0,π]", "domain": [0, "π"], "solution": "π"}
|
43 |
+
]
|
44 |
+
}
|
45 |
+
},
|
46 |
+
"Surface Area": {
|
47 |
+
"formula": "S = 2π * ∫[a,b] f(x) * sqrt(1 + (f'(x))^2) dx",
|
48 |
+
"functions": {
|
49 |
+
"easy": [
|
50 |
+
{"func": "x", "domain": [0, 3], "solution": "2π*4.5"},
|
51 |
+
{"func": "x^2", "domain": [0, 1], "solution": "approx. 2π*0.7169"},
|
52 |
+
{"func": "sqrt(x)", "domain": [0, 4], "solution": "approx. 2π*4.5177"},
|
53 |
+
{"func": "1", "domain": [0, 2], "solution": "2π*2"},
|
54 |
+
{"func": "x/2", "domain": [0, 4], "solution": "2π*4.1231"}
|
55 |
+
],
|
56 |
+
"hard": [
|
57 |
+
{"func": "x^3", "domain": [0, 1], "solution": "approx. 2π*0.6004"},
|
58 |
+
{"func": "e^x", "domain": [0, 1], "solution": "approx. 2π*1.1793"},
|
59 |
+
{"func": "sin(x)", "domain": [0, "π/2"], "solution": "approx. 2π*0.6366"},
|
60 |
+
{"func": "1/x", "domain": [1, 2], "solution": "approx. 2π*1.1478"},
|
61 |
+
{"func": "ln(x)", "domain": [1, 2], "solution": "approx. 2π*0.5593"}
|
62 |
+
]
|
63 |
+
}
|
64 |
+
},
|
65 |
+
"Differential Equations": {
|
66 |
+
"formula": "Various types",
|
67 |
+
"functions": {
|
68 |
+
"easy": [
|
69 |
+
{"func": "dy/dx = 2x", "domain": ["y(0)=1"], "solution": "y = x^2 + 1"},
|
70 |
+
{"func": "dy/dx = y", "domain": ["y(0)=1"], "solution": "y = e^x"},
|
71 |
+
{"func": "dy/dx = 3x^2", "domain": ["y(0)=2"], "solution": "y = x^3 + 2"},
|
72 |
+
{"func": "dy/dx = -y", "domain": ["y(0)=4"], "solution": "y = 4e^(-x)"},
|
73 |
+
{"func": "dy/dx = x+1", "domain": ["y(0)=-2"], "solution": "y = x^2/2 + x - 2"}
|
74 |
+
],
|
75 |
+
"hard": [
|
76 |
+
{"func": "y'' + 4y = 0", "domain": ["y(0)=1, y'(0)=0"], "solution": "y = cos(2x)"},
|
77 |
+
{"func": "y'' - y = x", "domain": ["y(0)=0, y'(0)=1"], "solution": "y = e^x/2 - e^(-x)/2 - x"},
|
78 |
+
{"func": "y' + y = e^x", "domain": ["y(0)=0"], "solution": "y = xe^x"},
|
79 |
+
{"func": "y'' + 2y' + y = 0", "domain": ["y(0)=1, y'(0)=-1"], "solution": "y = (1-x)e^(-x)"},
|
80 |
+
{"func": "y'' - 2y' + y = x^2", "domain": ["y(0)=1, y'(0)=1"], "solution": "y = (x^2)/2 + 2x + 1"}
|
81 |
+
]
|
82 |
+
}
|
83 |
+
},
|
84 |
+
"Area and Volume": {
|
85 |
+
"formula": "A = ∫[a,b] f(x) dx, V = π * ∫[a,b] [f(x)]^2 dx",
|
86 |
+
"functions": {
|
87 |
+
"easy": [
|
88 |
+
{"func": "f(x) = x^2, find area under the curve", "domain": [0, 3], "solution": "9"},
|
89 |
+
{"func": "f(x) = sin(x), find area under the curve", "domain": [0, "π"], "solution": "2"},
|
90 |
+
{"func": "f(x) = 4-x^2, find area under the curve", "domain": [-2, 2], "solution": "16/3"},
|
91 |
+
{"func": "f(x) = sqrt(x), find volume of revolution around x-axis", "domain": [0, 4], "solution": "16π/3"},
|
92 |
+
{"func": "f(x) = x, find volume of revolution around x-axis", "domain": [0, 2], "solution": "8π/3"}
|
93 |
+
],
|
94 |
+
"hard": [
|
95 |
+
{"func": "Area between f(x) = x^2 and g(x) = x^3", "domain": [0, 1], "solution": "1/12"},
|
96 |
+
{"func": "Volume of solid bounded by z = 4-x^2-y^2 and z = 0", "domain": ["x^2+y^2≤4"], "solution": "8π"},
|
97 |
+
{"func": "Volume of solid formed by rotating region bounded by y = x^2, y = 0, x = 2 around y-axis", "domain": [0, 2], "solution": "8π/5"},
|
98 |
+
{"func": "Area between f(x) = sin(x) and g(x) = cos(x)", "domain": [0, "π/4"], "solution": "sqrt(2)-1"},
|
99 |
+
{"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": "π(e^2-1)/2"}
|
100 |
+
]
|
101 |
+
}
|
102 |
+
},
|
103 |
+
"Parametric Curves and Equations": {
|
104 |
+
"formula": "x = x(t), y = y(t), Arc length = ∫[a,b] sqrt((dx/dt)^2 + (dy/dt)^2) dt",
|
105 |
+
"functions": {
|
106 |
+
"easy": [
|
107 |
+
{"func": "x = t, y = t^2, find dy/dx", "domain": ["t"], "solution": "dy/dx = 2t"},
|
108 |
+
{"func": "x = cos(t), y = sin(t), find the arc length", "domain": [0, "π/2"], "solution": "π/2"},
|
109 |
+
{"func": "x = t^2, y = t^3, find dy/dx", "domain": ["t"], "solution": "dy/dx = 3t/2"},
|
110 |
+
{"func": "x = 2t, y = t^2, find the area under the curve", "domain": [0, 2], "solution": "4/3"},
|
111 |
+
{"func": "x = t, y = sin(t), find dy/dx", "domain": ["t"], "solution": "dy/dx = cos(t)"}
|
112 |
+
],
|
113 |
+
"hard": [
|
114 |
+
{"func": "x = e^t*cos(t), y = e^t*sin(t), find dy/dx", "domain": ["t"], "solution": "dy/dx = tan(t) + 1"},
|
115 |
+
{"func": "x = t-sin(t), y = 1-cos(t), find the arc length", "domain": [0, "2π"], "solution": "8"},
|
116 |
+
{"func": "x = ln(sec(t)), y = tan(t), find dy/dx", "domain": ["t"], "solution": "dy/dx = sec^2(t)"},
|
117 |
+
{"func": "x = cos^3(t), y = sin^3(t), find the area enclosed", "domain": [0, "2π"], "solution": "3π/8"},
|
118 |
+
{"func": "x = cos(t)+t*sin(t), y = sin(t)-t*cos(t), find the arc length", "domain": [0, "2π"], "solution": "2π*sqrt(1+4π^2)"}
|
119 |
+
]
|
120 |
+
}
|
121 |
+
}
|
122 |
+
}
|
123 |
+
|
124 |
+
# Function to generate a single question
|
125 |
+
def generate_question(topic_name, difficulty):
|
126 |
+
topic_data = TOPICS[topic_name]
|
127 |
+
formula = topic_data["formula"]
|
128 |
+
|
129 |
+
# Select a random function from the available ones for this topic and difficulty
|
130 |
+
function_data = random.choice(topic_data["functions"][difficulty])
|
131 |
+
func = function_data["func"]
|
132 |
+
domain = function_data["domain"]
|
133 |
+
solution = function_data["solution"]
|
134 |
+
|
135 |
+
# Format domain for display
|
136 |
+
if isinstance(domain, list) and len(domain) == 2:
|
137 |
+
domain_str = f"[{domain[0]}, {domain[1]}]"
|
138 |
+
else:
|
139 |
+
domain_str = str(domain)
|
140 |
+
|
141 |
+
# Create question and solution based on difficulty
|
142 |
+
if difficulty == "easy":
|
143 |
+
question = f"Find the {topic_name.lower()} of {func} over the domain {domain_str}."
|
144 |
+
solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n"
|
145 |
+
solution_text += f"Step 2: Substitute f(x) = {func} and evaluate over {domain_str}\n\n"
|
146 |
+
solution_text += f"Step 3: Solve the resulting integral or calculation\n\n"
|
147 |
+
solution_text += f"Final Answer: {solution}"
|
148 |
+
else:
|
149 |
+
question = f"Compute the {topic_name.lower()} for {func} over {domain_str}."
|
150 |
+
solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n"
|
151 |
+
solution_text += f"Step 2: For {func}, substitute into the formula and evaluate over {domain_str}\n\n"
|
152 |
+
solution_text += f"Step 3: This requires advanced integration techniques or careful analysis\n\n"
|
153 |
+
solution_text += f"Step 4: After simplification and evaluation of the integral\n\n"
|
154 |
+
solution_text += f"Final Answer: {solution}"
|
155 |
+
|
156 |
+
return question, solution_text
|
157 |
+
|
158 |
+
# Function to generate multiple questions
|
159 |
+
def generate_multiple_questions(topic_name, difficulty, count):
|
160 |
+
questions = []
|
161 |
+
solutions = []
|
162 |
+
|
163 |
+
for _ in range(count):
|
164 |
+
question, solution = generate_question(topic_name, difficulty)
|
165 |
+
questions.append(question)
|
166 |
+
solutions.append(solution)
|
167 |
+
|
168 |
+
combined_questions = "\n\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)])
|
169 |
+
combined_solutions = "\n\n" + "-"*50 + "\n\n".join([f"Solution {i+1}:\n{s}" for i, s in enumerate(solutions)])
|
170 |
+
|
171 |
+
return combined_questions, combined_solutions
|
172 |
+
|
173 |
+
# Gradio app function
|
174 |
+
def generate_calculus_questions(topic, difficulty, count):
|
175 |
+
count = int(count) # Convert to int in case it's a string
|
176 |
+
questions, solutions = generate_multiple_questions(topic, difficulty, count)
|
177 |
+
return questions, solutions
|
178 |
+
|
179 |
+
# Create the Gradio interface
|
180 |
+
with gr.Blocks(title="Calculus Question Generator") as demo:
|
181 |
+
gr.Markdown("# Calculus Question Generator")
|
182 |
+
gr.Markdown("Select a topic, difficulty level, and the number of questions to generate.")
|
183 |
+
|
184 |
+
with gr.Row():
|
185 |
+
with gr.Column():
|
186 |
+
topic = gr.Dropdown(
|
187 |
+
choices=list(TOPICS.keys()),
|
188 |
+
label="Calculus Topic",
|
189 |
+
value="Average Value"
|
190 |
+
)
|
191 |
+
difficulty = gr.Radio(
|
192 |
+
choices=["easy", "hard"],
|
193 |
+
label="Difficulty Level",
|
194 |
+
value="easy"
|
195 |
+
)
|
196 |
+
count = gr.Slider(
|
197 |
+
minimum=1,
|
198 |
+
maximum=10,
|
199 |
+
value=3,
|
200 |
+
step=1,
|
201 |
+
label="Number of Questions"
|
202 |
+
)
|
203 |
+
generate_button = gr.Button("Generate Questions")
|
204 |
+
|
205 |
+
with gr.Column():
|
206 |
+
questions_output = gr.Textbox(label="Generated Questions", lines=10)
|
207 |
+
solutions_output = gr.Textbox(label="Solutions", lines=15)
|
208 |
+
|
209 |
+
generate_button.click(
|
210 |
+
generate_calculus_questions,
|
211 |
+
inputs=[topic, difficulty, count],
|
212 |
+
outputs=[questions_output, solutions_output]
|
213 |
+
)
|
214 |
+
|
215 |
+
gr.Markdown("### Created by KamogeloMosiai")
|
216 |
+
|
217 |
+
# Launch the app
|
218 |
+
if __name__ == "__main__":
|
219 |
+
demo.launch()
|