MasteredUltraInstinct commited on
Commit
f68692c
Β·
verified Β·
1 Parent(s): b4b9859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  from PIL import Image
3
- import numpy as np
4
  from pix2tex.cli import LatexOCR
5
  import sympy as sp
6
  from sympy.parsing.latex import parse_latex
@@ -11,7 +10,7 @@ model = LatexOCR()
11
  def preprocess_handwritten_image(pil_img):
12
  return pil_img.convert('RGB')
13
 
14
- # Tab 1 cleaning (unchanged)
15
  def clean_latex(latex):
16
  latex = latex.replace('\\ ', '')
17
  latex = latex.replace('\\\\', '\\')
@@ -52,42 +51,53 @@ def clean_latex(latex):
52
  latex = latex.replace('E', '2.7183')
53
  return latex
54
 
 
55
  def clean_latex2(latex):
56
  latex = latex.replace('\\ ', '')
57
  latex = latex.replace('\\\\', '\\')
 
 
 
58
  latex = latex.replace('βˆ’', '-').replace('–', '-')
59
- latex = re.sub(r'\\times', '*', latex)
60
- latex = re.sub(r'\\cdot', '*', latex)
61
 
62
- # Strip LaTeX wrappers
63
  latex = re.sub(r'\\(text|mathbf|mathrm|mathit|textbf|mathcal|cal)\s*\{([^{}]+)\}', r'\2', latex)
64
- latex = re.sub(r'\\[a-zA-Z]+\s*', '', latex) # leftover LaTeX commands
65
- latex = latex.replace('{', '').replace('}', '').replace('&', '')
66
 
67
- # Normalize spacing and equation delimiters
 
 
 
 
 
 
 
68
  latex = re.sub(r'[;,]', '\n', latex)
69
- latex = re.sub(r'=+', '=', latex)
70
- latex = re.sub(r'\s+', ' ', latex).strip()
71
 
72
- # Fix OCR misreads
73
- latex = re.sub(r'\bchi\b|\bxi\b|\balpha\b', 'x', latex)
74
- latex = re.sub(r'\bbeta\b', 'b', latex)
75
- latex = re.sub(r'\bgamma\b', 'y', latex)
76
 
77
- return latex
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  def solve_polynomial(image):
80
  try:
81
- if isinstance(image, np.ndarray):
82
- image = Image.fromarray(image)
83
  img = preprocess_handwritten_image(image)
84
  latex_result = model(img)
85
-
86
- if not latex_result or len(latex_result.strip()) < 2 or "ndarray" in latex_result:
87
  return "❌ Could not extract valid LaTeX from image.", "", ""
88
-
89
  cleaned_latex = clean_latex(latex_result)
90
-
91
  try:
92
  expr = parse_latex(cleaned_latex)
93
  expr = expr.subs(sp.pi, sp.Float(3.1416)).subs(sp.E, sp.Float(2.7183))
@@ -122,17 +132,18 @@ def solve_polynomial(image):
122
  return f"❌ Error: {str(e)}", "", ""
123
 
124
  def wrapped_solver(img):
125
- return solve_polynomial(img)
 
126
 
 
127
  def solve_system_of_equations(image):
128
  try:
129
  img = preprocess_handwritten_image(image)
130
  raw_latex = model(img)
131
-
132
  if not raw_latex or len(raw_latex.strip()) < 3:
133
  return "❌ Could not extract valid LaTeX."
134
 
135
- # Handle \array environment
136
  if r"\begin{array" in raw_latex:
137
  lines = re.findall(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
138
  if lines:
@@ -146,12 +157,13 @@ def solve_system_of_equations(image):
146
  cleaned_equations = []
147
  for eq in equations:
148
  cleaned = clean_latex2(eq)
 
149
  if '=' in cleaned:
150
  try:
151
  parsed = parse_latex(cleaned)
152
  if isinstance(parsed, sp.Equality):
153
  cleaned_equations.append(cleaned)
154
- except:
155
  continue
156
 
157
  sympy_eqs = []
@@ -179,8 +191,6 @@ def solve_system_of_equations(image):
179
  except Exception as e:
180
  return f"❌ Error: {str(e)}"
181
 
182
-
183
-
184
  # === UI ===
185
  with gr.Blocks() as demo:
186
  with gr.Tab("πŸ–ΌοΈ Parse from Image"):
 
1
  import gradio as gr
2
  from PIL import Image
 
3
  from pix2tex.cli import LatexOCR
4
  import sympy as sp
5
  from sympy.parsing.latex import parse_latex
 
10
  def preprocess_handwritten_image(pil_img):
11
  return pil_img.convert('RGB')
12
 
13
+ # Tab 1 cleaning (KEEP UNCHANGED)
14
  def clean_latex(latex):
15
  latex = latex.replace('\\ ', '')
16
  latex = latex.replace('\\\\', '\\')
 
51
  latex = latex.replace('E', '2.7183')
52
  return latex
53
 
54
+ # βœ… Tab 2 specific cleaner
55
  def clean_latex2(latex):
56
  latex = latex.replace('\\ ', '')
57
  latex = latex.replace('\\\\', '\\')
58
+ latex = re.sub(r'\\[ \t\n\r\f\v]*', '', latex)
59
+ latex = latex.replace(r'\times', '*')
60
+ latex = latex.replace(r'\cdot', '*')
61
  latex = latex.replace('βˆ’', '-').replace('–', '-')
 
 
62
 
63
+ # Remove LaTeX wrappers like \mathcal{}, \mathbf{}, etc.
64
  latex = re.sub(r'\\(text|mathbf|mathrm|mathit|textbf|mathcal|cal)\s*\{([^{}]+)\}', r'\2', latex)
 
 
65
 
66
+ # Remove remaining LaTeX commands (like \!\!\! and \,)
67
+ latex = re.sub(r'\\[a-zA-Z]+', '', latex)
68
+
69
+ # Remove nested or empty braces
70
+ latex = re.sub(r'\{+\}+', '', latex)
71
+ latex = re.sub(r'\{([^\{\}]*)\}', r'\1', latex)
72
+
73
+ # Remove extra commas, semicolons, weird formatting characters
74
  latex = re.sub(r'[;,]', '\n', latex)
75
+ latex = re.sub(r'[^\w\s=+\-*/().]', '', latex)
 
76
 
77
+ # Replace multiple spaces with single
78
+ latex = re.sub(r'\s+', ' ', latex)
 
 
79
 
80
+ # Normalize equals
81
+ latex = re.sub(r'(?<![=<>])=(?![=<>])', ' = ', latex)
82
+
83
+ # Replace ambiguous variable artifacts
84
+ replacements = {
85
+ 'chi': 'x', 'xi': 'x', 'alpha': 'x', 'beta': 'b',
86
+ 'gamma': 'y', 'vartheta': '3', 'mathcal': '',
87
+ 'cal': '', 'mathrm': ''
88
+ }
89
+ for wrong, right in replacements.items():
90
+ latex = re.sub(wrong, right, latex)
91
+
92
+ return latex.strip()
93
 
94
  def solve_polynomial(image):
95
  try:
 
 
96
  img = preprocess_handwritten_image(image)
97
  latex_result = model(img)
98
+ if not latex_result or len(latex_result.strip()) < 2:
 
99
  return "❌ Could not extract valid LaTeX from image.", "", ""
 
100
  cleaned_latex = clean_latex(latex_result)
 
101
  try:
102
  expr = parse_latex(cleaned_latex)
103
  expr = expr.subs(sp.pi, sp.Float(3.1416)).subs(sp.E, sp.Float(2.7183))
 
132
  return f"❌ Error: {str(e)}", "", ""
133
 
134
  def wrapped_solver(img):
135
+ result, cleaned, prompt = solve_polynomial(img)
136
+ return result, cleaned, prompt
137
 
138
+ # βœ… FIXED: moved outside of gr.Blocks()
139
  def solve_system_of_equations(image):
140
  try:
141
  img = preprocess_handwritten_image(image)
142
  raw_latex = model(img)
 
143
  if not raw_latex or len(raw_latex.strip()) < 3:
144
  return "❌ Could not extract valid LaTeX."
145
 
146
+ # Handle array environment
147
  if r"\begin{array" in raw_latex:
148
  lines = re.findall(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
149
  if lines:
 
157
  cleaned_equations = []
158
  for eq in equations:
159
  cleaned = clean_latex2(eq)
160
+ cleaned = re.sub(r'(?<![=<>])=(?![=<>])', ' = ', cleaned)
161
  if '=' in cleaned:
162
  try:
163
  parsed = parse_latex(cleaned)
164
  if isinstance(parsed, sp.Equality):
165
  cleaned_equations.append(cleaned)
166
+ except Exception:
167
  continue
168
 
169
  sympy_eqs = []
 
191
  except Exception as e:
192
  return f"❌ Error: {str(e)}"
193
 
 
 
194
  # === UI ===
195
  with gr.Blocks() as demo:
196
  with gr.Tab("πŸ–ΌοΈ Parse from Image"):