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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -54,35 +54,26 @@ def clean_latex(latex):
54
 
55
  def clean_latex2(latex):
56
  latex = latex.replace('\\ ', '')
57
- latex = latex.replace('\\\\', '\n')
58
- latex = latex.replace('~', '')
59
  latex = latex.replace('βˆ’', '-').replace('–', '-')
60
-
61
- # Replace math operators
62
  latex = re.sub(r'\\times', '*', latex)
63
  latex = re.sub(r'\\cdot', '*', latex)
64
 
65
- # Strip unwanted styles
66
  latex = re.sub(r'\\(text|mathbf|mathrm|mathit|textbf|mathcal|cal)\s*\{([^{}]+)\}', r'\2', latex)
67
- latex = re.sub(r'\\[a-zA-Z]+', '', latex) # remove leftover LaTeX commands
68
-
69
- # Remove symbols that interfere
70
  latex = latex.replace('{', '').replace('}', '').replace('&', '')
71
 
72
- # Fix splitting
73
  latex = re.sub(r'[;,]', '\n', latex)
74
  latex = re.sub(r'=+', '=', latex)
75
-
76
- # Clean extra space
77
  latex = re.sub(r'\s+', ' ', latex).strip()
78
 
79
- # OCR character corrections
80
  latex = re.sub(r'\bchi\b|\bxi\b|\balpha\b', 'x', latex)
81
  latex = re.sub(r'\bbeta\b', 'b', latex)
82
  latex = re.sub(r'\bgamma\b', 'y', latex)
83
 
84
- # Insert * where missing
85
- latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
86
  return latex
87
 
88
  def solve_polynomial(image):
@@ -135,19 +126,17 @@ def wrapped_solver(img):
135
 
136
  def solve_system_of_equations(image):
137
  try:
138
- if isinstance(image, np.ndarray):
139
- image = Image.fromarray(image)
140
  img = preprocess_handwritten_image(image)
141
  raw_latex = model(img)
142
 
143
  if not raw_latex or len(raw_latex.strip()) < 3:
144
  return "❌ Could not extract valid LaTeX."
145
 
146
- # Extract inside array block
147
  if r"\begin{array" in raw_latex:
148
- match = re.search(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
149
- if match:
150
- raw_latex = match.group(1)
151
  raw_latex = raw_latex.replace(r'\\', '\n')
152
  raw_latex = re.sub(r'&', '', raw_latex)
153
 
@@ -162,7 +151,7 @@ def solve_system_of_equations(image):
162
  parsed = parse_latex(cleaned)
163
  if isinstance(parsed, sp.Equality):
164
  cleaned_equations.append(cleaned)
165
- except Exception:
166
  continue
167
 
168
  sympy_eqs = []
@@ -190,6 +179,7 @@ def solve_system_of_equations(image):
190
  except Exception as e:
191
  return f"❌ Error: {str(e)}"
192
 
 
193
 
194
  # === UI ===
195
  with gr.Blocks() as demo:
 
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):
 
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:
139
+ raw_latex = lines[0]
140
  raw_latex = raw_latex.replace(r'\\', '\n')
141
  raw_latex = re.sub(r'&', '', raw_latex)
142
 
 
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
  except Exception as e:
180
  return f"❌ Error: {str(e)}"
181
 
182
+
183
 
184
  # === UI ===
185
  with gr.Blocks() as demo: