Spaces:
Sleeping
Sleeping
File size: 1,824 Bytes
2ef9449 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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() |