Vishal Patil commited on
Update pipeline.py
Browse files- pipeline.py +19 -7
pipeline.py
CHANGED
|
@@ -24,8 +24,8 @@ logger = logging.getLogger(__name__)
|
|
| 24 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 26 |
WHISPER_MODEL = os.environ.get("WHISPER_MODEL", "base")
|
|
|
|
| 27 |
|
| 28 |
-
# Login once at import time — works with all pyannote/huggingface_hub versions
|
| 29 |
if HF_TOKEN:
|
| 30 |
login(token=HF_TOKEN)
|
| 31 |
|
|
@@ -80,9 +80,9 @@ class SpeechAnnotationPipeline:
|
|
| 80 |
"HF_TOKEN not set. Add it in Space Settings > Variables and secrets."
|
| 81 |
)
|
| 82 |
logger.info("Loading pyannote diarization pipeline...")
|
| 83 |
-
# No token param needed — login() at top handles auth
|
| 84 |
self._diarization_pipeline = PyannotePipeline.from_pretrained(
|
| 85 |
-
"pyannote/speaker-diarization-3.1"
|
|
|
|
| 86 |
)
|
| 87 |
if DEVICE == "cuda":
|
| 88 |
self._diarization_pipeline = self._diarization_pipeline.to(torch.device("cuda"))
|
|
@@ -94,10 +94,22 @@ class SpeechAnnotationPipeline:
|
|
| 94 |
result = model.transcribe(audio_path, verbose=False)
|
| 95 |
return result["segments"]
|
| 96 |
|
| 97 |
-
def _diarize(self, audio_path):
|
| 98 |
logger.info("Step 2/2 - Diarizing speakers...")
|
| 99 |
pipeline = self._load_diarization()
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
segments = []
|
| 102 |
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 103 |
segments.append({"start": turn.start, "end": turn.end, "speaker": speaker})
|
|
@@ -124,10 +136,10 @@ class SpeechAnnotationPipeline:
|
|
| 124 |
))
|
| 125 |
return results
|
| 126 |
|
| 127 |
-
def process(self, audio_path):
|
| 128 |
t0 = time.time()
|
| 129 |
transcript = self._transcribe(audio_path)
|
| 130 |
-
diarization = self._diarize(audio_path)
|
| 131 |
annotations = self._assign_speakers(transcript, diarization)
|
| 132 |
logger.info(f"Done in {time.time()-t0:.1f}s — {len(annotations)} segments")
|
| 133 |
return annotations
|
|
|
|
| 24 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 26 |
WHISPER_MODEL = os.environ.get("WHISPER_MODEL", "base")
|
| 27 |
+
NUM_SPEAKERS = int(os.environ.get("NUM_SPEAKERS", "0")) # 0 = auto-detect
|
| 28 |
|
|
|
|
| 29 |
if HF_TOKEN:
|
| 30 |
login(token=HF_TOKEN)
|
| 31 |
|
|
|
|
| 80 |
"HF_TOKEN not set. Add it in Space Settings > Variables and secrets."
|
| 81 |
)
|
| 82 |
logger.info("Loading pyannote diarization pipeline...")
|
|
|
|
| 83 |
self._diarization_pipeline = PyannotePipeline.from_pretrained(
|
| 84 |
+
"pyannote/speaker-diarization-3.1",
|
| 85 |
+
use_auth_token=HF_TOKEN,
|
| 86 |
)
|
| 87 |
if DEVICE == "cuda":
|
| 88 |
self._diarization_pipeline = self._diarization_pipeline.to(torch.device("cuda"))
|
|
|
|
| 94 |
result = model.transcribe(audio_path, verbose=False)
|
| 95 |
return result["segments"]
|
| 96 |
|
| 97 |
+
def _diarize(self, audio_path, num_speakers=0, min_speakers=None, max_speakers=None):
|
| 98 |
logger.info("Step 2/2 - Diarizing speakers...")
|
| 99 |
pipeline = self._load_diarization()
|
| 100 |
+
|
| 101 |
+
# Build diarization params
|
| 102 |
+
params = {}
|
| 103 |
+
if num_speakers and num_speakers > 0:
|
| 104 |
+
params["num_speakers"] = num_speakers
|
| 105 |
+
logger.info(f"Using fixed num_speakers={num_speakers}")
|
| 106 |
+
else:
|
| 107 |
+
if min_speakers:
|
| 108 |
+
params["min_speakers"] = min_speakers
|
| 109 |
+
if max_speakers:
|
| 110 |
+
params["max_speakers"] = max_speakers
|
| 111 |
+
|
| 112 |
+
diarization = pipeline(audio_path, **params)
|
| 113 |
segments = []
|
| 114 |
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 115 |
segments.append({"start": turn.start, "end": turn.end, "speaker": speaker})
|
|
|
|
| 136 |
))
|
| 137 |
return results
|
| 138 |
|
| 139 |
+
def process(self, audio_path, num_speakers=0, min_speakers=None, max_speakers=None):
|
| 140 |
t0 = time.time()
|
| 141 |
transcript = self._transcribe(audio_path)
|
| 142 |
+
diarization = self._diarize(audio_path, num_speakers, min_speakers, max_speakers)
|
| 143 |
annotations = self._assign_speakers(transcript, diarization)
|
| 144 |
logger.info(f"Done in {time.time()-t0:.1f}s — {len(annotations)} segments")
|
| 145 |
return annotations
|