Spaces:
Paused
Paused
makaveli
commited on
Commit
•
b03c4ad
1
Parent(s):
ea46c22
Create tts_service.py
Browse files- tts_service.py +36 -0
tts_service.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import functools
|
2 |
+
|
3 |
+
from websockets.sync.server import serve
|
4 |
+
from whisperspeech.pipeline import Pipeline
|
5 |
+
|
6 |
+
class WhisperSpeechTTS:
|
7 |
+
def __init__(self):
|
8 |
+
pass
|
9 |
+
|
10 |
+
def initialize_model(self):
|
11 |
+
self.pipe = Pipeline(s2a_ref='collabora/whisperspeech:s2a-q4-tiny-en+pl.model')
|
12 |
+
|
13 |
+
def run(self, host, port=6080, audio_queue=None):
|
14 |
+
with serve(
|
15 |
+
functools.partial(self.start_whisperspeech_tts, audio_queue=audio_queue),
|
16 |
+
host, port
|
17 |
+
) as server:
|
18 |
+
server.serve_forever()
|
19 |
+
|
20 |
+
def start_whisperspeech_tts(self, websocket, audio_queue=None):
|
21 |
+
self.initialize_model()
|
22 |
+
|
23 |
+
while True:
|
24 |
+
if audio_queue.empty(): continue
|
25 |
+
|
26 |
+
llm_output = audio_queue.get()[0]
|
27 |
+
audio = self.pipe.vocoder.decode(self.pipe.generate_atoks(llm_output.strip()))
|
28 |
+
audio = audio.cpu().numpy()
|
29 |
+
audio = audio * 32768.0
|
30 |
+
|
31 |
+
# send audio to client on another websocket
|
32 |
+
try:
|
33 |
+
websocket.send(audio.astype('int16').tobytes())
|
34 |
+
except Exception as e:
|
35 |
+
print("Audio error:", e)
|
36 |
+
|