Update app.py
Browse files
app.py
CHANGED
|
@@ -57,15 +57,33 @@ def solve_polynomial(image, llm_url):
|
|
| 57 |
|
| 58 |
try:
|
| 59 |
expr = parse_latex(cleaned_latex)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception:
|
| 61 |
# Fallback to LLM microservice
|
| 62 |
-
fixed_latex = request_llm_fallback(cleaned_latex
|
| 63 |
cleaned_latex = clean_latex(fixed_latex)
|
| 64 |
try:
|
| 65 |
expr = parse_latex(cleaned_latex)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
except Exception:
|
| 67 |
expr = None
|
| 68 |
|
|
|
|
| 69 |
if expr is None:
|
| 70 |
return (
|
| 71 |
"❌ Could not parse expression even after fallback:\n\n"
|
|
|
|
| 57 |
|
| 58 |
try:
|
| 59 |
expr = parse_latex(cleaned_latex)
|
| 60 |
+
|
| 61 |
+
# Trigger fallback if not a polynomial (i.e., not a SymPy Equality with a polynomial lhs - rhs)
|
| 62 |
+
if not isinstance(expr, sp.Equality):
|
| 63 |
+
raise ValueError("Expression is not an equation.")
|
| 64 |
+
|
| 65 |
+
lhs_minus_rhs = expr.lhs - expr.rhs
|
| 66 |
+
if not lhs_minus_rhs.is_polynomial():
|
| 67 |
+
raise ValueError("Expression is not a polynomial.")
|
| 68 |
+
|
| 69 |
except Exception:
|
| 70 |
# Fallback to LLM microservice
|
| 71 |
+
fixed_latex = request_llm_fallback(cleaned_latex)
|
| 72 |
cleaned_latex = clean_latex(fixed_latex)
|
| 73 |
try:
|
| 74 |
expr = parse_latex(cleaned_latex)
|
| 75 |
+
|
| 76 |
+
# Try again: check if fallback result is polynomial
|
| 77 |
+
if not isinstance(expr, sp.Equality):
|
| 78 |
+
raise ValueError("Expression is not an equation.")
|
| 79 |
+
|
| 80 |
+
lhs_minus_rhs = expr.lhs - expr.rhs
|
| 81 |
+
if not lhs_minus_rhs.is_polynomial():
|
| 82 |
+
raise ValueError("Expression is not a polynomial.")
|
| 83 |
except Exception:
|
| 84 |
expr = None
|
| 85 |
|
| 86 |
+
|
| 87 |
if expr is None:
|
| 88 |
return (
|
| 89 |
"❌ Could not parse expression even after fallback:\n\n"
|