Spaces:
Runtime error
Runtime error
Create proj
Browse files
proj
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
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 |
+
img = np.array(pil_img.convert('L')) # Grayscale
|
| 13 |
+
_, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
|
| 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 |
+
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 23 |
+
latex = latex.replace('{', '').replace('}', '')
|
| 24 |
+
latex = latex.strip().rstrip(',.')
|
| 25 |
+
if '=' not in latex:
|
| 26 |
+
latex += '=0'
|
| 27 |
+
return latex
|
| 28 |
+
|
| 29 |
+
# Main function
|
| 30 |
+
def solve_polynomial(image):
|
| 31 |
+
try:
|
| 32 |
+
img = preprocess_handwritten_image(image)
|
| 33 |
+
latex_result = model(img)
|
| 34 |
+
cleaned_latex = clean_latex(latex_result)
|
| 35 |
+
|
| 36 |
+
expr = parse_latex(cleaned_latex)
|
| 37 |
+
output = f"### π Extracted LaTeX\n`{latex_result}`\n\n"
|
| 38 |
+
output += f"### π§Ή Cleaned LaTeX Used\n`{cleaned_latex}`\n\n"
|
| 39 |
+
output += f"### π§ Parsed Expression\n${sp.latex(expr)}$\n\n"
|
| 40 |
+
|
| 41 |
+
if isinstance(expr, sp.Equality):
|
| 42 |
+
lhs = expr.lhs - expr.rhs
|
| 43 |
+
output += "### βοΈ Step 1: Convert to Polynomial Equation\n"
|
| 44 |
+
output += f"${sp.latex(lhs)} = 0$\n\n"
|
| 45 |
+
|
| 46 |
+
output += "### π§© Step 2: Factor the Polynomial\n"
|
| 47 |
+
factored = sp.factor(lhs)
|
| 48 |
+
output += f"${sp.latex(factored)} = 0$\n\n"
|
| 49 |
+
|
| 50 |
+
output += "### β
Step 3: Solve for Roots\n"
|
| 51 |
+
roots = sp.solve(sp.Eq(lhs, 0), dict=True)
|
| 52 |
+
for i, sol in enumerate(roots, 1):
|
| 53 |
+
for var, val in sol.items():
|
| 54 |
+
output += f"**Root {i}:** ${var} = {sp.latex(val)}$\n\n"
|
| 55 |
+
else:
|
| 56 |
+
simplified = sp.simplify(expr)
|
| 57 |
+
output += "### β Simplified Expression\n"
|
| 58 |
+
output += f"${sp.latex(simplified)}$"
|
| 59 |
+
|
| 60 |
+
return output
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"β Error: {str(e)}"
|
| 64 |
+
|
| 65 |
+
# Gradio UI
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=solve_polynomial,
|
| 68 |
+
inputs=gr.Image(type="pil", label="π· Upload Image of Polynomial"),
|
| 69 |
+
outputs=gr.Markdown(label="π Step-by-step Solution"),
|
| 70 |
+
title="π§ Polynomial Solver from Image",
|
| 71 |
+
description="Upload an image of a polynomial (typed or handwritten). The app will extract, solve, and explain it step-by-step.",
|
| 72 |
+
allow_flagging="never"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
demo.launch()
|