Edwardx001 commited on
Commit
bf2554d
·
verified ·
1 Parent(s): 79080a2

Update handler.py

Browse files

Update for version mismatch

Files changed (1) hide show
  1. handler.py +127 -166
handler.py CHANGED
@@ -1,166 +1,127 @@
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)
62
- - 'parameters' (optional): Dictionary with:
63
- - 'min_speakers': Minimum number of speakers (default: None)
64
- - 'max_speakers': Maximum number of speakers (default: None)
65
-
66
- Returns:
67
- List of speaker segments:
68
- [
69
- {
70
- "start": 0.5, # Start time in seconds
71
- "end": 5.2, # End time in seconds
72
- "speaker": "SPEAKER_00",
73
- "confidence": 0.95
74
- },
75
- ...
76
- ]
77
- """
78
- # Get audio data from request
79
- inputs = data.get("inputs")
80
- if not inputs:
81
- return {"error": "No audio data provided in 'inputs' field"}
82
-
83
- # Get optional parameters
84
- parameters = data.get("parameters", {})
85
- min_speakers = parameters.get("min_speakers", None)
86
- max_speakers = parameters.get("max_speakers", None)
87
-
88
- try:
89
- # Convert bytes to file-like object
90
- if isinstance(inputs, bytes):
91
- audio_file = io.BytesIO(inputs)
92
- elif isinstance(inputs, str):
93
- # If base64 encoded string (fallback)
94
- import base64
95
- audio_bytes = base64.b64decode(inputs)
96
- audio_file = io.BytesIO(audio_bytes)
97
- else:
98
- audio_file = inputs
99
-
100
- # Run diarization
101
- # The pipeline expects a file path or file-like object
102
- diarization_kwargs = {}
103
- if min_speakers:
104
- diarization_kwargs["min_speakers"] = min_speakers
105
- if max_speakers:
106
- diarization_kwargs["max_speakers"] = max_speakers
107
-
108
- diarization = self.pipeline(
109
- {"audio": audio_file},
110
- **diarization_kwargs
111
- )
112
-
113
- # Convert diarization result to JSON-serializable format
114
- segments = []
115
- for turn, _, speaker in diarization.itertracks(yield_label=True):
116
- segments.append({
117
- "start": float(turn.start),
118
- "end": float(turn.end),
119
- "speaker": speaker,
120
- "confidence": 0.95, # Pyannote doesn't provide per-segment confidence
121
- })
122
-
123
- return segments
124
-
125
- except Exception as e:
126
- return {
127
- "error": f"Diarization failed: {str(e)}",
128
- "type": type(e).__name__
129
- }
130
-
131
-
132
- # For local testing
133
- if __name__ == "__main__":
134
- import sys
135
-
136
- if len(sys.argv) < 2:
137
- print("Usage: python handler.py <audio_file.wav>")
138
- sys.exit(1)
139
-
140
- # Set token for local testing (get from environment or command line)
141
- os.environ["HF_TOKEN"] = os.environ.get("HF_TOKEN", "your-token-here")
142
-
143
- # Initialize handler
144
- print("Loading diarization pipeline...")
145
- handler = EndpointHandler()
146
-
147
- # Test with local file
148
- audio_path = sys.argv[1]
149
- print(f"Processing: {audio_path}")
150
-
151
- with open(audio_path, "rb") as f:
152
- audio_bytes = f.read()
153
-
154
- # Run inference
155
- result = handler({"inputs": audio_bytes})
156
-
157
- # Print results
158
- if isinstance(result, dict) and "error" in result:
159
- print(f"❌ Error: {result['error']}")
160
- else:
161
- print(f"✅ Found {len(result)} segments")
162
- print(f"✅ Speakers: {len(set(s['speaker'] for s in result))}")
163
- print("\nFirst 5 segments:")
164
- for segment in result[:5]:
165
- print(f" {segment['start']:.2f}s - {segment['end']:.2f}s: {segment['speaker']}")
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
+ # handler.py
11
+ import os
12
+ os.environ.setdefault("OMP_NUM_THREADS", "1") # silence libgomp warning
13
+
14
+ import io
15
+ import json
16
+ import time
17
+ import uuid
18
+ import tempfile
19
+ import requests
20
+ import torch
21
+ from typing import Any, Dict
22
+
23
+ from pyannote.audio import Pipeline
24
+
25
+
26
+ class EndpointHandler:
27
+ """
28
+ Hugging Face Inference Endpoint / Serverless handler
29
+ Expects JSON like:
30
+ {
31
+ "session_id": "sess_123",
32
+ "audio_url": "https://signed-eu-url/file.m4a",
33
+ "min_speakers": 2,
34
+ "max_speakers": 3,
35
+ "offset": 0.0,
36
+ "duration": null,
37
+ "trace_id": "optional-uuid"
38
+ }
39
+ """
40
+
41
+ def __init__(self, path=""):
42
+ # HF_TOKEN must be provided as a secret / env var in the endpoint
43
+ hf_token = os.getenv("HF_TOKEN")
44
+ if not hf_token:
45
+ raise RuntimeError("HF_TOKEN environment variable is required to download pyannote weights.")
46
+
47
+ # Load the diarization pipeline once
48
+ self.pipeline = Pipeline.from_pretrained(
49
+ "pyannote/speaker-diarization-3.1",
50
+ use_auth_token=hf_token
51
+ )
52
+
53
+ if torch.cuda.is_available():
54
+ self.pipeline.to(torch.device("cuda"))
55
+
56
+ def _download_to_temp(self, url: str) -> str:
57
+ # Minimal URL fetch to a temp file; delete-on-close is False so pyannote can read it
58
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".bin") as tmp:
59
+ with requests.get(url, stream=True, timeout=60) as r:
60
+ r.raise_for_status()
61
+ for chunk in r.iter_content(1024 * 1024):
62
+ if chunk:
63
+ tmp.write(chunk)
64
+ return tmp.name
65
+
66
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
67
+ t0 = time.time()
68
+
69
+ # Basic payload validation
70
+ if not isinstance(data, dict):
71
+ try:
72
+ data = json.loads(data)
73
+ except Exception:
74
+ raise ValueError("Expected JSON dict or JSON-encoded string.")
75
+
76
+ session_id = data.get("session_id") or "unknown"
77
+ audio_url = data.get("audio_url")
78
+ if not audio_url:
79
+ raise ValueError("Missing required field: 'audio_url'")
80
+
81
+ min_speakers = data.get("min_speakers")
82
+ max_speakers = data.get("max_speakers")
83
+ offset = float(data.get("offset") or 0.0)
84
+ duration = data.get("duration")
85
+ trace_id = data.get("trace_id") or str(uuid.uuid4())
86
+
87
+ # Build kwargs to pass into pyannote pipeline
88
+ kwargs = {}
89
+ if min_speakers is not None:
90
+ kwargs["min_speakers"] = int(min_speakers)
91
+ if max_speakers is not None:
92
+ kwargs["max_speakers"] = int(max_speakers)
93
+ if duration is not None:
94
+ kwargs["duration"] = float(duration)
95
+ if offset:
96
+ kwargs["offset"] = float(offset)
97
+
98
+ # Download, diarize, cleanup
99
+ tmp_path = None
100
+ try:
101
+ tmp_path = self._download_to_temp(audio_url)
102
+ diarization = self.pipeline(tmp_path, **kwargs)
103
+
104
+ segments = []
105
+ for turn, _, speaker in diarization.itertracks(yield_label=True):
106
+ segments.append({
107
+ "start": float(turn.start),
108
+ "end": float(turn.end),
109
+ "speaker": str(speaker)
110
+ })
111
+
112
+ runtime_ms = int((time.time() - t0) * 1000)
113
+ return {
114
+ "session_id": session_id,
115
+ "trace_id": trace_id,
116
+ "segments": segments,
117
+ "model": "pyannote/speaker-diarization-3.1",
118
+ "runtime_ms": runtime_ms
119
+ }
120
+
121
+ finally:
122
+ # Best-effort temp file cleanup
123
+ if tmp_path:
124
+ try:
125
+ os.remove(tmp_path)
126
+ except Exception:
127
+ pass