File size: 5,946 Bytes
119685d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
"""
HuggingFace Inference Endpoint Handler for Pyannote Speaker Diarization
Compatible with HuggingFace Inference API format
"""

import base64
import io
import os
import tempfile
from typing import Any, Dict

from pyannote.audio import Pipeline
import torch
import soundfile as sf


class EndpointHandler:
    """
    Custom handler for HuggingFace Inference Endpoints.
    Processes speaker diarization requests in JSON format.
    """
    
    def __init__(self, path=""):
        """Initialize the model on endpoint startup"""
        print("πŸŽ™οΈ Initializing Pyannote speaker diarization pipeline...")
        
        # Get HuggingFace token from environment
        self.hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
        
        if not self.hf_token:
            raise ValueError("HF_TOKEN or HUGGING_FACE_HUB_TOKEN environment variable must be set")
        
        # Load the diarization pipeline
        self.pipeline = Pipeline.from_pretrained(
            "pyannote/speaker-diarization-3.1",
            use_auth_token=self.hf_token
        )
        
        # Move to GPU if available
        if torch.cuda.is_available():
            self.pipeline.to(torch.device("cuda"))
            print("βœ… Pipeline loaded on GPU")
        else:
            print("⚠️ GPU not available, using CPU")
        
        print("βœ… Model initialized successfully")
    
    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Process inference request.
        
        Expected input format:
        {
            "inputs": "<base64_encoded_audio>",
            "parameters": {
                "min_speakers": 2,  # optional
                "max_speakers": 3   # optional
            }
        }
        
        Returns:
        {
            "segments": [
                {
                    "start": 0.0,
                    "end": 5.2,
                    "speaker": "SPEAKER_00",
                    "confidence": 0.95
                },
                ...
            ],
            "speakers": ["SPEAKER_00", "SPEAKER_01", ...],
            "duration": 120.5
        }
        """
        try:
            # Extract inputs
            inputs = data.get("inputs", "")
            parameters = data.get("parameters", {})
            
            # Log parameters (for debugging)
            param_count = len(parameters)
            print(f"πŸŽ™οΈ Running diarization with {param_count} parameters...")
            if param_count > 0:
                print(f"   Parameters: {parameters}")
            
            # Decode base64 audio
            if not inputs:
                return {"error": "No audio data provided in 'inputs' field"}
            
            try:
                audio_bytes = base64.b64decode(inputs)
            except Exception as e:
                return {"error": f"Failed to decode base64 audio: {str(e)}"}
            
            # Write to temporary file (pyannote requires file path)
            with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
                temp_path = temp_file.name
                
                # Try to load audio with soundfile
                try:
                    audio_data, sample_rate = sf.read(io.BytesIO(audio_bytes))
                    sf.write(temp_path, audio_data, sample_rate)
                except Exception as e:
                    # If soundfile fails, write raw bytes (works for WAV/MP3)
                    temp_file.write(audio_bytes)
            
            try:
                # Prepare diarization parameters
                diarization_kwargs = {}
                
                min_speakers = parameters.get("min_speakers")
                if min_speakers is not None and int(min_speakers) > 0:
                    diarization_kwargs["min_speakers"] = int(min_speakers)
                    print(f"   βœ“ min_speakers: {min_speakers}")
                
                max_speakers = parameters.get("max_speakers")
                if max_speakers is not None and int(max_speakers) > 0:
                    diarization_kwargs["max_speakers"] = int(max_speakers)
                    print(f"   βœ“ max_speakers: {max_speakers}")
                
                print(f"πŸ”„ Processing audio file...")
                
                # Run diarization
                diarization = self.pipeline(temp_path, **diarization_kwargs)
                
                # Convert to list of segments
                segments = []
                speakers_set = set()
                max_end = 0.0
                
                for turn, _, speaker in diarization.itertracks(yield_label=True):
                    segments.append({
                        "start": float(turn.start),
                        "end": float(turn.end),
                        "speaker": speaker,
                        "confidence": 0.95,  # Pyannote doesn't provide per-segment confidence
                    })
                    speakers_set.add(speaker)
                    max_end = max(max_end, float(turn.end))
                
                # Prepare response
                result = {
                    "segments": segments,
                    "speakers": sorted(list(speakers_set)),
                    "duration": max_end
                }
                
                print(f"βœ… Diarization complete: {len(segments)} segments, {len(speakers_set)} speakers")
                
                return result
                
            finally:
                # Clean up temporary file
                try:
                    os.unlink(temp_path)
                except:
                    pass
        
        except Exception as e:
            error_msg = f"Error during diarization: {str(e)}"
            print(f"❌ {error_msg}")
            import traceback
            traceback.print_exc()
            return {"error": error_msg}