MasteredUltraInstinct commited on
Commit
0c917a6
·
verified ·
1 Parent(s): c776b6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -58,13 +58,19 @@ def solve_polynomial(image, llm_url):
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
@@ -73,17 +79,20 @@ def solve_polynomial(image, llm_url):
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"
 
58
  try:
59
  expr = parse_latex(cleaned_latex)
60
 
61
+ # Trigger fallback if not a polynomial
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
+ # 🔍 Fallback trigger if junk variables found
70
+ junk_symbols = {"pm", "cdot", "mathrm", "boldmath", "bar", "L"}
71
+ symbols = {str(s) for s in expr.free_symbols}
72
+ if symbols & junk_symbols:
73
+ raise ValueError("Expression contains junk symbols.")
74
 
75
  except Exception:
76
  # Fallback to LLM microservice
 
79
  try:
80
  expr = parse_latex(cleaned_latex)
81
 
 
82
  if not isinstance(expr, sp.Equality):
83
  raise ValueError("Expression is not an equation.")
84
 
85
  lhs_minus_rhs = expr.lhs - expr.rhs
86
  if not lhs_minus_rhs.is_polynomial():
87
  raise ValueError("Expression is not a polynomial.")
88
+
89
+ # 🔁 Fallback again if junk remains
90
+ symbols = {str(s) for s in expr.free_symbols}
91
+ if symbols & junk_symbols:
92
+ raise ValueError("Expression contains junk symbols.")
93
  except Exception:
94
  expr = None
95
 
 
96
  if expr is None:
97
  return (
98
  "❌ Could not parse expression even after fallback:\n\n"