Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,39 +4,48 @@ from pix2tex.cli import LatexOCR
|
|
| 4 |
import sympy as sp
|
| 5 |
from sympy.parsing.latex import parse_latex
|
| 6 |
import re
|
| 7 |
-
import cv2
|
| 8 |
-
import numpy as np
|
| 9 |
|
| 10 |
# Preprocessing for handwritten image
|
| 11 |
def preprocess_handwritten_image(pil_img):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
denoised = cv2.medianBlur(binary, 3)
|
| 15 |
-
return Image.fromarray(255 - denoised) # Text black on white
|
| 16 |
|
| 17 |
# Load Pix2Tex model (once)
|
| 18 |
model = LatexOCR()
|
| 19 |
|
| 20 |
# Clean LaTeX output
|
| 21 |
def clean_latex(latex):
|
| 22 |
-
# Replace mathcal X with x
|
| 23 |
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 24 |
|
| 25 |
-
# Remove
|
| 26 |
latex = latex.replace('{', '').replace('}', '')
|
| 27 |
latex = latex.strip().rstrip(',.')
|
| 28 |
|
| 29 |
-
# Replace \pi
|
|
|
|
| 30 |
latex = latex.replace(r'\pi', '3.1416')
|
| 31 |
-
latex = latex.replace('Ο', '3.1416')
|
| 32 |
|
| 33 |
-
# Replace
|
|
|
|
| 34 |
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 35 |
|
| 36 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if '=' not in latex:
|
| 38 |
latex += '=0'
|
| 39 |
-
|
| 40 |
return latex
|
| 41 |
|
| 42 |
|
|
@@ -77,8 +86,6 @@ def solve_polynomial(image):
|
|
| 77 |
output += f"\\text{{Root {i}}}:\\quad {var} &= {sp.latex(val)} \\\\\n"
|
| 78 |
output += "\\end{aligned}\n$$\n"
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
else:
|
| 83 |
simplified = sp.simplify(expr)
|
| 84 |
output += "## β Simplified Expression\n"
|
|
@@ -101,4 +108,4 @@ demo = gr.Interface(
|
|
| 101 |
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
-
demo.launch()
|
|
|
|
| 4 |
import sympy as sp
|
| 5 |
from sympy.parsing.latex import parse_latex
|
| 6 |
import re
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Preprocessing for handwritten image
|
| 9 |
def preprocess_handwritten_image(pil_img):
|
| 10 |
+
return pil_img.convert('RGB') # Minimal processing, just convert to RGB
|
| 11 |
+
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Load Pix2Tex model (once)
|
| 14 |
model = LatexOCR()
|
| 15 |
|
| 16 |
# Clean LaTeX output
|
| 17 |
def clean_latex(latex):
|
| 18 |
+
# Replace \mathcal{X} or \cal X with 'x'
|
| 19 |
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 20 |
|
| 21 |
+
# Remove curly braces
|
| 22 |
latex = latex.replace('{', '').replace('}', '')
|
| 23 |
latex = latex.strip().rstrip(',.')
|
| 24 |
|
| 25 |
+
# Replace coefficients like 5\pi with (5*3.1416)
|
| 26 |
+
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
| 27 |
latex = latex.replace(r'\pi', '3.1416')
|
|
|
|
| 28 |
|
| 29 |
+
# Replace coefficients like 5e with (5*2.7183)
|
| 30 |
+
latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
|
| 31 |
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 32 |
|
| 33 |
+
# Insert * between number and variable (e.g., 45x β 45*x)
|
| 34 |
+
latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
|
| 35 |
+
|
| 36 |
+
# Replace number followed by i with number*I
|
| 37 |
+
latex = re.sub(r'(\d+)\s*i', r'\1*I', latex)
|
| 38 |
+
|
| 39 |
+
# Replace standalone i with I
|
| 40 |
+
latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
|
| 41 |
+
|
| 42 |
+
# Wrap complex coefficients with variables: (a+bI)x^n β (a+b*I)*x^n
|
| 43 |
+
latex = re.sub(r'\(([^()]+?)\)\s*([xX](\^\d+)?)', r'(\1)*\2', latex)
|
| 44 |
+
|
| 45 |
+
# Append '=0' if not already present
|
| 46 |
if '=' not in latex:
|
| 47 |
latex += '=0'
|
| 48 |
+
|
| 49 |
return latex
|
| 50 |
|
| 51 |
|
|
|
|
| 86 |
output += f"\\text{{Root {i}}}:\\quad {var} &= {sp.latex(val)} \\\\\n"
|
| 87 |
output += "\\end{aligned}\n$$\n"
|
| 88 |
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
simplified = sp.simplify(expr)
|
| 91 |
output += "## β Simplified Expression\n"
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
+
demo.launch()
|