File size: 11,068 Bytes
6fc87f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
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()