File size: 12,788 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import json
import sys
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from src import video_utils


class VideoUtilsDiarizationTests(unittest.TestCase):
    def test_format_transcript_for_analysis_uses_diarized_utterances(self):
        transcript = SimpleNamespace(
            utterances=[
                SimpleNamespace(
                    start=0,
                    end=2200,
                    speaker="A",
                    text="Hello there.",
                ),
                SimpleNamespace(
                    start=2200,
                    end=4600,
                    speaker="B",
                    text="General Kenobi.",
                ),
            ],
            words=[],
        )

        formatted = video_utils.format_transcript_for_analysis(transcript)

        self.assertEqual(
            formatted,
            [
                "[00:00 - 00:02] Speaker A: Hello there.",
                "[00:02 - 00:04] Speaker B: General Kenobi.",
            ],
        )

    def test_cache_transcript_data_stores_speakers_and_utterances(self):
        transcript = SimpleNamespace(
            text="Hello there.",
            words=[
                SimpleNamespace(
                    text="Hello",
                    start=0,
                    end=400,
                    confidence=0.98,
                    speaker="A",
                ),
                SimpleNamespace(
                    text="there.",
                    start=401,
                    end=900,
                    confidence=0.97,
                    speaker="A",
                ),
            ],
            utterances=[
                SimpleNamespace(
                    text="Hello there.",
                    start=0,
                    end=900,
                    speaker="A",
                    words=[
                        SimpleNamespace(
                            text="Hello",
                            start=0,
                            end=400,
                            confidence=0.98,
                            speaker="A",
                        ),
                        SimpleNamespace(
                            text="there.",
                            start=401,
                            end=900,
                            confidence=0.97,
                            speaker="A",
                        ),
                    ],
                )
            ],
        )

        with tempfile.TemporaryDirectory() as temp_dir:
            video_path = Path(temp_dir) / "sample.mp4"
            video_path.touch()

            video_utils.cache_transcript_data(video_path, transcript)

            cache_path = video_path.with_suffix(".transcript_cache.json")
            payload = json.loads(cache_path.read_text())

        self.assertEqual(payload["version"], video_utils.TRANSCRIPT_CACHE_SCHEMA_VERSION)
        self.assertEqual(payload["words"][0]["speaker"], "A")
        self.assertEqual(payload["utterances"][0]["speaker"], "A")
        self.assertEqual(payload["utterances"][0]["words"][0]["speaker"], "A")

    # Each transcription builds its own SDK client, which needs an API key.
    @patch("src.media.transcription.aai.Client")
    @patch("src.media.transcription.aai.Transcriber")
    @patch("src.media.transcription.aai.TranscriptionConfig")
    def test_get_video_transcript_enables_speaker_labels(
        self, mock_transcription_config, mock_transcriber, _mock_client
    ):
        transcript = SimpleNamespace(
            status=video_utils.aai.TranscriptStatus.completed,
            error=None,
            text="Hello there.",
            words=[
                SimpleNamespace(
                    text="Hello",
                    start=0,
                    end=400,
                    confidence=0.98,
                    speaker="A",
                )
            ],
            utterances=[
                SimpleNamespace(
                    start=0,
                    end=2200,
                    speaker="A",
                    text="Hello there.",
                    words=[],
                )
            ],
        )
        with patch(
            "src.media.transcription._submit_and_wait_for_assemblyai_transcript",
            return_value=transcript,
        ):
            with tempfile.TemporaryDirectory() as temp_dir:
                video_path = Path(temp_dir) / "sample.mp4"
                video_path.touch()
                result = video_utils.get_video_transcript(video_path)

        self.assertIn("Speaker A: Hello there.", result)
        mock_transcription_config.assert_called_once()
        self.assertTrue(mock_transcription_config.call_args.kwargs["speaker_labels"])

    def test_load_cached_transcript_data_supports_legacy_word_only_cache(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            video_path = Path(temp_dir) / "sample.mp4"
            video_path.touch()
            cache_path = video_path.with_suffix(".transcript_cache.json")
            cache_path.write_text(
                json.dumps(
                    {
                        "words": [
                            {"text": "legacy", "start": 0, "end": 300, "confidence": 1.0}
                        ],
                        "text": "legacy",
                    }
                )
            )

            payload = video_utils.load_cached_transcript_data(video_path)

        self.assertIsNotNone(payload)
        self.assertEqual(payload["words"][0]["text"], "legacy")

    def test_assemblyai_speech_models_value_maps_legacy_aliases(self):
        # AssemblyAI deprecated the singular speech_model; the plural list
        # prefers the current Pro model with universal-2 as the fallback.
        self.assertEqual(
            video_utils._assemblyai_speech_models_value("nano"), ["universal-2"]
        )
        self.assertEqual(
            video_utils._assemblyai_speech_models_value("universal-2"),
            ["universal-2"],
        )
        # Everything else (best, universal, default) prefers universal-3-5-pro
        # with universal-2 as the fallback.
        self.assertEqual(
            video_utils._assemblyai_speech_models_value("best"),
            ["universal-3-5-pro", "universal-2"],
        )
        self.assertEqual(
            video_utils._assemblyai_speech_models_value("universal"),
            ["universal-3-5-pro", "universal-2"],
        )
        self.assertEqual(
            video_utils._assemblyai_speech_models_value(None),
            ["universal-3-5-pro", "universal-2"],
        )

    def test_get_video_transcript_dispatches_to_whisper(self):
        whisper_result = {
            "text": "Hello there. General Kenobi.",
            "segments": [
                {
                    "start": 0.0,
                    "end": 2.2,
                    "text": "Hello there.",
                    "words": [
                        {"word": "Hello", "start": 0.0, "end": 0.4, "probability": 0.98},
                    ],
                },
                {
                    "start": 2.2,
                    "end": 4.6,
                    "text": "General Kenobi.",
                    "words": [
                        {"word": "General", "start": 2.2, "end": 3.0, "probability": 0.99},
                        {"word": "Kenobi.", "start": 3.0, "end": 4.6, "probability": 0.99},
                    ],
                },
            ],
        }

        mock_config = SimpleNamespace(
            transcription_provider="whisper",
            whisper_model="base",
            transcription_language="",
        )
        with patch("src.media.transcription.get_config", return_value=mock_config), patch(
            "src.media.transcription.transcribe_with_whisper", return_value=whisper_result
        ) as mock_transcribe:
            with tempfile.TemporaryDirectory() as temp_dir:
                video_path = Path(temp_dir) / "sample.mp4"
                video_path.touch()
                result = video_utils.get_video_transcript(video_path)

        self.assertIn("Hello there.", result)
        self.assertIn("General Kenobi.", result)
        mock_transcribe.assert_called_once()

    def test_format_transcript_for_analysis_handles_whisper_dict(self):
        whisper_result = {
            "text": "Hello there. General Kenobi.",
            "segments": [
                {
                    "start": 0.0,
                    "end": 2.2,
                    "text": " Hello there.",
                    "words": [
                        {"word": "Hello", "start": 0.0, "end": 0.4, "probability": 0.98},
                    ],
                },
                {
                    "start": 2.2,
                    "end": 4.6,
                    "text": " General Kenobi.",
                    "words": [
                        {"word": "General", "start": 2.2, "end": 3.0, "probability": 0.99},
                    ],
                },
            ],
        }
        formatted = video_utils.format_transcript_for_analysis(whisper_result)
        self.assertEqual(
            formatted,
            [
                "[00:00 - 00:02] Hello there.",
                "[00:02 - 00:04] General Kenobi.",
            ],
        )

    def test_cache_transcript_data_handles_whisper_dict(self):
        whisper_result = {
            "text": "Hello there.",
            "segments": [
                {
                    "start": 0.0,
                    "end": 0.9,
                    "text": "Hello there.",
                    "words": [
                        {"word": "Hello", "start": 0.0, "end": 0.4, "probability": 0.98},
                        {"word": "there.", "start": 0.4, "end": 0.9, "probability": 0.97},
                    ],
                }
            ],
        }
        with tempfile.TemporaryDirectory() as temp_dir:
            video_path = Path(temp_dir) / "sample.mp4"
            video_path.touch()
            video_utils.cache_transcript_data(video_path, whisper_result)
            cache_path = video_path.with_suffix(".transcript_cache.json")
            payload = json.loads(cache_path.read_text())

        self.assertEqual(
            payload["version"], video_utils.TRANSCRIPT_CACHE_SCHEMA_VERSION
        )
        # Whisper seconds are stored as milliseconds in the cache.
        self.assertEqual(payload["words"][0]["text"], "Hello")
        self.assertEqual(payload["words"][0]["start"], 0)
        self.assertEqual(payload["words"][0]["end"], 400)
        self.assertEqual(payload["utterances"][0]["text"], "Hello there.")
        self.assertIsNone(payload["utterances"][0]["speaker"])
    def test_format_transcript_for_analysis_splits_long_diarized_utterances(self):
        words = []
        for index, token in enumerate(
            [
                "This",
                "is",
                "a",
                "very",
                "long",
                "utterance",
                "that",
                "should",
                "be",
                "split",
                "into",
                "multiple",
                "segments.",
            ]
        ):
            start = index * 5000
            words.append(
                SimpleNamespace(
                    text=token,
                    start=start,
                    end=start + 1500,
                    confidence=0.99,
                    speaker="A",
                )
            )

        transcript = SimpleNamespace(
            utterances=[
                SimpleNamespace(
                    start=0,
                    end=65000,
                    speaker="A",
                    text=" ".join(word.text for word in words),
                    words=words,
                )
            ],
            words=words,
        )

        formatted = video_utils.format_transcript_for_analysis(transcript)

        self.assertGreater(len(formatted), 1)
        self.assertEqual(
            formatted[0],
            "[00:00 - 00:36] Speaker A: This is a very long utterance that should",
        )

    def test_get_transcript_text_in_range_reconstructs_exact_words(self):
        transcript_data = {
            "words": [
                {"text": "Hello", "start": 0, "end": 400, "confidence": 1.0},
                {"text": "world.", "start": 401, "end": 900, "confidence": 1.0},
                {"text": "Again", "start": 901, "end": 1300, "confidence": 1.0},
            ]
        }

        text = video_utils.get_transcript_text_in_range(transcript_data, 0.0, 0.95)

        self.assertEqual(text, "Hello world. Again")


if __name__ == "__main__":
    unittest.main()