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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -53,42 +53,38 @@ def clean_latex(latex):
53
  return latex
54
 
55
  def clean_latex2(latex):
56
- # Step 1: Replace basic formatting issues
57
  latex = latex.replace('\\ ', '')
58
  latex = latex.replace('\\\\', '\n')
59
  latex = latex.replace('~', '')
60
  latex = latex.replace('βˆ’', '-').replace('–', '-')
61
 
62
- # Step 2: Fix common LaTeX math ops
63
  latex = re.sub(r'\\times', '*', latex)
64
  latex = re.sub(r'\\cdot', '*', latex)
65
 
66
- # Step 3: Strip away wrappers like \mathcal{}, \mathbf{}, etc.
67
- latex = re.sub(r'\\(text|mathbf|mathrm|mathit|textbf|mathcal|cal|nabla)\s*\{([^{}]+)\}', r'\2', latex)
 
68
 
69
- # Step 4: Remove leftover backslash commands (\!, \,, etc.)
70
- latex = re.sub(r'\\[a-zA-Z]+', '', latex)
71
-
72
- # Step 5: Strip non-math braces, ampersands, etc.
73
  latex = latex.replace('{', '').replace('}', '').replace('&', '')
74
 
75
- # Step 6: Break into lines if multiple equations
76
  latex = re.sub(r'[;,]', '\n', latex)
77
-
78
- # Step 7: Fix repeated symbols
79
- latex = re.sub(r'-{2,}', '-', latex)
80
  latex = re.sub(r'=+', '=', latex)
81
 
82
- # Step 8: Symbol OCR replacements
 
 
 
83
  latex = re.sub(r'\bchi\b|\bxi\b|\balpha\b', 'x', latex)
84
  latex = re.sub(r'\bbeta\b', 'b', latex)
85
  latex = re.sub(r'\bgamma\b', 'y', latex)
86
 
87
- # Step 9: Normalize spacing
88
- latex = re.sub(r'\s+', ' ', latex).strip()
89
-
90
  return latex
91
-
92
  def solve_polynomial(image):
93
  try:
94
  if isinstance(image, np.ndarray):
@@ -147,15 +143,15 @@ def solve_system_of_equations(image):
147
  if not raw_latex or len(raw_latex.strip()) < 3:
148
  return "❌ Could not extract valid LaTeX."
149
 
150
- # Extract from array if applicable
151
  if r"\begin{array" in raw_latex:
152
- array_match = re.search(r'\\begin{array}{[^}]*}(.+?)\\end{array}', raw_latex, re.DOTALL)
153
- if array_match:
154
- raw_latex = array_match.group(1)
155
- raw_latex = raw_latex.replace('\\\\', '\n')
156
- raw_latex = raw_latex.replace('&', '')
157
 
158
- raw_latex = raw_latex.replace('\n', ' ')
159
  equations = re.split(r'[;,]', raw_latex)
160
 
161
  cleaned_equations = []
@@ -181,7 +177,6 @@ def solve_system_of_equations(image):
181
  return f"❌ Not enough valid equations detected.\n\n```latex\n{raw_latex}\n```"
182
 
183
  sol = sp.solve(sympy_eqs, list(symbols), dict=True)
184
-
185
  output = f"## 🧾 Detected Equations:\n```latex\n{raw_latex}\n```\n"
186
  output += "---\n## ✏️ Parsed & Cleaned:\n"
187
  for eq in sympy_eqs:
@@ -194,6 +189,7 @@ def solve_system_of_equations(image):
194
  return output
195
  except Exception as e:
196
  return f"❌ Error: {str(e)}"
 
197
 
198
  # === UI ===
199
  with gr.Blocks() as demo:
 
53
  return 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):
89
  try:
90
  if isinstance(image, np.ndarray):
 
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
 
154
+ raw_latex = raw_latex.replace("\n", " ")
155
  equations = re.split(r'[;,]', raw_latex)
156
 
157
  cleaned_equations = []
 
177
  return f"❌ Not enough valid equations detected.\n\n```latex\n{raw_latex}\n```"
178
 
179
  sol = sp.solve(sympy_eqs, list(symbols), dict=True)
 
180
  output = f"## 🧾 Detected Equations:\n```latex\n{raw_latex}\n```\n"
181
  output += "---\n## ✏️ Parsed & Cleaned:\n"
182
  for eq in sympy_eqs:
 
189
  return output
190
  except Exception as e:
191
  return f"❌ Error: {str(e)}"
192
+
193
 
194
  # === UI ===
195
  with gr.Blocks() as demo: