Dupaja commited on
Commit
aaf0168
·
1 Parent(s): df81a21

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +37 -0
handler.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import torch
3
+ import soundfile as sf
4
+ import base64
5
+ import io
6
+
7
+ class EndpointHandler:
8
+ def __init__(self):
9
+ self.synthesiser = pipeline("text-to-speech", model="microsoft/speecht5_tts")
10
+ self.embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
11
+
12
+ def __call__(self, data):
13
+ text = data.get("inputs", "")
14
+ speaker_embedding = torch.tensor(self.embeddings_dataset[7306]["xvector"]).unsqueeze(0)
15
+
16
+ # Generate speech using the synthesiser
17
+ speech = self.synthesiser(text, forward_params={"speaker_embeddings": speaker_embedding})
18
+
19
+ # Convert numpy audio array to bytes
20
+ audio_bytes = io.BytesIO()
21
+ sf.write(audio_bytes, speech["audio"], samplerate=speech["sampling_rate"], format='WAV')
22
+ audio_bytes.seek(0)
23
+ audio_base64 = base64.b64encode(audio_bytes.read()).decode('utf-8')
24
+
25
+ # Create response
26
+ response = {
27
+ "statusCode": 200,
28
+ "body": {
29
+ "audio": audio_base64,
30
+ "sampling_rate": speech["sampling_rate"]
31
+ },
32
+ "headers": {
33
+ "Content-Type": "audio/wav"
34
+ }
35
+ }
36
+
37
+ return response