Skip to content

Commit e5f4e55

Browse files
authored
Merge pull request #124 from songguocola/audio_0511
(model/cosyvoice): support flush api
2 parents 58940f1 + 6a7fde5 commit e5f4e55

1 file changed

Lines changed: 46 additions & 13 deletions

File tree

dashscope/audio/tts_v2/speech_synthesizer.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def __init__( # pylint: disable=redefined-builtin
151151
instruction=None,
152152
language_hints: list = None,
153153
):
154-
self.task_id = self.genUid()
154+
self.task_id = self.gen_uid()
155155
self.apikey = apikey
156156
self.voice = voice
157157
self.model = model
@@ -166,11 +166,11 @@ def __init__( # pylint: disable=redefined-builtin
166166
self.instruction = instruction
167167
self.language_hints = language_hints
168168

169-
def genUid(self):
169+
def gen_uid(self):
170170
# 生成随机UUID
171171
return uuid.uuid4().hex
172172

173-
def getWebsocketHeaders(self, headers, workspace):
173+
def get_websocket_headers(self, headers, workspace):
174174
ua = get_user_agent()
175175
self.headers = {
176176
"user-agent": ua,
@@ -185,7 +185,7 @@ def getWebsocketHeaders(self, headers, workspace):
185185
}
186186
return self.headers
187187

188-
def getStartRequest(self, additional_params=None):
188+
def get_start_request(self, additional_params=None):
189189
cmd = {
190190
HEADER: {
191191
ACTION_KEY: ActionType.START,
@@ -223,7 +223,7 @@ def getStartRequest(self, additional_params=None):
223223
] = self.language_hints
224224
return json.dumps(cmd)
225225

226-
def getContinueRequest(self, text):
226+
def get_continue_request(self, text):
227227
cmd = {
228228
HEADER: {
229229
ACTION_KEY: ActionType.CONTINUE,
@@ -242,7 +242,7 @@ def getContinueRequest(self, text):
242242
}
243243
return json.dumps(cmd)
244244

245-
def getFinishRequest(self):
245+
def get_finish_request(self):
246246
cmd = {
247247
HEADER: {
248248
ACTION_KEY: ActionType.FINISHED,
@@ -255,6 +255,25 @@ def getFinishRequest(self):
255255
}
256256
return json.dumps(cmd)
257257

258+
def get_flush_request(self):
259+
cmd = {
260+
HEADER: {
261+
ACTION_KEY: ActionType.CONTINUE, # CONTINUE task
262+
TASK_ID: self.task_id,
263+
"streaming": WebsocketStreamingMode.DUPLEX,
264+
},
265+
"payload": {
266+
"model": self.model,
267+
"task_group": "audio",
268+
"task": "tts",
269+
"function": "SpeechSynthesizer",
270+
"input": {
271+
"flush": True,
272+
},
273+
},
274+
}
275+
return json.dumps(cmd)
276+
258277

259278
class SpeechSynthesizer:
260279
def __init__( # pylint: disable=redefined-builtin
@@ -373,7 +392,7 @@ def __connect(self, timeout_seconds=5) -> None:
373392
"""
374393
self.ws = websocket.WebSocketApp(
375394
self.url,
376-
header=self.request.getWebsocketHeaders(
395+
header=self.request.get_websocket_headers(
377396
headers=self.headers,
378397
workspace=self.workspace,
379398
),
@@ -525,7 +544,7 @@ def __start_stream(self):
525544
if self.ws is None:
526545
self.__connect(5)
527546
# 发送run-task指令
528-
request = self.request.getStartRequest(self.additional_params)
547+
request = self.request.get_start_request(self.additional_params)
529548
self.__send_str(request)
530549
if not self.start_event.wait(10):
531550
raise TimeoutError("start speech synthesizer failed within 5s.")
@@ -539,13 +558,14 @@ def __submit_text(self, text):
539558

540559
if self._stopped.is_set():
541560
raise InvalidTask("speech synthesizer task has stopped.")
542-
request = self.request.getContinueRequest(text)
561+
request = self.request.get_continue_request(text)
543562
self.__send_str(request)
544563

545564
# pylint: disable=useless-return
546565
def streaming_call(self, text: str):
547566
"""
548-
Streaming input mode: You can call the stream_call function multiple times to send text. # noqa: E501 # pylint: disable=line-too-long
567+
Streaming input mode:
568+
You can call the streaming_call function multiple times to send text.
549569
A session will be created on the first call.
550570
The session ends after calling streaming_complete.
551571
Parameters:
@@ -559,6 +579,19 @@ def streaming_call(self, text: str):
559579
self.__submit_text(text)
560580
return None
561581

582+
def streaming_flush(self):
583+
"""
584+
send tts flush request.
585+
"""
586+
if not self._is_started:
587+
raise InvalidTask("speech synthesizer has not been started.")
588+
589+
if self._stopped.is_set():
590+
raise InvalidTask("speech synthesizer task has stopped.")
591+
request = self.request.get_flush_request()
592+
self.__send_str(request)
593+
return None
594+
562595
def streaming_complete(self, complete_timeout_millis=600000):
563596
"""
564597
Synchronously stop the streaming input speech synthesis task.
@@ -575,7 +608,7 @@ def streaming_complete(self, complete_timeout_millis=600000):
575608
raise InvalidTask("speech synthesizer has not been started.")
576609
if self._stopped.is_set():
577610
raise InvalidTask("speech synthesizer task has stopped.")
578-
request = self.request.getFinishRequest()
611+
request = self.request.get_finish_request()
579612
self.__send_str(request)
580613
if complete_timeout_millis is not None and complete_timeout_millis > 0:
581614
if not self.complete_event.wait(
@@ -623,7 +656,7 @@ def async_streaming_complete(self, complete_timeout_millis=600000):
623656
raise InvalidTask("speech synthesizer has not been started.")
624657
if self._stopped.is_set():
625658
raise InvalidTask("speech synthesizer task has stopped.")
626-
request = self.request.getFinishRequest()
659+
request = self.request.get_finish_request()
627660
self.__send_str(request)
628661
thread = threading.Thread(
629662
target=self.__waiting_for_complete,
@@ -641,7 +674,7 @@ def streaming_cancel(self):
641674
raise InvalidTask("speech synthesizer has not been started.")
642675
if self._stopped.is_set():
643676
return
644-
request = self.request.getFinishRequest()
677+
request = self.request.get_finish_request()
645678
self.__send_str(request)
646679
self.ws.close()
647680
self.start_event.set()

0 commit comments

Comments
 (0)