Update app.py
Browse files
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 |
-
#
|
| 63 |
latex = re.sub(r'\\times', '*', latex)
|
| 64 |
latex = re.sub(r'\\cdot', '*', latex)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
latex = re.sub(r'\\(text|mathbf|mathrm|mathit|textbf|mathcal|cal
|
|
|
|
| 68 |
|
| 69 |
-
#
|
| 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 |
-
#
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 88 |
-
latex = re.sub(r'\
|
| 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
|
| 151 |
if r"\begin{array" in raw_latex:
|
| 152 |
-
|
| 153 |
-
if
|
| 154 |
-
raw_latex =
|
| 155 |
-
raw_latex = raw_latex.replace('
|
| 156 |
-
raw_latex =
|
| 157 |
|
| 158 |
-
raw_latex = raw_latex.replace(
|
| 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:
|