File size: 18,464 Bytes
ed6e039 9f23e99 ed6e039 280fb96 ed6e039 280fb96 ed6e039 263897e 280fb96 ed6e039 280fb96 263897e 280fb96 263897e 280fb96 263897e 280fb96 263897e 280fb96 263897e 280fb96 263897e 280fb96 263897e 280fb96 263897e 280fb96 a35ebae 280fb96 a35ebae 280fb96 a35ebae 280fb96 a35ebae 280fb96 a35ebae 280fb96 a35ebae 280fb96 a35ebae 280fb96 a35ebae 280fb96 263897e 280fb96 263897e 280fb96 263897e 280fb96 9e98a85 280fb96 ed6e039 263897e ed6e039 263897e ed6e039 263897e ed6e039 280fb96 9f23e99 ed6e039 9f23e99 ed6e039 263897e 9f23e99 ed6e039 9f23e99 ed6e039 9f23e99 ed6e039 9f23e99 ed6e039 9f23e99 ed6e039 113b746 263897e 9f23e99 ed6e039 9f23e99 ed6e039 280fb96 ab8fb66 ed6e039 113b746 ed6e039 9f23e99 280fb96 263897e 280fb96 ed6e039 9f23e99 ed6e039 263897e ed6e039 280fb96 ed6e039 ab8fb66 ed6e039 280fb96 ed6e039 280fb96 9f23e99 ed6e039 9f23e99 263897e 9f23e99 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
import gradio as gr
import sympy as sp
from pix2text import Pix2Text
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import re
import io
import logging
from llm_utils import explain_with_llm # β
Added for LLM explanation
# Configure logging for debugging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define symbolic variables
x, y = sp.symbols('x y')
# β
Helper to get safe variable symbol
def get_variable_symbol(varname):
if varname in {"pi", "e", "I", "i"}:
return x
try:
return sp.Symbol(varname)
except Exception:
return x
# Initialize Pix2Text model globally
try:
p2t_model = Pix2Text.from_config()
logger.info("Pix2Text model loaded successfully")
except Exception as e:
logger.error(f"Failed to load Pix2Text model: {e}")
p2t_model = None
def clean_latex_expression(latex_str):
"""Clean and normalize LaTeX expression for SymPy parsing"""
if not latex_str:
return ""
latex_str = latex_str.strip()
latex_str = re.sub(r'^\$\$|\$\$$', '', latex_str) # Remove $$ delimiters
latex_str = re.sub(r'\\[a-zA-Z]+\{([^}]*)\}', r'\1', latex_str) # Remove LaTeX commands
latex_str = re.sub(r'\\{2,}', r'\\', latex_str) # Fix multiple backslashes
latex_str = re.sub(r'\s+', ' ', latex_str) # Normalize whitespace
latex_str = re.sub(r'\^{([^}]+)}', r'**\1', latex_str) # Convert x^{n} to x**n
latex_str = re.sub(r'(\d*\.?\d+)\s*([xy])', r'\1*\2', latex_str) # Add multiplication: 1.0x -> 1.0*x
latex_str = re.sub(r'\s*([+\-*/=])\s*', r'\1', latex_str) # Remove spaces around operators
if '=' in latex_str:
left, right = latex_str.split('=')
latex_str = f"{left} - ({right})" # Move right-hand side to left
# β
Insert missing multiplication
latex_str = re.sub(r'(\))([a-zA-Z])', r'\1*\2', latex_str)
latex_str = re.sub(r'(\d|\w)\(', r'\1*(', latex_str)
# β
Replace LaTeX constants with sympy
latex_str = latex_str.replace(r'\pi', 'pi')
latex_str = latex_str.replace(r'\mathrm{e}', 'e')
latex_str = latex_str.replace(r'\cdot', '*')
latex_str = latex_str.replace(r'\times', '*')
latex_str = latex_str.replace(r'\\', '')
latex_str = re.sub(r'\\sqrt\{([^}]+)\}', r'sqrt(\1)', latex_str)
return latex_str.strip()
def parse_equation_type(latex_str):
"""Determine if the equation is polynomial (single-variable) or linear system (two-variable)"""
try:
cleaned = clean_latex_expression(latex_str)
if not cleaned:
return 'polynomial'
# Check for two-variable system
if 'y' in cleaned and 'x' in cleaned:
if '\\\\' in latex_str or '\n' in latex_str or len(re.split(r'\\\\|\n|;', latex_str)) >= 2:
return 'linear_system'
return 'linear' # Single equation with x and y
# Check for single-variable polynomial
try:
expr = sp.sympify(cleaned.split('-')[0] if '-' in cleaned else cleaned)
if x in expr.free_symbols and y not in expr.free_symbols:
degree = sp.degree(expr, x)
return 'polynomial' if degree > 0 else 'linear'
elif x not in expr.free_symbols and y in expr.free_symbols:
return 'polynomial'
else:
return 'polynomial'
except:
if 'x**' in cleaned or '^' in latex_str:
return 'polynomial'
return 'polynomial'
except Exception as e:
logger.error(f"Error determining equation type: {e}")
return 'polynomial'
def extract_polynomial_coefficients(latex_str):
try:
cleaned = clean_latex_expression(latex_str)
expr = sp.sympify(cleaned, evaluate=False)
if x not in expr.free_symbols and y not in expr.free_symbols:
raise ValueError("No variable (x or y) found in expression")
variable = next(iter(expr.free_symbols))
degree = sp.degree(expr, variable)
if degree < 1 or degree > 8:
raise ValueError(f"Polynomial degree {degree} is out of supported range (1-8)")
poly = sp.Poly(expr, variable)
coeffs = [poly.coeff_monomial(variable**i).evalf() for i in range(degree, -1, -1)]
return {
"type": "polynomial",
"degree": degree,
"coeffs": " ".join(map(str, coeffs)),
"latex": latex_str,
"success": True,
"variable": str(variable)
}
except Exception as e:
logger.error(f"Error extracting polynomial coefficients: {e}")
return {
"type": "polynomial",
"degree": 2,
"coeffs": "1 0 0",
"latex": latex_str,
"success": False,
"error": str(e),
"variable": "x"
}
def solve_polynomial(degree, coeff_string, real_only, variable_name="x"):
try:
variable = sp.Symbol(variable_name)
coeffs = list(map(float, coeff_string.strip().split()))
if len(coeffs) != degree + 1:
return f"β οΈ Please enter exactly {degree + 1} coefficients.", None, None
# Build the polynomial expression
poly = sum([coeffs[i] * variable**(degree - i) for i in range(degree + 1)])
simplified = sp.simplify(poly)
factored = sp.factor(simplified)
roots = sp.solve(sp.Eq(simplified, 0), variable)
if real_only:
roots = [r for r in roots if sp.im(r) == 0]
# Format roots in LaTeX
roots_output = "$$\n" + "\\ ".join(
[f"r_{{{i}}} = {sp.latex(sp.nsimplify(r, rational=True))}" for i, r in enumerate(roots, 1)]
) + "\n$$"
# Format steps in LaTeX
steps_output = f"""
### Polynomial Expression
$$ {sp.latex(poly)} = 0 $$
### Simplified
$$ {sp.latex(simplified)} = 0 $$
### Factored
$$ {sp.latex(factored)} = 0 $$
### Roots {'(Only Real)' if real_only else '(All Roots)'}
{roots_output}
"""
# Generate plot using numeric x-axis
x_vals = np.linspace(-10, 10, 400)
y_vals = np.polyval(coeffs, x_vals)
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(x_vals, y_vals, label="Polynomial", color="blue")
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.grid(True)
ax.set_title("Graph of the Polynomial")
ax.set_xlabel(str(variable))
ax.set_ylabel("f(" + str(variable) + ")")
ax.legend()
return steps_output, fig, ""
except Exception as e:
return f"β Error: {e}", None, ""
def extract_linear_system_coefficients(latex_str):
try:
cleaned = clean_latex_expression(latex_str)
equations = re.split(r'\\\\|\n|;', latex_str)
if len(equations) < 2:
equations = re.split(r'(?<=[0-9])\s*(?=[+-]?\s*[0-9]*[xy])', cleaned)
if len(equations) < 2 or 'y' not in cleaned or 'x' not in cleaned:
raise ValueError("Could not find two equations or two variables (x, y) in system")
eq1_str = equations[0].strip()
eq2_str = equations[1].strip()
def parse_linear_eq(eq_str):
if '-' not in eq_str:
raise ValueError("No equals sign (converted to '-') found")
left, right = eq_str.split('-')
expr = sp.sympify(left) - sp.sympify(right or '0')
a = float(expr.coeff(x, 1)) if expr.coeff(x, 1) else 0
b = float(expr.coeff(y, 1)) if expr.coeff(y, 1) else 0
c = float(-expr.as_coefficients_dict()[1]) if 1 in expr.as_coefficients_dict() else 0
return f"{a} {b} {c}"
eq1_coeffs = parse_linear_eq(eq1_str)
eq2_coeffs = parse_linear_eq(eq2_str)
return {
"type": "linear",
"eq1_coeffs": eq1_coeffs,
"eq2_coeffs": eq2_coeffs,
"latex": latex_str,
"success": True
}
except Exception as e:
logger.error(f"Error extracting linear system coefficients: {e}")
return {
"type": "linear",
"eq1_coeffs": "1 1 3",
"eq2_coeffs": "1 -1 1",
"latex": latex_str,
"success": False,
"error": str(e)
}
def solve_linear_system_from_coeffs(eq1_str, eq2_str):
try:
coeffs1 = list(map(float, eq1_str.strip().split()))
coeffs2 = list(map(float, eq2_str.strip().split()))
if len(coeffs1) != 3 or len(coeffs2) != 3:
return "β οΈ Please enter exactly 3 coefficients for each equation.", None, None, None
a1, b1, c1 = coeffs1
a2, b2, c2 = coeffs2
eq1 = sp.Eq(a1 * x + b1 * y, c1)
eq2 = sp.Eq(a2 * x + b2 * y, c2)
sol = sp.solve([eq1, eq2], (x, y), dict=True)
if not sol:
return "β No unique solution.", None, None, None
solution = sol[0]
eq_latex = f"$$ {sp.latex(eq1)} \\ {sp.latex(eq2)} $$"
steps = rf"""
### Step-by-step Solution
1. **Original Equations:**
$$ {sp.latex(eq1)} $$
$$ {sp.latex(eq2)} $$
2. **Standard Form:** Already provided.
3. **Solve using SymPy `solve`:** Internally applies substitution/elimination.
4. **Solve for `x` and `y`:**
$$ x = {sp.latex(solution[x])}, \quad y = {sp.latex(solution[y])} $$
5. **Verification:** Substitute back into both equations."""
x_vals = np.linspace(-10, 10, 400)
f1 = sp.solve(eq1, y)
f2 = sp.solve(eq2, y)
fig, ax = plt.subplots()
if f1:
f1_func = sp.lambdify(x, f1[0], modules='numpy')
ax.plot(x_vals, f1_func(x_vals), label=sp.latex(eq1))
if f2:
f2_func = sp.lambdify(x, f2[0], modules='numpy')
ax.plot(x_vals, f2_func(x_vals), label=sp.latex(eq2))
ax.plot(solution[x], solution[y], 'ro', label=f"Solution ({solution[x]}, {solution[y]})")
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.legend()
ax.set_title("Graph of the Linear System")
ax.grid(True)
return eq_latex, steps, fig, ""
except Exception as e:
return f"β Error: {e}", None, None, None
def extract_equation_from_image(image_file):
try:
if p2t_model is None:
return {
"type": "error",
"latex": "Pix2Text model not loaded. Please check installation.",
"success": False
}
if image_file is None:
return {
"type": "error",
"latex": "No image file provided.",
"success": False
}
if isinstance(image_file, str):
image = Image.open(image_file)
else:
image = Image.open(image_file.name)
if image.mode != 'RGB':
image = image.convert('RGB')
logger.info(f"Processing image of size: {image.size}")
result = p2t_model.recognize_text_formula(image)
if not result or result.strip() == "":
return {
"type": "error",
"latex": "No text or formulas detected in the image.",
"success": False
}
logger.info(f"Extracted text: {result}")
eq_type = parse_equation_type(result)
if eq_type == 'polynomial':
return extract_polynomial_coefficients(result)
elif eq_type == 'linear_system':
return extract_linear_system_coefficients(result)
else:
return {
"type": "error",
"latex": f"Unsupported equation type detected: {eq_type}",
"success": False
}
except Exception as e:
logger.error(f"Error processing image: {e}")
return {
"type": "error",
"latex": f"Error processing image: {str(e)}",
"success": False
}
def solve_extracted_equation(eq_data, real_only):
if eq_data["type"] == "polynomial":
return solve_polynomial(eq_data["degree"], eq_data["coeffs"], real_only, eq_data.get("variable", "x"))
elif eq_data["type"] == "linear":
return "β Single linear equation not supported. Please upload a system of equations.", None, ""
elif eq_data["type"] == "linear_system":
return solve_linear_system_from_coeffs(eq_data["eq1_coeffs"], eq_data["eq2_coeffs"])
else:
return "β Unknown equation type", None, ""
def image_tab():
"""Create the Image Upload Solver tab"""
with gr.Tab("Image Upload Solver"):
gr.Markdown("## Solve Equations from Image")
with gr.Row():
image_input = gr.File(
label="Upload Question Image",
file_types=[".pdf", ".png", ".jpg", ".jpeg"],
file_count="single"
)
image_upload_btn = gr.Button("Process Image")
gr.Markdown("**Supported Formats:** .pdf, .png, .jpg, .jpeg")
with gr.Row():
real_image_checkbox = gr.Checkbox(label="Show Only Real Roots (for Polynomials)", value=False)
preview_image_btn = gr.Button("Preview Equation")
image_equation_display = gr.Markdown()
with gr.Row():
confirm_image_btn = gr.Button("Display Solution", visible=False)
edit_image_btn = gr.Button("Make Changes Manually", visible=False)
edit_latex_input = gr.Textbox(label="Edit LaTeX Equation", visible=False, lines=3)
save_edit_btn = gr.Button("Save Changes", visible=False)
image_steps_md = gr.Markdown()
image_plot_output = gr.Plot()
extracted_eq_state = gr.State()
llm_url_input = gr.Textbox(label="LLM Microservice URL (optional)", placeholder="https://your-llm.ngrok.app")
explain_image_btn = gr.Button("Explain with LLM")
image_solution_txt = gr.Textbox(visible=False)
def handle_image_upload(image_file):
if image_file is None:
return "", None, "", None, None
try:
eq_data = extract_equation_from_image(image_file)
return "", eq_data, "", None, None
except Exception:
return "", None, "", None, None
image_upload_btn.click(
fn=handle_image_upload,
inputs=[image_input],
outputs=[image_equation_display, extracted_eq_state, image_steps_md,
image_plot_output, edit_latex_input]
)
def preview_image_equation(eq_data, real_only):
if not eq_data:
return "β οΈ No equation data available.", gr.update(visible=False), gr.update(visible=False), "", None
if eq_data["type"] == "error":
return eq_data["latex"], gr.update(visible=False), gr.update(visible=False), "", None
preview_text = f"""
### β
Confirm {'Polynomial' if eq_data['type'] == 'polynomial' else 'Linear System'}
**Extracted LaTeX:** {eq_data['latex']}
"""
return preview_text, gr.update(visible=True), gr.update(visible=True), "", None
preview_image_btn.click(
fn=preview_image_equation,
inputs=[extracted_eq_state, real_image_checkbox],
outputs=[image_equation_display, confirm_image_btn, edit_image_btn,
image_steps_md, image_plot_output]
)
def confirm_image_solution(eq_data, real_only):
if not eq_data or eq_data["type"] == "error":
return "β οΈ No valid equation to solve.", None, ""
try:
steps, plot, _ = solve_extracted_equation(eq_data, real_only)
return steps, plot, gr.update(value=steps)
except Exception as e:
return f"β Error solving equation: {str(e)}", None, ""
confirm_image_btn.click(
fn=confirm_image_solution,
inputs=[extracted_eq_state, real_image_checkbox],
outputs=[image_steps_md, image_plot_output, image_solution_txt]
)
def enable_manual_edit(eq_data):
latex_value = eq_data.get("latex", "") if eq_data and eq_data["type"] != "error" else "Error in extraction."
return (
gr.update(visible=True, value=latex_value),
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False)
)
edit_image_btn.click(
fn=enable_manual_edit,
inputs=[extracted_eq_state],
outputs=[edit_latex_input, save_edit_btn, confirm_image_btn, edit_image_btn]
)
def save_manual_changes(latex_input, real_only):
try:
if not latex_input.strip():
return "β οΈ Please enter a valid equation.", None, ""
eq_type = parse_equation_type(latex_input)
if eq_type == 'polynomial':
eq_data = extract_polynomial_coefficients(latex_input)
steps, plot, _ = solve_polynomial(eq_data["degree"], eq_data["coeffs"], real_only, eq_data.get("variable", "x"))
elif eq_type == 'linear_system':
eq_data = extract_linear_system_coefficients(latex_input)
_, steps, plot, _ = solve_linear_system_from_coeffs(eq_data["eq1_coeffs"], eq_data["eq2_coeffs"])
else:
return "β Unsupported equation type", None, ""
return steps, plot, gr.update(value=steps)
except Exception as e:
return f"β Error parsing manual input: {str(e)}", None, ""
save_edit_btn.click(
fn=save_manual_changes,
inputs=[edit_latex_input, real_image_checkbox],
outputs=[image_steps_md, image_plot_output, image_solution_txt]
)
explain_image_btn.click(
fn=lambda sol, url: explain_with_llm(sol, "image", url),
inputs=[image_solution_txt, llm_url_input],
outputs=[image_steps_md]
)
return (
image_input, image_upload_btn, real_image_checkbox, preview_image_btn,
image_equation_display, confirm_image_btn, edit_image_btn, edit_latex_input,
save_edit_btn, image_steps_md, image_plot_output, extracted_eq_state,
llm_url_input, explain_image_btn, image_solution_txt
)
|