Update app.py
Browse files
app.py
CHANGED
|
@@ -53,45 +53,42 @@ def clean_latex(latex):
|
|
| 53 |
return latex
|
| 54 |
|
| 55 |
def clean_latex2(latex):
|
| 56 |
-
#
|
| 57 |
latex = latex.replace('\\ ', '')
|
| 58 |
latex = latex.replace('\\\\', '\n')
|
| 59 |
latex = latex.replace('~', '')
|
| 60 |
latex = latex.replace('β', '-').replace('β', '-')
|
| 61 |
-
latex = latex.replace('β', '-')
|
| 62 |
|
| 63 |
-
#
|
| 64 |
latex = re.sub(r'\\times', '*', latex)
|
| 65 |
latex = re.sub(r'\\cdot', '*', latex)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
latex = re.sub(r'
|
| 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
|
| 74 |
latex = re.sub(r'\\[a-zA-Z]+', '', latex)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
latex = latex.replace('{', '').replace('}', '').replace('&', '')
|
| 78 |
|
| 79 |
-
#
|
| 80 |
latex = re.sub(r'[;,]', '\n', latex)
|
| 81 |
|
| 82 |
-
#
|
|
|
|
| 83 |
latex = re.sub(r'=+', '=', latex)
|
| 84 |
|
| 85 |
-
#
|
| 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 |
-
|
|
|
|
| 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 |
-
|
| 155 |
-
if
|
| 156 |
-
raw_latex =
|
| 157 |
-
raw_latex = raw_latex.replace(
|
| 158 |
-
raw_latex =
|
| 159 |
|
| 160 |
-
raw_latex = raw_latex.replace(
|
| 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 |
-
|
| 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"):
|