Spaces:
Running
Running
Dipti Dhawade commited on
Commit ·
c993217
1
Parent(s): 0b1c7ae
Strip OCR images
Browse files- backend/llm_pipeline.py +17 -2
backend/llm_pipeline.py
CHANGED
|
@@ -145,6 +145,21 @@ def _prepare_document_for_document_intel(file_bytes: bytes, mime: str) -> tuple[
|
|
| 145 |
return _prepare_image_for_document_intel(file_bytes, m or "image/jpeg")
|
| 146 |
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
def _extract_markdown_from_output_zip(zip_path: str) -> str:
|
| 149 |
with zipfile.ZipFile(zip_path, "r") as z:
|
| 150 |
names = sorted(z.namelist())
|
|
@@ -155,7 +170,7 @@ def _extract_markdown_from_output_zip(zip_path: str) -> str:
|
|
| 155 |
)
|
| 156 |
if not pick:
|
| 157 |
return ""
|
| 158 |
-
return z.read(pick).decode("utf-8", errors="replace").strip()
|
| 159 |
|
| 160 |
|
| 161 |
def _strings_in(value) -> list[str]:
|
|
@@ -188,7 +203,7 @@ def _page_texts_from_output_zip(zip_path: str) -> list[str]:
|
|
| 188 |
data = json.loads(z.read(name).decode("utf-8", errors="replace"))
|
| 189 |
except json.JSONDecodeError:
|
| 190 |
continue
|
| 191 |
-
pages.append((int(m.group(1)), " ".join(_strings_in(data))))
|
| 192 |
except zipfile.BadZipFile:
|
| 193 |
return []
|
| 194 |
return [text for _, text in sorted(pages)]
|
|
|
|
| 145 |
return _prepare_image_for_document_intel(file_bytes, m or "image/jpeg")
|
| 146 |
|
| 147 |
|
| 148 |
+
# Sarvam's markdown embeds every figure it finds as a base64 image: often hundreds of KB per page,
|
| 149 |
+
# which would flood search with junk passages. Keep a short marker instead.
|
| 150 |
+
_INLINE_IMAGE_RES = (
|
| 151 |
+
re.compile(r"!\[[^\]]*\]\(\s*data:[^)]*\)"),
|
| 152 |
+
re.compile(r"<img\b[^>]*\bsrc\s*=\s*[\"']data:[^\"']*[\"'][^>]*>", re.IGNORECASE),
|
| 153 |
+
re.compile(r"data:image/[\w.+-]+;base64,[A-Za-z0-9+/=\s]{64,}"),
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def strip_inline_images(text: str) -> str:
|
| 158 |
+
for pattern in _INLINE_IMAGE_RES:
|
| 159 |
+
text = pattern.sub("[figure]", text or "")
|
| 160 |
+
return text
|
| 161 |
+
|
| 162 |
+
|
| 163 |
def _extract_markdown_from_output_zip(zip_path: str) -> str:
|
| 164 |
with zipfile.ZipFile(zip_path, "r") as z:
|
| 165 |
names = sorted(z.namelist())
|
|
|
|
| 170 |
)
|
| 171 |
if not pick:
|
| 172 |
return ""
|
| 173 |
+
return strip_inline_images(z.read(pick).decode("utf-8", errors="replace")).strip()
|
| 174 |
|
| 175 |
|
| 176 |
def _strings_in(value) -> list[str]:
|
|
|
|
| 203 |
data = json.loads(z.read(name).decode("utf-8", errors="replace"))
|
| 204 |
except json.JSONDecodeError:
|
| 205 |
continue
|
| 206 |
+
pages.append((int(m.group(1)), strip_inline_images(" ".join(_strings_in(data)))))
|
| 207 |
except zipfile.BadZipFile:
|
| 208 |
return []
|
| 209 |
return [text for _, text in sorted(pages)]
|