File size: 2,748 Bytes
501bc8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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