-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscripto.py
More file actions
436 lines (352 loc) · 12.6 KB
/
transcripto.py
File metadata and controls
436 lines (352 loc) · 12.6 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# whisper_to_text_diarized.py
"""
WhisperX transcription + alignment + 2-speaker diarization (Speaker A / Speaker B)
Windows / WSL friendly. Uses .env for HF_TOKEN.
Outputs:
- <input>.txt (human readable)
- <input>.json (structured segments)
- transcription.log
"""
import os
import sys
import json
import logging
import subprocess
from pathlib import Path
from typing import Dict, Any, List
from dotenv import load_dotenv
# Load .env early
load_dotenv()
import torch
# --- PyTorch 2.6+ safe loading allowlist for trusted HF/Pyannote checkpoints ---
try:
allowlist = []
# Torch internal version object sometimes present in checkpoints
try:
from torch.torch_version import TorchVersion # type: ignore
allowlist.append(TorchVersion)
except Exception:
pass
# OmegaConf objects sometimes present in checkpoints
try:
from omegaconf import DictConfig, ListConfig # type: ignore
allowlist.extend([DictConfig, ListConfig])
except Exception:
pass
# Pyannote task objects sometimes present in checkpoints
try:
from pyannote.audio.core.task import Specifications, Problem, Resolution # type: ignore
allowlist.extend([Specifications, Problem, Resolution])
except Exception:
pass
if allowlist and hasattr(torch.serialization, "add_safe_globals"):
torch.serialization.add_safe_globals(allowlist)
except Exception:
pass
import whisperx
# -------------------------
# Config via env
# -------------------------
HF_TOKEN = os.getenv("HF_TOKEN")
MODEL_SIZE = os.getenv("MODEL_SIZE", "base")
FORCE_CPU = os.getenv("FORCE_CPU", "0") == "1"
DEVICE = os.getenv("DEVICE", "auto").lower()
COMPUTE_TYPE = os.getenv("COMPUTE_TYPE", "").strip()
ENABLE_TF32 = os.getenv("ENABLE_TF32", "0") == "1"
MIN_SPEAKERS = int(os.getenv("MIN_SPEAKERS", "2"))
MAX_SPEAKERS = int(os.getenv("MAX_SPEAKERS", "2"))
# Helps avoid Transformers importing torchvision (which can explode with torch/torchvision mismatches)
# You can also set this in your shell: set TRANSFORMERS_NO_TORCHVISION=1
os.environ.setdefault("TRANSFORMERS_NO_TORCHVISION", "1")
LOG_FILE = os.getenv("LOG_FILE", "transcription.log")
SUPPORTED_CUDA = ["12.8", "12.6", "11.8"]
def setup_logging() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
filename=LOG_FILE,
filemode="a",
)
def is_wsl() -> bool:
if os.name != "posix":
return False
try:
for path in ("/proc/version", "/proc/sys/kernel/osrelease"):
text = Path(path).read_text(encoding="utf-8", errors="ignore").lower()
if "microsoft" in text or "wsl" in text:
return True
except Exception:
pass
return False
def detect_cuda_version() -> str:
try:
result = subprocess.run(
["nvidia-smi", "--query-gpu=cuda_version", "--format=csv,noheader"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode == 0 and result.stdout.strip():
return result.stdout.strip().splitlines()[0].strip()
except Exception:
pass
try:
result = subprocess.run(
["nvidia-smi"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode == 0:
for line in result.stdout.splitlines():
if "CUDA Version" in line:
parts = line.split("CUDA Version", 1)[-1]
version = parts.split(":", 1)[-1].strip().split()[0]
return version
except Exception:
pass
return ""
def pick_supported_cuda(cuda_version: str) -> str:
def as_tuple(v: str) -> tuple[int, int]:
major, minor = v.split(".", 1)
return int(major), int(minor)
if not cuda_version:
return ""
try:
target = as_tuple(cuda_version)
except Exception:
return ""
supported = []
for v in SUPPORTED_CUDA:
try:
supported.append((as_tuple(v), v))
except Exception:
continue
supported.sort(reverse=True)
for (maj_min, v) in supported:
if maj_min <= target:
return v
return ""
def cuda_index_url(cuda_version: str) -> str:
return f"https://download.pytorch.org/whl/cu{cuda_version.replace('.', '')}"
def warn_if_cuda_missing() -> None:
if torch.cuda.is_available():
return
try:
result = subprocess.run(
["nvidia-smi", "-L"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
except FileNotFoundError:
return
if result.returncode != 0 or not result.stdout.strip():
return
cuda_version = detect_cuda_version()
supported = pick_supported_cuda(cuda_version)
if supported:
index_url = cuda_index_url(supported)
pip_cmd = f"python -m pip install torch torchvision torchaudio --index-url {index_url}"
else:
pip_cmd = "python -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu"
if is_wsl():
platform_hint = "WSL2"
elif os.name == "nt":
platform_hint = "Windows"
else:
platform_hint = "Linux"
msg = [
"NVIDIA GPU detected but CUDA is not available in PyTorch.",
f"Platform: {platform_hint}",
]
if cuda_version:
msg.append(f"Detected CUDA version (driver): {cuda_version}")
msg.append(f"Suggested pip install: {pip_cmd}")
msg.append("If this fails, use the PyTorch selector to pick a matching CUDA build.")
msg.append("Tip: run `python setup_gpu.py` for a full GPU diagnostic.")
logging.warning(" ".join(msg))
def to_wav_16k_mono(input_path: str) -> str:
"""
Convert any media file to mono 16k WAV (best for whisper/diarization stability).
If the WAV already exists, reuse it.
"""
in_path = Path(input_path)
wav_path = in_path.with_suffix(".wav")
if wav_path.exists():
logging.info(f"Using existing WAV: {wav_path}")
return str(wav_path)
cmd = [
"ffmpeg",
"-y",
"-i", str(in_path),
"-ac", "1",
"-ar", "16000",
str(wav_path),
]
logging.info("Converting input to WAV (mono, 16k) via ffmpeg...")
try:
subprocess.run(cmd, check=True)
except FileNotFoundError as e:
raise RuntimeError(
"ffmpeg not found. Install ffmpeg and ensure it's on PATH, then retry."
) from e
return str(wav_path)
def pick_device() -> Dict[str, str]:
if FORCE_CPU:
device = "cpu"
else:
if DEVICE in {"", "auto"}:
if torch.cuda.is_available():
device = "cuda"
elif getattr(torch.backends, "mps", None) and torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
elif DEVICE in {"cuda", "mps", "cpu"}:
device = DEVICE
else:
logging.warning(f"Unknown DEVICE '{DEVICE}', falling back to CPU.")
device = "cpu"
if device == "cuda" and not torch.cuda.is_available():
logging.warning("DEVICE=cuda requested but CUDA is not available; falling back to CPU.")
device = "cpu"
if device == "mps":
if not (getattr(torch.backends, "mps", None) and torch.backends.mps.is_available()):
logging.warning("DEVICE=mps requested but MPS is not available; falling back to CPU.")
device = "cpu"
if ENABLE_TF32 and device == "cuda":
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
compute_type = COMPUTE_TYPE or ("float16" if device == "cuda" else "float32")
return {"device": device, "compute_type": compute_type}
def import_diarization_pipeline():
"""
WhisperX has moved diarization classes around across versions.
Try the common locations.
"""
try:
from whisperx.diarize import DiarizationPipeline # type: ignore
return DiarizationPipeline
except Exception:
pass
try:
from whisperx.diarization import DiarizationPipeline # type: ignore
return DiarizationPipeline
except Exception as e:
raise RuntimeError(
"Could not import DiarizationPipeline from whisperx. "
"Your whisperx version may not include diarization helpers."
) from e
def map_speakers_to_letters(segments: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Map SPEAKER_00/SPEAKER_01/etc to Speaker A/Speaker B based on first appearance.
"""
mapping: Dict[str, str] = {}
next_letter = ord("A")
for seg in segments:
raw = seg.get("speaker", "Unknown")
if raw not in mapping:
mapping[raw] = f"Speaker {chr(next_letter)}"
next_letter += 1
seg["speaker_label"] = mapping[raw]
return segments
def write_outputs(input_file: str, segments: List[Dict[str, Any]]) -> None:
txt_path = Path(input_file).with_suffix(".txt")
json_path = Path(input_file).with_suffix(".json")
lines: List[str] = []
structured: List[Dict[str, Any]] = []
for seg in segments:
speaker = seg.get("speaker_label", "Speaker ?")
start = float(seg.get("start", 0.0))
end = float(seg.get("end", 0.0))
text = (seg.get("text") or "").strip()
line = f"[{start:0.1f}s - {end:0.1f}s] {speaker}: {text}"
print(line)
lines.append(line)
structured.append({
"speaker": speaker,
"start": start,
"end": end,
"text": text,
})
txt_path.write_text("\n".join(lines), encoding="utf-8")
json_path.write_text(json.dumps(structured, indent=2, ensure_ascii=False), encoding="utf-8")
logging.info(f"Wrote: {txt_path}")
logging.info(f"Wrote: {json_path}")
def main() -> None:
setup_logging()
warn_if_cuda_missing()
if len(sys.argv) < 2:
print("Usage: python whisper_to_text_diarized.py <audio_or_video_file>")
sys.exit(1)
input_file = sys.argv[1]
logging.info(f"Input: {input_file}")
# Device config
dc = pick_device()
device = dc["device"]
compute_type = dc["compute_type"]
logging.info(f"Device: {device} | compute_type: {compute_type} | model: {MODEL_SIZE}")
if device == "cuda":
try:
props = torch.cuda.get_device_properties(0)
gb = props.total_memory / (1024 ** 3)
logging.info(f"CUDA device: {torch.cuda.get_device_name(0)} ({gb:0.1f} GB)")
except Exception:
pass
elif device == "mps":
logging.info("Using Apple Metal (MPS) backend.")
# Convert and load audio
wav_file = to_wav_16k_mono(input_file)
audio = whisperx.load_audio(wav_file)
# Load ASR with silero VAD (more robust on Windows and avoids pyannote VAD checkpoint issues)
logging.info("Loading WhisperX ASR...")
model = whisperx.load_model(
MODEL_SIZE,
device=device,
compute_type=compute_type,
vad_method="silero",
)
# Transcribe
logging.info("Transcribing...")
result = model.transcribe(audio)
language = result.get("language", "unknown")
logging.info(f"Detected language: {language}")
# Align
logging.info("Loading align model...")
align_model, metadata = whisperx.load_align_model(language_code=language, device=device)
logging.info("Aligning...")
result = whisperx.align(
result["segments"],
align_model,
metadata,
audio,
device,
)
# Diarize (optional if token provided)
if HF_TOKEN:
# Ensure omegaconf exists if needed
try:
import omegaconf # noqa: F401
except Exception:
logging.warning("omegaconf not installed; install with: pip install -U omegaconf")
logging.info("Running diarization...")
DiarizationPipeline = import_diarization_pipeline()
diarize_model = DiarizationPipeline(use_auth_token=HF_TOKEN, device=device)
diarize_segments = diarize_model(
audio,
min_speakers=MIN_SPEAKERS,
max_speakers=MAX_SPEAKERS,
)
result = whisperx.assign_word_speakers(diarize_segments, result)
else:
logging.warning("HF_TOKEN missing: diarization skipped (no Speaker A/B).")
segments = map_speakers_to_letters(result["segments"])
logging.info("Writing outputs...")
write_outputs(input_file, segments)
logging.info("Done.")
if __name__ == "__main__":
main()