MasteredUltraInstinct commited on
Commit
de2c13c
Β·
verified Β·
1 Parent(s): dbedf23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -1,46 +1,55 @@
1
  import gradio as gr
2
- from PIL import Image, ImageOps, ImageFilter
3
  from pix2tex.cli import LatexOCR
4
  import sympy as sp
5
  from sympy.parsing.latex import parse_latex
6
  import re
7
 
8
- # Preprocessing for printed image
9
- def preprocess_printed_image(pil_img):
10
- img = pil_img.convert('L') # grayscale
11
- img = ImageOps.autocontrast(img)
12
- img = img.filter(ImageFilter.MedianFilter(size=3)) # reduce noise
13
- img = ImageOps.invert(img) # black on white
14
- img = img.resize((512, 128), Image.LANCZOS) # Resize to match model input
15
- return img
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 = re.sub(r'\\(cal|mathcal)\s*Y', 'y', latex)
 
24
  latex = re.sub(r'\\(cal|mathcal)\s*Z', 'z', latex)
25
 
 
26
  latex = latex.replace('{', '').replace('}', '')
27
  latex = latex.strip().rstrip(',.')
28
 
 
29
  latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
30
  latex = latex.replace(r'\pi', '3.1416')
31
 
32
- # FIXED: prevent replacing "e" in "e^x" incorrectly
33
- latex = re.sub(r'(?<![a-zA-Z0-9])(\d+)\s*e(?![a-zA-Z0-9])', r'(\1*2.7183)', latex)
34
  latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
35
 
 
36
  latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
 
 
37
  latex = re.sub(r'(\d+)\s*i', r'\1*I', latex)
 
 
38
  latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
 
 
39
  latex = re.sub(r'\(([^()]+?)\)\s*([a-zA-Z](\^\d+)?)', r'(\1)*\2', latex)
40
 
 
41
  latex = latex.replace(r'\cdot', '*')
42
- latex = latex.replace('βˆ’', '-')
43
 
 
44
  if '=' not in latex:
45
  latex += '=0'
46
 
@@ -49,9 +58,10 @@ def clean_latex(latex):
49
  # Main function
50
  def solve_polynomial(image):
51
  try:
52
- img = preprocess_printed_image(image)
53
  latex_result = model(img)
54
 
 
55
  if not latex_result or len(latex_result.strip()) < 2:
56
  return "❌ Could not extract valid LaTeX from image."
57
 
@@ -70,17 +80,23 @@ def solve_polynomial(image):
70
  "```\n"
71
  )
72
 
 
 
 
73
  output = (
74
  f"## πŸ“„ Extracted LaTeX\n\n"
75
  f"```latex\n{latex_result}\n```\n"
76
- "---\n"
77
  f"## 🧹 Cleaned LaTeX Used\n\n"
78
  f"```latex\n{cleaned_latex}\n```\n"
79
- "---\n"
80
  f"## 🧠 Parsed Expression\n\n$$ {sp.latex(expr)} $$\n"
81
- "---\n"
82
  )
83
 
 
 
 
84
  if isinstance(expr, sp.Equality):
85
  lhs = expr.lhs - expr.rhs
86
 
@@ -125,3 +141,4 @@ demo = gr.Interface(
125
 
126
  if __name__ == "__main__":
127
  demo.launch()
 
 
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
+ # Preprocessing for handwritten image
9
+ def preprocess_handwritten_image(pil_img):
10
+ return pil_img.convert('RGB') # Minimal processing, just convert to RGB
 
 
 
 
 
11
 
12
  # Load Pix2Tex model (once)
13
  model = LatexOCR()
14
 
15
  # Clean LaTeX output
16
  def clean_latex(latex):
17
+ # Replace \mathcal{X} or \cal X with 'x'
18
  latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
19
+ # Replace \mathcal{Y} or \cal Y with 'y'
20
  latex = re.sub(r'\\(cal|mathcal)\s*Y', 'y', latex)
21
+ # Replace \mathcal{Z} or \cal Z with 'z'
22
  latex = re.sub(r'\\(cal|mathcal)\s*Z', 'z', latex)
23
 
24
+ # Remove curly braces
25
  latex = latex.replace('{', '').replace('}', '')
26
  latex = latex.strip().rstrip(',.')
27
 
28
+ # Replace coefficients like 5\pi with (5*3.1416)
29
  latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
30
  latex = latex.replace(r'\pi', '3.1416')
31
 
32
+ # Replace coefficients like 5e with (5*2.7183)
33
+ latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
34
  latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
35
 
36
+ # Insert * between number and variable (e.g., 45x β†’ 45*x)
37
  latex = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', latex)
38
+
39
+ # Replace number followed by i with number*I
40
  latex = re.sub(r'(\d+)\s*i', r'\1*I', latex)
41
+
42
+ # Replace standalone i with I
43
  latex = re.sub(r'(?<![a-zA-Z0-9])i(?![a-zA-Z0-9])', 'I', latex)
44
+
45
+ # Wrap complex coefficients with variables: (a+bI)x^n β†’ (a+b*I)*x^n
46
  latex = re.sub(r'\(([^()]+?)\)\s*([a-zA-Z](\^\d+)?)', r'(\1)*\2', latex)
47
 
48
+ # Additional minimal cleanup
49
  latex = latex.replace(r'\cdot', '*')
50
+ latex = latex.replace('βˆ’', '-') # Unicode minus to ASCII
51
 
52
+ # Append '=0' if not already present
53
  if '=' not in latex:
54
  latex += '=0'
55
 
 
58
  # Main function
59
  def solve_polynomial(image):
60
  try:
61
+ img = preprocess_handwritten_image(image)
62
  latex_result = model(img)
63
 
64
+ # Check for empty/invalid OCR output
65
  if not latex_result or len(latex_result.strip()) < 2:
66
  return "❌ Could not extract valid LaTeX from image."
67
 
 
80
  "```\n"
81
  )
82
 
83
+
84
+
85
+
86
  output = (
87
  f"## πŸ“„ Extracted LaTeX\n\n"
88
  f"```latex\n{latex_result}\n```\n"
89
+ "\n---\n"
90
  f"## 🧹 Cleaned LaTeX Used\n\n"
91
  f"```latex\n{cleaned_latex}\n```\n"
92
+ "\n---\n"
93
  f"## 🧠 Parsed Expression\n\n$$ {sp.latex(expr)} $$\n"
94
+ "\n---\n"
95
  )
96
 
97
+
98
+
99
+
100
  if isinstance(expr, sp.Equality):
101
  lhs = expr.lhs - expr.rhs
102
 
 
141
 
142
  if __name__ == "__main__":
143
  demo.launch()
144
+ #gg