Spaces:
Running
Update working_yolo_pipeline.py
Browse filesdef get_latex_from_base64(base64_string: str) -> str:
if p2t is None:
return "[P2T_ERROR: Model not initialized]"
try:
image_data = base64.b64decode(base64_string)
image = Image.open(io.BytesIO(image_data))
result = p2t.recognize(
image,
save_formula_images=False,
use_analyzer=True,
keep_original_image=False
)
extracted = []
if isinstance(result, list):
for item in result:
text = item.text if hasattr(item, "text") else str(item)
extracted.append(text)
else:
extracted = [result]
latex = " ".join(extracted).strip()
latex = re.sub(r"\s+", "", latex)
if not latex:
return "[P2T_WARNING: No formula found]"
# Correct way: fix ONLY real double-backslashes produced by Pix2Text
latex = latex.replace(r"\\", r"\")
return latex
except Exception as e:
print(f"❌ Pix2Text Recognition failed: {e}")
return f"[P2T_ERROR: Recognition failed: {e}]"
- working_yolo_pipeline.py +80 -38
|
@@ -273,54 +273,97 @@ except Exception as e:
|
|
| 273 |
|
| 274 |
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
def get_latex_from_base64(base64_string: str) -> str:
|
| 277 |
-
"""
|
| 278 |
-
Decodes a Base64 image string, uses Pix2Text to recognize the formula,
|
| 279 |
-
and returns the LaTeX code, stripped of all whitespace, as requested,
|
| 280 |
-
and corrects unintended double backslashes.
|
| 281 |
-
"""
|
| 282 |
if p2t is None:
|
| 283 |
return "[P2T_ERROR: Model not initialized]"
|
| 284 |
|
| 285 |
try:
|
| 286 |
-
# 1. Decode Base64 to Image
|
| 287 |
image_data = base64.b64decode(base64_string)
|
| 288 |
image = Image.open(io.BytesIO(image_data))
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
| 296 |
if isinstance(result, list):
|
| 297 |
for item in result:
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
# *** CORE CHANGE 2: Fix unintended double backslashes for LaTeX rendering ***
|
| 314 |
-
# This replaces every sequence of two literal backslashes ('\\') with one literal backslash ('\'),
|
| 315 |
-
# ensuring LaTeX commands like '\frac' are correctly formed.
|
| 316 |
-
final_latex = cleaned_latex.replace('\\\\', '\\')
|
| 317 |
-
|
| 318 |
-
# Return the clean and corrected LaTeX string.
|
| 319 |
-
return final_latex
|
| 320 |
|
| 321 |
except Exception as e:
|
| 322 |
-
|
| 323 |
-
print(f" ❌ Pix2Text Recognition failed: {e}")
|
| 324 |
return f"[P2T_ERROR: Recognition failed: {e}]"
|
| 325 |
|
| 326 |
|
|
@@ -329,7 +372,6 @@ def get_latex_from_base64(base64_string: str) -> str:
|
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
-
|
| 333 |
# # Initialize the YOLO model
|
| 334 |
# model = YOLO(WEIGHTS_PATH)
|
| 335 |
|
|
|
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
+
# def get_latex_from_base64(base64_string: str) -> str:
|
| 277 |
+
# """
|
| 278 |
+
# Decodes a Base64 image string, uses Pix2Text to recognize the formula,
|
| 279 |
+
# and returns the LaTeX code, stripped of all whitespace, as requested,
|
| 280 |
+
# and corrects unintended double backslashes.
|
| 281 |
+
# """
|
| 282 |
+
# if p2t is None:
|
| 283 |
+
# return "[P2T_ERROR: Model not initialized]"
|
| 284 |
+
|
| 285 |
+
# try:
|
| 286 |
+
# # 1. Decode Base64 to Image
|
| 287 |
+
# image_data = base64.b64decode(base64_string)
|
| 288 |
+
# image = Image.open(io.BytesIO(image_data))
|
| 289 |
+
|
| 290 |
+
# # 2. Recognize text and formulas
|
| 291 |
+
# # Use keep_original_image=False to save memory
|
| 292 |
+
# result = p2t.recognize(image, save_formula_images=False, use_analyzer=True, keep_original_image=False)
|
| 293 |
+
|
| 294 |
+
# # 3. Parse the result for LaTeX
|
| 295 |
+
# extracted_latex_parts = []
|
| 296 |
+
# if isinstance(result, list):
|
| 297 |
+
# for item in result:
|
| 298 |
+
# # Use .text for structured output, item itself for string output
|
| 299 |
+
# text = item.text if hasattr(item, 'text') else str(item)
|
| 300 |
+
# extracted_latex_parts.append(text)
|
| 301 |
+
# elif isinstance(result, str):
|
| 302 |
+
# extracted_latex_parts = [result]
|
| 303 |
+
|
| 304 |
+
# # Join with a space first, then clean all whitespace
|
| 305 |
+
# extracted_latex = " ".join(extracted_latex_parts).strip()
|
| 306 |
+
|
| 307 |
+
# # *** CORE CHANGE 1: Remove all spaces/line breaks ***
|
| 308 |
+
# cleaned_latex = re.sub(r'\s+', '', extracted_latex)
|
| 309 |
+
|
| 310 |
+
# if not cleaned_latex:
|
| 311 |
+
# return "[P2T_WARNING: No formula found]"
|
| 312 |
+
|
| 313 |
+
# # *** CORE CHANGE 2: Fix unintended double backslashes for LaTeX rendering ***
|
| 314 |
+
# # This replaces every sequence of two literal backslashes ('\\') with one literal backslash ('\'),
|
| 315 |
+
# # ensuring LaTeX commands like '\frac' are correctly formed.
|
| 316 |
+
# final_latex = cleaned_latex.replace('\\\\', '\\')
|
| 317 |
+
|
| 318 |
+
# # Return the clean and corrected LaTeX string.
|
| 319 |
+
# return final_latex
|
| 320 |
+
|
| 321 |
+
# except Exception as e:
|
| 322 |
+
# # Catch any unexpected errors
|
| 323 |
+
# print(f" ❌ Pix2Text Recognition failed: {e}")
|
| 324 |
+
# return f"[P2T_ERROR: Recognition failed: {e}]"
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
|
| 331 |
def get_latex_from_base64(base64_string: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
if p2t is None:
|
| 333 |
return "[P2T_ERROR: Model not initialized]"
|
| 334 |
|
| 335 |
try:
|
|
|
|
| 336 |
image_data = base64.b64decode(base64_string)
|
| 337 |
image = Image.open(io.BytesIO(image_data))
|
| 338 |
+
|
| 339 |
+
result = p2t.recognize(
|
| 340 |
+
image,
|
| 341 |
+
save_formula_images=False,
|
| 342 |
+
use_analyzer=True,
|
| 343 |
+
keep_original_image=False
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
extracted = []
|
| 347 |
if isinstance(result, list):
|
| 348 |
for item in result:
|
| 349 |
+
text = item.text if hasattr(item, "text") else str(item)
|
| 350 |
+
extracted.append(text)
|
| 351 |
+
else:
|
| 352 |
+
extracted = [result]
|
| 353 |
+
|
| 354 |
+
latex = " ".join(extracted).strip()
|
| 355 |
+
latex = re.sub(r"\s+", "", latex)
|
| 356 |
+
|
| 357 |
+
if not latex:
|
| 358 |
+
return "[P2T_WARNING: No formula found]"
|
| 359 |
+
|
| 360 |
+
# Correct way: fix ONLY real double-backslashes produced by Pix2Text
|
| 361 |
+
latex = latex.replace(r"\\", r"\")
|
| 362 |
+
|
| 363 |
+
return latex
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
except Exception as e:
|
| 366 |
+
print(f"❌ Pix2Text Recognition failed: {e}")
|
|
|
|
| 367 |
return f"[P2T_ERROR: Recognition failed: {e}]"
|
| 368 |
|
| 369 |
|
|
|
|
| 372 |
|
| 373 |
|
| 374 |
|
|
|
|
| 375 |
# # Initialize the YOLO model
|
| 376 |
# model = YOLO(WEIGHTS_PATH)
|
| 377 |
|