Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
from PIL import Image
|
| 3 |
from pix2tex.cli import LatexOCR
|
| 4 |
import sympy as sp
|
|
@@ -7,20 +7,17 @@ import re
|
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
|
|
|
|
| 10 |
def preprocess_handwritten_image(pil_img):
|
| 11 |
-
img = np.array(pil_img.convert('L')) #
|
| 12 |
_, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
|
| 13 |
denoised = cv2.medianBlur(binary, 3)
|
| 14 |
-
return Image.fromarray(255 - denoised) #
|
| 15 |
|
| 16 |
-
# Load
|
| 17 |
-
|
| 18 |
-
def load_model():
|
| 19 |
-
return LatexOCR()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Function to clean the LaTeX output
|
| 24 |
def clean_latex(latex):
|
| 25 |
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 26 |
latex = latex.replace('{', '').replace('}', '')
|
|
@@ -29,56 +26,52 @@ def clean_latex(latex):
|
|
| 29 |
latex += '=0'
|
| 30 |
return latex
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"])
|
| 37 |
-
|
| 38 |
-
if uploaded_file:
|
| 39 |
-
# Display the uploaded image
|
| 40 |
-
raw_img = Image.open(uploaded_file)
|
| 41 |
-
img = preprocess_handwritten_image(raw_img)
|
| 42 |
-
st.image(img, caption="Preprocessed Image", use_column_width=True)
|
| 43 |
-
|
| 44 |
-
with st.spinner("๐ Extracting LaTeX from image..."):
|
| 45 |
latex_result = model(img)
|
| 46 |
cleaned_latex = clean_latex(latex_result)
|
| 47 |
|
| 48 |
-
st.subheader("๐ Extracted LaTeX")
|
| 49 |
-
st.code(latex_result, language="latex")
|
| 50 |
-
|
| 51 |
-
st.subheader("๐งน Cleaned LaTeX Used")
|
| 52 |
-
st.code(cleaned_latex, language="latex")
|
| 53 |
-
|
| 54 |
-
# Try to parse and solve
|
| 55 |
-
try:
|
| 56 |
expr = parse_latex(cleaned_latex)
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
if isinstance(expr, sp.Equality):
|
| 61 |
-
lhs = expr.lhs - expr.rhs
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
st.markdown("### โ๏ธ Step 1: Convert to Polynomial Equation")
|
| 65 |
-
st.latex(f"{sp.latex(lhs)} = 0")
|
| 66 |
|
| 67 |
-
|
| 68 |
factored = sp.factor(lhs)
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
roots = sp.solve(sp.Eq(lhs, 0), dict=True)
|
| 73 |
-
|
| 74 |
-
for i, sol in enumerate(clean_roots, 1):
|
| 75 |
for var, val in sol.items():
|
| 76 |
-
|
| 77 |
-
|
| 78 |
else:
|
| 79 |
simplified = sp.simplify(expr)
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
from pix2tex.cli import LatexOCR
|
| 4 |
import sympy as sp
|
|
|
|
| 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('}', '')
|
|
|
|
| 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()
|
| 77 |
+
|