Spaces:
Sleeping
Sleeping
| import io | |
| import fitz | |
| import docx | |
| MAX_BYTES = 10 * 1024 * 1024 # 10 MB | |
| MAX_PAGES = 100 | |
| MAX_CHARS = MAX_PAGES * 3000 # ~3000 chars/page ceiling | |
| def extract_pdf(data: bytes) -> str: | |
| doc = fitz.open(stream=data, filetype="pdf") | |
| pages = min(len(doc), MAX_PAGES) | |
| parts = [] | |
| for i in range(pages): | |
| text = doc[i].get_text("text") | |
| parts.append(text) | |
| return "\n".join(parts)[:MAX_CHARS] | |
| def extract_docx(data: bytes) -> str: | |
| doc = docx.Document(io.BytesIO(data)) | |
| lines = [] | |
| for para in doc.paragraphs: | |
| t = para.text.strip() | |
| if t: | |
| lines.append(t) | |
| return "\n".join(lines)[:MAX_CHARS] | |
| def extract_text(data: bytes, filename: str) -> str: | |
| if len(data) > MAX_BYTES: | |
| raise ValueError("File exceeds 10 MB limit.") | |
| name = filename.lower() | |
| if name.endswith(".pdf"): | |
| return extract_pdf(data) | |
| if name.endswith(".docx"): | |
| return extract_docx(data) | |
| raise ValueError("Unsupported file type. Upload PDF or DOCX.") | |