Spaces:
Sleeping
Sleeping
Download services/document_parser_service.py from Qurrah/replyflow-rag: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/services/document_parser_service.py
- Command line
-
hf download hf://spaces/Qurrah/replyflow-rag/services/document_parser_service.py
-
curl -L -o document_parser_service.py https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/services/document_parser_service.py
1.04 kB
| import re | |
| from io import BytesIO | |
| import pdfplumber | |
| SUPPORTED_MIME = ["application/pdf", "text/markdown", "text/plain"] | |
| def parse_file(content: bytes, mime: str) -> str: | |
| if mime == "application/pdf": | |
| text = [] | |
| with pdfplumber.open(BytesIO(content)) as pdf: | |
| for page in pdf.pages: | |
| page_text = page.extract_text() | |
| if page_text: | |
| text.append(page_text.strip()) | |
| return "\n\n".join(text) | |
| if mime in ("text/markdown", "text/plain"): | |
| raw = content.decode("utf-8", errors="ignore") | |
| if mime == "text/markdown": | |
| raw = re.sub(r"#{1,6}\s*", "", raw) | |
| raw = re.sub(r"\*\*(.+?)\*\*", r"\1", raw) | |
| raw = re.sub(r"\*(.+?)\*", r"\1", raw) | |
| raw = re.sub(r"`{1,3}.*?`{1,3}", "", raw, flags=re.DOTALL) | |
| raw = re.sub(r"!\[.*?\]\(.*?\)", "", raw) | |
| raw = re.sub(r"\[(.+?)\]\(.*?\)", r"\1", raw) | |
| return raw.strip() | |
| raise ValueError(f"Unsupported mime type: {mime}") | |