speecht5_tts / handler.py
Dupaja's picture
Update handler.py
8869945
raw
history blame
1.16 kB
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})
# Write the response audio to a file
sf.write("speech.wav", response.audio, response.sampling_rate)
# Return the expected response format
return {
"statusCode": 200,
"body": {
"audio": response.audio, # Consider encoding this to a suitable format
"sampling_rate": response.sampling_rate
}
}
handler = EndpointHandler()