Spaces:
Runtime error
Runtime error
import gradio as gr | |
import json | |
import random | |
import math | |
# Define a list of topics with specific formulas and question templates | |
TOPICS = { | |
"Average Value": { | |
"formula": "f_avg = (1/(b-a)) * ∫[a,b] f(x) dx", | |
"functions": { | |
"easy": [ | |
{"func": "x^2", "domain": [0, 2], "solution": "4/3"}, | |
{"func": "sin(x)", "domain": [0, "π"], "solution": "2/π"}, | |
{"func": "e^x", "domain": [0, 1], "solution": "(e-1)"}, | |
{"func": "x", "domain": [1, 4], "solution": "5/2"}, | |
{"func": "x^3", "domain": [0, 1], "solution": "1/4"} | |
], | |
"hard": [ | |
{"func": "x*sin(x)", "domain": [0, "π"], "solution": "π/2"}, | |
{"func": "ln(x)", "domain": [1, "e"], "solution": "1-1/e"}, | |
{"func": "x^2*e^x", "domain": [0, 1], "solution": "2e-2"}, | |
{"func": "1/(1+x^2)", "domain": [0, 1], "solution": "π/4"}, | |
{"func": "sqrt(x)", "domain": [0, 4], "solution": "4/3"} | |
] | |
} | |
}, | |
"Arc Length": { | |
"formula": "L = ∫[a,b] sqrt(1 + (f'(x))^2) dx", | |
"functions": { | |
"easy": [ | |
{"func": "x^2", "domain": [0, 1], "solution": "approx. 1.4789"}, | |
{"func": "x^(3/2)", "domain": [0, 1], "solution": "approx. 1.1919"}, | |
{"func": "2x+1", "domain": [0, 2], "solution": "sqrt(5)*2"}, | |
{"func": "x^3", "domain": [0, 1], "solution": "approx. 1.0801"}, | |
{"func": "sin(x)", "domain": [0, "π/2"], "solution": "approx. 1.9118"} | |
], | |
"hard": [ | |
{"func": "ln(x)", "domain": [1, 3], "solution": "approx. 2.3861"}, | |
{"func": "e^x", "domain": [0, 1], "solution": "approx. 1.1752"}, | |
{"func": "cosh(x)", "domain": [0, 1], "solution": "sinh(1)"}, | |
{"func": "x^2 - ln(x)", "domain": [1, 2], "solution": "approx. 3.1623"}, | |
{"func": "parametric: x=cos(t), y=sin(t) for t∈[0,π]", "domain": [0, "π"], "solution": "π"} | |
] | |
} | |
}, | |
"Surface Area": { | |
"formula": "S = 2π * ∫[a,b] f(x) * sqrt(1 + (f'(x))^2) dx", | |
"functions": { | |
"easy": [ | |
{"func": "x", "domain": [0, 3], "solution": "2π*4.5"}, | |
{"func": "x^2", "domain": [0, 1], "solution": "approx. 2π*0.7169"}, | |
{"func": "sqrt(x)", "domain": [0, 4], "solution": "approx. 2π*4.5177"}, | |
{"func": "1", "domain": [0, 2], "solution": "2π*2"}, | |
{"func": "x/2", "domain": [0, 4], "solution": "2π*4.1231"} | |
], | |
"hard": [ | |
{"func": "x^3", "domain": [0, 1], "solution": "approx. 2π*0.6004"}, | |
{"func": "e^x", "domain": [0, 1], "solution": "approx. 2π*1.1793"}, | |
{"func": "sin(x)", "domain": [0, "π/2"], "solution": "approx. 2π*0.6366"}, | |
{"func": "1/x", "domain": [1, 2], "solution": "approx. 2π*1.1478"}, | |
{"func": "ln(x)", "domain": [1, 2], "solution": "approx. 2π*0.5593"} | |
] | |
} | |
}, | |
"Differential Equations": { | |
"formula": "Various types", | |
"functions": { | |
"easy": [ | |
{"func": "dy/dx = 2x", "domain": ["y(0)=1"], "solution": "y = x^2 + 1"}, | |
{"func": "dy/dx = y", "domain": ["y(0)=1"], "solution": "y = e^x"}, | |
{"func": "dy/dx = 3x^2", "domain": ["y(0)=2"], "solution": "y = x^3 + 2"}, | |
{"func": "dy/dx = -y", "domain": ["y(0)=4"], "solution": "y = 4e^(-x)"}, | |
{"func": "dy/dx = x+1", "domain": ["y(0)=-2"], "solution": "y = x^2/2 + x - 2"} | |
], | |
"hard": [ | |
{"func": "y'' + 4y = 0", "domain": ["y(0)=1, y'(0)=0"], "solution": "y = cos(2x)"}, | |
{"func": "y'' - y = x", "domain": ["y(0)=0, y'(0)=1"], "solution": "y = e^x/2 - e^(-x)/2 - x"}, | |
{"func": "y' + y = e^x", "domain": ["y(0)=0"], "solution": "y = xe^x"}, | |
{"func": "y'' + 2y' + y = 0", "domain": ["y(0)=1, y'(0)=-1"], "solution": "y = (1-x)e^(-x)"}, | |
{"func": "y'' - 2y' + y = x^2", "domain": ["y(0)=1, y'(0)=1"], "solution": "y = (x^2)/2 + 2x + 1"} | |
] | |
} | |
}, | |
"Area and Volume": { | |
"formula": "A = ∫[a,b] f(x) dx, V = π * ∫[a,b] [f(x)]^2 dx", | |
"functions": { | |
"easy": [ | |
{"func": "f(x) = x^2, find area under the curve", "domain": [0, 3], "solution": "9"}, | |
{"func": "f(x) = sin(x), find area under the curve", "domain": [0, "π"], "solution": "2"}, | |
{"func": "f(x) = 4-x^2, find area under the curve", "domain": [-2, 2], "solution": "16/3"}, | |
{"func": "f(x) = sqrt(x), find volume of revolution around x-axis", "domain": [0, 4], "solution": "16π/3"}, | |
{"func": "f(x) = x, find volume of revolution around x-axis", "domain": [0, 2], "solution": "8π/3"} | |
], | |
"hard": [ | |
{"func": "Area between f(x) = x^2 and g(x) = x^3", "domain": [0, 1], "solution": "1/12"}, | |
{"func": "Volume of solid bounded by z = 4-x^2-y^2 and z = 0", "domain": ["x^2+y^2≤4"], "solution": "8π"}, | |
{"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"}, | |
{"func": "Area between f(x) = sin(x) and g(x) = cos(x)", "domain": [0, "π/4"], "solution": "sqrt(2)-1"}, | |
{"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"} | |
] | |
} | |
}, | |
"Parametric Curves and Equations": { | |
"formula": "x = x(t), y = y(t), Arc length = ∫[a,b] sqrt((dx/dt)^2 + (dy/dt)^2) dt", | |
"functions": { | |
"easy": [ | |
{"func": "x = t, y = t^2, find dy/dx", "domain": ["t"], "solution": "dy/dx = 2t"}, | |
{"func": "x = cos(t), y = sin(t), find the arc length", "domain": [0, "π/2"], "solution": "π/2"}, | |
{"func": "x = t^2, y = t^3, find dy/dx", "domain": ["t"], "solution": "dy/dx = 3t/2"}, | |
{"func": "x = 2t, y = t^2, find the area under the curve", "domain": [0, 2], "solution": "4/3"}, | |
{"func": "x = t, y = sin(t), find dy/dx", "domain": ["t"], "solution": "dy/dx = cos(t)"} | |
], | |
"hard": [ | |
{"func": "x = e^t*cos(t), y = e^t*sin(t), find dy/dx", "domain": ["t"], "solution": "dy/dx = tan(t) + 1"}, | |
{"func": "x = t-sin(t), y = 1-cos(t), find the arc length", "domain": [0, "2π"], "solution": "8"}, | |
{"func": "x = ln(sec(t)), y = tan(t), find dy/dx", "domain": ["t"], "solution": "dy/dx = sec^2(t)"}, | |
{"func": "x = cos^3(t), y = sin^3(t), find the area enclosed", "domain": [0, "2π"], "solution": "3π/8"}, | |
{"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)"} | |
] | |
} | |
} | |
} | |
# Function to generate a single question | |
def generate_question(topic_name, difficulty): | |
topic_data = TOPICS[topic_name] | |
formula = topic_data["formula"] | |
# Select a random function from the available ones for this topic and difficulty | |
function_data = random.choice(topic_data["functions"][difficulty]) | |
func = function_data["func"] | |
domain = function_data["domain"] | |
solution = function_data["solution"] | |
# Format domain for display | |
if isinstance(domain, list) and len(domain) == 2: | |
domain_str = f"[{domain[0]}, {domain[1]}]" | |
else: | |
domain_str = str(domain) | |
# Create question and solution based on difficulty | |
if difficulty == "easy": | |
question = f"Find the {topic_name.lower()} of {func} over the domain {domain_str}." | |
solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n" | |
solution_text += f"Step 2: Substitute f(x) = {func} and evaluate over {domain_str}\n\n" | |
solution_text += f"Step 3: Solve the resulting integral or calculation\n\n" | |
solution_text += f"Final Answer: {solution}" | |
else: | |
question = f"Compute the {topic_name.lower()} for {func} over {domain_str}." | |
solution_text = f"Step 1: Apply the formula for {topic_name.lower()}: {formula}\n\n" | |
solution_text += f"Step 2: For {func}, substitute into the formula and evaluate over {domain_str}\n\n" | |
solution_text += f"Step 3: This requires advanced integration techniques or careful analysis\n\n" | |
solution_text += f"Step 4: After simplification and evaluation of the integral\n\n" | |
solution_text += f"Final Answer: {solution}" | |
return question, solution_text | |
# Function to generate multiple questions | |
def generate_multiple_questions(topic_name, difficulty, count): | |
questions = [] | |
solutions = [] | |
for _ in range(count): | |
question, solution = generate_question(topic_name, difficulty) | |
questions.append(question) | |
solutions.append(solution) | |
combined_questions = "\n\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)]) | |
combined_solutions = "\n\n" + "-"*50 + "\n\n".join([f"Solution {i+1}:\n{s}" for i, s in enumerate(solutions)]) | |
return combined_questions, combined_solutions | |
# Gradio app function | |
def generate_calculus_questions(topic, difficulty, count): | |
count = int(count) # Convert to int in case it's a string | |
questions, solutions = generate_multiple_questions(topic, difficulty, count) | |
return questions, solutions | |
# Create the Gradio interface | |
with gr.Blocks(title="Calculus Question Generator") as demo: | |
gr.Markdown("# Calculus Question Generator") | |
gr.Markdown("Select a topic, difficulty level, and the number of questions to generate.") | |
with gr.Row(): | |
with gr.Column(): | |
topic = gr.Dropdown( | |
choices=list(TOPICS.keys()), | |
label="Calculus Topic", | |
value="Average Value" | |
) | |
difficulty = gr.Radio( | |
choices=["easy", "hard"], | |
label="Difficulty Level", | |
value="easy" | |
) | |
count = gr.Slider( | |
minimum=1, | |
maximum=10, | |
value=3, | |
step=1, | |
label="Number of Questions" | |
) | |
generate_button = gr.Button("Generate Questions") | |
with gr.Column(): | |
questions_output = gr.Textbox(label="Generated Questions", lines=10) | |
solutions_output = gr.Textbox(label="Solutions", lines=15) | |
generate_button.click( | |
generate_calculus_questions, | |
inputs=[topic, difficulty, count], | |
outputs=[questions_output, solutions_output] | |
) | |
gr.Markdown("### Created by KamogeloMosiai") | |
# Launch the app | |
if __name__ == "__main__": | |
demo.launch() |