Spaces:
Runtime error
Runtime error
| # File : app/documents/parser.py | |
| # Purpose : Extract text from PDF, DOCX, TXT files | |
| import io | |
| from pathlib import Path | |
| from typing import List, Dict | |
| import pdfplumber | |
| from docx import Document as DocxDocument | |
| def parse_pdf(file_bytes: bytes) -> List[Dict]: | |
| pages = [] | |
| with pdfplumber.open(io.BytesIO(file_bytes)) as pdf: | |
| for i, page in enumerate(pdf.pages): | |
| text = page.extract_text() or "" | |
| text = text.strip() | |
| if text: | |
| pages.append({"page_number": i + 1, "content": text}) | |
| return pages | |
| def parse_docx(file_bytes: bytes) -> List[Dict]: | |
| doc = DocxDocument(io.BytesIO(file_bytes)) | |
| full_text = "\n".join([p.text for p in doc.paragraphs if p.text.strip()]) | |
| return [{"page_number": 1, "content": full_text}] | |
| def parse_txt(file_bytes: bytes) -> List[Dict]: | |
| text = file_bytes.decode("utf-8", errors="ignore").strip() | |
| return [{"page_number": 1, "content": text}] | |
| def parse_document(file_bytes: bytes, filename: str) -> List[Dict]: | |
| ext = Path(filename).suffix.lower() | |
| if ext == ".pdf": | |
| return parse_pdf(file_bytes) | |
| elif ext == ".docx": | |
| return parse_docx(file_bytes) | |
| elif ext == ".txt": | |
| return parse_txt(file_bytes) | |
| else: | |
| raise ValueError(f"Unsupported file type: {ext}") |