Update app.py
Browse files
app.py
CHANGED
|
@@ -15,31 +15,31 @@ model = LatexOCR()
|
|
| 15 |
# Clean LaTeX output
|
| 16 |
def clean_latex(latex):
|
| 17 |
# Replace \mathcal{X} or \cal X with 'x'
|
| 18 |
-
latex = re.sub(r'\\(cal|mathcal)
|
| 19 |
|
| 20 |
# Remove curly braces
|
| 21 |
latex = latex.replace('{', '').replace('}', '')
|
| 22 |
latex = latex.strip().rstrip(',.')
|
| 23 |
-
|
| 24 |
# Replace coefficients like 5\pi with (5*3.1416)
|
| 25 |
-
latex = re.sub(r'(\d+)
|
| 26 |
latex = latex.replace(r'\pi', '3.1416')
|
| 27 |
|
| 28 |
# Replace coefficients like 5e with (5*2.7183)
|
| 29 |
-
latex = re.sub(r'(\d+)
|
| 30 |
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 31 |
|
| 32 |
# Insert * between number and variable (e.g., 45x β 45*x)
|
| 33 |
latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
|
| 34 |
|
| 35 |
# Replace number followed by i with number*I
|
| 36 |
-
latex = re.sub(r'(\d+)
|
| 37 |
|
| 38 |
# Replace standalone i with I
|
| 39 |
latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
|
| 40 |
|
| 41 |
# Wrap complex coefficients with variables: (a+bI)x^n β (a+b*I)*x^n
|
| 42 |
-
latex = re.sub(r'
|
| 43 |
|
| 44 |
# Fix common OCR mistakes
|
| 45 |
latex = re.sub(r'\bL(\d)', r'1\1', latex)
|
|
@@ -80,14 +80,11 @@ def solve_polynomial(image):
|
|
| 80 |
expr = fallback_parse(cleaned_latex)
|
| 81 |
|
| 82 |
if not expr:
|
| 83 |
-
return f"β Could not parse expression from cleaned LaTeX:\n```
|
| 84 |
-
{cleaned_latex}\n```"
|
| 85 |
|
| 86 |
-
output = f"## π Extracted LaTeX\n
|
| 87 |
-
{latex_result}\n```\n"
|
| 88 |
output += "---\n"
|
| 89 |
-
output += f"## π§Ή Cleaned LaTeX Used\n
|
| 90 |
-
{cleaned_latex}\n```\n"
|
| 91 |
output += "---\n"
|
| 92 |
output += f"## π§ Parsed Expression\n\n$$ {sp.latex(expr)} $$\n"
|
| 93 |
output += "---\n"
|
|
@@ -111,7 +108,7 @@ def solve_polynomial(image):
|
|
| 111 |
output += "$$\n\\begin{aligned}\n"
|
| 112 |
for i, sol in enumerate(roots, 1):
|
| 113 |
for var, val in sol.items():
|
| 114 |
-
output += f"\\text{{Root {i}}}:\\quad {var} &= {sp.latex(val)}
|
| 115 |
output += "\\end{aligned}\n$$\n"
|
| 116 |
|
| 117 |
else:
|
|
@@ -136,4 +133,3 @@ demo = gr.Interface(
|
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
demo.launch()
|
| 139 |
-
|
|
|
|
| 15 |
# Clean LaTeX output
|
| 16 |
def clean_latex(latex):
|
| 17 |
# Replace \mathcal{X} or \cal X with 'x'
|
| 18 |
+
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 19 |
|
| 20 |
# Remove curly braces
|
| 21 |
latex = latex.replace('{', '').replace('}', '')
|
| 22 |
latex = latex.strip().rstrip(',.')
|
| 23 |
+
|
| 24 |
# Replace coefficients like 5\pi with (5*3.1416)
|
| 25 |
+
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
| 26 |
latex = latex.replace(r'\pi', '3.1416')
|
| 27 |
|
| 28 |
# Replace coefficients like 5e with (5*2.7183)
|
| 29 |
+
latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
|
| 30 |
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 31 |
|
| 32 |
# Insert * between number and variable (e.g., 45x β 45*x)
|
| 33 |
latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
|
| 34 |
|
| 35 |
# Replace number followed by i with number*I
|
| 36 |
+
latex = re.sub(r'(\d+)\s*i', r'\1*I', latex)
|
| 37 |
|
| 38 |
# Replace standalone i with I
|
| 39 |
latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
|
| 40 |
|
| 41 |
# Wrap complex coefficients with variables: (a+bI)x^n β (a+b*I)*x^n
|
| 42 |
+
latex = re.sub(r'\(([^()]+?)\)\s*([xX](\^\d+)?)', r'(\1)*\2', latex)
|
| 43 |
|
| 44 |
# Fix common OCR mistakes
|
| 45 |
latex = re.sub(r'\bL(\d)', r'1\1', latex)
|
|
|
|
| 80 |
expr = fallback_parse(cleaned_latex)
|
| 81 |
|
| 82 |
if not expr:
|
| 83 |
+
return f"β Could not parse expression from cleaned LaTeX:\n```\n{cleaned_latex}\n```"
|
|
|
|
| 84 |
|
| 85 |
+
output = f"## π Extracted LaTeX\n```\n{latex_result}\n```\n"
|
|
|
|
| 86 |
output += "---\n"
|
| 87 |
+
output += f"## π§Ή Cleaned LaTeX Used\n```\n{cleaned_latex}\n```\n"
|
|
|
|
| 88 |
output += "---\n"
|
| 89 |
output += f"## π§ Parsed Expression\n\n$$ {sp.latex(expr)} $$\n"
|
| 90 |
output += "---\n"
|
|
|
|
| 108 |
output += "$$\n\\begin{aligned}\n"
|
| 109 |
for i, sol in enumerate(roots, 1):
|
| 110 |
for var, val in sol.items():
|
| 111 |
+
output += f"\\text{{Root {i}}}:\\quad {var} &= {sp.latex(val)} \\\\\n"
|
| 112 |
output += "\\end{aligned}\n$$\n"
|
| 113 |
|
| 114 |
else:
|
|
|
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
demo.launch()
|
|
|