Download src/engine/document_processor.py from YGCXLab/ux-research-agent-slackbot: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/src/engine/document_processor.py
- Command line
-
hf download hf://spaces/YGCXLab/ux-research-agent-slackbot/src/engine/document_processor.py
-
curl -L -o document_processor.py https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/src/engine/document_processor.py
2.75 kB
| import io | |
| import logging | |
| import base64 | |
| from typing import Dict, Optional, Tuple, Any | |
| # Lazy imports for heavy libraries could be considered, but user asked to move imports to top. | |
| # pypdf and docx are used for document processing. | |
| try: | |
| from pypdf import PdfReader | |
| except ImportError: | |
| PdfReader = None | |
| try: | |
| from docx import Document | |
| except ImportError: | |
| Document = None | |
| logger = logging.getLogger("DocumentProcessor") | |
| class DocumentProcessor: | |
| def __init__(self): | |
| pass | |
| def process_media_file(self, media_file: Dict) -> Tuple[bool, str, Optional[str], Optional[str]]: | |
| """ | |
| Process media file and return (is_visual, attached_text, image_b64, mime_type). | |
| """ | |
| if not media_file: | |
| return False, "", None, None | |
| mime_type = media_file.get('mime_type', '') | |
| file_data = media_file.get('data') # bytes | |
| is_visual = False | |
| attached_text = "" | |
| image_b64 = None | |
| if "image" in mime_type or "video" in mime_type: | |
| is_visual = True | |
| image_b64 = base64.b64encode(file_data).decode('utf-8') | |
| logger.info(f"Processed Visual Media: {mime_type}") | |
| elif "pdf" in mime_type or "word" in mime_type or "docx" in mime_type: | |
| # Text Document | |
| extracted = self._extract_text_from_file(file_data, mime_type) | |
| if extracted: | |
| attached_text = f"\n\n[Attached Document Content]:\n{extracted[:10000]}..." | |
| logger.info(f"Extracted Text Length: {len(attached_text)}") | |
| else: | |
| attached_text = "\n\n[Attached Document]: (Failed to extract text)" | |
| return is_visual, attached_text, image_b64, mime_type | |
| def _extract_text_from_file(self, file_data: bytes, mime_type: str) -> str: | |
| text = "" | |
| try: | |
| if "pdf" in mime_type: | |
| if PdfReader: | |
| reader = PdfReader(io.BytesIO(file_data)) | |
| for page in reader.pages: | |
| extracted = page.extract_text() | |
| if extracted: | |
| text += extracted + "\n" | |
| else: | |
| logger.error("pypdf not installed.") | |
| elif "word" in mime_type or "docx" in mime_type: | |
| if Document: | |
| doc = Document(io.BytesIO(file_data)) | |
| for para in doc.paragraphs: | |
| text += para.text + "\n" | |
| else: | |
| logger.error("python-docx not installed.") | |
| except Exception as e: | |
| logger.error(f"Error extracting text from {mime_type}: {e}", exc_info=True) | |
| return "" | |
| return text | |