Spaces:
Running
Running
Update working_yolo_pipeline.py
Browse files- working_yolo_pipeline.py +50 -4
working_yolo_pipeline.py
CHANGED
|
@@ -91,6 +91,52 @@ def sanitize_text(text: Optional[str]) -> str:
|
|
| 91 |
|
| 92 |
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def get_latex_from_base64(base64_string: str) -> str:
|
| 95 |
"""
|
| 96 |
Decodes a Base64 image string and uses the pre-initialized TrOCR/ORT model
|
|
@@ -123,7 +169,10 @@ def get_latex_from_base64(base64_string: str) -> str:
|
|
| 123 |
# A. Remove all spaces/line breaks
|
| 124 |
cleaned_latex = re.sub(r'\s+', '', latex_string)
|
| 125 |
|
| 126 |
-
# B. CRITICAL FIX: Replace double backslashes with single backslashes.
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
return cleaned_latex
|
| 129 |
|
|
@@ -136,9 +185,6 @@ def get_latex_from_base64(base64_string: str) -> str:
|
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
# ============================================================================
|
| 143 |
# --- CONFIGURATION AND CONSTANTS ---
|
| 144 |
# ============================================================================
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
+
# def get_latex_from_base64(base64_string: str) -> str:
|
| 95 |
+
# """
|
| 96 |
+
# Decodes a Base64 image string and uses the pre-initialized TrOCR/ORT model
|
| 97 |
+
# to recognize the formula. It cleans the output by removing spaces and
|
| 98 |
+
# crucially, replacing double backslashes with single backslashes for correct LaTeX.
|
| 99 |
+
# """
|
| 100 |
+
# if ort_model is None or processor is None:
|
| 101 |
+
# return "[MODEL_ERROR: Model not initialized]"
|
| 102 |
+
|
| 103 |
+
# try:
|
| 104 |
+
# # 1. Decode Base64 to Image
|
| 105 |
+
# image_data = base64.b64decode(base64_string)
|
| 106 |
+
# # We must ensure the image is RGB format for the model input
|
| 107 |
+
# image = Image.open(io.BytesIO(image_data)).convert('RGB')
|
| 108 |
+
|
| 109 |
+
# # 2. Preprocess the image
|
| 110 |
+
# pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 111 |
+
|
| 112 |
+
# # 3. Text Generation (OCR)
|
| 113 |
+
# generated_ids = ort_model.generate(pixel_values)
|
| 114 |
+
# raw_generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 115 |
+
|
| 116 |
+
# if not raw_generated_text:
|
| 117 |
+
# return "[OCR_WARNING: No formula found]"
|
| 118 |
+
|
| 119 |
+
# latex_string = raw_generated_text[0]
|
| 120 |
+
|
| 121 |
+
# # --- 4. Post-processing and Cleanup ---
|
| 122 |
+
|
| 123 |
+
# # A. Remove all spaces/line breaks
|
| 124 |
+
# cleaned_latex = re.sub(r'\s+', '', latex_string)
|
| 125 |
+
|
| 126 |
+
# # B. CRITICAL FIX: Replace double backslashes with single backslashes.
|
| 127 |
+
|
| 128 |
+
# return cleaned_latex
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# except Exception as e:
|
| 132 |
+
# # Catch any unexpected errors
|
| 133 |
+
# print(f" ❌ TR-OCR Recognition failed: {e}")
|
| 134 |
+
# return f"[TR_OCR_ERROR: Recognition failed: {e}]"
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
def get_latex_from_base64(base64_string: str) -> str:
|
| 141 |
"""
|
| 142 |
Decodes a Base64 image string and uses the pre-initialized TrOCR/ORT model
|
|
|
|
| 169 |
# A. Remove all spaces/line breaks
|
| 170 |
cleaned_latex = re.sub(r'\s+', '', latex_string)
|
| 171 |
|
| 172 |
+
# B. CRITICAL FIX: Replace double backslashes (\\) with single backslashes (\).
|
| 173 |
+
# This corrects model output that already over-escaped the LaTeX commands.
|
| 174 |
+
# Python literal: '\\\\' is replaced with '\\'.
|
| 175 |
+
cleaned_latex = cleaned_latex.replace('\\\\', '\\')
|
| 176 |
|
| 177 |
return cleaned_latex
|
| 178 |
|
|
|
|
| 185 |
|
| 186 |
|
| 187 |
|
|
|
|
|
|
|
|
|
|
| 188 |
# ============================================================================
|
| 189 |
# --- CONFIGURATION AND CONSTANTS ---
|
| 190 |
# ============================================================================
|