Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from together import Together
|
|
| 8 |
from PIL import Image #pillow for image processing
|
| 9 |
import io
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
def generate_math_solution_openrouter(api_key, problem_text, history=None):
|
| 14 |
if not api_key.strip():
|
|
@@ -59,9 +59,9 @@ def generate_math_solution_openrouter(api_key, problem_text, history=None):
|
|
| 59 |
solution=completion.choices[0].message.content
|
| 60 |
|
| 61 |
# Update history
|
| 62 |
-
if history is None: #no convo
|
| 63 |
-
history = [] #my all convo saved here
|
| 64 |
-
history.append((problem_text, solution)) #now i update my convo history
|
| 65 |
|
| 66 |
return solution, history
|
| 67 |
|
|
@@ -70,7 +70,7 @@ def generate_math_solution_openrouter(api_key, problem_text, history=None):
|
|
| 70 |
return error_message, history
|
| 71 |
|
| 72 |
|
| 73 |
-
#image processing
|
| 74 |
|
| 75 |
def image_to_base64(image_path):
|
| 76 |
if image_path is None:
|
|
@@ -83,7 +83,7 @@ def image_to_base64(image_path):
|
|
| 83 |
print(f"Error converting image to base64: {str(e)}")
|
| 84 |
return None
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
def generate_math_solution_together(api_key, problem_text, image_path=None, history=None):
|
| 89 |
if not api_key.strip():
|
|
@@ -121,13 +121,13 @@ def generate_math_solution_together(api_key, problem_text, image_path=None, hist
|
|
| 121 |
|
| 122 |
# calculate the area of a circle with radius 6 cm (Image of a problem)
|
| 123 |
|
| 124 |
-
#problem text / user message = Before calculating area convert radius of 6 cm to meter first
|
| 125 |
|
| 126 |
# Add text content if provided
|
| 127 |
if problem_text.strip(): #image+instruction
|
| 128 |
user_message_content.append({
|
| 129 |
"type": "text",
|
| 130 |
-
"text": f"Solve this math problem: {problem_text}" #Before calculating area convert radius of 6 cm to meter first
|
| 131 |
})
|
| 132 |
else: #image
|
| 133 |
user_message_content.append({
|
|
@@ -149,18 +149,18 @@ def generate_math_solution_together(api_key, problem_text, image_path=None, hist
|
|
| 149 |
|
| 150 |
# Add the user message with content
|
| 151 |
messages.append({
|
| 152 |
-
"role": "user", #conversation saved with image data+usermessage/instruction
|
| 153 |
"content": user_message_content
|
| 154 |
})
|
| 155 |
|
| 156 |
|
| 157 |
-
response=client.chat.completions.create( #Together
|
| 158 |
model="ServiceNow-AI/Apriel-1.5-15b-Thinker",
|
| 159 |
messages=messages,
|
| 160 |
stream=False
|
| 161 |
)
|
| 162 |
|
| 163 |
-
solution=response.choices[0].message.content #problem
|
| 164 |
|
| 165 |
# Update history - for simplicity, just store the text problem
|
| 166 |
if history is None:
|
|
@@ -173,19 +173,17 @@ def generate_math_solution_together(api_key, problem_text, image_path=None, hist
|
|
| 173 |
error_message = f"Error: {str(e)}"
|
| 174 |
return error_message, history
|
| 175 |
|
| 176 |
-
def create_demo(): #interface design complete
|
| 177 |
with gr.Blocks(theme=gr.themes.Ocean(primary_hue="blue")) as demo:
|
| 178 |
gr.Markdown("# 📚 Advanced Math Tutor")
|
| 179 |
gr.Markdown("""
|
| 180 |
This application provides step-by-step solutions to math problems using advanced AI models.
|
| 181 |
-
Choose between OpenRouter's Phi-4-reasoning-plus for text-based problems or Together AI's
|
| 182 |
-
Llama-Vision for problems with images.
|
| 183 |
""")
|
| 184 |
|
| 185 |
with gr.Tabs():
|
| 186 |
with gr.TabItem("Text Problem Solver (OpenRouter)"):
|
| 187 |
with gr.Row():
|
| 188 |
-
with gr.Column(scale=1): #left side column design complete
|
| 189 |
openrouter_api_key = gr.Textbox(
|
| 190 |
label="OpenRouter API Key",
|
| 191 |
placeholder="Enter your OpenRouter API key (starts with sk-or-)",
|
|
@@ -193,7 +191,7 @@ def create_demo(): #interface design complete
|
|
| 193 |
)
|
| 194 |
text_problem_input = gr.Textbox(
|
| 195 |
label="Math Problem",
|
| 196 |
-
placeholder="Enter your math problem here...", #Solve the quadratic equation: 3x² + 5x - 2 = 0"
|
| 197 |
lines=5
|
| 198 |
)
|
| 199 |
example_problems = gr.Examples(
|
|
@@ -218,7 +216,7 @@ def create_demo(): #interface design complete
|
|
| 218 |
|
| 219 |
# Button actions
|
| 220 |
openrouter_submit_btn.click(
|
| 221 |
-
fn=generate_math_solution_openrouter, #text problem solve
|
| 222 |
inputs=[openrouter_api_key, text_problem_input, openrouter_conversation_history],
|
| 223 |
outputs=[openrouter_solution_output, openrouter_conversation_history]
|
| 224 |
)
|
|
|
|
| 8 |
from PIL import Image #pillow for image processing
|
| 9 |
import io
|
| 10 |
|
| 11 |
+
# function to create math solution from math problem text
|
| 12 |
|
| 13 |
def generate_math_solution_openrouter(api_key, problem_text, history=None):
|
| 14 |
if not api_key.strip():
|
|
|
|
| 59 |
solution=completion.choices[0].message.content
|
| 60 |
|
| 61 |
# Update history
|
| 62 |
+
if history is None: # no convo
|
| 63 |
+
history = [] # my all convo saved here
|
| 64 |
+
history.append((problem_text, solution)) # now i update my convo history
|
| 65 |
|
| 66 |
return solution, history
|
| 67 |
|
|
|
|
| 70 |
return error_message, history
|
| 71 |
|
| 72 |
|
| 73 |
+
# image processing
|
| 74 |
|
| 75 |
def image_to_base64(image_path):
|
| 76 |
if image_path is None:
|
|
|
|
| 83 |
print(f"Error converting image to base64: {str(e)}")
|
| 84 |
return None
|
| 85 |
|
| 86 |
+
# function for a math problem that uses image data (Together)
|
| 87 |
|
| 88 |
def generate_math_solution_together(api_key, problem_text, image_path=None, history=None):
|
| 89 |
if not api_key.strip():
|
|
|
|
| 121 |
|
| 122 |
# calculate the area of a circle with radius 6 cm (Image of a problem)
|
| 123 |
|
| 124 |
+
# problem text / user message = Before calculating area convert radius of 6 cm to meter first
|
| 125 |
|
| 126 |
# Add text content if provided
|
| 127 |
if problem_text.strip(): #image+instruction
|
| 128 |
user_message_content.append({
|
| 129 |
"type": "text",
|
| 130 |
+
"text": f"Solve this math problem: {problem_text}" # Before calculating area convert radius of 6 cm to meter first
|
| 131 |
})
|
| 132 |
else: #image
|
| 133 |
user_message_content.append({
|
|
|
|
| 149 |
|
| 150 |
# Add the user message with content
|
| 151 |
messages.append({
|
| 152 |
+
"role": "user", # conversation saved with image data+usermessage/instruction
|
| 153 |
"content": user_message_content
|
| 154 |
})
|
| 155 |
|
| 156 |
|
| 157 |
+
response=client.chat.completions.create( # Together
|
| 158 |
model="ServiceNow-AI/Apriel-1.5-15b-Thinker",
|
| 159 |
messages=messages,
|
| 160 |
stream=False
|
| 161 |
)
|
| 162 |
|
| 163 |
+
solution=response.choices[0].message.content # problem -> ans syntax
|
| 164 |
|
| 165 |
# Update history - for simplicity, just store the text problem
|
| 166 |
if history is None:
|
|
|
|
| 173 |
error_message = f"Error: {str(e)}"
|
| 174 |
return error_message, history
|
| 175 |
|
| 176 |
+
def create_demo(): # interface design complete
|
| 177 |
with gr.Blocks(theme=gr.themes.Ocean(primary_hue="blue")) as demo:
|
| 178 |
gr.Markdown("# 📚 Advanced Math Tutor")
|
| 179 |
gr.Markdown("""
|
| 180 |
This application provides step-by-step solutions to math problems using advanced AI models.
|
|
|
|
|
|
|
| 181 |
""")
|
| 182 |
|
| 183 |
with gr.Tabs():
|
| 184 |
with gr.TabItem("Text Problem Solver (OpenRouter)"):
|
| 185 |
with gr.Row():
|
| 186 |
+
with gr.Column(scale=1): # left side column design complete
|
| 187 |
openrouter_api_key = gr.Textbox(
|
| 188 |
label="OpenRouter API Key",
|
| 189 |
placeholder="Enter your OpenRouter API key (starts with sk-or-)",
|
|
|
|
| 191 |
)
|
| 192 |
text_problem_input = gr.Textbox(
|
| 193 |
label="Math Problem",
|
| 194 |
+
placeholder="Enter your math problem here...", # Solve the quadratic equation: 3x² + 5x - 2 = 0"
|
| 195 |
lines=5
|
| 196 |
)
|
| 197 |
example_problems = gr.Examples(
|
|
|
|
| 216 |
|
| 217 |
# Button actions
|
| 218 |
openrouter_submit_btn.click(
|
| 219 |
+
fn=generate_math_solution_openrouter, # text problem solve
|
| 220 |
inputs=[openrouter_api_key, text_problem_input, openrouter_conversation_history],
|
| 221 |
outputs=[openrouter_solution_output, openrouter_conversation_history]
|
| 222 |
)
|