Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import re
|
|
| 9 |
def preprocess_handwritten_image(pil_img):
|
| 10 |
return pil_img.convert('RGB') # Minimal processing, just convert to RGB
|
| 11 |
|
|
|
|
| 12 |
# Load Pix2Tex model (once)
|
| 13 |
model = LatexOCR()
|
| 14 |
|
|
@@ -41,28 +42,12 @@ def clean_latex(latex):
|
|
| 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)
|
| 46 |
-
latex = latex.replace('\\pm', '').replace('!', '')
|
| 47 |
-
latex = re.sub(r'[^\w\s\^\+\-\*/=xX\(\)\.\*]', '', latex)
|
| 48 |
-
|
| 49 |
# Append '=0' if not already present
|
| 50 |
if '=' not in latex:
|
| 51 |
latex += '=0'
|
| 52 |
|
| 53 |
return latex
|
| 54 |
|
| 55 |
-
# Fallback parser if LaTeX parsing fails
|
| 56 |
-
def fallback_parse(cleaned_latex):
|
| 57 |
-
try:
|
| 58 |
-
if '=' in cleaned_latex:
|
| 59 |
-
cleaned_latex = cleaned_latex.split('=')[0]
|
| 60 |
-
cleaned_latex = cleaned_latex.replace('^', '**')
|
| 61 |
-
x = sp.symbols('x')
|
| 62 |
-
expr = eval(cleaned_latex, {"x": x, "I": sp.I, "pi": sp.pi})
|
| 63 |
-
return sp.Eq(expr, 0)
|
| 64 |
-
except Exception:
|
| 65 |
-
return None
|
| 66 |
|
| 67 |
# Main function
|
| 68 |
def solve_polynomial(image):
|
|
@@ -71,20 +56,18 @@ def solve_polynomial(image):
|
|
| 71 |
latex_result = model(img)
|
| 72 |
cleaned_latex = clean_latex(latex_result)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
except Exception:
|
| 77 |
-
expr = None
|
| 78 |
-
|
| 79 |
-
if not expr:
|
| 80 |
-
expr = fallback_parse(cleaned_latex)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
return f"β Could not parse expression from cleaned LaTeX:\n```\n{cleaned_latex}\n```"
|
| 84 |
|
| 85 |
-
|
| 86 |
output += "---\n"
|
| 87 |
-
output += f"## π§Ή Cleaned LaTeX Used\n
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
output += "---\n"
|
| 89 |
output += f"## π§ Parsed Expression\n\n$$ {sp.latex(expr)} $$\n"
|
| 90 |
output += "---\n"
|
|
@@ -121,6 +104,7 @@ def solve_polynomial(image):
|
|
| 121 |
except Exception as e:
|
| 122 |
return f"β **Error**: {str(e)}"
|
| 123 |
|
|
|
|
| 124 |
# Gradio UI
|
| 125 |
demo = gr.Interface(
|
| 126 |
fn=solve_polynomial,
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
| 52 |
# Main function
|
| 53 |
def solve_polynomial(image):
|
|
|
|
| 56 |
latex_result = model(img)
|
| 57 |
cleaned_latex = clean_latex(latex_result)
|
| 58 |
|
| 59 |
+
expr = parse_latex(cleaned_latex)
|
| 60 |
+
output = f"## π Extracted LaTeX\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
\n{latex_result}\n
|
|
|
|
| 63 |
|
| 64 |
+
\n"
|
| 65 |
output += "---\n"
|
| 66 |
+
output += f"## π§Ή Cleaned LaTeX Used\n
|
| 67 |
+
|
| 68 |
+
\n{cleaned_latex}\n
|
| 69 |
+
|
| 70 |
+
\n"
|
| 71 |
output += "---\n"
|
| 72 |
output += f"## π§ Parsed Expression\n\n$$ {sp.latex(expr)} $$\n"
|
| 73 |
output += "---\n"
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
return f"β **Error**: {str(e)}"
|
| 106 |
|
| 107 |
+
|
| 108 |
# Gradio UI
|
| 109 |
demo = gr.Interface(
|
| 110 |
fn=solve_polynomial,
|