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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -34
app.py CHANGED
@@ -130,40 +130,48 @@ with gr.Blocks() as demo:
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
- raw_latex = raw_latex.replace("\n", " ")
140
- equations = re.split(r'[;,]', raw_latex)
141
- cleaned_equations = [clean_latex2(eq) for eq in equations if '=' in eq]
142
-
143
- sympy_eqs = []
144
- symbols = set()
145
- for eq in cleaned_equations:
146
- parsed = parse_latex(eq)
147
- if isinstance(parsed, sp.Equality):
148
- sympy_eqs.append(parsed)
149
- symbols.update(parsed.free_symbols)
150
-
151
- if len(sympy_eqs) < 2:
152
- return f"❌ Not enough valid equations detected.\n\n```latex\n{raw_latex}\n```"
153
-
154
- sol = sp.solve(sympy_eqs, list(symbols), dict=True)
155
- output = f"## 🧾 Detected Equations:\n```latex\n{raw_latex}\n```\n"
156
- output += "---\n## ✏️ Parsed & Cleaned:\n"
157
- for eq in sympy_eqs:
158
- output += f"$$ {sp.latex(eq)} $$\n"
159
- output += "---\n## βœ… Solution:\n"
160
- if sol:
161
- output += "$$" + sp.latex(sol[0]) + "$$"
162
- else:
163
- output += "❌ No solution found or system may be inconsistent."
164
- return output
165
- except Exception as e:
166
- return f"❌ Error: {str(e)}"
 
 
 
 
 
 
 
 
167
 
168
  sim_button.click(fn=solve_system_of_equations, inputs=[sim_image_input], outputs=[sim_output])
169
 
 
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)
149
+ cleaned_equations = [clean_latex2(eq) for eq in equations if '=' in eq]
150
+
151
+ sympy_eqs = []
152
+ symbols = set()
153
+ for eq in cleaned_equations:
154
+ parsed = parse_latex(eq)
155
+ if isinstance(parsed, sp.Equality):
156
+ sympy_eqs.append(parsed)
157
+ symbols.update(parsed.free_symbols)
158
+
159
+ if len(sympy_eqs) < 2:
160
+ return f"❌ Not enough valid equations detected.\n\n```latex\n{raw_latex}\n```"
161
+
162
+ sol = sp.solve(sympy_eqs, list(symbols), dict=True)
163
+ output = f"## 🧾 Detected Equations:\n```latex\n{raw_latex}\n```\n"
164
+ output += "---\n## ✏️ Parsed & Cleaned:\n"
165
+ for eq in sympy_eqs:
166
+ output += f"$$ {sp.latex(eq)} $$\n"
167
+ output += "---\n## βœ… Solution:\n"
168
+ if sol:
169
+ output += "$$" + sp.latex(sol[0]) + "$$"
170
+ else:
171
+ output += "❌ No solution found or system may be inconsistent."
172
+ return output
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