Update handler.py
Browse files- handler.py +174 -96
handler.py
CHANGED
|
@@ -1,108 +1,186 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
import torch
|
| 7 |
-
import
|
|
|
|
| 8 |
from pyannote.audio import Pipeline
|
| 9 |
|
| 10 |
|
| 11 |
class EndpointHandler:
|
| 12 |
"""
|
| 13 |
-
Custom handler for
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"url": "https://.../audio.wav", # OR
|
| 18 |
-
"audio": "<base64 bytes>", # wav/mp3/flac/m4a
|
| 19 |
-
"min_speakers": 1, # optional
|
| 20 |
-
"max_speakers": 3 # optional
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
Output JSON:
|
| 24 |
-
{
|
| 25 |
-
"segments": [{"start": 0.12, "end": 1.83, "speaker": "SPEAKER_00"}, ...],
|
| 26 |
-
"num_speakers": 2
|
| 27 |
-
}
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
-
def __init__(self, path: str = "
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
self.pipeline = Pipeline.from_pretrained(
|
| 37 |
"pyannote/speaker-diarization-3.1",
|
| 38 |
-
use_auth_token=
|
| 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 |
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Custom Inference Handler for Pyannote Speaker Diarization
|
| 3 |
+
|
| 4 |
+
This handler is required because Pyannote models don't follow the standard
|
| 5 |
+
HuggingFace Transformers format and need special loading/inference logic.
|
| 6 |
+
|
| 7 |
+
Deployment: Upload this file when creating the HuggingFace Inference Endpoint
|
| 8 |
+
"""
|
| 9 |
|
| 10 |
+
from typing import Dict, List, Any
|
| 11 |
import torch
|
| 12 |
+
import io
|
| 13 |
+
import os
|
| 14 |
from pyannote.audio import Pipeline
|
| 15 |
|
| 16 |
|
| 17 |
class EndpointHandler:
|
| 18 |
"""
|
| 19 |
+
Custom handler for Pyannote Speaker Diarization pipeline.
|
| 20 |
+
|
| 21 |
+
This class is instantiated once when the endpoint starts up,
|
| 22 |
+
and the __call__ method is invoked for each inference request.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
+
|
| 25 |
+
def __init__(self, path: str = ""):
|
| 26 |
+
"""
|
| 27 |
+
Initialize the diarization pipeline.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
path: Path to the model (automatically provided by HuggingFace)
|
| 31 |
+
"""
|
| 32 |
+
# Get HuggingFace token from environment (set in endpoint config)
|
| 33 |
+
self.hf_token = os.environ.get("HF_TOKEN", None)
|
| 34 |
+
|
| 35 |
+
if not self.hf_token:
|
| 36 |
+
raise ValueError(
|
| 37 |
+
"HF_TOKEN environment variable is required. "
|
| 38 |
+
"Set it in the endpoint's Environment Variables."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Load the pretrained pipeline
|
| 42 |
+
# use_auth_token is required because the model needs authentication
|
| 43 |
self.pipeline = Pipeline.from_pretrained(
|
| 44 |
"pyannote/speaker-diarization-3.1",
|
| 45 |
+
use_auth_token=self.hf_token
|
| 46 |
)
|
| 47 |
+
|
| 48 |
+
# Move to GPU if available
|
| 49 |
+
if torch.cuda.is_available():
|
| 50 |
+
self.pipeline.to(torch.device("cuda"))
|
| 51 |
+
print("β
Pipeline loaded on GPU")
|
| 52 |
+
else:
|
| 53 |
+
print("β οΈ GPU not available, using CPU (will be slower)")
|
| 54 |
+
|
| 55 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 56 |
+
"""
|
| 57 |
+
Process inference request.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
data: Dictionary containing:
|
| 61 |
+
- 'inputs': Audio file bytes (WAV format) OR URL string
|
| 62 |
+
- 'url': Alternative to 'inputs' - URL to download audio from
|
| 63 |
+
- 'audio': Alternative to 'inputs' - base64 encoded audio string
|
| 64 |
+
- 'parameters' (optional): Dictionary with:
|
| 65 |
+
- 'min_speakers': Minimum number of speakers (default: None)
|
| 66 |
+
- 'max_speakers': Maximum number of speakers (default: None)
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
List of speaker segments:
|
| 70 |
+
[
|
| 71 |
+
{
|
| 72 |
+
"start": 0.5, # Start time in seconds
|
| 73 |
+
"end": 5.2, # End time in seconds
|
| 74 |
+
"speaker": "SPEAKER_00",
|
| 75 |
+
"confidence": 0.95
|
| 76 |
+
},
|
| 77 |
+
...
|
| 78 |
+
]
|
| 79 |
+
"""
|
| 80 |
+
# Get audio data from request - support multiple input formats
|
| 81 |
+
inputs = data.get("inputs") or data.get("url") or data.get("audio")
|
| 82 |
+
if not inputs:
|
| 83 |
+
return {"error": "Provide either \"url\" or \"audio\" (base64)."}
|
| 84 |
+
|
| 85 |
+
# Get optional parameters
|
| 86 |
+
parameters = data.get("parameters", {})
|
| 87 |
+
min_speakers = parameters.get("min_speakers", None)
|
| 88 |
+
max_speakers = parameters.get("max_speakers", None)
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
# Handle URL input (download the file)
|
| 92 |
+
if isinstance(inputs, str) and (inputs.startswith("http://") or inputs.startswith("https://")):
|
| 93 |
+
import requests
|
| 94 |
+
print(f"π₯ Downloading audio from URL: {inputs[:100]}...")
|
| 95 |
+
response = requests.get(inputs, timeout=30)
|
| 96 |
+
response.raise_for_status()
|
| 97 |
+
audio_bytes = response.content
|
| 98 |
+
audio_file = io.BytesIO(audio_bytes)
|
| 99 |
+
print(f"β
Downloaded {len(audio_bytes)} bytes")
|
| 100 |
+
|
| 101 |
+
# Handle bytes input
|
| 102 |
+
elif isinstance(inputs, bytes):
|
| 103 |
+
audio_file = io.BytesIO(inputs)
|
| 104 |
+
|
| 105 |
+
# Handle base64 string input
|
| 106 |
+
elif isinstance(inputs, str):
|
| 107 |
+
import base64
|
| 108 |
+
audio_bytes = base64.b64decode(inputs)
|
| 109 |
+
audio_file = io.BytesIO(audio_bytes)
|
| 110 |
+
|
| 111 |
+
else:
|
| 112 |
+
audio_file = inputs
|
| 113 |
+
|
| 114 |
+
# Run diarization
|
| 115 |
+
# β
CRITICAL FIX: Pass file directly to pipeline, not wrapped in dict!
|
| 116 |
+
# The Pipeline.__call__ expects: pipeline(file, min_speakers=..., max_speakers=...)
|
| 117 |
+
diarization_kwargs = {}
|
| 118 |
+
if min_speakers:
|
| 119 |
+
diarization_kwargs["min_speakers"] = min_speakers
|
| 120 |
+
if max_speakers:
|
| 121 |
+
diarization_kwargs["max_speakers"] = max_speakers
|
| 122 |
+
|
| 123 |
+
print(f"ποΈ Running diarization with {len(diarization_kwargs)} parameters...")
|
| 124 |
+
diarization = self.pipeline(
|
| 125 |
+
audio_file, # β
FIXED: Pass file directly, not {"audio": audio_file}
|
| 126 |
+
**diarization_kwargs
|
| 127 |
+
)
|
| 128 |
+
print(f"β
Diarization complete")
|
| 129 |
+
|
| 130 |
+
# Convert diarization result to JSON-serializable format
|
| 131 |
+
segments = []
|
| 132 |
+
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 133 |
+
segments.append({
|
| 134 |
+
"start": float(turn.start),
|
| 135 |
+
"end": float(turn.end),
|
| 136 |
+
"speaker": speaker,
|
| 137 |
+
"confidence": 0.95, # Pyannote doesn't provide per-segment confidence
|
| 138 |
+
})
|
| 139 |
+
|
| 140 |
+
print(f"β
Found {len(segments)} segments with {len(set(s['speaker'] for s in segments))} speakers")
|
| 141 |
+
return segments
|
| 142 |
+
|
| 143 |
+
except Exception as e:
|
| 144 |
+
import traceback
|
| 145 |
+
return {
|
| 146 |
+
"error": f"Diarization failed: {str(e)}",
|
| 147 |
+
"type": type(e).__name__,
|
| 148 |
+
"traceback": traceback.format_exc()
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# For local testing
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
import sys
|
| 155 |
+
|
| 156 |
+
if len(sys.argv) < 2:
|
| 157 |
+
print("Usage: python handler.py <audio_file.wav>")
|
| 158 |
+
sys.exit(1)
|
| 159 |
+
|
| 160 |
+
# Set token for local testing (get from environment or command line)
|
| 161 |
+
os.environ["HF_TOKEN"] = os.environ.get("HF_TOKEN", "your-token-here")
|
| 162 |
+
|
| 163 |
+
# Initialize handler
|
| 164 |
+
print("Loading diarization pipeline...")
|
| 165 |
+
handler = EndpointHandler()
|
| 166 |
+
|
| 167 |
+
# Test with local file
|
| 168 |
+
audio_path = sys.argv[1]
|
| 169 |
+
print(f"Processing: {audio_path}")
|
| 170 |
+
|
| 171 |
+
with open(audio_path, "rb") as f:
|
| 172 |
+
audio_bytes = f.read()
|
| 173 |
+
|
| 174 |
+
# Run inference
|
| 175 |
+
result = handler({"inputs": audio_bytes})
|
| 176 |
+
|
| 177 |
+
# Print results
|
| 178 |
+
if isinstance(result, dict) and "error" in result:
|
| 179 |
+
print(f"β Error: {result['error']}")
|
| 180 |
+
else:
|
| 181 |
+
print(f"β
Found {len(result)} segments")
|
| 182 |
+
print(f"β
Speakers: {len(set(s['speaker'] for s in result))}")
|
| 183 |
+
print("\nFirst 5 segments:")
|
| 184 |
+
for segment in result[:5]:
|
| 185 |
+
print(f" {segment['start']:.2f}s - {segment['end']:.2f}s: {segment['speaker']}")
|
| 186 |
|