Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,9 @@ from sympy.parsing.latex import parse_latex
|
|
| 6 |
import re
|
| 7 |
import requests
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Preprocessing
|
| 10 |
def preprocess_handwritten_image(pil_img):
|
| 11 |
return pil_img.convert('RGB')
|
|
@@ -18,6 +21,7 @@ def clean_latex(latex):
|
|
| 18 |
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 19 |
latex = re.sub(r'\\(cal|mathcal)\s*Y', 'y', latex)
|
| 20 |
latex = re.sub(r'\\(cal|mathcal)\s*Z', 'z', latex)
|
|
|
|
| 21 |
latex = latex.replace('{', '').replace('}', '')
|
| 22 |
latex = latex.strip().rstrip(',.')
|
| 23 |
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
|
@@ -36,7 +40,6 @@ def clean_latex(latex):
|
|
| 36 |
|
| 37 |
# Fallback to external LLM microservice
|
| 38 |
def request_llm_fallback(bad_latex, llm_url):
|
| 39 |
-
# Clean known junk before sending to LLM
|
| 40 |
pre_cleaned = re.sub(
|
| 41 |
r'(\\!?pm|\\not=?|\\!|\\L|\\perp|\\bar|\\Sigma|\\boldmath|G|L(?=\^))', '', bad_latex
|
| 42 |
)
|
|
@@ -48,7 +51,6 @@ def request_llm_fallback(bad_latex, llm_url):
|
|
| 48 |
print(f"LLM fallback failed: {e}")
|
| 49 |
return pre_cleaned
|
| 50 |
|
| 51 |
-
|
| 52 |
# Main function
|
| 53 |
def solve_polynomial(image, llm_url):
|
| 54 |
try:
|
|
@@ -61,9 +63,8 @@ def solve_polynomial(image, llm_url):
|
|
| 61 |
cleaned_latex = clean_latex(latex_result)
|
| 62 |
|
| 63 |
try:
|
| 64 |
-
expr = parse_latex(cleaned_latex)
|
| 65 |
|
| 66 |
-
# Trigger fallback if not a polynomial
|
| 67 |
if not isinstance(expr, sp.Equality):
|
| 68 |
raise ValueError("Expression is not an equation.")
|
| 69 |
|
|
@@ -71,18 +72,16 @@ def solve_polynomial(image, llm_url):
|
|
| 71 |
if not lhs_minus_rhs.is_polynomial():
|
| 72 |
raise ValueError("Expression is not a polynomial.")
|
| 73 |
|
| 74 |
-
# π Fallback trigger if junk variables found
|
| 75 |
junk_symbols = {"pm", "cdot", "mathrm", "boldmath", "bar", "L"}
|
| 76 |
symbols = {str(s) for s in expr.free_symbols}
|
| 77 |
if symbols & junk_symbols:
|
| 78 |
raise ValueError("Expression contains junk symbols.")
|
| 79 |
|
| 80 |
except Exception:
|
| 81 |
-
# Fallback to LLM microservice
|
| 82 |
fixed_latex = request_llm_fallback(cleaned_latex, llm_url)
|
| 83 |
cleaned_latex = clean_latex(fixed_latex)
|
| 84 |
try:
|
| 85 |
-
expr = parse_latex(cleaned_latex)
|
| 86 |
|
| 87 |
if not isinstance(expr, sp.Equality):
|
| 88 |
raise ValueError("Expression is not an equation.")
|
|
@@ -91,7 +90,6 @@ def solve_polynomial(image, llm_url):
|
|
| 91 |
if not lhs_minus_rhs.is_polynomial():
|
| 92 |
raise ValueError("Expression is not a polynomial.")
|
| 93 |
|
| 94 |
-
# π Fallback again if junk remains
|
| 95 |
symbols = {str(s) for s in expr.free_symbols}
|
| 96 |
if symbols & junk_symbols:
|
| 97 |
raise ValueError("Expression contains junk symbols.")
|
|
|
|
| 6 |
import re
|
| 7 |
import requests
|
| 8 |
|
| 9 |
+
# β
Predeclare common variables
|
| 10 |
+
x, y, z = sp.symbols('x y z')
|
| 11 |
+
|
| 12 |
# Preprocessing
|
| 13 |
def preprocess_handwritten_image(pil_img):
|
| 14 |
return pil_img.convert('RGB')
|
|
|
|
| 21 |
latex = re.sub(r'\\(cal|mathcal)\s*X', 'x', latex)
|
| 22 |
latex = re.sub(r'\\(cal|mathcal)\s*Y', 'y', latex)
|
| 23 |
latex = re.sub(r'\\(cal|mathcal)\s*Z', 'z', latex)
|
| 24 |
+
latex = re.sub(r'\\(hat|bf|tau|enspace)', '', latex)
|
| 25 |
latex = latex.replace('{', '').replace('}', '')
|
| 26 |
latex = latex.strip().rstrip(',.')
|
| 27 |
latex = re.sub(r'(\d+)\s*\\pi', r'(\1*3.1416)', latex)
|
|
|
|
| 40 |
|
| 41 |
# Fallback to external LLM microservice
|
| 42 |
def request_llm_fallback(bad_latex, llm_url):
|
|
|
|
| 43 |
pre_cleaned = re.sub(
|
| 44 |
r'(\\!?pm|\\not=?|\\!|\\L|\\perp|\\bar|\\Sigma|\\boldmath|G|L(?=\^))', '', bad_latex
|
| 45 |
)
|
|
|
|
| 51 |
print(f"LLM fallback failed: {e}")
|
| 52 |
return pre_cleaned
|
| 53 |
|
|
|
|
| 54 |
# Main function
|
| 55 |
def solve_polynomial(image, llm_url):
|
| 56 |
try:
|
|
|
|
| 63 |
cleaned_latex = clean_latex(latex_result)
|
| 64 |
|
| 65 |
try:
|
| 66 |
+
expr = parse_latex(cleaned_latex, local_dict={'x': x, 'y': y, 'z': z})
|
| 67 |
|
|
|
|
| 68 |
if not isinstance(expr, sp.Equality):
|
| 69 |
raise ValueError("Expression is not an equation.")
|
| 70 |
|
|
|
|
| 72 |
if not lhs_minus_rhs.is_polynomial():
|
| 73 |
raise ValueError("Expression is not a polynomial.")
|
| 74 |
|
|
|
|
| 75 |
junk_symbols = {"pm", "cdot", "mathrm", "boldmath", "bar", "L"}
|
| 76 |
symbols = {str(s) for s in expr.free_symbols}
|
| 77 |
if symbols & junk_symbols:
|
| 78 |
raise ValueError("Expression contains junk symbols.")
|
| 79 |
|
| 80 |
except Exception:
|
|
|
|
| 81 |
fixed_latex = request_llm_fallback(cleaned_latex, llm_url)
|
| 82 |
cleaned_latex = clean_latex(fixed_latex)
|
| 83 |
try:
|
| 84 |
+
expr = parse_latex(cleaned_latex, local_dict={'x': x, 'y': y, 'z': z})
|
| 85 |
|
| 86 |
if not isinstance(expr, sp.Equality):
|
| 87 |
raise ValueError("Expression is not an equation.")
|
|
|
|
| 90 |
if not lhs_minus_rhs.is_polynomial():
|
| 91 |
raise ValueError("Expression is not a polynomial.")
|
| 92 |
|
|
|
|
| 93 |
symbols = {str(s) for s in expr.free_symbols}
|
| 94 |
if symbols & junk_symbols:
|
| 95 |
raise ValueError("Expression contains junk symbols.")
|