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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -53,45 +53,42 @@ def clean_latex(latex):
53
  return latex
54
 
55
  def clean_latex2(latex):
56
- # Remove unnecessary LaTeX symbols and spaces
57
  latex = latex.replace('\\ ', '')
58
  latex = latex.replace('\\\\', '\n')
59
  latex = latex.replace('~', '')
60
  latex = latex.replace('βˆ’', '-').replace('–', '-')
61
- latex = latex.replace('–', '-')
62
 
63
- # Convert math symbols
64
  latex = re.sub(r'\\times', '*', latex)
65
  latex = re.sub(r'\\cdot', '*', latex)
66
 
67
- # Fix multiple dashes (e.g., -- becomes -)
68
- latex = re.sub(r'-{2,}', '-', latex)
69
-
70
- # Remove commands like \mathbf{}, \mathcal{}, etc.
71
- latex = re.sub(r'\\(text|mathbf|mathrm|mathit|textbf|mathcal|cal)\s*\{([^{}]+)\}', r'\2', latex)
72
 
73
- # Remove leftover LaTeX commands
74
  latex = re.sub(r'\\[a-zA-Z]+', '', latex)
75
 
76
- # Remove curly braces and ampersands
77
  latex = latex.replace('{', '').replace('}', '').replace('&', '')
78
 
79
- # Replace comma/semicolon with newlines to separate equations
80
  latex = re.sub(r'[;,]', '\n', latex)
81
 
82
- # Fix multiple equals (== becomes =)
 
83
  latex = re.sub(r'=+', '=', latex)
84
 
85
- # Normalize space
86
- latex = re.sub(r'\s+', ' ', latex).strip()
87
-
88
- # Symbol replacements (OCR common confusions)
89
  latex = re.sub(r'\bchi\b|\bxi\b|\balpha\b', 'x', latex)
90
  latex = re.sub(r'\bbeta\b', 'b', latex)
91
  latex = re.sub(r'\bgamma\b', 'y', latex)
92
 
93
- return latex
 
94
 
 
 
95
  def solve_polynomial(image):
96
  try:
97
  if isinstance(image, np.ndarray):
@@ -150,14 +147,15 @@ def solve_system_of_equations(image):
150
  if not raw_latex or len(raw_latex.strip()) < 3:
151
  return "❌ Could not extract valid LaTeX."
152
 
 
153
  if r"\begin{array" in raw_latex:
154
- lines = re.findall(r'\\begin{array}{[^}]+}(.+?)\\end{array}', raw_latex, re.DOTALL)
155
- if lines:
156
- raw_latex = lines[0]
157
- raw_latex = raw_latex.replace(r'\\', '\n')
158
- raw_latex = re.sub(r'&', '', raw_latex)
159
 
160
- raw_latex = raw_latex.replace("\n", " ")
161
  equations = re.split(r'[;,]', raw_latex)
162
 
163
  cleaned_equations = []
@@ -183,20 +181,20 @@ def solve_system_of_equations(image):
183
  return f"❌ Not enough valid equations detected.\n\n```latex\n{raw_latex}\n```"
184
 
185
  sol = sp.solve(sympy_eqs, list(symbols), dict=True)
 
186
  output = f"## 🧾 Detected Equations:\n```latex\n{raw_latex}\n```\n"
187
  output += "---\n## ✏️ Parsed & Cleaned:\n"
188
  for eq in sympy_eqs:
189
  output += f"$$ {sp.latex(eq)} $$\n"
190
  output += "---\n## βœ… Solution:\n"
191
  if sol:
192
- renamed = {str(k).replace('times', 'x'): v for k, v in sol[0].items()} # βœ… fix
193
- output += "$$" + sp.latex(renamed) + "$$"
194
  else:
195
  output += "❌ No solution found or system may be inconsistent."
196
  return output
197
  except Exception as e:
198
  return f"❌ Error: {str(e)}"
199
-
200
  # === UI ===
201
  with gr.Blocks() as demo:
202
  with gr.Tab("πŸ–ΌοΈ Parse from Image"):
 
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
  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
  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:
188
  output += f"$$ {sp.latex(eq)} $$\n"
189
  output += "---\n## βœ… Solution:\n"
190
  if sol:
191
+ output += "$$" + sp.latex(sol[0]) + "$$"
 
192
  else:
193
  output += "❌ No solution found or system may be inconsistent."
194
  return output
195
  except Exception as e:
196
  return f"❌ Error: {str(e)}"
197
+
198
  # === UI ===
199
  with gr.Blocks() as demo:
200
  with gr.Tab("πŸ–ΌοΈ Parse from Image"):