Update app.py
Browse files
app.py
CHANGED
|
@@ -32,6 +32,8 @@ def clean_latex(latex):
|
|
| 32 |
latex = latex.replace('−', '-')
|
| 33 |
return latex.strip()
|
| 34 |
|
|
|
|
|
|
|
| 35 |
def solve_polynomial(image):
|
| 36 |
try:
|
| 37 |
img = preprocess_handwritten_image(image)
|
|
@@ -76,7 +78,7 @@ def wrapped_solver(img):
|
|
| 76 |
result, cleaned, prompt = solve_polynomial(img)
|
| 77 |
return result, cleaned, prompt
|
| 78 |
|
| 79 |
-
#
|
| 80 |
|
| 81 |
def solve_simultaneous_equations(img):
|
| 82 |
try:
|
|
@@ -84,14 +86,14 @@ def solve_simultaneous_equations(img):
|
|
| 84 |
latex_result = model(img)
|
| 85 |
cleaned_latex = clean_latex(latex_result)
|
| 86 |
|
| 87 |
-
# Extract lines
|
| 88 |
if 'beginarray' in cleaned_latex or 'begin{aligned}' in cleaned_latex:
|
| 89 |
body = re.sub(r'\\?begin\{?array\}?|\\?end\{?array\}?', '', cleaned_latex)
|
| 90 |
-
if '\\\\'
|
| 91 |
-
# Fallback: separate equations if `\\` missing
|
| 92 |
-
lines = re.findall(r'[^=]+=.*?(?=[a-zA-Z]|$)', body)
|
| 93 |
-
else:
|
| 94 |
lines = re.findall(r'(.*?)\\\\', body + '\\\\')
|
|
|
|
|
|
|
|
|
|
| 95 |
else:
|
| 96 |
lines = [line.strip() for line in cleaned_latex.split('\n') if line.strip()]
|
| 97 |
|
|
|
|
| 32 |
latex = latex.replace('−', '-')
|
| 33 |
return latex.strip()
|
| 34 |
|
| 35 |
+
# ------------------ Tab 1: Polynomial Solver ------------------
|
| 36 |
+
|
| 37 |
def solve_polynomial(image):
|
| 38 |
try:
|
| 39 |
img = preprocess_handwritten_image(image)
|
|
|
|
| 78 |
result, cleaned, prompt = solve_polynomial(img)
|
| 79 |
return result, cleaned, prompt
|
| 80 |
|
| 81 |
+
# ------------------ Tab 2: Simultaneous Equations ------------------
|
| 82 |
|
| 83 |
def solve_simultaneous_equations(img):
|
| 84 |
try:
|
|
|
|
| 86 |
latex_result = model(img)
|
| 87 |
cleaned_latex = clean_latex(latex_result)
|
| 88 |
|
| 89 |
+
# Extract lines from environments OR fallback to splitting by '='
|
| 90 |
if 'beginarray' in cleaned_latex or 'begin{aligned}' in cleaned_latex:
|
| 91 |
body = re.sub(r'\\?begin\{?array\}?|\\?end\{?array\}?', '', cleaned_latex)
|
| 92 |
+
if '\\\\' in cleaned_latex:
|
|
|
|
|
|
|
|
|
|
| 93 |
lines = re.findall(r'(.*?)\\\\', body + '\\\\')
|
| 94 |
+
else:
|
| 95 |
+
# fallback: split into equations using = and variable heuristics
|
| 96 |
+
lines = re.findall(r'[^\n=]+=.*?(?=\s*[a-zA-Z]|$)', body)
|
| 97 |
else:
|
| 98 |
lines = [line.strip() for line in cleaned_latex.split('\n') if line.strip()]
|
| 99 |
|