File size: 9,708 Bytes
6d6b8af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""

Codette Multimodal Analyzer



Provides lightweight, dependency-safe analysis helpers for text, image,

audio and video inputs. Functions accept either raw bytes, a filesystem

path (str) or simple Python objects (e.g. numpy arrays for audio).



This module intentionally avoids heavy mandatory dependencies. If

optional libraries (Pillow, OpenCV) are installed, the analyzer will use

them for richer metadata extraction; otherwise it falls back to safe

heuristics and headers.

"""

from typing import Dict, Any, List, Union, Optional
import io
import os
import imghdr
import mimetypes
import wave
import struct

try:
    from PIL import Image
except Exception:
    Image = None

try:
    import cv2
except Exception:
    cv2 = None

try:
    import numpy as np
except Exception:
    np = None


class MultimodalAnalyzer:
    def __init__(self):
        self.supported_modalities = {
            "text": self._analyze_text,
            "image": self._analyze_image,
            "audio": self._analyze_audio,
            "video": self._analyze_video,
        }

    def analyze_content(self, content: Dict[str, Any]) -> Dict[str, Any]:
        results: Dict[str, Any] = {}
        for modality, data in content.items():
            handler = self.supported_modalities.get(modality)
            if handler is None:
                results[modality] = {"error": "Unsupported modality"}
                continue
            try:
                results[modality] = handler(data)
            except Exception as e:
                results[modality] = {"error": str(e)}
        return results

    def _analyze_text(self, text: Union[str, bytes]) -> Dict[str, Any]:
        if isinstance(text, bytes):
            try:
                text = text.decode("utf-8", errors="replace")
            except Exception:
                text = str(text)
        text = text or ""
        words = [w for w in text.split() if w]
        unique_words = set(w.strip(".,!?;:\"()[]{}") for w in words)
        avg_word_len = sum(len(w) for w in words) / len(words) if words else 0
        has_questions = "?" in text
        has_exclamations = "!" in text
        language = "en" if all(ord(c) < 128 for c in text) else "non-en"
        return {
            "type": "text",
            "length": len(text),
            "word_count": len(words),
            "unique_word_count": len(unique_words),
            "avg_word_length": round(avg_word_len, 2),
            "has_content": bool(text.strip()),
            "has_questions": has_questions,
            "has_exclamations": has_exclamations,
            "language_estimate": language,
        }

    def _read_bytes_or_path(self, data: Union[bytes, str]) -> Optional[bytes]:
        if data is None:
            return None
        if isinstance(data, bytes):
            return data
        if isinstance(data, str):
            if os.path.exists(data):
                try:
                    with open(data, "rb") as f:
                        return f.read()
                except Exception:
                    return None
            # treat string as raw small payload
            return data.encode("utf-8", errors="replace")
        return None

    def _analyze_image(self, image_data: Union[bytes, str, None]) -> Dict[str, Any]:
        raw = self._read_bytes_or_path(image_data)
        info: Dict[str, Any] = {"type": "image", "has_content": bool(raw)}
        if not raw:
            return info
        fmt = None
        try:
            fmt = imghdr.what(None, h=raw)
        except Exception:
            fmt = None
        if fmt:
            info["format"] = fmt
        else:
            # fallback to mime type by filename if provided
            if isinstance(image_data, str):
                mt, _ = mimetypes.guess_type(image_data)
                info["format"] = mt or "unknown"
            else:
                info["format"] = "unknown"
        if Image is not None:
            try:
                img = Image.open(io.BytesIO(raw))
                info.update({
                    "width": img.width,
                    "height": img.height,
                    "mode": img.mode,
                    "has_alpha": "A" in img.getbands(),
                })
                img.close()
            except Exception:
                pass
        return info

    def _analyze_audio(self, audio_data: Union[bytes, str, Any, None]) -> Dict[str, Any]:
        info: Dict[str, Any] = {"type": "audio", "has_content": False, "format": "unknown"}
        if audio_data is None:
            return info
        if np is not None and isinstance(audio_data, np.ndarray):
            arr = audio_data
            info["has_content"] = getattr(arr, "size", 0) > 0
            info["format"] = "numpy.ndarray"
            try:
                samples = arr.astype(float)
                rms = float(np.sqrt(np.mean(samples ** 2)))
                info["rms"] = float(rms)
                info["duration_seconds_estimate"] = None
            except Exception:
                pass
            return info

        raw = self._read_bytes_or_path(audio_data)
        if not raw:
            return info
        info["has_content"] = True
        # Try WAV detection
        try:
            bio = io.BytesIO(raw)
            with wave.open(bio, "rb") as w:
                nchannels = w.getnchannels()
                sampwidth = w.getsampwidth()
                framerate = w.getframerate()
                nframes = w.getnframes()
                duration = nframes / float(framerate) if framerate else None
                info.update({
                    "format": "wav",
                    "channels": nchannels,
                    "sample_width": sampwidth,
                    "frame_rate": framerate,
                    "n_frames": nframes,
                    "duration_seconds": duration,
                })
                try:
                    frames = w.readframes(min(nframes, 44100))
                    if frames:
                        # unpack frames to numpy array for RMS
                        if sampwidth == 1:
                            dtype = np.uint8
                        elif sampwidth == 2:
                            dtype = np.int16
                        elif sampwidth == 4:
                            dtype = np.int32
                        else:
                            dtype = np.int16
                        samples = np.frombuffer(frames, dtype=dtype).astype(float)
                        if nchannels > 1:
                            samples = samples.reshape(-1, nchannels)
                            samples = samples.mean(axis=1)
                        rms = float(np.sqrt(np.mean((samples) ** 2)))
                        info["rms"] = rms
                except Exception:
                    pass
                return info
        except wave.Error:
            pass
        except Exception:
            pass
        # fallback: try to guess mime type by filename
        if isinstance(audio_data, str):
            mt, _ = mimetypes.guess_type(audio_data)
            if mt:
                info["format"] = mt
        return info

    def _analyze_video(self, video_data: Union[bytes, str, None]) -> Dict[str, Any]:
        info: Dict[str, Any] = {"type": "video", "has_content": False, "format": "unknown"}
        raw = self._read_bytes_or_path(video_data)
        if not raw:
            return info
        info["has_content"] = True
        if isinstance(video_data, str):
            mt, _ = mimetypes.guess_type(video_data)
            if mt:
                info["format"] = mt
        # If OpenCV is available, try to extract metadata
        if cv2 is not None and isinstance(video_data, str) and os.path.exists(video_data):
            try:
                cap = cv2.VideoCapture(video_data)
                if cap.isOpened():
                    fps = cap.get(cv2.CAP_PROP_FPS) or None
                    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
                    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
                    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)
                    duration = frame_count / fps if fps else None
                    info.update({
                        "fps": fps,
                        "frame_count": frame_count,
                        "width": width,
                        "height": height,
                        "duration_seconds": duration,
                    })
                cap.release()
            except Exception:
                pass
        return info

    def combine_modalities(self, analyses: Dict[str, Any]) -> Dict[str, Any]:
        modalities_present = [k for k, v in analyses.items() if not v.get("error")]
        summary = {
            "modalities_present": modalities_present,
            "modality_count": len(modalities_present),
            "complete_analysis": all(not v.get("error") for v in analyses.values()),
            "analyses": analyses,
        }
        if "text" in analyses and "image" in analyses:
            t = analyses.get("text", {})
            img = analyses.get("image", {})
            summary["text_and_image"] = {
                "text_length": t.get("length"),
                "image_size": (img.get("width"), img.get("height")),
            }
        return summary

    def get_supported_modalities(self) -> List[str]:
        return list(self.supported_modalities.keys())