Delete v1.txt
Browse files
v1.txt
DELETED
@@ -1,329 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import re
|
4 |
-
import time
|
5 |
-
import base64
|
6 |
-
from openai import OpenAI
|
7 |
-
from together import Together
|
8 |
-
from PIL import Image
|
9 |
-
import io
|
10 |
-
|
11 |
-
# Function to generate math solution using the Phi-4-reasoning-plus model via OpenRouter
|
12 |
-
def generate_math_solution_openrouter(api_key, problem_text, history=None):
|
13 |
-
if not api_key.strip():
|
14 |
-
return "Please enter your OpenRouter API key.", history
|
15 |
-
|
16 |
-
if not problem_text.strip():
|
17 |
-
return "Please enter a math problem.", history
|
18 |
-
|
19 |
-
try:
|
20 |
-
client = OpenAI(
|
21 |
-
base_url="https://openrouter.ai/api/v1",
|
22 |
-
api_key=api_key,
|
23 |
-
)
|
24 |
-
|
25 |
-
messages = [
|
26 |
-
{"role": "system", "content":
|
27 |
-
"""You are an expert math tutor who explains concepts clearly and thoroughly.
|
28 |
-
Analyze the given math problem and provide a detailed step-by-step solution.
|
29 |
-
For each step:
|
30 |
-
1. Show the mathematical operation
|
31 |
-
2. Explain why this step is necessary
|
32 |
-
3. Connect it to relevant mathematical concepts
|
33 |
-
|
34 |
-
Format your response with clear section headers using markdown.
|
35 |
-
Begin with an "Initial Analysis" section, follow with numbered steps,
|
36 |
-
and conclude with a "Final Answer" section."""},
|
37 |
-
]
|
38 |
-
|
39 |
-
# Add conversation history if it exists
|
40 |
-
if history:
|
41 |
-
for exchange in history:
|
42 |
-
messages.append({"role": "user", "content": exchange[0]})
|
43 |
-
if exchange[1]: # Check if there's a response
|
44 |
-
messages.append({"role": "assistant", "content": exchange[1]})
|
45 |
-
|
46 |
-
# Add the current problem
|
47 |
-
messages.append({"role": "user", "content": f"Solve this math problem step-by-step: {problem_text}"})
|
48 |
-
|
49 |
-
# Create the completion
|
50 |
-
completion = client.chat.completions.create(
|
51 |
-
model="microsoft/phi-4-reasoning-plus:free",
|
52 |
-
messages=messages,
|
53 |
-
extra_headers={
|
54 |
-
"HTTP-Referer": "https://advancedmathtutor.edu",
|
55 |
-
"X-Title": "Advanced Math Tutor",
|
56 |
-
}
|
57 |
-
)
|
58 |
-
|
59 |
-
solution = completion.choices[0].message.content
|
60 |
-
|
61 |
-
# Update history
|
62 |
-
if history is None:
|
63 |
-
history = []
|
64 |
-
history.append((problem_text, solution))
|
65 |
-
|
66 |
-
return solution, history
|
67 |
-
|
68 |
-
except Exception as e:
|
69 |
-
error_message = f"Error: {str(e)}"
|
70 |
-
return error_message, history
|
71 |
-
|
72 |
-
# Function to convert image to base64
|
73 |
-
def image_to_base64(image_path):
|
74 |
-
if image_path is None:
|
75 |
-
return None
|
76 |
-
|
77 |
-
try:
|
78 |
-
with open(image_path, "rb") as img_file:
|
79 |
-
return base64.b64encode(img_file.read()).decode("utf-8")
|
80 |
-
except Exception as e:
|
81 |
-
print(f"Error converting image to base64: {str(e)}")
|
82 |
-
return None
|
83 |
-
|
84 |
-
# Function to generate math solution using Together AI with support for images
|
85 |
-
def generate_math_solution_together(api_key, problem_text, image_path=None, history=None):
|
86 |
-
if not api_key.strip():
|
87 |
-
return "Please enter your Together AI API key.", history
|
88 |
-
|
89 |
-
if not problem_text.strip() and image_path is None:
|
90 |
-
return "Please enter a math problem or upload an image of a math problem.", history
|
91 |
-
|
92 |
-
try:
|
93 |
-
client = Together(api_key=api_key)
|
94 |
-
|
95 |
-
# Create the base message structure
|
96 |
-
messages = [
|
97 |
-
{
|
98 |
-
"role": "system",
|
99 |
-
"content": """You are an expert math tutor who explains concepts clearly and thoroughly.
|
100 |
-
Analyze the given math problem and provide a detailed step-by-step solution.
|
101 |
-
For each step:
|
102 |
-
1. Show the mathematical operation
|
103 |
-
2. Explain why this step is necessary
|
104 |
-
3. Connect it to relevant mathematical concepts
|
105 |
-
|
106 |
-
Format your response with clear section headers using markdown.
|
107 |
-
Begin with an "Initial Analysis" section, follow with numbered steps,
|
108 |
-
and conclude with a "Final Answer" section."""
|
109 |
-
}
|
110 |
-
]
|
111 |
-
|
112 |
-
# Add conversation history if it exists
|
113 |
-
if history:
|
114 |
-
for exchange in history:
|
115 |
-
messages.append({"role": "user", "content": exchange[0]})
|
116 |
-
if exchange[1]: # Check if there's a response
|
117 |
-
messages.append({"role": "assistant", "content": exchange[1]})
|
118 |
-
|
119 |
-
# Prepare the user message content
|
120 |
-
user_message_content = []
|
121 |
-
|
122 |
-
# Add text content if provided
|
123 |
-
if problem_text.strip():
|
124 |
-
user_message_content.append({
|
125 |
-
"type": "text",
|
126 |
-
"text": f"Solve this math problem: {problem_text}"
|
127 |
-
})
|
128 |
-
else:
|
129 |
-
user_message_content.append({
|
130 |
-
"type": "text",
|
131 |
-
"text": "Solve this math problem from the image:"
|
132 |
-
})
|
133 |
-
|
134 |
-
# Add image if provided
|
135 |
-
if image_path:
|
136 |
-
# Convert image to base64
|
137 |
-
base64_image = image_to_base64(image_path)
|
138 |
-
if base64_image:
|
139 |
-
user_message_content.append({
|
140 |
-
"type": "image_url",
|
141 |
-
"image_url": {
|
142 |
-
"url": f"data:image/jpeg;base64,{base64_image}"
|
143 |
-
}
|
144 |
-
})
|
145 |
-
|
146 |
-
# Add the user message with content
|
147 |
-
messages.append({
|
148 |
-
"role": "user",
|
149 |
-
"content": user_message_content
|
150 |
-
})
|
151 |
-
|
152 |
-
# Create the completion
|
153 |
-
response = client.chat.completions.create(
|
154 |
-
model="meta-llama/Llama-Vision-Free",
|
155 |
-
messages=messages,
|
156 |
-
stream=False
|
157 |
-
)
|
158 |
-
|
159 |
-
solution = response.choices[0].message.content
|
160 |
-
|
161 |
-
# Update history - for simplicity, just store the text problem
|
162 |
-
if history is None:
|
163 |
-
history = []
|
164 |
-
history.append((problem_text if problem_text.strip() else "Image problem", solution))
|
165 |
-
|
166 |
-
return solution, history
|
167 |
-
|
168 |
-
except Exception as e:
|
169 |
-
error_message = f"Error: {str(e)}"
|
170 |
-
return error_message, history
|
171 |
-
|
172 |
-
# Define the Gradio interface
|
173 |
-
def create_demo():
|
174 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
175 |
-
gr.Markdown("# 📚 Advanced Math Tutor")
|
176 |
-
gr.Markdown("""
|
177 |
-
This application provides step-by-step solutions to math problems using advanced AI models.
|
178 |
-
Choose between OpenRouter's Phi-4-reasoning-plus for text-based problems or Together AI's
|
179 |
-
Llama-Vision for problems with images.
|
180 |
-
""")
|
181 |
-
|
182 |
-
# Main tabs
|
183 |
-
with gr.Tabs():
|
184 |
-
# Text-based problem solver (OpenRouter)
|
185 |
-
with gr.TabItem("Text Problem Solver (OpenRouter)"):
|
186 |
-
with gr.Row():
|
187 |
-
with gr.Column(scale=1):
|
188 |
-
openrouter_api_key = gr.Textbox(
|
189 |
-
label="OpenRouter API Key",
|
190 |
-
placeholder="Enter your OpenRouter API key (starts with sk-or-)",
|
191 |
-
type="password"
|
192 |
-
)
|
193 |
-
text_problem_input = gr.Textbox(
|
194 |
-
label="Math Problem",
|
195 |
-
placeholder="Enter your math problem here...",
|
196 |
-
lines=5
|
197 |
-
)
|
198 |
-
example_problems = gr.Examples(
|
199 |
-
examples=[
|
200 |
-
["Solve the quadratic equation: 3x² + 5x - 2 = 0"],
|
201 |
-
["Find the derivative of f(x) = x³ln(x)"],
|
202 |
-
["Calculate the area of a circle with radius 5 cm"],
|
203 |
-
["Find all values of x that satisfy the equation: log₂(x-1) + log₂(x+3) = 5"]
|
204 |
-
],
|
205 |
-
inputs=[text_problem_input],
|
206 |
-
label="Example Problems"
|
207 |
-
)
|
208 |
-
with gr.Row():
|
209 |
-
openrouter_submit_btn = gr.Button("Solve Problem", variant="primary")
|
210 |
-
openrouter_clear_btn = gr.Button("Clear")
|
211 |
-
|
212 |
-
with gr.Column(scale=2):
|
213 |
-
openrouter_solution_output = gr.Markdown(label="Solution")
|
214 |
-
|
215 |
-
# Store conversation history (invisible to user)
|
216 |
-
openrouter_conversation_history = gr.State(value=None)
|
217 |
-
|
218 |
-
# Button actions
|
219 |
-
openrouter_submit_btn.click(
|
220 |
-
fn=generate_math_solution_openrouter,
|
221 |
-
inputs=[openrouter_api_key, text_problem_input, openrouter_conversation_history],
|
222 |
-
outputs=[openrouter_solution_output, openrouter_conversation_history]
|
223 |
-
)
|
224 |
-
|
225 |
-
openrouter_clear_btn.click(
|
226 |
-
fn=lambda: ("", None),
|
227 |
-
inputs=[],
|
228 |
-
outputs=[openrouter_solution_output, openrouter_conversation_history]
|
229 |
-
)
|
230 |
-
|
231 |
-
# Image-based problem solver (Together AI)
|
232 |
-
with gr.TabItem("Image Problem Solver (Together AI)"):
|
233 |
-
with gr.Row():
|
234 |
-
with gr.Column(scale=1):
|
235 |
-
together_api_key = gr.Textbox(
|
236 |
-
label="Together AI API Key",
|
237 |
-
placeholder="Enter your Together AI API key",
|
238 |
-
type="password"
|
239 |
-
)
|
240 |
-
together_problem_input = gr.Textbox(
|
241 |
-
label="Problem Description (Optional)",
|
242 |
-
placeholder="Enter additional context for the image problem...",
|
243 |
-
lines=3
|
244 |
-
)
|
245 |
-
together_image_input = gr.Image(
|
246 |
-
label="Upload Math Problem Image",
|
247 |
-
type="filepath"
|
248 |
-
)
|
249 |
-
with gr.Row():
|
250 |
-
together_submit_btn = gr.Button("Solve Problem", variant="primary")
|
251 |
-
together_clear_btn = gr.Button("Clear")
|
252 |
-
|
253 |
-
with gr.Column(scale=2):
|
254 |
-
together_solution_output = gr.Markdown(label="Solution")
|
255 |
-
|
256 |
-
# Store conversation history (invisible to user)
|
257 |
-
together_conversation_history = gr.State(value=None)
|
258 |
-
|
259 |
-
# Button actions
|
260 |
-
together_submit_btn.click(
|
261 |
-
fn=generate_math_solution_together,
|
262 |
-
inputs=[together_api_key, together_problem_input, together_image_input, together_conversation_history],
|
263 |
-
outputs=[together_solution_output, together_conversation_history]
|
264 |
-
)
|
265 |
-
|
266 |
-
together_clear_btn.click(
|
267 |
-
fn=lambda: ("", None),
|
268 |
-
inputs=[],
|
269 |
-
outputs=[together_solution_output, together_conversation_history]
|
270 |
-
)
|
271 |
-
|
272 |
-
# Help tab
|
273 |
-
with gr.TabItem("Help"):
|
274 |
-
gr.Markdown("""
|
275 |
-
## How to Use the Advanced Math Tutor
|
276 |
-
|
277 |
-
### Getting Started
|
278 |
-
|
279 |
-
#### For Text-Based Problems (OpenRouter)
|
280 |
-
1. You'll need an API key from OpenRouter
|
281 |
-
2. Sign up at [OpenRouter](https://openrouter.ai/) to get your API key
|
282 |
-
3. Enter your API key in the designated field in the "Text Problem Solver" tab
|
283 |
-
|
284 |
-
#### For Image-Based Problems (Together AI)
|
285 |
-
1. You'll need an API key from Together AI
|
286 |
-
2. Sign up at [Together AI](https://www.together.ai/) to get your API key
|
287 |
-
3. Enter your API key in the designated field in the "Image Problem Solver" tab
|
288 |
-
4. Upload an image of your math problem
|
289 |
-
5. Optionally add text to provide additional context
|
290 |
-
|
291 |
-
### Solving Math Problems
|
292 |
-
- For text problems: Type or paste your math problem in the input field
|
293 |
-
- For image problems: Upload a clear image of the math problem
|
294 |
-
- Click "Solve Problem" to get a detailed step-by-step solution
|
295 |
-
- The solution will include explanations for each step
|
296 |
-
|
297 |
-
### Tips for Best Results
|
298 |
-
- Be specific in your problem description
|
299 |
-
- Include all necessary information
|
300 |
-
- For complex equations, use clear notation
|
301 |
-
- For algebraic expressions, use ^ for exponents (e.g., x^2 for x²)
|
302 |
-
- Use parentheses to group terms clearly
|
303 |
-
- For images, ensure the math problem is clearly visible and well-lit
|
304 |
-
|
305 |
-
### Types of Problems You Can Solve
|
306 |
-
- Algebra (equations, inequalities, systems of equations)
|
307 |
-
- Calculus (derivatives, integrals, limits)
|
308 |
-
- Trigonometry
|
309 |
-
- Geometry
|
310 |
-
- Statistics and Probability
|
311 |
-
- Number Theory
|
312 |
-
- And many more!
|
313 |
-
""")
|
314 |
-
|
315 |
-
# Footer
|
316 |
-
gr.Markdown("""
|
317 |
-
---
|
318 |
-
### About
|
319 |
-
This application uses Microsoft's Phi-4-reasoning-plus model via OpenRouter for text-based problems
|
320 |
-
and Llama-Vision-Free via Together AI for image-based problems.
|
321 |
-
Your API keys are required but not stored permanently.
|
322 |
-
""")
|
323 |
-
|
324 |
-
return demo
|
325 |
-
|
326 |
-
# Launch the app
|
327 |
-
if __name__ == "__main__":
|
328 |
-
demo = create_demo()
|
329 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|