Run this model with HF Jobs
Run sanoramyun8/speaker-embedding-endpoint on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/sanoramyun8/speaker-embedding-endpoint. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/sanoramyun8/speaker-embedding-endpoint on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use sanoramyun8/speaker-embedding-endpoint with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- speechbrain
How to use sanoramyun8/speaker-embedding-endpoint with speechbrain:
# interface not specified in config.json
- Notebooks
- Google Colab
- Kaggle
Download handler.py from sanoramyun8/speaker-embedding-endpoint: direct link, hf CLI and curl.
- Browser
- Download file 3.33 kB
-
https://huggingface.co/sanoramyun8/speaker-embedding-endpoint/resolve/main/handler.py
- Command line
-
hf download hf://sanoramyun8/speaker-embedding-endpoint/handler.py
-
curl -L -o handler.py https://huggingface.co/sanoramyun8/speaker-embedding-endpoint/resolve/main/handler.py
3.33 kB
| """ | |
| Custom Handler for Speaker Embedding Extraction | |
| Using SpeechBrain ECAPA-TDNN model for HuggingFace Inference Endpoints | |
| """ | |
| from typing import Dict, List, Any | |
| import torch | |
| import torchaudio | |
| import io | |
| import numpy as np | |
| class EndpointHandler: | |
| """ | |
| HuggingFace Inference Endpoint Handler for Speaker Embedding | |
| Extracts speaker embeddings using SpeechBrain's ECAPA-TDNN model. | |
| Returns 192-dimensional embedding vectors for speaker verification. | |
| """ | |
| def __init__(self, path: str = ""): | |
| """ | |
| Initialize the handler by loading the SpeechBrain model. | |
| Args: | |
| path: Path to the model directory (provided by HuggingFace) | |
| """ | |
| from speechbrain.inference.speaker import EncoderClassifier | |
| # Load ECAPA-TDNN model from SpeechBrain | |
| self.model = EncoderClassifier.from_hparams( | |
| source="speechbrain/spkrec-ecapa-voxceleb", | |
| savedir=path if path else "/tmp/spkrec-ecapa-voxceleb", | |
| run_opts={"device": "cpu"} | |
| ) | |
| self.sample_rate = 16000 | |
| print("[SpeakerEmbedding] Model loaded successfully") | |
| def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: | |
| """ | |
| Process audio input and return speaker embedding. | |
| Args: | |
| data: Dictionary containing: | |
| - "inputs": Audio bytes or base64 encoded audio | |
| - "parameters": Optional parameters | |
| Returns: | |
| Dictionary with embedding vector | |
| """ | |
| try: | |
| # Get audio data from request | |
| inputs = data.get("inputs") | |
| if inputs is None: | |
| return {"error": "No audio input provided"} | |
| # Handle different input formats | |
| if isinstance(inputs, bytes): | |
| audio_bytes = inputs | |
| elif isinstance(inputs, str): | |
| # Base64 encoded | |
| import base64 | |
| audio_bytes = base64.b64decode(inputs) | |
| else: | |
| return {"error": f"Unsupported input type: {type(inputs)}"} | |
| # Load audio from bytes | |
| audio_buffer = io.BytesIO(audio_bytes) | |
| waveform, sample_rate = torchaudio.load(audio_buffer) | |
| # Resample if necessary | |
| if sample_rate != self.sample_rate: | |
| resampler = torchaudio.transforms.Resample( | |
| orig_freq=sample_rate, | |
| new_freq=self.sample_rate | |
| ) | |
| waveform = resampler(waveform) | |
| # Convert to mono if stereo | |
| if waveform.shape[0] > 1: | |
| waveform = torch.mean(waveform, dim=0, keepdim=True) | |
| # Extract embedding | |
| with torch.no_grad(): | |
| embedding = self.model.encode_batch(waveform) | |
| embedding = embedding.squeeze().cpu().numpy() | |
| # Normalize embedding | |
| embedding = embedding / np.linalg.norm(embedding) | |
| return { | |
| "embedding": embedding.tolist(), | |
| "dimension": len(embedding), | |
| "model": "speechbrain/spkrec-ecapa-voxceleb" | |
| } | |
| except Exception as e: | |
| return {"error": str(e)} | |