videopython 0.60.1__tar.gz → 0.61.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {videopython-0.60.1 → videopython-0.61.1}/PKG-INFO +2 -2
- {videopython-0.60.1 → videopython-0.61.1}/pyproject.toml +4 -2
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/models.py +2 -25
- videopython-0.61.1/src/videopython/ai/understanding/_pyannote_patches.py +192 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/audio.py +2 -27
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/video_analysis/models.py +6 -63
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/__init__.py +1 -1
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/description.py +27 -195
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/transcription.py +53 -96
- {videopython-0.60.1 → videopython-0.61.1}/.gitignore +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/LICENSE +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/README.md +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/_exceptions.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/_ffmpeg.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/_device.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/_ollama.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/_optional.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/_predictor.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/_revisions.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/auto_edit/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/auto_edit/backend.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/auto_edit/catalog.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/auto_edit/editor.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/auto_edit/local.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/auto_edit/models.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/auto_edit/resolve.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/_tts_backend.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/audio_ops.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/config.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/dubber.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/pipeline.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/quality.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/remux.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/separation.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/timing.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/translation.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/dubbing/voice_sample.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/effects.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/errors.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/generation/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/generation/audio.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/generation/image.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/generation/video.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/keyframe.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/ops.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/transforms.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/_detector.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/classification.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/faces.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/image.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/objects.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/understanding/temporal.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/video_analysis/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/video_analysis/analyzer.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/video_analysis/detectors.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/video_analysis/sampling.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/ai/video_analysis/source_metadata.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/audio/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/audio/analysis.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/audio/audio.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/_dimensions.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/_video_io.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/draw_detections.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/Anton-OFL.txt +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/Anton-Regular.ttf +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/BebasNeue-OFL.txt +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/BebasNeue-Regular.ttf +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/DejaVuSans.ttf +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/LICENSE_DEJAVU +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/Lato-Bold.ttf +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/Lato-OFL.txt +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/Poppins-Bold.ttf +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/Poppins-OFL.txt +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/fonts/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/base/video.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/_ass.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/_easing.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/_schema.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/audio_ops.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/effects.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/errors.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/operation.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/streaming.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/transcription_overlay.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/transforms.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/editing/video_edit.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/mcp/__init__.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/mcp/_models.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/mcp/server.py +0 -0
- {videopython-0.60.1 → videopython-0.61.1}/src/videopython/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: videopython
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.61.1
|
|
4
4
|
Summary: LLM-friendly Python video editing with JSON plans, local AI, and MCP tools.
|
|
5
5
|
Project-URL: Homepage, https://videopython.com
|
|
6
6
|
Project-URL: Repository, https://github.com/bartwojtowicz/videopython/
|
|
@@ -33,7 +33,7 @@ Requires-Dist: ftfy>=6.1; extra == 'ai'
|
|
|
33
33
|
Requires-Dist: imagehash>=4.3; extra == 'ai'
|
|
34
34
|
Requires-Dist: numba>=0.62; extra == 'ai'
|
|
35
35
|
Requires-Dist: ollama>=0.5.0; extra == 'ai'
|
|
36
|
-
Requires-Dist: pyannote-audio
|
|
36
|
+
Requires-Dist: pyannote-audio<4.1,>=4.0.7; extra == 'ai'
|
|
37
37
|
Requires-Dist: pyloudnorm>=0.1.1; extra == 'ai'
|
|
38
38
|
Requires-Dist: silero-vad>=5.1; extra == 'ai'
|
|
39
39
|
Requires-Dist: torch>=2.8.0; extra == 'ai'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "videopython"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.61.1"
|
|
4
4
|
description = "LLM-friendly Python video editing with JSON plans, local AI, and MCP tools."
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Bartosz Wójtowicz", email = "bartoszwojtowicz@outlook.com" },
|
|
@@ -81,7 +81,9 @@ ai = [
|
|
|
81
81
|
# videopython-chatterbox imports librosa, which imports numba. Keep the resolver
|
|
82
82
|
# from backtracking to a numba release incompatible with the selected NumPy.
|
|
83
83
|
"numba>=0.62",
|
|
84
|
-
|
|
84
|
+
# Diarization overrides private pipeline steps verified against 4.0.7.
|
|
85
|
+
# Revalidate inactive-pair filtering and split-frame semantics before 4.1.
|
|
86
|
+
"pyannote-audio>=4.0.7,<4.1",
|
|
85
87
|
"silero-vad>=5.1",
|
|
86
88
|
# Visual understanding: detection (D-FINE/YuNet via transformers + opencv core
|
|
87
89
|
# dep), scene/temporal, VLM (understanding/*, video_analysis)
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
|
-
from typing import Annotated, Any
|
|
7
6
|
|
|
8
|
-
from pydantic import BaseModel,
|
|
7
|
+
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
9
8
|
|
|
10
9
|
from videopython.ai.dubbing.quality import TranscriptQuality
|
|
11
10
|
from videopython.audio import Audio
|
|
@@ -39,26 +38,6 @@ class TimingAdjustment:
|
|
|
39
38
|
CLEAN_SPEED_TOLERANCE = 0.01
|
|
40
39
|
|
|
41
40
|
|
|
42
|
-
# TranscriptionSegment and Transcription still live in videopython.base as
|
|
43
|
-
# plain dataclasses with hand-rolled to_dict/from_dict. Bridge them at
|
|
44
|
-
# the field boundary so the dubbing cache wire format stays identical.
|
|
45
|
-
def _validate_transcription_segment(value: Any) -> Any:
|
|
46
|
-
if value is None or isinstance(value, TranscriptionSegment):
|
|
47
|
-
return value
|
|
48
|
-
return TranscriptionSegment.from_dict(value)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def _serialize_with_to_dict(value: Any) -> Any:
|
|
52
|
-
return value.to_dict() if value is not None else None
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
_TranscriptionSegmentField = Annotated[
|
|
56
|
-
TranscriptionSegment,
|
|
57
|
-
BeforeValidator(_validate_transcription_segment),
|
|
58
|
-
PlainSerializer(_serialize_with_to_dict, return_type=dict, when_used="always"),
|
|
59
|
-
]
|
|
60
|
-
|
|
61
|
-
|
|
62
41
|
class Expressiveness(BaseModel):
|
|
63
42
|
"""Chatterbox ``generate()`` knobs derived from source-segment prosody.
|
|
64
43
|
|
|
@@ -108,9 +87,7 @@ class TranslatedSegment(BaseModel):
|
|
|
108
87
|
end: End time in seconds.
|
|
109
88
|
"""
|
|
110
89
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
original_segment: _TranscriptionSegmentField
|
|
90
|
+
original_segment: TranscriptionSegment
|
|
114
91
|
translated_text: str
|
|
115
92
|
source_lang: str
|
|
116
93
|
target_lang: str
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from types import MethodType
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _shared_embedding_batch(embedding: Any, batch: list[tuple[int, int, Any, Any]], masks: Any) -> np.ndarray:
|
|
11
|
+
"""Run a backend's mask-independent frame extractor once per distinct chunk.
|
|
12
|
+
|
|
13
|
+
Keep pooling in the original pair order.
|
|
14
|
+
Only the convolutional work is shared; every speaker keeps its own mask.
|
|
15
|
+
"""
|
|
16
|
+
import torch
|
|
17
|
+
|
|
18
|
+
unique: dict[int, tuple[int, Any]] = {}
|
|
19
|
+
indices = []
|
|
20
|
+
for chunk_index, _, waveform, _ in batch:
|
|
21
|
+
if chunk_index not in unique:
|
|
22
|
+
unique[chunk_index] = (len(unique), waveform)
|
|
23
|
+
indices.append(unique[chunk_index][0])
|
|
24
|
+
with torch.inference_mode():
|
|
25
|
+
frames = embedding.model_.forward_frames(
|
|
26
|
+
torch.vstack([waveform for _, waveform in unique.values()]).to(embedding.device)
|
|
27
|
+
)
|
|
28
|
+
vectors = embedding.model_.forward_embedding(frames[indices], weights=masks.to(embedding.device))
|
|
29
|
+
return vectors.cpu().numpy()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _reconstruct(pipeline: Any, segmentations: Any, hard_clusters: Any, count: Any) -> Any:
|
|
33
|
+
"""Reconstruct global clusters without promoting segmentation data to float64."""
|
|
34
|
+
num_chunks, num_frames, _ = segmentations.data.shape
|
|
35
|
+
num_clusters = int(np.max(hard_clusters)) + 1
|
|
36
|
+
clustered_data = np.full(
|
|
37
|
+
(num_chunks, num_frames, num_clusters),
|
|
38
|
+
np.nan,
|
|
39
|
+
dtype=segmentations.data.dtype,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
for chunk_index, (cluster, (_, segmentation)) in enumerate(zip(hard_clusters, segmentations)):
|
|
43
|
+
for cluster_index in np.unique(cluster):
|
|
44
|
+
if cluster_index == -2:
|
|
45
|
+
continue
|
|
46
|
+
clustered_data[chunk_index, :, cluster_index] = np.max(segmentation[:, cluster == cluster_index], axis=1)
|
|
47
|
+
|
|
48
|
+
clustered_segmentations = type(segmentations)(clustered_data, segmentations.sliding_window)
|
|
49
|
+
return pipeline.to_diarization(clustered_segmentations, count)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _silent_embedding(pipeline: Any, num_frames: int, num_samples: int) -> np.ndarray:
|
|
53
|
+
"""Embedding the model returns for an all-zero weight mask.
|
|
54
|
+
|
|
55
|
+
Statistics pooling with zero weights removes every dependency on the
|
|
56
|
+
waveform, so this vector is a model constant.
|
|
57
|
+
"""
|
|
58
|
+
import torch
|
|
59
|
+
|
|
60
|
+
return pipeline._embedding(
|
|
61
|
+
torch.zeros(1, 1, num_samples),
|
|
62
|
+
masks=torch.zeros(1, num_frames),
|
|
63
|
+
)[0]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _get_embeddings(
|
|
67
|
+
pipeline: Any,
|
|
68
|
+
file: Any,
|
|
69
|
+
binary_segmentations: Any,
|
|
70
|
+
exclude_overlap: bool = False,
|
|
71
|
+
hook: Any = None,
|
|
72
|
+
) -> np.ndarray:
|
|
73
|
+
"""Extract embeddings, skipping (chunk, speaker) pairs that cannot affect the result.
|
|
74
|
+
|
|
75
|
+
pyannote runs the embedding model on every pair, but a pair whose mask is
|
|
76
|
+
empty is discarded twice downstream: the clustering filter drops it, and the
|
|
77
|
+
pipeline force-assigns it to the throw-away cluster. Its embedding is the
|
|
78
|
+
silent-mask constant regardless of the audio, so reusing that constant keeps
|
|
79
|
+
the output bit-exact. For compatible deterministic pyannote backends,
|
|
80
|
+
also share frame extraction between speakers of the same chunk. Masks only
|
|
81
|
+
enter the network after frame extraction. Different batch shapes can still
|
|
82
|
+
introduce small floating-point differences in the embeddings.
|
|
83
|
+
"""
|
|
84
|
+
import torch
|
|
85
|
+
|
|
86
|
+
embedding = pipeline._embedding
|
|
87
|
+
duration = binary_segmentations.sliding_window.duration
|
|
88
|
+
num_chunks, num_frames, num_speakers = binary_segmentations.data.shape
|
|
89
|
+
|
|
90
|
+
if exclude_overlap:
|
|
91
|
+
# A pair needs this many frames of non-overlapping speech before the
|
|
92
|
+
# clean mask is preferred over the full one.
|
|
93
|
+
min_num_samples = embedding.min_num_samples
|
|
94
|
+
min_num_frames = math.ceil(num_frames * min_num_samples / (duration * embedding.sample_rate))
|
|
95
|
+
clean_frames = 1.0 * (np.sum(binary_segmentations.data, axis=2, keepdims=True) < 2)
|
|
96
|
+
clean_data = binary_segmentations.data * clean_frames
|
|
97
|
+
else:
|
|
98
|
+
min_num_frames = -1
|
|
99
|
+
clean_data = binary_segmentations.data
|
|
100
|
+
|
|
101
|
+
# Same test the pipeline uses to force-assign pairs to the throw-away cluster.
|
|
102
|
+
active = np.sum(binary_segmentations.data, axis=1) != 0
|
|
103
|
+
|
|
104
|
+
num_samples = round(duration * embedding.sample_rate)
|
|
105
|
+
embeddings = np.tile(
|
|
106
|
+
_silent_embedding(pipeline, num_frames, num_samples),
|
|
107
|
+
(num_chunks, num_speakers, 1),
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
def iter_active_pairs():
|
|
111
|
+
for chunk_index, (chunk, masks) in enumerate(binary_segmentations):
|
|
112
|
+
speaker_indices = np.flatnonzero(active[chunk_index])
|
|
113
|
+
if not len(speaker_indices):
|
|
114
|
+
continue
|
|
115
|
+
waveform, _ = pipeline._audio.crop(file, chunk, mode="pad")
|
|
116
|
+
# Masks may contain NaN where a chunk was only partially stitched.
|
|
117
|
+
masks = np.nan_to_num(masks, nan=0.0).astype(np.float32)
|
|
118
|
+
clean_masks = np.nan_to_num(clean_data[chunk_index], nan=0.0).astype(np.float32)
|
|
119
|
+
for speaker_index in speaker_indices:
|
|
120
|
+
clean_mask = clean_masks[:, speaker_index]
|
|
121
|
+
used_mask = clean_mask if np.sum(clean_mask) > min_num_frames else masks[:, speaker_index]
|
|
122
|
+
yield chunk_index, speaker_index, waveform[None], torch.from_numpy(used_mask)[None]
|
|
123
|
+
|
|
124
|
+
batch_size = pipeline.embedding_batch_size
|
|
125
|
+
batch_chunks = getattr(pipeline, "_batch_embedding_chunks", False)
|
|
126
|
+
batch_count = math.ceil(int(np.any(active, axis=1).sum() if batch_chunks else active.sum()) / batch_size)
|
|
127
|
+
if hook is not None:
|
|
128
|
+
hook("embeddings", None, total=batch_count, completed=0)
|
|
129
|
+
|
|
130
|
+
batch: list[tuple[int, int, torch.Tensor, torch.Tensor]] = []
|
|
131
|
+
completed = 0
|
|
132
|
+
|
|
133
|
+
def flush() -> None:
|
|
134
|
+
nonlocal completed
|
|
135
|
+
masks = torch.vstack([mask for _, _, _, mask in batch])
|
|
136
|
+
if getattr(pipeline, "_share_embedding_frames", False):
|
|
137
|
+
embedding_batch = _shared_embedding_batch(embedding, batch, masks)
|
|
138
|
+
else:
|
|
139
|
+
embedding_batch = embedding(
|
|
140
|
+
torch.vstack([waveform for _, _, waveform, _ in batch]),
|
|
141
|
+
masks=masks,
|
|
142
|
+
)
|
|
143
|
+
for (chunk_index, speaker_index, _, _), vector in zip(batch, embedding_batch):
|
|
144
|
+
embeddings[chunk_index, speaker_index] = vector
|
|
145
|
+
completed += 1
|
|
146
|
+
if hook is not None:
|
|
147
|
+
hook("embeddings", embedding_batch, total=batch_count, completed=completed)
|
|
148
|
+
batch.clear()
|
|
149
|
+
|
|
150
|
+
for pair in iter_active_pairs():
|
|
151
|
+
if batch_chunks and batch:
|
|
152
|
+
if pair[0] != batch[-1][0] and len({item[0] for item in batch}) == batch_size:
|
|
153
|
+
flush()
|
|
154
|
+
batch.append(pair)
|
|
155
|
+
if not batch_chunks and len(batch) == batch_size:
|
|
156
|
+
flush()
|
|
157
|
+
if batch:
|
|
158
|
+
flush()
|
|
159
|
+
|
|
160
|
+
return embeddings
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _supports_shared_frames(embedding: Any) -> bool:
|
|
164
|
+
"""Check pyannote's split-frame contract without importing a model family.
|
|
165
|
+
|
|
166
|
+
The split API extracts mask-independent frames and then applies weighted
|
|
167
|
+
pooling. Unknown preprocessing settings and training mode keep the normal
|
|
168
|
+
embedding path. This is internal diarization work, not an identity embedding
|
|
169
|
+
API or a choice of downstream speaker-recognition model.
|
|
170
|
+
"""
|
|
171
|
+
model = getattr(embedding, "model_", None)
|
|
172
|
+
settings = getattr(model, "hparams", None)
|
|
173
|
+
get_setting = getattr(settings, "get", None)
|
|
174
|
+
return (
|
|
175
|
+
getattr(model, "training", True) is False
|
|
176
|
+
and callable(get_setting)
|
|
177
|
+
and get_setting("dither") == 0.0
|
|
178
|
+
and callable(getattr(model, "forward_frames", None))
|
|
179
|
+
and callable(getattr(model, "forward_embedding", None))
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def install(pipeline: Any) -> None:
|
|
184
|
+
"""Replace two pyannote pipeline steps with equivalent, cheaper versions."""
|
|
185
|
+
for name in ("reconstruct", "get_embeddings"):
|
|
186
|
+
if not callable(getattr(pipeline, name, None)):
|
|
187
|
+
raise RuntimeError(f"The pyannote diarization pipeline no longer provides {name}().")
|
|
188
|
+
share_embedding_frames = _supports_shared_frames(getattr(pipeline, "_embedding", None))
|
|
189
|
+
pipeline._share_embedding_frames = share_embedding_frames
|
|
190
|
+
pipeline._batch_embedding_chunks = share_embedding_frames
|
|
191
|
+
pipeline.reconstruct = MethodType(_reconstruct, pipeline)
|
|
192
|
+
pipeline.get_embeddings = MethodType(_get_embeddings, pipeline)
|
|
@@ -4,7 +4,6 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import logging
|
|
6
6
|
from bisect import bisect_left
|
|
7
|
-
from types import MethodType
|
|
8
7
|
from typing import Any, Literal
|
|
9
8
|
|
|
10
9
|
from videopython.ai._device import log_device_initialization, select_device
|
|
@@ -31,28 +30,6 @@ _WHISPER_MODELS = {
|
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
|
|
34
|
-
def _reconstruct_diarization(pipeline: Any, segmentations: Any, hard_clusters: Any, count: Any) -> Any:
|
|
35
|
-
"""Reconstruct global clusters without promoting segmentation data to float64."""
|
|
36
|
-
import numpy as np
|
|
37
|
-
|
|
38
|
-
num_chunks, num_frames, _ = segmentations.data.shape
|
|
39
|
-
num_clusters = int(np.max(hard_clusters)) + 1
|
|
40
|
-
clustered_data = np.full(
|
|
41
|
-
(num_chunks, num_frames, num_clusters),
|
|
42
|
-
np.nan,
|
|
43
|
-
dtype=segmentations.data.dtype,
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
for chunk_index, (cluster, (_, segmentation)) in enumerate(zip(hard_clusters, segmentations)):
|
|
47
|
-
for cluster_index in np.unique(cluster):
|
|
48
|
-
if cluster_index == -2:
|
|
49
|
-
continue
|
|
50
|
-
clustered_data[chunk_index, :, cluster_index] = np.max(segmentation[:, cluster == cluster_index], axis=1)
|
|
51
|
-
|
|
52
|
-
clustered_segmentations = type(segmentations)(clustered_data, segmentations.sliding_window)
|
|
53
|
-
return pipeline.to_diarization(clustered_segmentations, count)
|
|
54
|
-
|
|
55
|
-
|
|
56
33
|
def _normalize_vocabulary(vocabulary: list[str] | None) -> list[str]:
|
|
57
34
|
"""Strip, drop empties, and order-preserving dedup (case-insensitive key).
|
|
58
35
|
|
|
@@ -264,16 +241,14 @@ class AudioToText(ManagedPredictor):
|
|
|
264
241
|
import torch
|
|
265
242
|
|
|
266
243
|
from videopython.ai._optional import require
|
|
244
|
+
from videopython.ai.understanding import _pyannote_patches
|
|
267
245
|
|
|
268
246
|
Pipeline = require("pyannote.audio", feature="AudioToText diarization").Pipeline
|
|
269
247
|
|
|
270
248
|
self._diarization_pipeline = Pipeline.from_pretrained(
|
|
271
249
|
self.PYANNOTE_DIARIZATION_MODEL, revision=pinned(self.PYANNOTE_DIARIZATION_MODEL)
|
|
272
250
|
)
|
|
273
|
-
|
|
274
|
-
raise RuntimeError("The pyannote diarization pipeline no longer provides reconstruct().")
|
|
275
|
-
# pyannote otherwise promotes its duration-by-speaker workspace to float64.
|
|
276
|
-
self._diarization_pipeline.reconstruct = MethodType(_reconstruct_diarization, self._diarization_pipeline)
|
|
251
|
+
_pyannote_patches.install(self._diarization_pipeline)
|
|
277
252
|
self._diarization_pipeline.to(torch.device(self.device))
|
|
278
253
|
|
|
279
254
|
def _init_vad(self) -> None:
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from pathlib import Path
|
|
6
|
-
from typing import
|
|
6
|
+
from typing import Any, Literal
|
|
7
7
|
|
|
8
|
-
from pydantic import BaseModel,
|
|
8
|
+
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
9
9
|
|
|
10
10
|
from videopython.base.description import AudioClassification, FaceTrack, SceneDescription
|
|
11
11
|
from videopython.base.transcription import Transcription
|
|
@@ -51,59 +51,6 @@ ALL_ANALYZER_IDS: tuple[_AnalyzerId, ...] = (
|
|
|
51
51
|
)
|
|
52
52
|
|
|
53
53
|
|
|
54
|
-
# Nested-type bridges: Transcription, SceneDescription, AudioClassification,
|
|
55
|
-
# and FaceTrack still live in videopython.base as plain dataclasses with
|
|
56
|
-
# hand-rolled to_dict/from_dict. Until they migrate to BaseModel, we
|
|
57
|
-
# interop at the field boundary so the wire format stays identical.
|
|
58
|
-
def _build_validator(cls: Any) -> Any:
|
|
59
|
-
def _validate(value: Any) -> Any:
|
|
60
|
-
if value is None or isinstance(value, cls):
|
|
61
|
-
return value
|
|
62
|
-
return cls.from_dict(value)
|
|
63
|
-
|
|
64
|
-
return _validate
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
def _serialize(value: Any) -> Any:
|
|
68
|
-
return value.to_dict() if value is not None else None
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
_TranscriptionField = Annotated[
|
|
72
|
-
Transcription,
|
|
73
|
-
BeforeValidator(_build_validator(Transcription)),
|
|
74
|
-
PlainSerializer(_serialize, return_type=dict, when_used="always"),
|
|
75
|
-
]
|
|
76
|
-
_SceneDescriptionField = Annotated[
|
|
77
|
-
SceneDescription,
|
|
78
|
-
BeforeValidator(_build_validator(SceneDescription)),
|
|
79
|
-
PlainSerializer(_serialize, return_type=dict, when_used="always"),
|
|
80
|
-
]
|
|
81
|
-
_AudioClassificationField = Annotated[
|
|
82
|
-
AudioClassification,
|
|
83
|
-
BeforeValidator(_build_validator(AudioClassification)),
|
|
84
|
-
PlainSerializer(_serialize, return_type=dict, when_used="always"),
|
|
85
|
-
]
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
def _validate_face_tracks(value: Any) -> Any:
|
|
89
|
-
if value is None:
|
|
90
|
-
return None
|
|
91
|
-
return [item if isinstance(item, FaceTrack) else FaceTrack.from_dict(item) for item in value]
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def _serialize_face_tracks(value: Any) -> Any:
|
|
95
|
-
if value is None:
|
|
96
|
-
return None
|
|
97
|
-
return [track.to_dict() for track in value]
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
_FaceTracksField = Annotated[
|
|
101
|
-
list[FaceTrack],
|
|
102
|
-
BeforeValidator(_validate_face_tracks),
|
|
103
|
-
PlainSerializer(_serialize_face_tracks, return_type=list, when_used="always"),
|
|
104
|
-
]
|
|
105
|
-
|
|
106
|
-
|
|
107
54
|
class GeoMetadata(BaseModel):
|
|
108
55
|
"""Optional geolocation metadata attached to a video container."""
|
|
109
56
|
|
|
@@ -206,9 +153,7 @@ class VideoAnalysisConfig(BaseModel):
|
|
|
206
153
|
class AudioAnalysisSection(BaseModel):
|
|
207
154
|
"""Audio understanding outputs."""
|
|
208
155
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
transcription: _TranscriptionField | None = None
|
|
156
|
+
transcription: Transcription | None = None
|
|
212
157
|
|
|
213
158
|
|
|
214
159
|
class SceneAnalysisSample(BaseModel):
|
|
@@ -220,16 +165,14 @@ class SceneAnalysisSample(BaseModel):
|
|
|
220
165
|
trajectory internally.
|
|
221
166
|
"""
|
|
222
167
|
|
|
223
|
-
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
224
|
-
|
|
225
168
|
scene_index: int
|
|
226
169
|
start_second: float
|
|
227
170
|
end_second: float
|
|
228
171
|
start_frame: int | None = None
|
|
229
172
|
end_frame: int | None = None
|
|
230
|
-
scene_description:
|
|
231
|
-
audio_classification:
|
|
232
|
-
faces:
|
|
173
|
+
scene_description: SceneDescription | None = None
|
|
174
|
+
audio_classification: AudioClassification | None = None
|
|
175
|
+
faces: list[FaceTrack] | None = None
|
|
233
176
|
|
|
234
177
|
|
|
235
178
|
class SceneAnalysisSection(BaseModel):
|