Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ import re
|
|
| 8 |
def preprocess_handwritten_image(pil_img):
|
| 9 |
return pil_img.convert('RGB')
|
| 10 |
|
| 11 |
-
# Load Pix2Tex model (fine-tuned)
|
| 12 |
model = LatexOCR(weights='trained_model')
|
| 13 |
|
| 14 |
def clean_latex(latex):
|
|
@@ -33,34 +32,28 @@ def solve_polynomial(image):
|
|
| 33 |
latex_result = model(img)
|
| 34 |
cleaned_latex = clean_latex(latex_result)
|
| 35 |
expr = parse_latex(cleaned_latex)
|
| 36 |
-
|
| 37 |
-
output
|
| 38 |
-
output += f"## π§Ή Cleaned LaTeX Used\n```\n{cleaned_latex}\n```\n"
|
| 39 |
-
output += "---\n"
|
| 40 |
-
output += f"## π§ Parsed Expression\n\n$$ {sp.latex(expr)} $$\n"
|
| 41 |
-
output += "---\n"
|
| 42 |
|
| 43 |
if isinstance(expr, sp.Equality):
|
| 44 |
lhs = expr.lhs - expr.rhs
|
| 45 |
-
output += "## βοΈ Step 1: Standard Form
|
| 46 |
-
output += f"$$ {sp.latex(lhs)} = 0 $$\n"
|
| 47 |
-
output += "
|
| 48 |
-
output += "
|
| 49 |
-
|
| 50 |
-
output += f"$$ {sp.latex(factored)} = 0 $$\n"
|
| 51 |
-
output += "---\n"
|
| 52 |
-
output += "## β
Step 3: Solve for Roots\n"
|
| 53 |
roots = sp.solve(sp.Eq(lhs, 0), dict=True)
|
| 54 |
if roots:
|
| 55 |
output += "$$\n\\begin{aligned}\n"
|
| 56 |
for i, sol in enumerate(roots, 1):
|
| 57 |
for var, val in sol.items():
|
| 58 |
-
output += f"\\text{{Root {i}}}
|
| 59 |
-
output += "\\end{aligned}\n
|
| 60 |
else:
|
| 61 |
-
|
| 62 |
-
output += "
|
| 63 |
-
output += f"$$ {sp.latex(simplified)} $$"
|
| 64 |
|
| 65 |
return output
|
| 66 |
except Exception as e:
|
|
@@ -68,10 +61,10 @@ def solve_polynomial(image):
|
|
| 68 |
|
| 69 |
demo = gr.Interface(
|
| 70 |
fn=solve_polynomial,
|
| 71 |
-
inputs=gr.Image(type="pil", label="π· Upload Image
|
| 72 |
-
outputs=gr.Markdown(label="π
|
| 73 |
-
title="π§ Polynomial Solver
|
| 74 |
-
description="
|
| 75 |
allow_flagging="never"
|
| 76 |
)
|
| 77 |
|
|
|
|
| 8 |
def preprocess_handwritten_image(pil_img):
|
| 9 |
return pil_img.convert('RGB')
|
| 10 |
|
|
|
|
| 11 |
model = LatexOCR(weights='trained_model')
|
| 12 |
|
| 13 |
def clean_latex(latex):
|
|
|
|
| 32 |
latex_result = model(img)
|
| 33 |
cleaned_latex = clean_latex(latex_result)
|
| 34 |
expr = parse_latex(cleaned_latex)
|
| 35 |
+
|
| 36 |
+
output = f"## π Extracted LaTeX\n```\n{latex_result}\n```\n---\n"
|
| 37 |
+
output += f"## π§Ή Cleaned LaTeX Used\n```\n{cleaned_latex}\n```\n---\n"
|
| 38 |
+
output += f"## π§ Parsed Expression\n\n$$ {sp.latex(expr)} $$\n---\n"
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if isinstance(expr, sp.Equality):
|
| 41 |
lhs = expr.lhs - expr.rhs
|
| 42 |
+
output += "## βοΈ Step 1: Standard Form\n"
|
| 43 |
+
output += f"$$ {sp.latex(lhs)} = 0 $$\n---\n"
|
| 44 |
+
output += "## π§© Step 2: Factorized\n"
|
| 45 |
+
output += f"$$ {sp.latex(sp.factor(lhs))} = 0 $$\n---\n"
|
| 46 |
+
output += "## β
Step 3: Roots\n"
|
|
|
|
|
|
|
|
|
|
| 47 |
roots = sp.solve(sp.Eq(lhs, 0), dict=True)
|
| 48 |
if roots:
|
| 49 |
output += "$$\n\\begin{aligned}\n"
|
| 50 |
for i, sol in enumerate(roots, 1):
|
| 51 |
for var, val in sol.items():
|
| 52 |
+
output += f"\\text{{Root {i}}}: {var} &= {sp.latex(val)}\\\\\n"
|
| 53 |
+
output += "\\end{aligned}\n$$"
|
| 54 |
else:
|
| 55 |
+
output += "## β Simplified\n"
|
| 56 |
+
output += f"$$ {sp.latex(sp.simplify(expr))} $$"
|
|
|
|
| 57 |
|
| 58 |
return output
|
| 59 |
except Exception as e:
|
|
|
|
| 61 |
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=solve_polynomial,
|
| 64 |
+
inputs=gr.Image(type="pil", label="π· Upload Image"),
|
| 65 |
+
outputs=gr.Markdown(label="π Solution"),
|
| 66 |
+
title="π§ Handwritten Polynomial Solver",
|
| 67 |
+
description="Extract handwritten polynomials and solve step-by-step.",
|
| 68 |
allow_flagging="never"
|
| 69 |
)
|
| 70 |
|