Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,82 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
from PIL import Image
|
| 6 |
from pix2tex.cli import LatexOCR
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
latex = latex.strip().rstrip(',.')
|
| 20 |
-
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
| 21 |
-
latex = latex.replace(r'\pi', '3.1416')
|
| 22 |
-
latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
|
| 23 |
-
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 24 |
-
latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
|
| 25 |
-
latex = re.sub(r'(\d+)\s*i', r'\1*I', latex)
|
| 26 |
-
latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
|
| 27 |
-
latex = re.sub(r'\(([^()]+?)\)\s*([xX](\^\d+)?)', r'(\1)*\2', latex)
|
| 28 |
-
if '=' not in latex:
|
| 29 |
-
latex += '=0'
|
| 30 |
-
return latex
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
expr = parse_latex(cleaned_latex)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
demo = gr.Interface(
|
| 77 |
-
fn=lambda: "Training not completed yet. Please run `train.py` to generate `trained_model/`.",
|
| 78 |
-
inputs=[],
|
| 79 |
-
outputs="text",
|
| 80 |
-
title="β οΈ Model Not Ready",
|
| 81 |
-
description="Run training first to enable polynomial solving."
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import sympy as sp
|
| 5 |
+
from sympy.parsing.latex import parse_latex
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
+
try:
|
|
|
|
| 9 |
from pix2tex.cli import LatexOCR
|
| 10 |
+
if os.path.exists("trained_model"):
|
| 11 |
+
model = LatexOCR(weights="trained_model")
|
| 12 |
+
else:
|
| 13 |
+
model = None
|
| 14 |
+
except Exception as e:
|
| 15 |
+
model = None
|
| 16 |
|
| 17 |
+
def preprocess_handwritten_image(pil_img):
|
| 18 |
+
return pil_img.convert('RGB')
|
| 19 |
|
| 20 |
+
def clean_latex(latex):
|
| 21 |
+
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 22 |
+
latex = latex.replace('{', '').replace('}', '')
|
| 23 |
+
latex = latex.strip().rstrip(',.')
|
| 24 |
+
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
| 25 |
+
latex = latex.replace(r'\pi', '3.1416')
|
| 26 |
+
latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
|
| 27 |
+
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 28 |
+
latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
|
| 29 |
+
latex = re.sub(r'(\d+)\s*i', r'\1*I', latex)
|
| 30 |
+
latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
|
| 31 |
+
latex = re.sub(r'\(([^()]+?)\)\s*([xX](\^\d+)?)', r'(\1)*\2', latex)
|
| 32 |
+
if '=' not in latex:
|
| 33 |
+
latex += '=0'
|
| 34 |
+
return latex
|
| 35 |
|
| 36 |
+
def solve_polynomial(image):
|
| 37 |
+
if model is None:
|
| 38 |
+
return "β No trained model found. Please run training first to create `trained_model/`."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
img = preprocess_handwritten_image(image)
|
| 42 |
+
latex_result = model(img)
|
| 43 |
+
cleaned_latex = clean_latex(latex_result)
|
| 44 |
+
expr = parse_latex(cleaned_latex)
|
|
|
|
| 45 |
|
| 46 |
+
output = f"## π Extracted LaTeX\n```\n{latex_result}\n```\n---\n"
|
| 47 |
+
output += f"## π§Ή Cleaned LaTeX Used\n```\n{cleaned_latex}\n```\n---\n"
|
| 48 |
+
output += f"## π§ Parsed Expression\n\n$$ {sp.latex(expr)} $$\n---\n"
|
| 49 |
|
| 50 |
+
if isinstance(expr, sp.Equality):
|
| 51 |
+
lhs = expr.lhs - expr.rhs
|
| 52 |
+
output += "## βοΈ Step 1: Standard Form\n"
|
| 53 |
+
output += f"$$ {sp.latex(lhs)} = 0 $$\n---\n"
|
| 54 |
+
output += "## π§© Step 2: Factorized\n"
|
| 55 |
+
output += f"$$ {sp.latex(sp.factor(lhs))} = 0 $$\n---\n"
|
| 56 |
+
output += "## β
Step 3: Roots\n"
|
| 57 |
+
roots = sp.solve(sp.Eq(lhs, 0), dict=True)
|
| 58 |
+
if roots:
|
| 59 |
+
output += "$$\n\\begin{aligned}\n"
|
| 60 |
+
for i, sol in enumerate(roots, 1):
|
| 61 |
+
for var, val in sol.items():
|
| 62 |
+
output += f"\\text{{Root {i}}}: {var} &= {sp.latex(val)}\\\\\n"
|
| 63 |
+
output += "\\end{aligned}\n$$"
|
| 64 |
+
else:
|
| 65 |
+
output += "## β Simplified\n"
|
| 66 |
+
output += f"$$ {sp.latex(sp.simplify(expr))} $$"
|
| 67 |
|
| 68 |
+
return output
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"β **Error**: {str(e)}"
|
| 71 |
|
| 72 |
+
demo = gr.Interface(
|
| 73 |
+
fn=solve_polynomial,
|
| 74 |
+
inputs=gr.Image(type="pil", label="π· Upload Image"),
|
| 75 |
+
outputs=gr.Markdown(label="π Solution"),
|
| 76 |
+
title="π§ Handwritten Polynomial Solver",
|
| 77 |
+
description="Upload a handwritten polynomial. The app extracts, solves, and explains it step-by-step.",
|
| 78 |
+
allow_flagging="never"
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|