Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,6 @@ from sympy.parsing.latex import parse_latex
|
|
| 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,7 +18,6 @@ def clean_latex(latex):
|
|
| 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,6 +36,7 @@ def clean_latex(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 |
)
|
|
@@ -63,36 +60,37 @@ def solve_polynomial(image, llm_url):
|
|
| 63 |
cleaned_latex = clean_latex(latex_result)
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
expr = parse_latex(cleaned_latex
|
| 67 |
-
|
| 68 |
if not isinstance(expr, sp.Equality):
|
| 69 |
raise ValueError("Expression is not an equation.")
|
| 70 |
-
|
| 71 |
lhs_minus_rhs = expr.lhs - expr.rhs
|
| 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
|
| 85 |
-
|
| 86 |
if not isinstance(expr, sp.Equality):
|
| 87 |
raise ValueError("Expression is not an equation.")
|
| 88 |
-
|
| 89 |
lhs_minus_rhs = expr.lhs - expr.rhs
|
| 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.")
|
|
|
|
| 96 |
except Exception:
|
| 97 |
expr = None
|
| 98 |
|
|
@@ -134,13 +132,9 @@ with gr.Blocks() as demo:
|
|
| 134 |
llm_url = gr.Textbox(label="π Enter LLM Microservice URL (from Colab)", placeholder="https://xxxx.ngrok-free.app")
|
| 135 |
image_input = gr.Image(type="pil", label="π· Upload Image of Polynomial")
|
| 136 |
output_box = gr.Markdown(label="π Step-by-step Solution")
|
| 137 |
-
state = gr.State()
|
| 138 |
-
|
| 139 |
-
def wrapped_solver(img, url):
|
| 140 |
-
return solve_polynomial(img, url)
|
| 141 |
|
| 142 |
submit_btn = gr.Button("π Solve")
|
| 143 |
-
submit_btn.click(fn=
|
| 144 |
|
| 145 |
demo.title = "π§ Polynomial Solver from Image"
|
| 146 |
demo.description = "Upload an image of a polynomial (typed or handwritten). It will be solved step-by-step."
|
|
|
|
| 6 |
import re
|
| 7 |
import requests
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Preprocessing
|
| 10 |
def preprocess_handwritten_image(pil_img):
|
| 11 |
return pil_img.convert('RGB')
|
|
|
|
| 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 |
|
| 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 |
)
|
|
|
|
| 60 |
cleaned_latex = clean_latex(latex_result)
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
expr = parse_latex(cleaned_latex)
|
| 64 |
+
|
| 65 |
if not isinstance(expr, sp.Equality):
|
| 66 |
raise ValueError("Expression is not an equation.")
|
| 67 |
+
|
| 68 |
lhs_minus_rhs = expr.lhs - expr.rhs
|
| 69 |
if not lhs_minus_rhs.is_polynomial():
|
| 70 |
raise ValueError("Expression is not a polynomial.")
|
| 71 |
+
|
| 72 |
+
# Fallback if junk symbols found
|
| 73 |
junk_symbols = {"pm", "cdot", "mathrm", "boldmath", "bar", "L"}
|
| 74 |
symbols = {str(s) for s in expr.free_symbols}
|
| 75 |
if symbols & junk_symbols:
|
| 76 |
raise ValueError("Expression contains junk symbols.")
|
| 77 |
+
|
| 78 |
except Exception:
|
| 79 |
fixed_latex = request_llm_fallback(cleaned_latex, llm_url)
|
| 80 |
cleaned_latex = clean_latex(fixed_latex)
|
| 81 |
try:
|
| 82 |
+
expr = parse_latex(cleaned_latex)
|
|
|
|
| 83 |
if not isinstance(expr, sp.Equality):
|
| 84 |
raise ValueError("Expression is not an equation.")
|
| 85 |
+
|
| 86 |
lhs_minus_rhs = expr.lhs - expr.rhs
|
| 87 |
if not lhs_minus_rhs.is_polynomial():
|
| 88 |
raise ValueError("Expression is not a polynomial.")
|
| 89 |
+
|
| 90 |
symbols = {str(s) for s in expr.free_symbols}
|
| 91 |
if symbols & junk_symbols:
|
| 92 |
raise ValueError("Expression contains junk symbols.")
|
| 93 |
+
|
| 94 |
except Exception:
|
| 95 |
expr = None
|
| 96 |
|
|
|
|
| 132 |
llm_url = gr.Textbox(label="π Enter LLM Microservice URL (from Colab)", placeholder="https://xxxx.ngrok-free.app")
|
| 133 |
image_input = gr.Image(type="pil", label="π· Upload Image of Polynomial")
|
| 134 |
output_box = gr.Markdown(label="π Step-by-step Solution")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
submit_btn = gr.Button("π Solve")
|
| 137 |
+
submit_btn.click(fn=solve_polynomial, inputs=[image_input, llm_url], outputs=output_box)
|
| 138 |
|
| 139 |
demo.title = "π§ Polynomial Solver from Image"
|
| 140 |
demo.description = "Upload an image of a polynomial (typed or handwritten). It will be solved step-by-step."
|