Spaces:
Sleeping
Sleeping
| import fitz # PyMuPDF | |
| import docx | |
| import os | |
| def extract_text_from_file(file_path: str, original_filename: str) -> str: | |
| """ | |
| Enterprise router that ingests a file, identifies its type, | |
| and extracts raw text for the LLM pipeline. | |
| """ | |
| # Get the file extension and convert to lowercase | |
| _, file_extension = os.path.splitext(original_filename) | |
| ext = file_extension.lower() | |
| try: | |
| # Route 1: PDF Extraction | |
| if ext == '.pdf': | |
| return _extract_from_pdf(file_path) | |
| # Route 2: Word Document Extraction | |
| elif ext in ['.docx', '.doc']: | |
| return _extract_from_docx(file_path) | |
| # Route 3: Plain Text / Markdown Extraction | |
| elif ext in ['.txt', '.md', '.csv']: | |
| with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: | |
| return f.read() | |
| # The Bouncer: Reject unsupported files gracefully | |
| else: | |
| raise ValueError(f"Unsupported file type: {ext}. Please upload PDF, DOCX, or TXT.") | |
| except Exception as e: | |
| raise RuntimeError(f"Failed to parse document: {str(e)}") | |
| # --- Private Helper Functions --- | |
| def _extract_from_pdf(file_path: str) -> str: | |
| text_content = [] | |
| # fitz opens the document securely | |
| with fitz.open(file_path) as doc: | |
| for page_num in range(len(doc)): | |
| page = doc.load_page(page_num) | |
| text_content.append(page.get_text("text")) | |
| # Clean up the output by joining pages and stripping excess whitespace | |
| return "\n".join(text_content).strip() | |
| def _extract_from_docx(file_path: str) -> str: | |
| doc = docx.Document(file_path) | |
| text_content = [paragraph.text for paragraph in doc.paragraphs if paragraph.text.strip()] | |
| return "\n".join(text_content).strip() |