Upload 2 files
Browse files- handler.py +166 -186
- requirements.txt +8 -36
handler.py
CHANGED
|
@@ -1,186 +1,166 @@
|
|
| 1 |
-
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
from typing import
|
| 11 |
-
|
| 12 |
-
import
|
| 13 |
-
import
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
class EndpointHandler:
|
| 18 |
-
"""
|
| 19 |
-
Custom handler for
|
| 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 |
-
# 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 |
-
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HuggingFace Inference Endpoint Handler for Pyannote Speaker Diarization
|
| 3 |
+
Compatible with HuggingFace Inference API format
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import base64
|
| 7 |
+
import io
|
| 8 |
+
import os
|
| 9 |
+
import tempfile
|
| 10 |
+
from typing import Any, Dict
|
| 11 |
+
|
| 12 |
+
from pyannote.audio import Pipeline
|
| 13 |
+
import torch
|
| 14 |
+
import soundfile as sf
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class EndpointHandler:
|
| 18 |
+
"""
|
| 19 |
+
Custom handler for HuggingFace Inference Endpoints.
|
| 20 |
+
Processes speaker diarization requests in JSON format.
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
def __init__(self, path=""):
|
| 24 |
+
"""Initialize the model on endpoint startup"""
|
| 25 |
+
print("๐๏ธ Initializing Pyannote speaker diarization pipeline...")
|
| 26 |
+
|
| 27 |
+
# Get HuggingFace token from environment
|
| 28 |
+
self.hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 29 |
+
|
| 30 |
+
if not self.hf_token:
|
| 31 |
+
raise ValueError("HF_TOKEN or HUGGING_FACE_HUB_TOKEN environment variable must be set")
|
| 32 |
+
|
| 33 |
+
# Load the diarization pipeline
|
| 34 |
+
self.pipeline = Pipeline.from_pretrained(
|
| 35 |
+
"pyannote/speaker-diarization-3.1",
|
| 36 |
+
use_auth_token=self.hf_token
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Move to GPU if available
|
| 40 |
+
if torch.cuda.is_available():
|
| 41 |
+
self.pipeline.to(torch.device("cuda"))
|
| 42 |
+
print("โ
Pipeline loaded on GPU")
|
| 43 |
+
else:
|
| 44 |
+
print("โ ๏ธ GPU not available, using CPU")
|
| 45 |
+
|
| 46 |
+
print("โ
Model initialized successfully")
|
| 47 |
+
|
| 48 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 49 |
+
"""
|
| 50 |
+
Process inference request.
|
| 51 |
+
|
| 52 |
+
Expected input format:
|
| 53 |
+
{
|
| 54 |
+
"inputs": "<base64_encoded_audio>",
|
| 55 |
+
"parameters": {
|
| 56 |
+
"min_speakers": 2, # optional
|
| 57 |
+
"max_speakers": 3 # optional
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
Returns:
|
| 62 |
+
{
|
| 63 |
+
"segments": [
|
| 64 |
+
{
|
| 65 |
+
"start": 0.0,
|
| 66 |
+
"end": 5.2,
|
| 67 |
+
"speaker": "SPEAKER_00",
|
| 68 |
+
"confidence": 0.95
|
| 69 |
+
},
|
| 70 |
+
...
|
| 71 |
+
],
|
| 72 |
+
"speakers": ["SPEAKER_00", "SPEAKER_01", ...],
|
| 73 |
+
"duration": 120.5
|
| 74 |
+
}
|
| 75 |
+
"""
|
| 76 |
+
try:
|
| 77 |
+
# Extract inputs
|
| 78 |
+
inputs = data.get("inputs", "")
|
| 79 |
+
parameters = data.get("parameters", {})
|
| 80 |
+
|
| 81 |
+
# Log parameters (for debugging)
|
| 82 |
+
param_count = len(parameters)
|
| 83 |
+
print(f"๐๏ธ Running diarization with {param_count} parameters...")
|
| 84 |
+
if param_count > 0:
|
| 85 |
+
print(f" Parameters: {parameters}")
|
| 86 |
+
|
| 87 |
+
# Decode base64 audio
|
| 88 |
+
if not inputs:
|
| 89 |
+
return {"error": "No audio data provided in 'inputs' field"}
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
audio_bytes = base64.b64decode(inputs)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return {"error": f"Failed to decode base64 audio: {str(e)}"}
|
| 95 |
+
|
| 96 |
+
# Write to temporary file (pyannote requires file path)
|
| 97 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 98 |
+
temp_path = temp_file.name
|
| 99 |
+
|
| 100 |
+
# Try to load audio with soundfile
|
| 101 |
+
try:
|
| 102 |
+
audio_data, sample_rate = sf.read(io.BytesIO(audio_bytes))
|
| 103 |
+
sf.write(temp_path, audio_data, sample_rate)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
# If soundfile fails, write raw bytes (works for WAV/MP3)
|
| 106 |
+
temp_file.write(audio_bytes)
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
# Prepare diarization parameters
|
| 110 |
+
diarization_kwargs = {}
|
| 111 |
+
|
| 112 |
+
min_speakers = parameters.get("min_speakers")
|
| 113 |
+
if min_speakers is not None and int(min_speakers) > 0:
|
| 114 |
+
diarization_kwargs["min_speakers"] = int(min_speakers)
|
| 115 |
+
print(f" โ min_speakers: {min_speakers}")
|
| 116 |
+
|
| 117 |
+
max_speakers = parameters.get("max_speakers")
|
| 118 |
+
if max_speakers is not None and int(max_speakers) > 0:
|
| 119 |
+
diarization_kwargs["max_speakers"] = int(max_speakers)
|
| 120 |
+
print(f" โ max_speakers: {max_speakers}")
|
| 121 |
+
|
| 122 |
+
print(f"๐ Processing audio file...")
|
| 123 |
+
|
| 124 |
+
# Run diarization
|
| 125 |
+
diarization = self.pipeline(temp_path, **diarization_kwargs)
|
| 126 |
+
|
| 127 |
+
# Convert to list of segments
|
| 128 |
+
segments = []
|
| 129 |
+
speakers_set = set()
|
| 130 |
+
max_end = 0.0
|
| 131 |
+
|
| 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 |
+
speakers_set.add(speaker)
|
| 140 |
+
max_end = max(max_end, float(turn.end))
|
| 141 |
+
|
| 142 |
+
# Prepare response
|
| 143 |
+
result = {
|
| 144 |
+
"segments": segments,
|
| 145 |
+
"speakers": sorted(list(speakers_set)),
|
| 146 |
+
"duration": max_end
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
print(f"โ
Diarization complete: {len(segments)} segments, {len(speakers_set)} speakers")
|
| 150 |
+
|
| 151 |
+
return result
|
| 152 |
+
|
| 153 |
+
finally:
|
| 154 |
+
# Clean up temporary file
|
| 155 |
+
try:
|
| 156 |
+
os.unlink(temp_path)
|
| 157 |
+
except:
|
| 158 |
+
pass
|
| 159 |
+
|
| 160 |
+
except Exception as e:
|
| 161 |
+
error_msg = f"Error during diarization: {str(e)}"
|
| 162 |
+
print(f"โ {error_msg}")
|
| 163 |
+
import traceback
|
| 164 |
+
traceback.print_exc()
|
| 165 |
+
return {"error": error_msg}
|
| 166 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,36 +1,8 @@
|
|
| 1 |
-
# Requirements for Pyannote
|
| 2 |
-
# HuggingFace Inference
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
torch==2.1.2
|
| 10 |
-
torchaudio==2.1.2
|
| 11 |
-
torchvision==0.16.2
|
| 12 |
-
|
| 13 |
-
# HuggingFace ecosystem - compatible versions
|
| 14 |
-
# huggingface-hub 0.21.4 has split_torch_state_dict_into_shards (needed by transformers)
|
| 15 |
-
# diffusers 0.21.4 is compatible with huggingface-hub 0.21.4 (no DDUFEntry requirement)
|
| 16 |
-
transformers==4.36.2
|
| 17 |
-
huggingface-hub==0.21.4
|
| 18 |
-
sentence-transformers==2.3.1
|
| 19 |
-
diffusers==0.21.4
|
| 20 |
-
|
| 21 |
-
# Core pyannote dependency
|
| 22 |
-
pyannote.audio==3.1.1
|
| 23 |
-
|
| 24 |
-
# Audio processing
|
| 25 |
-
soundfile==0.12.1
|
| 26 |
-
librosa==0.10.1
|
| 27 |
-
|
| 28 |
-
# Additional dependencies for pyannote
|
| 29 |
-
onnxruntime==1.16.3
|
| 30 |
-
asteroid-filterbanks==0.4.0
|
| 31 |
-
|
| 32 |
-
# HTTP requests for URL downloading
|
| 33 |
-
requests==2.31.0
|
| 34 |
-
|
| 35 |
-
# Ensure compatibility
|
| 36 |
-
scipy==1.11.4
|
|
|
|
| 1 |
+
# Requirements for Pyannote Diarization Inference Endpoint
|
| 2 |
+
# Compatible with HuggingFace Inference Endpoints
|
| 3 |
+
|
| 4 |
+
pyannote.audio==3.1.1
|
| 5 |
+
torch==2.1.2
|
| 6 |
+
torchaudio==2.1.2
|
| 7 |
+
numpy==1.26.4
|
| 8 |
+
soundfile==0.12.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|