MasteredUltraInstinct commited on
Commit
fd61a77
Β·
verified Β·
1 Parent(s): d042fe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -50,7 +50,7 @@ def clean_latex(latex):
50
  latex = latex.replace('e', '2.7183')
51
  return latex
52
 
53
- # βœ… Tab 2 specific cleaner (gentle)
54
  def clean_latex2(latex):
55
  latex = latex.replace('\\ ', '')
56
  latex = latex.replace('\\\\', '\\')
@@ -115,34 +115,21 @@ def wrapped_solver(img):
115
  result, cleaned, prompt = solve_polynomial(img)
116
  return result, cleaned, prompt
117
 
118
- with gr.Blocks() as demo:
119
- with gr.Tab("πŸ–ΌοΈ Parse from Image"):
120
- image_input = gr.Image(type="pil", label="πŸ“· Upload Image of Polynomial")
121
- hidden_latex = gr.Textbox(visible=False)
122
- explanation_prompt = gr.Textbox(visible=False)
123
- output_box = gr.Markdown(label="πŸ“‹ Step-by-step Solution")
124
- submit_btn = gr.Button("πŸ” Solve")
125
- submit_btn.click(fn=wrapped_solver, inputs=[image_input], outputs=[output_box, hidden_latex, explanation_prompt])
126
-
127
- with gr.Tab("πŸ“ Solve Simultaneous Equations"):
128
- sim_image_input = gr.Image(type="pil", label="πŸ“· Upload Image with Simultaneous Equations")
129
- sim_output = gr.Markdown(label="πŸ“‹ Solved System Output")
130
- sim_button = gr.Button("πŸ“Œ Solve System")
131
-
132
- def solve_system_of_equations(image):
133
  try:
134
  img = preprocess_handwritten_image(image)
135
  raw_latex = model(img)
136
  if not raw_latex or len(raw_latex.strip()) < 3:
137
  return "❌ Could not extract valid LaTeX."
138
 
139
- # Clean up array environments if present
140
  if r"\begin{array" in raw_latex:
141
  lines = re.findall(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
142
  if lines:
143
  raw_latex = lines[0]
144
- raw_latex = raw_latex.replace(r'\\', '\n') # line separator
145
- raw_latex = re.sub(r'&', '', raw_latex) # remove LaTeX column markers
146
 
147
  raw_latex = raw_latex.replace("\n", " ")
148
  equations = re.split(r'[;,]', raw_latex)
@@ -173,6 +160,20 @@ with gr.Blocks() as demo:
173
  except Exception as e:
174
  return f"❌ Error: {str(e)}"
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  sim_button.click(fn=solve_system_of_equations, inputs=[sim_image_input], outputs=[sim_output])
177
 
178
  if __name__ == "__main__":
 
50
  latex = latex.replace('e', '2.7183')
51
  return latex
52
 
53
+ # βœ… Tab 2 specific cleaner
54
  def clean_latex2(latex):
55
  latex = latex.replace('\\ ', '')
56
  latex = latex.replace('\\\\', '\\')
 
115
  result, cleaned, prompt = solve_polynomial(img)
116
  return result, cleaned, prompt
117
 
118
+ # βœ… FIXED: moved outside of gr.Blocks()
119
+ def solve_system_of_equations(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  try:
121
  img = preprocess_handwritten_image(image)
122
  raw_latex = model(img)
123
  if not raw_latex or len(raw_latex.strip()) < 3:
124
  return "❌ Could not extract valid LaTeX."
125
 
126
+ # Handle array environment
127
  if r"\begin{array" in raw_latex:
128
  lines = re.findall(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
129
  if lines:
130
  raw_latex = lines[0]
131
+ raw_latex = raw_latex.replace(r'\\', '\n')
132
+ raw_latex = re.sub(r'&', '', raw_latex)
133
 
134
  raw_latex = raw_latex.replace("\n", " ")
135
  equations = re.split(r'[;,]', raw_latex)
 
160
  except Exception as e:
161
  return f"❌ Error: {str(e)}"
162
 
163
+ # === UI ===
164
+ with gr.Blocks() as demo:
165
+ with gr.Tab("πŸ–ΌοΈ Parse from Image"):
166
+ image_input = gr.Image(type="pil", label="πŸ“· Upload Image of Polynomial")
167
+ hidden_latex = gr.Textbox(visible=False)
168
+ explanation_prompt = gr.Textbox(visible=False)
169
+ output_box = gr.Markdown(label="πŸ“‹ Step-by-step Solution")
170
+ submit_btn = gr.Button("πŸ” Solve")
171
+ submit_btn.click(fn=wrapped_solver, inputs=[image_input], outputs=[output_box, hidden_latex, explanation_prompt])
172
+
173
+ with gr.Tab("πŸ“ Solve Simultaneous Equations"):
174
+ sim_image_input = gr.Image(type="pil", label="πŸ“· Upload Image with Simultaneous Equations")
175
+ sim_output = gr.Markdown(label="πŸ“‹ Solved System Output")
176
+ sim_button = gr.Button("πŸ“Œ Solve System")
177
  sim_button.click(fn=solve_system_of_equations, inputs=[sim_image_input], outputs=[sim_output])
178
 
179
  if __name__ == "__main__":