|
from huggingface_hub import InferenceClient |
|
from datasets import load_dataset |
|
import soundfile as sf |
|
from typing import Dict, List, Any |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
self.client = InferenceClient(repo_id="microsoft/speecht5_tts", task="text-to-speech") |
|
self.embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation") |
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: |
|
text = data.get("inputs", "") |
|
speaker_embedding = self.embeddings_dataset['xvector'][7306].unsqueeze(0).tolist() |
|
|
|
response = self.client(payload={"inputs": text, "forward_params": {"speaker_embeddings": speaker_embedding}}, options={"wait_for_model": True}) |
|
|
|
|
|
sf.write("speech.wav", response.audio, response.sampling_rate) |
|
|
|
|
|
return { |
|
"statusCode": 200, |
|
"body": { |
|
"audio": response.audio, |
|
"sampling_rate": response.sampling_rate |
|
} |
|
} |
|
|
|
handler = EndpointHandler() |