Skip to content

Commit 6e85785

Browse files
authored
Merge pull request #13 from lengjiayi/main
[Add] cosyvoice call api enbale ssml
2 parents 52ef917 + 2974514 commit 6e85785

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

dashscope/audio/asr/vocabulary.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313

1414
class VocabularyServiceException(Exception):
15-
def __init__(self, status_code: int, code: str,
15+
def __init__(self, request_id: str, status_code: int, code: str,
1616
error_message: str) -> None:
17+
self._request_id = request_id
1718
self._status_code = status_code
1819
self._code = code
1920
self._error_message = error_message
2021

2122
def __str__(self):
22-
return f'Status Code: {self._status_code}, Code: {self._code}, Error Message: {self._error_message}'
23+
return f'Request: {self._request_id}, Status Code: {self._status_code}, Code: {self._code}, Error Message: {self._error_message}'
2324

2425

2526
class VocabularyService(BaseApi):
@@ -86,7 +87,7 @@ def create_vocabulary(self, target_model: str, prefix: str,
8687
self._last_request_id = response.request_id
8788
return response.output['vocabulary_id']
8889
else:
89-
raise VocabularyServiceException(response.status_code,
90+
raise VocabularyServiceException(response.request_id, response.status_code,
9091
response.code, response.message)
9192

9293
def list_vocabularies(self,
@@ -117,7 +118,7 @@ def list_vocabularies(self,
117118
self._last_request_id = response.request_id
118119
return response.output['vocabulary_list']
119120
else:
120-
raise VocabularyServiceException(response.status_code,
121+
raise VocabularyServiceException(response.request_id, response.status_code,
121122
response.code, response.message)
122123

123124
def query_vocabulary(self, vocabulary_id: str) -> List[dict]:
@@ -134,7 +135,7 @@ def query_vocabulary(self, vocabulary_id: str) -> List[dict]:
134135
self._last_request_id = response.request_id
135136
return response.output
136137
else:
137-
raise VocabularyServiceException(response.status_code,
138+
raise VocabularyServiceException(response.request_id, response.status_code,
138139
response.code, response.message)
139140

140141
def update_vocabulary(self, vocabulary_id: str,
@@ -153,7 +154,7 @@ def update_vocabulary(self, vocabulary_id: str,
153154
self._last_request_id = response.request_id
154155
return
155156
else:
156-
raise VocabularyServiceException(response.status_code,
157+
raise VocabularyServiceException(response.request_id, response.status_code,
157158
response.code, response.message)
158159

159160
def delete_vocabulary(self, vocabulary_id: str) -> None:
@@ -169,7 +170,7 @@ def delete_vocabulary(self, vocabulary_id: str) -> None:
169170
self._last_request_id = response.request_id
170171
return
171172
else:
172-
raise VocabularyServiceException(response.status_code,
173+
raise VocabularyServiceException(response.request_id, response.status_code,
173174
response.code, response.message)
174175

175176
def get_last_request_id(self):

dashscope/audio/tts_v2/enrollment.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313

1414
class VoiceEnrollmentException(Exception):
15-
def __init__(self, status_code: int, code: str,
15+
def __init__(self, request_id: str, status_code: int, code: str,
1616
error_message: str) -> None:
17+
self._request_id = request_id
1718
self._status_code = status_code
1819
self._code = code
1920
self._error_message = error_message
2021

2122
def __str__(self):
22-
return f'Status Code: {self._status_code}, Code: {self._code}, Error Message: {self._error_message}'
23+
return f'Request: {self._request_id}, Status Code: {self._status_code}, Code: {self._code}, Error Message: {self._error_message}'
2324

2425

2526
class VoiceEnrollmentService(BaseApi):
@@ -81,11 +82,11 @@ def create_voice(self, target_model: str, prefix: str, url: str) -> str:
8182
'prefix': prefix,
8283
'url': url,
8384
}, )
85+
self._last_request_id = response.request_id
8486
if response.status_code == 200:
85-
self._last_request_id = response.request_id
8687
return response.output['voice_id']
8788
else:
88-
raise VoiceEnrollmentException(response.status_code, response.code,
89+
raise VoiceEnrollmentException(response.request_id, response.status_code, response.code,
8990
response.message)
9091

9192
def list_voices(self,
@@ -111,11 +112,11 @@ def list_voices(self,
111112
'page_index': page_index,
112113
'page_size': page_size,
113114
}, )
115+
self._last_request_id = response.request_id
114116
if response.status_code == 200:
115-
self._last_request_id = response.request_id
116117
return response.output['voice_list']
117118
else:
118-
raise VoiceEnrollmentException(response.status_code, response.code,
119+
raise VoiceEnrollmentException(response.request_id, response.status_code, response.code,
119120
response.message)
120121

121122
def query_voice(self, voice_id: str) -> List[str]:
@@ -128,11 +129,11 @@ def query_voice(self, voice_id: str) -> List[str]:
128129
'action': 'query_voice',
129130
'voice_id': voice_id,
130131
}, )
132+
self._last_request_id = response.request_id
131133
if response.status_code == 200:
132-
self._last_request_id = response.request_id
133134
return response.output
134135
else:
135-
raise VoiceEnrollmentException(response.status_code, response.code,
136+
raise VoiceEnrollmentException(response.request_id, response.status_code, response.code,
136137
response.message)
137138

138139
def update_voice(self, voice_id: str, url: str) -> None:
@@ -146,11 +147,11 @@ def update_voice(self, voice_id: str, url: str) -> None:
146147
'voice_id': voice_id,
147148
'url': url,
148149
}, )
150+
self._last_request_id = response.request_id
149151
if response.status_code == 200:
150-
self._last_request_id = response.request_id
151152
return
152153
else:
153-
raise VoiceEnrollmentException(response.status_code, response.code,
154+
raise VoiceEnrollmentException(response.request_id, response.status_code, response.code,
154155
response.message)
155156

156157
def delete_voice(self, voice_id: str) -> None:
@@ -162,11 +163,11 @@ def delete_voice(self, voice_id: str) -> None:
162163
'action': 'delete_voice',
163164
'voice_id': voice_id,
164165
}, )
166+
self._last_request_id = response.request_id
165167
if response.status_code == 200:
166-
self._last_request_id = response.request_id
167168
return
168169
else:
169-
raise VoiceEnrollmentException(response.status_code, response.code,
170+
raise VoiceEnrollmentException(response.request_id, response.status_code, response.code,
170171
response.message)
171172

172173
def get_last_request_id(self):

dashscope/audio/tts_v2/speech_synthesizer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ def getStartRequest(self, additional_params=None):
134134
'task_group': 'audio',
135135
'task': 'tts',
136136
'function': 'SpeechSynthesizer',
137-
'input': {
138-
'text': ''
139-
},
137+
'input': {},
140138
'parameters': {
141139
'voice': self.voice,
142140
'volume': self.volume,
@@ -179,9 +177,7 @@ def getFinishRequest(self):
179177
'streaming': WebsocketStreamingMode.DUPLEX,
180178
},
181179
'payload': {
182-
'input': {
183-
'text': ''
184-
},
180+
'input': {},
185181
},
186182
}
187183
return json.dumps(cmd)
@@ -514,6 +510,10 @@ def call(self, text: str, timeout_millis=None):
514510
otherwise, it will wait indefinitely.
515511
"""
516512
# print('还不支持非流式语音合成sdk调用大模型,使用流式模拟')
513+
if self.additional_params is None:
514+
self.additional_params = {"enable_ssml":True}
515+
else:
516+
self.additional_params["enable_ssml"] = True
517517
if not self.callback:
518518
self.callback = ResultCallback()
519519
self.__start_stream()

0 commit comments

Comments
 (0)