Edwardx001 commited on
Commit
a3394f5
Β·
verified Β·
1 Parent(s): 22f1024

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +174 -96
handler.py CHANGED
@@ -1,108 +1,186 @@
1
- import os
2
- import base64
3
- import tempfile
4
- from typing import Any, Dict, Optional, List
 
 
 
 
5
 
 
6
  import torch
7
- import requests
 
8
  from pyannote.audio import Pipeline
9
 
10
 
11
  class EndpointHandler:
12
  """
13
- Custom handler for pyannote/speaker-diarization-3.1
14
-
15
- Input JSON:
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
- token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
32
- if not token:
33
- raise RuntimeError("Set HF_TOKEN (or HUGGINGFACEHUB_API_TOKEN) in env vars.")
34
-
35
- # Load pipeline
 
 
 
 
 
 
 
 
 
 
 
 
36
  self.pipeline = Pipeline.from_pretrained(
37
  "pyannote/speaker-diarization-3.1",
38
- use_auth_token=token,
39
  )
40
-
41
- # IMPORTANT: pyannote expects a torch.device, not a str
42
- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
43
- self.pipeline.to(device)
44
-
45
- # ---------- helpers ----------
46
- def _tmp_from_url(self, url: str) -> str:
47
- r = requests.get(url, stream=True, timeout=120)
48
- r.raise_for_status()
49
- fd, p = tempfile.mkstemp(suffix=".bin")
50
- with os.fdopen(fd, "wb") as f:
51
- for chunk in r.iter_content(1 << 20):
52
- if chunk:
53
- f.write(chunk)
54
- return p
55
-
56
- def _tmp_from_b64(self, b64: str) -> str:
57
- raw = base64.b64decode(b64)
58
- fd, p = tempfile.mkstemp(suffix=".bin")
59
- with os.fdopen(fd, "wb") as f:
60
- f.write(raw)
61
- return p
62
-
63
- # ---------- inference entry ----------
64
- def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
65
- if not isinstance(data, dict):
66
- raise ValueError("Input must be a JSON object.")
67
-
68
- url: Optional[str] = data.get("url")
69
- b64: Optional[str] = data.get("audio")
70
- if not url and not b64:
71
- raise ValueError('Provide either "url" or "audio" (base64).')
72
-
73
- min_speakers: Optional[int] = data.get("min_speakers")
74
- max_speakers: Optional[int] = data.get("max_speakers")
75
-
76
- audio_path = self._tmp_from_url(url) if url else self._tmp_from_b64(b64)
77
-
78
- try:
79
- # Open file as file-like object for Pyannote Pipeline
80
- with open(audio_path, 'rb') as audio_file:
81
- diar = self.pipeline(
82
- audio_file, # Pass opened file handle, not path string
83
- min_speakers=min_speakers,
84
- max_speakers=max_speakers,
85
- )
86
-
87
- segments: List[Dict[str, Any]] = []
88
- speakers = set()
89
- for turn, _, spk in diar.itertracks(yield_label=True):
90
- segments.append({
91
- "start": round(float(turn.start), 3),
92
- "end": round(float(turn.end), 3),
93
- "speaker": spk,
94
- })
95
- speakers.add(spk)
96
-
97
- return {"segments": segments, "num_speakers": len(speakers)}
98
- finally:
99
- try:
100
- os.remove(audio_path)
101
- except Exception:
102
- pass
103
- finally:
104
- try:
105
- os.remove(audio_path)
106
- except Exception:
107
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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