Update image.py
Browse files
image.py
CHANGED
|
@@ -8,8 +8,6 @@ import re
|
|
| 8 |
import io
|
| 9 |
import logging
|
| 10 |
from llm_utils import explain_with_llm # β
Added for LLM explanation
|
| 11 |
-
from solver import solve_polynomial as solve_poly_from_solver, solve_linear_system as solve_sys_from_solver
|
| 12 |
-
|
| 13 |
|
| 14 |
# Configure logging for debugging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -42,13 +40,6 @@ def clean_latex_expression(latex_str):
|
|
| 42 |
if '=' in latex_str:
|
| 43 |
left, right = latex_str.split('=')
|
| 44 |
latex_str = f"{left} - ({right})" # Move right-hand side to left
|
| 45 |
-
|
| 46 |
-
# Add missing multiplication: (expr)x β (expr)*x
|
| 47 |
-
latex_str = re.sub(r'(\))([a-zA-Z])', r'\1*\2', latex_str)
|
| 48 |
-
|
| 49 |
-
# Add * between number/symbol and parenthesis: e.g., 3( β 3*(...
|
| 50 |
-
latex_str = re.sub(r'(\d|\w)\(', r'\1*(', latex_str)
|
| 51 |
-
|
| 52 |
return latex_str.strip()
|
| 53 |
|
| 54 |
def parse_equation_type(latex_str):
|
|
@@ -93,7 +84,7 @@ def extract_polynomial_coefficients(latex_str):
|
|
| 93 |
if degree < 1 or degree > 8:
|
| 94 |
raise ValueError(f"Polynomial degree {degree} is out of supported range (1-8)")
|
| 95 |
poly = sp.Poly(expr, variable)
|
| 96 |
-
coeffs = [poly.coeff_monomial(variable**i)
|
| 97 |
return {
|
| 98 |
"type": "polynomial",
|
| 99 |
"degree": degree,
|
|
@@ -304,15 +295,14 @@ def solve_linear_system_from_coeffs(eq1_str, eq2_str):
|
|
| 304 |
|
| 305 |
def solve_extracted_equation(eq_data, real_only):
|
| 306 |
if eq_data["type"] == "polynomial":
|
| 307 |
-
return
|
| 308 |
elif eq_data["type"] == "linear":
|
| 309 |
return "β Single linear equation not supported. Please upload a system of equations.", None, ""
|
| 310 |
elif eq_data["type"] == "linear_system":
|
| 311 |
-
return
|
| 312 |
else:
|
| 313 |
return "β Unknown equation type", None, ""
|
| 314 |
|
| 315 |
-
|
| 316 |
def image_tab():
|
| 317 |
"""Create the Image Upload Solver tab"""
|
| 318 |
with gr.Tab("Image Upload Solver"):
|
|
@@ -451,4 +441,3 @@ def image_tab():
|
|
| 451 |
save_edit_btn, image_steps_md, image_plot_output, extracted_eq_state,
|
| 452 |
llm_url_input, explain_image_btn, image_solution_txt # β
added for LLM
|
| 453 |
)
|
| 454 |
-
|
|
|
|
| 8 |
import io
|
| 9 |
import logging
|
| 10 |
from llm_utils import explain_with_llm # β
Added for LLM explanation
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Configure logging for debugging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 40 |
if '=' in latex_str:
|
| 41 |
left, right = latex_str.split('=')
|
| 42 |
latex_str = f"{left} - ({right})" # Move right-hand side to left
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return latex_str.strip()
|
| 44 |
|
| 45 |
def parse_equation_type(latex_str):
|
|
|
|
| 84 |
if degree < 1 or degree > 8:
|
| 85 |
raise ValueError(f"Polynomial degree {degree} is out of supported range (1-8)")
|
| 86 |
poly = sp.Poly(expr, variable)
|
| 87 |
+
coeffs = [float(poly.coeff_monomial(variable**i)) for i in range(degree, -1, -1)]
|
| 88 |
return {
|
| 89 |
"type": "polynomial",
|
| 90 |
"degree": degree,
|
|
|
|
| 295 |
|
| 296 |
def solve_extracted_equation(eq_data, real_only):
|
| 297 |
if eq_data["type"] == "polynomial":
|
| 298 |
+
return solve_polynomial(eq_data["degree"], eq_data["coeffs"], real_only)
|
| 299 |
elif eq_data["type"] == "linear":
|
| 300 |
return "β Single linear equation not supported. Please upload a system of equations.", None, ""
|
| 301 |
elif eq_data["type"] == "linear_system":
|
| 302 |
+
return solve_linear_system_from_coeffs(eq_data["eq1_coeffs"], eq_data["eq2_coeffs"])
|
| 303 |
else:
|
| 304 |
return "β Unknown equation type", None, ""
|
| 305 |
|
|
|
|
| 306 |
def image_tab():
|
| 307 |
"""Create the Image Upload Solver tab"""
|
| 308 |
with gr.Tab("Image Upload Solver"):
|
|
|
|
| 441 |
save_edit_btn, image_steps_md, image_plot_output, extracted_eq_state,
|
| 442 |
llm_url_input, explain_image_btn, image_solution_txt # β
added for LLM
|
| 443 |
)
|
|
|