aigenrec/luminabackend
0
1import os2from typing import Optional3import pymupdf4llm4from docx import Document5from bs4 import BeautifulSoup6import markdown7from utils.logger import logger8 9class FileParser:10 @staticmethod11 def extract_text(file_path: str) -> Optional[str]:12 """Extract text from various file formats"""13 try:14 _, ext = os.path.splitext(file_path)15 ext = ext.lower()16 17 if ext == '.pdf':18 return FileParser._extract_pdf(file_path)19 elif ext in ['.docx', '.doc']:20 return FileParser._extract_docx(file_path)21 elif ext == '.txt':22 return FileParser._extract_txt(file_path)23 elif ext == '.html':24 return FileParser._extract_html(file_path)25 elif ext == '.md':26 return FileParser._extract_markdown(file_path)27 else:28 logger.warning(f"Unsupported file type: {ext}")29 return None30 31 except Exception as e:32 logger.error(f"Error extracting text from {file_path}: {str(e)}")33 return None34 35 @staticmethod36 def _extract_pdf(file_path: str) -> str:37 """Extract text from PDF as Markdown using PyMuPDF4LLM"""38 try:39 # Convert PDF to Markdown40 # This handles tables, images (metadata), and headers much better than raw text extraction41 text = pymupdf4llm.to_markdown(file_path)42 return text.strip()43 except Exception as e:44 logger.error(f"PyMuPDF4LLM extraction failed: {str(e)}")45 # Fallback (optional, but let's fail hard or return empty for now to debug)46 raise e47 48 @staticmethod49 def _extract_docx(file_path: str) -> str:50 """Extract text from DOCX"""51 doc = Document(file_path)52 text = "\n".join([paragraph.text for paragraph in doc.paragraphs])53 return text.strip()54 55 @staticmethod56 def _extract_txt(file_path: str) -> str:57 """Extract text from TXT"""58 with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:59 return f.read().strip()60 61 @staticmethod62 def _extract_html(file_path: str) -> str:63 """Extract text from HTML"""64 with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:65 soup = BeautifulSoup(f.read(), 'html.parser')66 return soup.get_text().strip()67 68 @staticmethod69 def _extract_markdown(file_path: str) -> str:70 """Extract text from Markdown"""71 with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:72 md_text = f.read()73 html = markdown.markdown(md_text)74 soup = BeautifulSoup(html, 'html.parser')75 return soup.get_text().strip()76 