Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,10 @@ import re
|
|
| 7 |
|
| 8 |
# Preprocessing for handwritten image
|
| 9 |
def preprocess_handwritten_image(pil_img):
|
| 10 |
-
return pil_img.convert('RGB')
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
model = LatexOCR()
|
| 14 |
|
| 15 |
# Clean LaTeX output
|
|
@@ -25,7 +26,7 @@ def clean_latex(latex):
|
|
| 25 |
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
| 26 |
latex = latex.replace(r'\pi', '3.1416')
|
| 27 |
|
| 28 |
-
# Replace coefficients like
|
| 29 |
latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
|
| 30 |
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 31 |
|
|
@@ -41,22 +42,21 @@ def clean_latex(latex):
|
|
| 41 |
# Wrap complex coefficients with variables: (a+bI)x^n → (a+b*I)*x^n
|
| 42 |
latex = re.sub(r'\(([^()]+?)\)\s*([xX](\^\d+)?)', r'(\1)*\2', latex)
|
| 43 |
|
| 44 |
-
# Append =0 if not already present
|
| 45 |
if '=' not in latex:
|
| 46 |
latex += '=0'
|
| 47 |
|
| 48 |
return latex
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
def solve_polynomial(image):
|
| 52 |
try:
|
| 53 |
img = preprocess_handwritten_image(image)
|
| 54 |
latex_result = model(img)
|
| 55 |
cleaned_latex = clean_latex(latex_result)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
expr = parse_latex(cleaned_latex, local_dict={'I': sp.I})
|
| 59 |
-
|
| 60 |
output = f"## 📄 Extracted LaTeX\n```\n{latex_result}\n```\n"
|
| 61 |
output += "---\n"
|
| 62 |
output += f"## 🧹 Cleaned LaTeX Used\n```\n{cleaned_latex}\n```\n"
|
|
@@ -96,6 +96,7 @@ def solve_polynomial(image):
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"❌ **Error**: {str(e)}"
|
| 98 |
|
|
|
|
| 99 |
# Gradio UI
|
| 100 |
demo = gr.Interface(
|
| 101 |
fn=solve_polynomial,
|
|
|
|
| 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 |
+
|
| 13 |
+
# Load Pix2Tex model (once)
|
| 14 |
model = LatexOCR()
|
| 15 |
|
| 16 |
# Clean LaTeX output
|
|
|
|
| 26 |
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
| 27 |
latex = latex.replace(r'\pi', '3.1416')
|
| 28 |
|
| 29 |
+
# Replace coefficients like 5e with (5*2.7183)
|
| 30 |
latex = re.sub(r'(\d+)\s*e', r'(\1*2.7183)', latex)
|
| 31 |
latex = re.sub(r'(?<![a-zA-Z0-9])e(?![a-zA-Z0-9])', '2.7183', latex)
|
| 32 |
|
|
|
|
| 42 |
# Wrap complex coefficients with variables: (a+bI)x^n → (a+b*I)*x^n
|
| 43 |
latex = re.sub(r'\(([^()]+?)\)\s*([xX](\^\d+)?)', r'(\1)*\2', latex)
|
| 44 |
|
| 45 |
+
# Append '=0' if not already present
|
| 46 |
if '=' not in latex:
|
| 47 |
latex += '=0'
|
| 48 |
|
| 49 |
return latex
|
| 50 |
|
| 51 |
+
|
| 52 |
+
# Main function
|
| 53 |
def solve_polynomial(image):
|
| 54 |
try:
|
| 55 |
img = preprocess_handwritten_image(image)
|
| 56 |
latex_result = model(img)
|
| 57 |
cleaned_latex = clean_latex(latex_result)
|
| 58 |
|
| 59 |
+
expr = parse_latex(cleaned_latex)
|
|
|
|
|
|
|
| 60 |
output = f"## 📄 Extracted LaTeX\n```\n{latex_result}\n```\n"
|
| 61 |
output += "---\n"
|
| 62 |
output += f"## 🧹 Cleaned LaTeX Used\n```\n{cleaned_latex}\n```\n"
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"❌ **Error**: {str(e)}"
|
| 98 |
|
| 99 |
+
|
| 100 |
# Gradio UI
|
| 101 |
demo = gr.Interface(
|
| 102 |
fn=solve_polynomial,
|