Update app.py
Browse files
app.py
CHANGED
@@ -65,44 +65,6 @@ def generate_math_solution(api_key, problem_text, history=None):
|
|
65 |
error_message = f"Error: {str(e)}"
|
66 |
return error_message, history
|
67 |
|
68 |
-
# Function to generate practice problems
|
69 |
-
def generate_practice_problem(api_key, topic, difficulty, history=None):
|
70 |
-
if not api_key.strip():
|
71 |
-
return "Please enter your OpenRouter API key.", history
|
72 |
-
|
73 |
-
if not topic.strip():
|
74 |
-
return "Please specify a math topic.", history
|
75 |
-
|
76 |
-
try:
|
77 |
-
client = OpenAI(
|
78 |
-
base_url="https://openrouter.ai/api/v1",
|
79 |
-
api_key=api_key,
|
80 |
-
)
|
81 |
-
|
82 |
-
prompt = f"""Generate a {difficulty} level math problem related to {topic}.
|
83 |
-
The problem should be challenging but solvable, and relevant to a student studying {topic}.
|
84 |
-
Only provide the problem statement, not the solution."""
|
85 |
-
|
86 |
-
completion = client.chat.completions.create(
|
87 |
-
model="microsoft/phi-4-reasoning-plus:free",
|
88 |
-
messages=[
|
89 |
-
{"role": "system", "content": "You are an expert math teacher who creates engaging practice problems."},
|
90 |
-
{"role": "user", "content": prompt}
|
91 |
-
],
|
92 |
-
extra_headers={
|
93 |
-
"HTTP-Referer": "https://advancedmathtutor.edu",
|
94 |
-
"X-Title": "Advanced Math Tutor",
|
95 |
-
}
|
96 |
-
)
|
97 |
-
|
98 |
-
practice_problem = completion.choices[0].message.content
|
99 |
-
|
100 |
-
return practice_problem, history
|
101 |
-
|
102 |
-
except Exception as e:
|
103 |
-
error_message = f"Error: {str(e)}"
|
104 |
-
return error_message, history
|
105 |
-
|
106 |
# Function to verify API key format
|
107 |
def validate_api_key(api_key):
|
108 |
# This is a simple check - OpenRouter keys typically start with "sk-or-"
|
@@ -140,6 +102,16 @@ def create_demo():
|
|
140 |
placeholder="Enter your math problem here...",
|
141 |
lines=5
|
142 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
with gr.Row():
|
144 |
submit_btn = gr.Button("Solve Problem", variant="primary")
|
145 |
clear_btn = gr.Button("Clear")
|
@@ -163,49 +135,6 @@ def create_demo():
|
|
163 |
outputs=[solution_output, conversation_history]
|
164 |
)
|
165 |
|
166 |
-
with gr.TabItem("Practice Problems"):
|
167 |
-
with gr.Row():
|
168 |
-
with gr.Column(scale=1):
|
169 |
-
practice_api_key = gr.Textbox(
|
170 |
-
label="OpenRouter API Key",
|
171 |
-
placeholder="Enter your OpenRouter API key (starts with sk-or-)",
|
172 |
-
type="password"
|
173 |
-
)
|
174 |
-
topic_input = gr.Textbox(
|
175 |
-
label="Math Topic",
|
176 |
-
placeholder="e.g., Algebra, Calculus, Geometry...",
|
177 |
-
)
|
178 |
-
difficulty_input = gr.Dropdown(
|
179 |
-
label="Difficulty Level",
|
180 |
-
choices=["Easy", "Medium", "Hard", "Challenge"],
|
181 |
-
value="Medium"
|
182 |
-
)
|
183 |
-
generate_btn = gr.Button("Generate Practice Problem", variant="primary")
|
184 |
-
|
185 |
-
with gr.Column(scale=2):
|
186 |
-
practice_problem_output = gr.Markdown(label="Practice Problem")
|
187 |
-
solve_practice_btn = gr.Button("Solve This Practice Problem")
|
188 |
-
|
189 |
-
# Store practice problem history
|
190 |
-
practice_history = gr.State(value=None)
|
191 |
-
|
192 |
-
# Button actions for practice problems
|
193 |
-
generate_btn.click(
|
194 |
-
fn=generate_practice_problem,
|
195 |
-
inputs=[practice_api_key, topic_input, difficulty_input, practice_history],
|
196 |
-
outputs=[practice_problem_output, practice_history]
|
197 |
-
)
|
198 |
-
|
199 |
-
# Button to transfer practice problem to solver
|
200 |
-
solve_practice_btn.click(
|
201 |
-
fn=lambda problem: problem,
|
202 |
-
inputs=[practice_problem_output],
|
203 |
-
outputs=[problem_input]
|
204 |
-
)
|
205 |
-
|
206 |
-
# Add instructions for manual tab switching
|
207 |
-
gr.Markdown("*After clicking 'Solve This Practice Problem', please switch to the Problem Solver tab manually*")
|
208 |
-
|
209 |
with gr.TabItem("Help"):
|
210 |
gr.Markdown("""
|
211 |
## How to Use the Advanced Math Tutor
|
@@ -219,24 +148,23 @@ def create_demo():
|
|
219 |
- Type or paste your math problem in the input field
|
220 |
- Click "Solve Problem" to get a detailed step-by-step solution
|
221 |
- The solution will include explanations for each step
|
222 |
-
|
223 |
-
### Practice Problems
|
224 |
-
- Go to the Practice Problems tab
|
225 |
-
- Enter a math topic (e.g., "Integration", "Linear Equations")
|
226 |
-
- Select a difficulty level
|
227 |
-
- Click "Generate Practice Problem" to get a new problem
|
228 |
-
- Use "Solve This Practice Problem" to transfer it to the solver
|
229 |
|
230 |
### Tips for Best Results
|
231 |
- Be specific in your problem description
|
232 |
- Include all necessary information
|
233 |
- For complex equations, use clear notation
|
|
|
|
|
234 |
|
235 |
-
###
|
236 |
-
|
237 |
-
-
|
238 |
-
-
|
239 |
-
-
|
|
|
|
|
|
|
240 |
""")
|
241 |
|
242 |
# Footer
|
|
|
65 |
error_message = f"Error: {str(e)}"
|
66 |
return error_message, history
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Function to verify API key format
|
69 |
def validate_api_key(api_key):
|
70 |
# This is a simple check - OpenRouter keys typically start with "sk-or-"
|
|
|
102 |
placeholder="Enter your math problem here...",
|
103 |
lines=5
|
104 |
)
|
105 |
+
example_problems = gr.Examples(
|
106 |
+
examples=[
|
107 |
+
["Solve the quadratic equation: 3x² + 5x - 2 = 0"],
|
108 |
+
["Find the derivative of f(x) = x³ln(x)"],
|
109 |
+
["Calculate the area of a circle with radius 5 cm"],
|
110 |
+
["Find all values of x that satisfy the equation: log₂(x-1) + log₂(x+3) = 5"]
|
111 |
+
],
|
112 |
+
inputs=[problem_input],
|
113 |
+
label="Example Problems"
|
114 |
+
)
|
115 |
with gr.Row():
|
116 |
submit_btn = gr.Button("Solve Problem", variant="primary")
|
117 |
clear_btn = gr.Button("Clear")
|
|
|
135 |
outputs=[solution_output, conversation_history]
|
136 |
)
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
with gr.TabItem("Help"):
|
139 |
gr.Markdown("""
|
140 |
## How to Use the Advanced Math Tutor
|
|
|
148 |
- Type or paste your math problem in the input field
|
149 |
- Click "Solve Problem" to get a detailed step-by-step solution
|
150 |
- The solution will include explanations for each step
|
151 |
+
- You can also try one of the provided example problems
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
### Tips for Best Results
|
154 |
- Be specific in your problem description
|
155 |
- Include all necessary information
|
156 |
- For complex equations, use clear notation
|
157 |
+
- For algebraic expressions, use ^ for exponents (e.g., x^2 for x²)
|
158 |
+
- Use parentheses to group terms clearly
|
159 |
|
160 |
+
### Types of Problems You Can Solve
|
161 |
+
- Algebra (equations, inequalities, systems of equations)
|
162 |
+
- Calculus (derivatives, integrals, limits)
|
163 |
+
- Trigonometry
|
164 |
+
- Geometry
|
165 |
+
- Statistics and Probability
|
166 |
+
- Number Theory
|
167 |
+
- And many more!
|
168 |
""")
|
169 |
|
170 |
# Footer
|