QuickLearnerAI commited on
Commit
205fe04
·
verified ·
1 Parent(s): 5951891

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -135
app.py CHANGED
@@ -1,129 +1,144 @@
1
  import gradio as gr
2
- import os
3
  import re
4
- import time
5
- import base64
6
- from openai import OpenAI # access the open router api
7
- from together import Together
8
- from PIL import Image
9
  import io
10
 
 
11
 
12
- def math_solution_openrouter(api_key, prob_text, history=None):
13
  if not api_key.strip():
14
- return "please enter your OpenRouter Key.",history
15
- if not prob_text.strip():
16
- return "please enter a math problem so that I can solve it for you",
 
17
 
18
  try:
19
- client= OpenAI(
20
  base_url="https://openrouter.ai/api/v1",
21
  api_key=api_key,
22
  )
 
23
  messages= [
24
- { "role": "system", "content":
25
- """You are a very smart math tutor who can able to solve math and give proper explanations clearly and in a precise manner.
26
- You can analyze the math problem given to you and provide the details solution step by step.
27
- For every step:
28
- 1. Show all the mathematical explanations.
29
- 2. Explain each step and its necessity.
30
- 3. Connect it to the relevant mathematical concept.
31
- Format your response with clear section of headers using markdown and comment if necessary.
32
- Begin with a section of analysis named "Initail step" and followed number of step and conclude with "Final step" section.
33
- """},
 
34
  ]
35
 
36
  if history:
37
  for exchange in history:
38
- messages.append({"role":"user","content":exchange[0]}) #@ task a math problem
39
- if exchange[1]: # check if there is a problem
40
- messages.append({"role": "assistant","content": exchange[1]}) # AI response with solution.
41
 
42
- messages.append({"role":"user","content":f"Solve the given math problem step-by-step: {prob_text}"}) # math problem
 
43
 
 
44
  completion = client.chat.completions.create(
45
- model= "openai/gpt-4o",
46
- message= messages,
47
  extra_headers={
48
- "HTTP-Referer": "https://smartmathtutor.edu", # Optional. Site URL for rankings on openrouter.ai.
49
- "X-Title": "Smart Math Tutor", # Optional. Site title for rankings on openrouter.ai.
50
- }
51
-
52
  )
53
 
54
- solution = completion.choices[0].message.content
55
 
56
- #update history
57
- if history is None:
58
- history=[]
59
- history.append((prob_text, solution ))
60
 
61
  return solution, history
62
-
63
- except Exception as e:
64
- error_message= f"Error:{str(e)}"
65
 
 
 
66
  return error_message, history
67
-
68
-
69
  #image processing
70
- def img_conv_base64(img_path):
71
- if img_path is None:
72
- return None
73
 
 
 
 
 
74
  try:
75
- with open(img_path,"rb") as img_file:
76
  return base64.b64encode(img_file.read()).decode("utf-8")
77
-
78
  except Exception as e:
79
- print(f"Error converting image to base64:{str(e)}")
80
  return None
81
 
 
82
 
83
- # function for a given math problem for image data[Togetherai]
84
- def gen_math_sol_together(api_key, prob_text, img_path=None, history=None):
85
  if not api_key.strip():
86
- return "Please enter your Valid Together API key",history
 
 
 
87
 
88
- if not prob_text.strip() and img_path is None:
89
- return "please enter a valid math problem or upload an image of a math problem", history
90
-
91
  try:
92
- client= Together(api_key=api_key)
93
- messages=[{ "role": "system", "content":
94
- """You are a very smart math tutor who can able to solve math and give proper explanations clearly and in a precise manner.
95
- You can analyze the math problem from image given to you and provide the details solution step by step.
96
- For every step:
97
- 1. Show all the mathematical explanations.
98
- 2. Explain each step and its necessity.
99
- 3. Connect it to the relevant mathematical concept.
100
- Format your response with clear section of headers using markdown and comment if necessary.
101
- Begin with a section of analysis named "Initail step" and followed number of step and conclude with "Final step" section.
102
- """},]
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  if history:
105
  for exchange in history:
106
- messages.append({"role":"user","content":exchange[0]}) #@ task a math problem
107
- if exchange[1]: # check if there is a problem
108
- messages.append({"role": "assistant","content": exchange[1]}) # AI response with solution.
109
 
 
 
110
 
 
111
 
112
- user_message_content= [] # if we want to add some instructions regaring how to solve the math from given image
113
 
114
-
115
- if prob_text.strip(): # for text
116
  user_message_content.append({
117
  "type": "text",
118
- "text": f"Solve this math problem:{prob_text}"
119
  })
120
  else: #image
121
  user_message_content.append({
122
  "type": "text",
123
- "text": "Solve this math problem from the given image"
124
  })
125
- if img_path:
126
- base64_image= img_conv_base64(img_path)
 
 
 
127
  if base64_image:
128
  user_message_content.append({
129
  "type": "image_url",
@@ -131,122 +146,130 @@ def gen_math_sol_together(api_key, prob_text, img_path=None, history=None):
131
  "url": f"data:image/jpeg;base64,{base64_image}"
132
  }
133
  })
 
 
134
  messages.append({
135
- "role": "user", #conversation saved with image data
136
  "content": user_message_content
137
-
138
  })
139
 
140
- response = client.chat.completions.create(
 
141
  model="meta-llama/Llama-Vision-Free",
142
- messages=messages, # ✅ Use the correct key
143
  stream=False
144
  )
145
 
146
- solution= response.choices[0].message.content
147
 
 
148
  if history is None:
149
- history=[]
150
- history.append((prob_text if prob_text.strip() else "Image problem ..", solution))
151
-
152
-
153
- return solution, history
154
 
155
- except Exception as e:
156
- error_msg= f"Error: {str(e)}"
157
- return error_msg, history
158
 
 
 
 
159
 
160
- def create_demo():
161
  with gr.Blocks(theme=gr.themes.Ocean(primary_hue="blue")) as demo:
162
- gr.Markdown("# Smart Math Tutor")
163
- gr.Markdown("""This applications provide step by step solution to a math paroblem using AI Tools.
164
- Choose between OpenPouter's Phi-4-reasoning-plus for text based analysis and Together AI's Llama-Vision for problem with images""")\
165
-
166
-
 
167
 
168
  with gr.Tabs():
169
- with gr.TabItem("Text problem solver (OpenRouter)"):
170
  with gr.Row():
171
- with gr.Column(scale=1): #left side
172
- openrouter_api_key= gr.Textbox(
173
- label= "OpenRouter API key",
174
- placeholder= "enter your API key here",
175
- type= "password"
176
  )
177
- text_prob_input= gr.Textbox(
178
- label="Math Problem",
179
- placeholder= "Enter ypur math problem here..",
180
- lines= 5
181
  )
182
- example_problems= gr.Examples(
183
  examples=[
184
  ["Solve the quadratic equation: 3x² + 5x - 2 = 0"],
185
- ["Find the derivative of f(x) = x²ln(x)"],
186
  ["Calculate the area of a circle with radius 5 cm"],
187
- ["Find all values of x that satisfy the equation: log₁₀(x+1) + log₁₀(x²) = 5"]
188
  ],
189
- inputs=[text_prob_input],
190
  label="Example Problems"
191
  )
192
  with gr.Row():
193
- openrouter_submit_btn= gr.Button("solve problem", variant= "primary")
194
- openrouter_clear_btn= gr.Button("Clear")
 
 
 
195
 
196
- with gr.Column(scale=2): #right side
197
- openrouter_solution_output= gr.Markdown(label="Solution")
198
 
199
- openrouter_conversation_history= gr.State(value=None)
200
  openrouter_submit_btn.click(
201
- fn=math_solution_openrouter,
202
- inputs=[openrouter_api_key, text_prob_input, openrouter_conversation_history],
203
  outputs=[openrouter_solution_output, openrouter_conversation_history]
204
  )
205
 
206
-
207
  openrouter_clear_btn.click(
208
- fn=lambda: ("",None),
209
  inputs=[],
210
  outputs=[openrouter_solution_output, openrouter_conversation_history]
211
  )
212
 
213
- with gr.TabItem("Text problem solver (Together AI)"):
214
  with gr.Row():
215
- with gr.Column(scale=1): #left side
216
- together_api_key= gr.Textbox(
217
- label= "Together API key",
218
- placeholder= "enter your API key here",
219
- type= "password"
220
  )
221
- together_prob_input= gr.Textbox(
222
- label="Math Problem Description",
223
- placeholder= "Enter additional math problem here..",
224
- lines= 3
225
  )
226
- together_image_input= gr.Image(
227
- label="upload math problem image",
228
  type="filepath"
229
  )
230
  with gr.Row():
231
- together_submit_btn= gr.Button("Solve Problem", variant="primary")
232
  together_clear_btn = gr.Button("Clear")
233
 
234
  with gr.Column(scale=2):
235
- together_solution_output= gr.Markdown(label="Solution")
 
 
 
236
 
237
- together_conversation_history= gr.State(value=None)
238
  # Button actions
239
  together_submit_btn.click(
240
- fn=gen_math_sol_together,
241
- inputs=[together_api_key, together_prob_input, together_image_input, together_conversation_history],
242
  outputs=[together_solution_output, together_conversation_history]
243
  )
 
244
  together_clear_btn.click(
245
- fn=lambda: ("", None),
246
  inputs=[],
247
  outputs=[together_solution_output, together_conversation_history]
248
  )
249
 
 
250
  # Footer
251
  gr.Markdown("""
252
  ---
@@ -257,8 +280,8 @@ def create_demo():
257
  """)
258
 
259
  return demo
 
260
  # Launch the app
261
  if __name__ == "__main__":
262
  demo = create_demo()
263
  demo.launch()
264
-
 
1
  import gradio as gr
2
+ import os
3
  import re
4
+ import time
5
+ import base64 # image has to be converted to base64
6
+ from openai import OpenAI #openrouter access
7
+ from together import Together
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():
15
+ return "Please enter your OpenRouter API key.", history
16
+
17
+ if not problem_text.strip():
18
+ return "Please enter a math problem so that I can solve it for you!",
19
 
20
  try:
21
+ client=OpenAI(
22
  base_url="https://openrouter.ai/api/v1",
23
  api_key=api_key,
24
  )
25
+
26
  messages= [
27
+ {"role": "system", "content":
28
+ """You are an expert math tutor who explains concepts clearly and thoroughly.
29
+ Analyze the given math problem and provide a detailed step-by-step solution.
30
+ For each step:
31
+ 1. Show the mathematical operation
32
+ 2. Explain why this step is necessary
33
+ 3. Connect it to relevant mathematical concepts
34
+
35
+ Format your response with clear section headers using markdown.
36
+ Begin with an "Initial Analysis" section, follow with numbered steps,
37
+ and conclude with a "Final Answer" section."""},
38
  ]
39
 
40
  if history:
41
  for exchange in history:
42
+ messages.append({"role": "user", "content": exchange[0]}) #asks a math prob
43
+ if exchange[1]: # Check if there's a response
44
+ messages.append({"role": "assistant", "content": exchange[1]}) #AI responses with a solution
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="deepseek/deepseek-r1-0528: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: #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
 
68
+ except Exception as e:
69
+ error_message = f"Error: {str(e)}"
70
  return error_message, history
71
+
72
+
73
  #image processing
 
 
 
74
 
75
+ def image_to_base64(image_path):
76
+ if image_path is None:
77
+ return None
78
+
79
  try:
80
+ with open(image_path, "rb") as img_file:
81
  return base64.b64encode(img_file.read()).decode("utf-8")
 
82
  except Exception as e:
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():
90
+ return "Please enter your Together AI API key.", history
91
+
92
+ if not problem_text.strip() and image_path is None:
93
+ return "Please enter a math problem or upload an image of a math problem.", history
94
 
 
 
 
95
  try:
96
+ client = Together(api_key=api_key)
 
 
 
 
 
 
 
 
 
 
97
 
98
+ messages= [
99
+ {"role": "system", "content":
100
+ """You are an expert math tutor who explains concepts clearly and thoroughly.
101
+ Analyze the given math problem and provide a detailed step-by-step solution.
102
+ For each step:
103
+ 1. Show the mathematical operation
104
+ 2. Explain why this step is necessary
105
+ 3. Connect it to relevant mathematical concepts
106
+
107
+ Format your response with clear section headers using markdown.
108
+ Begin with an "Initial Analysis" section, follow with numbered steps,
109
+ and conclude with a "Final Answer" section."""},
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 = [] # WE are going to add some instructions regarding how to solve the math problem in the image
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({
134
  "type": "text",
135
+ "text": "Solve this math problem from the image:"
136
  })
137
+
138
+ # Add image if provided
139
+ if image_path:
140
+ # Convert image to base64
141
+ base64_image = image_to_base64(image_path)
142
  if base64_image:
143
  user_message_content.append({
144
  "type": "image_url",
 
146
  "url": f"data:image/jpeg;base64,{base64_image}"
147
  }
148
  })
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="meta-llama/Llama-Vision-Free",
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:
167
+ history = []
168
+ history.append((problem_text if problem_text.strip() else "Image problem", solution))
 
 
 
169
 
170
+ return solution, history
 
 
171
 
172
+ except Exception as e:
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-)",
192
+ type="password"
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(
200
  examples=[
201
  ["Solve the quadratic equation: 3x² + 5x - 2 = 0"],
202
+ ["Find the derivative of f(x) = x³ln(x)"],
203
  ["Calculate the area of a circle with radius 5 cm"],
204
+ ["Find all values of x that satisfy the equation: log(x-1) + log(x+3) = 5"]
205
  ],
206
+ inputs=[text_problem_input],
207
  label="Example Problems"
208
  )
209
  with gr.Row():
210
+ openrouter_submit_btn = gr.Button("Solve Problem", variant="primary")
211
+ openrouter_clear_btn = gr.Button("Clear")
212
+
213
+ with gr.Column(scale=2):
214
+ openrouter_solution_output = gr.Markdown(label="Solution")
215
 
216
+ # Store conversation history (invisible to user)
217
+ openrouter_conversation_history = gr.State(value=None)
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
  )
225
 
 
226
  openrouter_clear_btn.click(
227
+ fn=lambda: ("", None),
228
  inputs=[],
229
  outputs=[openrouter_solution_output, openrouter_conversation_history]
230
  )
231
 
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/Instruction (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
+
273
  # Footer
274
  gr.Markdown("""
275
  ---
 
280
  """)
281
 
282
  return demo
283
+
284
  # Launch the app
285
  if __name__ == "__main__":
286
  demo = create_demo()
287
  demo.launch()