MasteredUltraInstinct commited on
Commit
c92fbb4
Β·
verified Β·
1 Parent(s): ae72fa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -24
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
- 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:
@@ -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 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
 
 
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