Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
|
|
|
| 3 |
import json
|
| 4 |
import zipfile
|
| 5 |
import asyncio
|
|
@@ -236,6 +237,41 @@ def prepare_file(file_bytes: bytes, content_type: str, filename: str) -> tuple[b
|
|
| 236 |
with open(out_path, "rb") as f:
|
| 237 |
return f.read(), Path(filename).stem + ".pdf"
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
# ---------------------------------------------------------------------------
|
| 240 |
# Step 1: Extract raw text via Sarvam Document Intelligence SDK
|
| 241 |
# ---------------------------------------------------------------------------
|
|
@@ -322,7 +358,6 @@ Respond with the JSON object only."""
|
|
| 322 |
|
| 323 |
raw = resp.json()["choices"][0]["message"]["content"].strip()
|
| 324 |
|
| 325 |
-
import re
|
| 326 |
raw = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL).strip()
|
| 327 |
|
| 328 |
if raw.startswith("```"):
|
|
@@ -410,6 +445,9 @@ async def extract_document(
|
|
| 410 |
)
|
| 411 |
raise
|
| 412 |
|
|
|
|
|
|
|
|
|
|
| 413 |
try:
|
| 414 |
extracted = await extract_json_via_chat(document_type, raw_text, param_list)
|
| 415 |
except HTTPException as exc:
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
+
import re
|
| 4 |
import json
|
| 5 |
import zipfile
|
| 6 |
import asyncio
|
|
|
|
| 237 |
with open(out_path, "rb") as f:
|
| 238 |
return f.read(), Path(filename).stem + ".pdf"
|
| 239 |
|
| 240 |
+
|
| 241 |
+
# ---------------------------------------------------------------------------
|
| 242 |
+
# Text cleanup — strip base64-encoded image blobs from OCR output
|
| 243 |
+
# ---------------------------------------------------------------------------
|
| 244 |
+
|
| 245 |
+
def clean_raw_text(text: str) -> str:
|
| 246 |
+
"""
|
| 247 |
+
Remove base64-encoded image data that Sarvam Document Intelligence
|
| 248 |
+
carries over from PDFs/images into the markdown output.
|
| 249 |
+
|
| 250 |
+
These blobs are never useful for field extraction and can easily push
|
| 251 |
+
the prompt far beyond sarvam-m's 7 192-token context window.
|
| 252 |
+
|
| 253 |
+
Two patterns are handled:
|
| 254 |
+
1. Markdown image syntax: 
|
| 255 |
+
2. Bare data URIs that leaked outside markdown syntax
|
| 256 |
+
"""
|
| 257 |
+
# Pattern 1 — markdown image embed
|
| 258 |
+
text = re.sub(
|
| 259 |
+
r'!\[[^\]]*\]\(data:[a-zA-Z]+/[a-zA-Z+\-]+;base64,[A-Za-z0-9+/=\s]+\)',
|
| 260 |
+
'',
|
| 261 |
+
text,
|
| 262 |
+
flags=re.DOTALL,
|
| 263 |
+
)
|
| 264 |
+
# Pattern 2 — bare data URI (must be at least 100 chars of base64 to avoid
|
| 265 |
+
# accidentally removing short legitimate strings)
|
| 266 |
+
text = re.sub(
|
| 267 |
+
r'data:[a-zA-Z]+/[a-zA-Z+\-]+;base64,[A-Za-z0-9+/=\s]{100,}',
|
| 268 |
+
'',
|
| 269 |
+
text,
|
| 270 |
+
flags=re.DOTALL,
|
| 271 |
+
)
|
| 272 |
+
return text.strip()
|
| 273 |
+
|
| 274 |
+
|
| 275 |
# ---------------------------------------------------------------------------
|
| 276 |
# Step 1: Extract raw text via Sarvam Document Intelligence SDK
|
| 277 |
# ---------------------------------------------------------------------------
|
|
|
|
| 358 |
|
| 359 |
raw = resp.json()["choices"][0]["message"]["content"].strip()
|
| 360 |
|
|
|
|
| 361 |
raw = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL).strip()
|
| 362 |
|
| 363 |
if raw.startswith("```"):
|
|
|
|
| 445 |
)
|
| 446 |
raise
|
| 447 |
|
| 448 |
+
# Strip base64 image blobs before sending to sarvam-m
|
| 449 |
+
raw_text = clean_raw_text(raw_text)
|
| 450 |
+
|
| 451 |
try:
|
| 452 |
extracted = await extract_json_via_chat(document_type, raw_text, param_list)
|
| 453 |
except HTTPException as exc:
|