Edwardx001 commited on
Commit
191c4f7
ยท
verified ยท
1 Parent(s): 8797275

Upload 2 files

Browse files
Files changed (2) hide show
  1. handler.py +166 -186
  2. requirements.txt +8 -36
handler.py CHANGED
@@ -1,186 +1,166 @@
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
-
 
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 Speaker Diarization Custom Handler
2
- # HuggingFace Inference Endpoint
3
- # Version-locked for stability and compatibility (tested working combination)
4
-
5
- # NumPy - MUST be < 2.0 for pyannote.audio compatibility
6
- numpy>=1.24.0,<2.0.0
7
-
8
- # PyTorch - specific versions that work together
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