Download handler.py from Edwardx001/ragasa-diarization-v2: direct link, hf CLI and curl.
- Browser
- Download file 6.11 kB
-
https://huggingface.co/Edwardx001/ragasa-diarization-v2/resolve/main/handler.py
- Command line
-
hf download hf://Edwardx001/ragasa-diarization-v2/handler.py
-
curl -L -o handler.py https://huggingface.co/Edwardx001/ragasa-diarization-v2/resolve/main/handler.py
6.11 kB
| """ | |
| 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} | |