Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from sympy.parsing.latex import parse_latex
|
|
| 6 |
import re
|
| 7 |
import requests
|
| 8 |
|
| 9 |
-
# Load Pix2Tex model
|
| 10 |
model = LatexOCR()
|
| 11 |
|
| 12 |
def preprocess_handwritten_image(pil_img):
|
|
@@ -61,9 +60,9 @@ def request_llm_fallback(bad_latex, llm_url):
|
|
| 61 |
print(f"LLM fallback failed: {e}")
|
| 62 |
return pre_cleaned
|
| 63 |
|
| 64 |
-
def request_llm_explanation(
|
| 65 |
try:
|
| 66 |
-
response = requests.post(f"{llm_url}/explain", json={"prompt":
|
| 67 |
if response.status_code == 200:
|
| 68 |
return response.json().get("explanation", "❌ No explanation returned.")
|
| 69 |
else:
|
|
@@ -144,7 +143,6 @@ def wrapped_solver(img, url):
|
|
| 144 |
result, cleaned, _ = solve_polynomial(img, url)
|
| 145 |
return result, cleaned
|
| 146 |
|
| 147 |
-
# ✅ REWRITTEN: Build raw string (not LaTeX) for LLM
|
| 148 |
def solve_from_coeffs(degree, coeff_str):
|
| 149 |
try:
|
| 150 |
coeffs = [float(c.strip()) for c in coeff_str.split(",")]
|
|
@@ -160,7 +158,6 @@ def solve_from_coeffs(degree, coeff_str):
|
|
| 160 |
for i, r in enumerate(roots, 1):
|
| 161 |
result += f"- Root {i}: $${sp.latex(sp.N(r, 6))}$$\n"
|
| 162 |
|
| 163 |
-
# Build readable equation string
|
| 164 |
terms = []
|
| 165 |
for i, c in enumerate(coeffs):
|
| 166 |
power = int(degree) - i
|
|
@@ -171,8 +168,6 @@ def solve_from_coeffs(degree, coeff_str):
|
|
| 171 |
else:
|
| 172 |
terms.append(f"{c}x^{power}")
|
| 173 |
equation_str = " + ".join(terms).replace("+ -", "- ") + " = 0"
|
| 174 |
-
|
| 175 |
-
# Hidden prompt string to send to LLM
|
| 176 |
roots_str = f"[{', '.join(str(sp.N(r, 6)) for r in roots)}]"
|
| 177 |
factored_str = sp.pretty(factored)
|
| 178 |
|
|
|
|
| 6 |
import re
|
| 7 |
import requests
|
| 8 |
|
|
|
|
| 9 |
model = LatexOCR()
|
| 10 |
|
| 11 |
def preprocess_handwritten_image(pil_img):
|
|
|
|
| 60 |
print(f"LLM fallback failed: {e}")
|
| 61 |
return pre_cleaned
|
| 62 |
|
| 63 |
+
def request_llm_explanation(prompt, llm_url):
|
| 64 |
try:
|
| 65 |
+
response = requests.post(f"{llm_url}/explain", json={"prompt": prompt})
|
| 66 |
if response.status_code == 200:
|
| 67 |
return response.json().get("explanation", "❌ No explanation returned.")
|
| 68 |
else:
|
|
|
|
| 143 |
result, cleaned, _ = solve_polynomial(img, url)
|
| 144 |
return result, cleaned
|
| 145 |
|
|
|
|
| 146 |
def solve_from_coeffs(degree, coeff_str):
|
| 147 |
try:
|
| 148 |
coeffs = [float(c.strip()) for c in coeff_str.split(",")]
|
|
|
|
| 158 |
for i, r in enumerate(roots, 1):
|
| 159 |
result += f"- Root {i}: $${sp.latex(sp.N(r, 6))}$$\n"
|
| 160 |
|
|
|
|
| 161 |
terms = []
|
| 162 |
for i, c in enumerate(coeffs):
|
| 163 |
power = int(degree) - i
|
|
|
|
| 168 |
else:
|
| 169 |
terms.append(f"{c}x^{power}")
|
| 170 |
equation_str = " + ".join(terms).replace("+ -", "- ") + " = 0"
|
|
|
|
|
|
|
| 171 |
roots_str = f"[{', '.join(str(sp.N(r, 6)) for r in roots)}]"
|
| 172 |
factored_str = sp.pretty(factored)
|
| 173 |
|