Update v1.txt
Browse files
v1.txt
CHANGED
@@ -1,11 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import re
|
4 |
-
from openai import OpenAI
|
5 |
import time
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Function to generate math solution using the Phi-4-reasoning-plus model
|
8 |
-
def
|
9 |
if not api_key.strip():
|
10 |
return "Please enter your OpenRouter API key.", history
|
11 |
|
@@ -65,39 +69,128 @@ 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
|
69 |
-
def
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
# Function to
|
76 |
-
def
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Define the Gradio interface
|
82 |
def create_demo():
|
83 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
84 |
gr.Markdown("# 📚 Advanced Math Tutor")
|
85 |
gr.Markdown("""
|
86 |
-
This application
|
87 |
-
|
|
|
88 |
""")
|
89 |
|
90 |
# Main tabs
|
91 |
with gr.Tabs():
|
92 |
-
|
|
|
93 |
with gr.Row():
|
94 |
with gr.Column(scale=1):
|
95 |
-
|
96 |
label="OpenRouter API Key",
|
97 |
placeholder="Enter your OpenRouter API key (starts with sk-or-)",
|
98 |
type="password"
|
99 |
)
|
100 |
-
|
101 |
label="Math Problem",
|
102 |
placeholder="Enter your math problem here...",
|
103 |
lines=5
|
@@ -109,46 +202,97 @@ def create_demo():
|
|
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=[
|
113 |
label="Example Problems"
|
114 |
)
|
115 |
with gr.Row():
|
116 |
-
|
117 |
-
|
118 |
|
119 |
with gr.Column(scale=2):
|
120 |
-
|
121 |
|
122 |
# Store conversation history (invisible to user)
|
123 |
-
|
124 |
|
125 |
# Button actions
|
126 |
-
|
127 |
-
fn=
|
128 |
-
inputs=[
|
129 |
-
outputs=[
|
130 |
)
|
131 |
|
132 |
-
|
133 |
fn=lambda: ("", None),
|
134 |
inputs=[],
|
135 |
-
outputs=[
|
136 |
)
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
with gr.TabItem("Help"):
|
139 |
gr.Markdown("""
|
140 |
## How to Use the Advanced Math Tutor
|
141 |
|
142 |
### Getting Started
|
143 |
-
|
|
|
|
|
144 |
2. Sign up at [OpenRouter](https://openrouter.ai/) to get your API key
|
145 |
-
3. Enter your API key in the designated field
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
### Solving Math Problems
|
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
|
@@ -156,6 +300,7 @@ def create_demo():
|
|
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)
|
@@ -171,8 +316,9 @@ def create_demo():
|
|
171 |
gr.Markdown("""
|
172 |
---
|
173 |
### About
|
174 |
-
This application uses Microsoft's Phi-4-reasoning-plus model via OpenRouter
|
175 |
-
|
|
|
176 |
""")
|
177 |
|
178 |
return demo
|
|
|
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 |
|
|
|
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
|
|
|
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
|
|
|
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)
|
|
|
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
|