MasteredUltraInstinct commited on
Commit
a84e0f7
Β·
verified Β·
1 Parent(s): 4e32738

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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):
15
+ latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
16
+ latex = latex.replace('{', '').replace('}', '')
17
+ latex = latex.strip().rstrip(',.')
18
+ latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
19
+ latex = latex.replace(r'\pi', '3.1416')
20
+ latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
21
+ latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
22
+ latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
23
+ latex = re.sub(r'(\d+)\s*i', r'\1*I', latex)
24
+ latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
25
+ latex = re.sub(r'\(([^()]+?)\)\s*([xX](\^\d+)?)', r'(\1)*\2', latex)
26
+ if '=' not in latex:
27
+ latex += '=0'
28
+ return latex
29
+
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
+ expr = parse_latex(cleaned_latex)
36
+ output = f"## πŸ“„ Extracted LaTeX\n```\n{latex_result}\n```\n"
37
+ output += "---\n"
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 of the Polynomial\n"
46
+ output += f"$$ {sp.latex(lhs)} = 0 $$\n"
47
+ output += "---\n"
48
+ output += "## 🧩 Step 2: Factor the Polynomial\n"
49
+ factored = sp.factor(lhs)
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}}}:\\quad {var} &= {sp.latex(val)} \\\\\n"
59
+ output += "\\end{aligned}\n$$\n"
60
+ else:
61
+ simplified = sp.simplify(expr)
62
+ output += "## βž• Simplified Expression\n"
63
+ output += f"$$ {sp.latex(simplified)} $$"
64
+
65
+ return output
66
+ except Exception as e:
67
+ return f"❌ **Error**: {str(e)}"
68
+
69
+ demo = gr.Interface(
70
+ fn=solve_polynomial,
71
+ inputs=gr.Image(type="pil", label="πŸ“· Upload Image of Polynomial"),
72
+ outputs=gr.Markdown(label="πŸ“‹ Step-by-step Solution"),
73
+ title="🧠 Polynomial Solver from Image",
74
+ description="Upload an image of a polynomial (typed or handwritten). The app will extract, solve, and explain it step-by-step.",
75
+ allow_flagging="never"
76
+ )
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()