Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 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,13 +66,89 @@ 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 verify API key format
|
| 69 |
-
def
|
| 70 |
# This is a simple check - OpenRouter keys typically start with "sk-or-"
|
| 71 |
if api_key.startswith("sk-or-") and len(api_key) > 20:
|
| 72 |
return True
|
| 73 |
return False
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Function to process LaTeX in the solution
|
| 76 |
def process_solution(solution):
|
| 77 |
# Replace $...$ with $$...$$ for better rendering in Gradio markdown
|
|
@@ -83,21 +160,23 @@ 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 +188,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 +286,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 +302,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 |
+
from openai import OpenAI
|
| 6 |
+
from together import Together
|
| 7 |
|
| 8 |
+
# Function to generate math solution using the Phi-4-reasoning-plus model via OpenRouter
|
| 9 |
+
def generate_math_solution_openrouter(api_key, problem_text, history=None):
|
| 10 |
if not api_key.strip():
|
| 11 |
return "Please enter your OpenRouter API key.", history
|
| 12 |
|
|
|
|
| 66 |
error_message = f"Error: {str(e)}"
|
| 67 |
return error_message, history
|
| 68 |
|
| 69 |
+
# Function to generate math solution using Together AI with support for images
|
| 70 |
+
def generate_math_solution_together(api_key, problem_text, image=None, history=None):
|
| 71 |
+
if not api_key.strip():
|
| 72 |
+
return "Please enter your Together AI API key.", history
|
| 73 |
+
|
| 74 |
+
if not problem_text.strip() and image is None:
|
| 75 |
+
return "Please enter a math problem or upload an image of a math problem.", history
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
client = Together(api_key=api_key)
|
| 79 |
+
|
| 80 |
+
system_message = {
|
| 81 |
+
"role": "system",
|
| 82 |
+
"content": """You are an expert math tutor who explains concepts clearly and thoroughly.
|
| 83 |
+
Analyze the given math problem and provide a detailed step-by-step solution.
|
| 84 |
+
For each step:
|
| 85 |
+
1. Show the mathematical operation
|
| 86 |
+
2. Explain why this step is necessary
|
| 87 |
+
3. Connect it to relevant mathematical concepts
|
| 88 |
+
|
| 89 |
+
Format your response with clear section headers using markdown.
|
| 90 |
+
Begin with an "Initial Analysis" section, follow with numbered steps,
|
| 91 |
+
and conclude with a "Final Answer" section."""
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
# Start with text content
|
| 95 |
+
user_content = [{"type": "text", "text": f"Solve this math problem: {problem_text}"}]
|
| 96 |
+
|
| 97 |
+
# Add image if provided
|
| 98 |
+
if image is not None:
|
| 99 |
+
user_content.append({
|
| 100 |
+
"type": "image_url",
|
| 101 |
+
"image_url": {"url": image}
|
| 102 |
+
})
|
| 103 |
+
|
| 104 |
+
messages = [system_message]
|
| 105 |
+
|
| 106 |
+
# Add conversation history if it exists
|
| 107 |
+
if history:
|
| 108 |
+
for exchange in history:
|
| 109 |
+
# For simplicity, we're assuming text-only history here
|
| 110 |
+
messages.append({"role": "user", "content": exchange[0]})
|
| 111 |
+
if exchange[1]: # Check if there's a response
|
| 112 |
+
messages.append({"role": "assistant", "content": exchange[1]})
|
| 113 |
+
|
| 114 |
+
# Add the current problem
|
| 115 |
+
messages.append({"role": "user", "content": user_content})
|
| 116 |
+
|
| 117 |
+
# Create the completion
|
| 118 |
+
response = client.chat.completions.create(
|
| 119 |
+
model="meta-llama/Llama-Vision-Free",
|
| 120 |
+
messages=messages,
|
| 121 |
+
stream=False
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
solution = response.choices[0].message.content
|
| 125 |
+
|
| 126 |
+
# Update history - for simplicity, just store the text problem
|
| 127 |
+
if history is None:
|
| 128 |
+
history = []
|
| 129 |
+
history.append((problem_text, solution))
|
| 130 |
+
|
| 131 |
+
return solution, history
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
error_message = f"Error: {str(e)}"
|
| 135 |
+
return error_message, history
|
| 136 |
+
|
| 137 |
# Function to verify API key format
|
| 138 |
+
def validate_openrouter_key(api_key):
|
| 139 |
# This is a simple check - OpenRouter keys typically start with "sk-or-"
|
| 140 |
if api_key.startswith("sk-or-") and len(api_key) > 20:
|
| 141 |
return True
|
| 142 |
return False
|
| 143 |
|
| 144 |
+
# Function to validate Together AI API key (basic format check)
|
| 145 |
+
def validate_together_key(api_key):
|
| 146 |
+
# Together API keys usually have a specific format
|
| 147 |
+
# This is just a simple length check for now
|
| 148 |
+
if len(api_key) > 20:
|
| 149 |
+
return True
|
| 150 |
+
return False
|
| 151 |
+
|
| 152 |
# Function to process LaTeX in the solution
|
| 153 |
def process_solution(solution):
|
| 154 |
# Replace $...$ with $$...$$ for better rendering in Gradio markdown
|
|
|
|
| 160 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 161 |
gr.Markdown("# 📚 Advanced Math Tutor")
|
| 162 |
gr.Markdown("""
|
| 163 |
+
This application provides step-by-step solutions to math problems using advanced AI models.
|
| 164 |
+
Choose between OpenRouter's Phi-4-reasoning-plus for text-based problems or Together AI's
|
| 165 |
+
Llama-Vision for problems with images.
|
| 166 |
""")
|
| 167 |
|
| 168 |
# Main tabs
|
| 169 |
with gr.Tabs():
|
| 170 |
+
# Text-based problem solver (OpenRouter)
|
| 171 |
+
with gr.TabItem("Text Problem Solver (OpenRouter)"):
|
| 172 |
with gr.Row():
|
| 173 |
with gr.Column(scale=1):
|
| 174 |
+
openrouter_api_key = gr.Textbox(
|
| 175 |
label="OpenRouter API Key",
|
| 176 |
placeholder="Enter your OpenRouter API key (starts with sk-or-)",
|
| 177 |
type="password"
|
| 178 |
)
|
| 179 |
+
text_problem_input = gr.Textbox(
|
| 180 |
label="Math Problem",
|
| 181 |
placeholder="Enter your math problem here...",
|
| 182 |
lines=5
|
|
|
|
| 188 |
["Calculate the area of a circle with radius 5 cm"],
|
| 189 |
["Find all values of x that satisfy the equation: log₂(x-1) + log₂(x+3) = 5"]
|
| 190 |
],
|
| 191 |
+
inputs=[text_problem_input],
|
| 192 |
label="Example Problems"
|
| 193 |
)
|
| 194 |
with gr.Row():
|
| 195 |
+
openrouter_submit_btn = gr.Button("Solve Problem", variant="primary")
|
| 196 |
+
openrouter_clear_btn = gr.Button("Clear")
|
| 197 |
|
| 198 |
with gr.Column(scale=2):
|
| 199 |
+
openrouter_solution_output = gr.Markdown(label="Solution")
|
| 200 |
|
| 201 |
# Store conversation history (invisible to user)
|
| 202 |
+
openrouter_conversation_history = gr.State(value=None)
|
| 203 |
|
| 204 |
# Button actions
|
| 205 |
+
openrouter_submit_btn.click(
|
| 206 |
+
fn=generate_math_solution_openrouter,
|
| 207 |
+
inputs=[openrouter_api_key, text_problem_input, openrouter_conversation_history],
|
| 208 |
+
outputs=[openrouter_solution_output, openrouter_conversation_history]
|
| 209 |
)
|
| 210 |
|
| 211 |
+
openrouter_clear_btn.click(
|
| 212 |
fn=lambda: ("", None),
|
| 213 |
inputs=[],
|
| 214 |
+
outputs=[openrouter_solution_output, openrouter_conversation_history]
|
| 215 |
)
|
| 216 |
|
| 217 |
+
# Image-based problem solver (Together AI)
|
| 218 |
+
with gr.TabItem("Image Problem Solver (Together AI)"):
|
| 219 |
+
with gr.Row():
|
| 220 |
+
with gr.Column(scale=1):
|
| 221 |
+
together_api_key = gr.Textbox(
|
| 222 |
+
label="Together AI API Key",
|
| 223 |
+
placeholder="Enter your Together AI API key",
|
| 224 |
+
type="password"
|
| 225 |
+
)
|
| 226 |
+
together_problem_input = gr.Textbox(
|
| 227 |
+
label="Problem Description (Optional)",
|
| 228 |
+
placeholder="Enter additional context for the image problem...",
|
| 229 |
+
lines=3
|
| 230 |
+
)
|
| 231 |
+
together_image_input = gr.Image(
|
| 232 |
+
label="Upload Math Problem Image",
|
| 233 |
+
type="filepath"
|
| 234 |
+
)
|
| 235 |
+
with gr.Row():
|
| 236 |
+
together_submit_btn = gr.Button("Solve Problem", variant="primary")
|
| 237 |
+
together_clear_btn = gr.Button("Clear")
|
| 238 |
+
|
| 239 |
+
with gr.Column(scale=2):
|
| 240 |
+
together_solution_output = gr.Markdown(label="Solution")
|
| 241 |
+
|
| 242 |
+
# Store conversation history (invisible to user)
|
| 243 |
+
together_conversation_history = gr.State(value=None)
|
| 244 |
+
|
| 245 |
+
# Button actions
|
| 246 |
+
together_submit_btn.click(
|
| 247 |
+
fn=generate_math_solution_together,
|
| 248 |
+
inputs=[together_api_key, together_problem_input, together_image_input, together_conversation_history],
|
| 249 |
+
outputs=[together_solution_output, together_conversation_history]
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
together_clear_btn.click(
|
| 253 |
+
fn=lambda: ("", None),
|
| 254 |
+
inputs=[],
|
| 255 |
+
outputs=[together_solution_output, together_conversation_history]
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
# Help tab
|
| 259 |
with gr.TabItem("Help"):
|
| 260 |
gr.Markdown("""
|
| 261 |
## How to Use the Advanced Math Tutor
|
| 262 |
|
| 263 |
### Getting Started
|
| 264 |
+
|
| 265 |
+
#### For Text-Based Problems (OpenRouter)
|
| 266 |
+
1. You'll need an API key from OpenRouter
|
| 267 |
2. Sign up at [OpenRouter](https://openrouter.ai/) to get your API key
|
| 268 |
+
3. Enter your API key in the designated field in the "Text Problem Solver" tab
|
| 269 |
+
|
| 270 |
+
#### For Image-Based Problems (Together AI)
|
| 271 |
+
1. You'll need an API key from Together AI
|
| 272 |
+
2. Sign up at [Together AI](https://www.together.ai/) to get your API key
|
| 273 |
+
3. Enter your API key in the designated field in the "Image Problem Solver" tab
|
| 274 |
+
4. Upload an image of your math problem
|
| 275 |
+
5. Optionally add text to provide additional context
|
| 276 |
|
| 277 |
### Solving Math Problems
|
| 278 |
+
- For text problems: Type or paste your math problem in the input field
|
| 279 |
+
- For image problems: Upload a clear image of the math problem
|
| 280 |
- Click "Solve Problem" to get a detailed step-by-step solution
|
| 281 |
- The solution will include explanations for each step
|
|
|
|
| 282 |
|
| 283 |
### Tips for Best Results
|
| 284 |
- Be specific in your problem description
|
|
|
|
| 286 |
- For complex equations, use clear notation
|
| 287 |
- For algebraic expressions, use ^ for exponents (e.g., x^2 for x²)
|
| 288 |
- Use parentheses to group terms clearly
|
| 289 |
+
- For images, ensure the math problem is clearly visible and well-lit
|
| 290 |
|
| 291 |
### Types of Problems You Can Solve
|
| 292 |
- Algebra (equations, inequalities, systems of equations)
|
|
|
|
| 302 |
gr.Markdown("""
|
| 303 |
---
|
| 304 |
### About
|
| 305 |
+
This application uses Microsoft's Phi-4-reasoning-plus model via OpenRouter for text-based problems
|
| 306 |
+
and Llama-Vision-Free via Together AI for image-based problems.
|
| 307 |
+
Your API keys are required but not stored permanently.
|
| 308 |
""")
|
| 309 |
|
| 310 |
return demo
|