dashscope 1.20.8__py3-none-any.whl → 1.20.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dashscope might be problematic. Click here for more details.

@@ -6,6 +6,7 @@ from dashscope.client.base_api import BaseAsyncApi
6
6
  from dashscope.common.constants import IMAGES, NEGATIVE_PROMPT, PROMPT
7
7
  from dashscope.common.error import InputRequired
8
8
  from dashscope.common.utils import _get_task_group_and_task
9
+ from dashscope.utils.oss_utils import check_and_upload_local
9
10
 
10
11
 
11
12
  class ImageSynthesis(BaseAsyncApi):
@@ -38,7 +39,8 @@ class ImageSynthesis(BaseAsyncApi):
38
39
  images (List[str]): The input list of images url,
39
40
  currently not supported.
40
41
  api_key (str, optional): The api api_key. Defaults to None.
41
- sketch_image_url (str, optional): Only for wanx-sketch-to-image-v1.
42
+ sketch_image_url (str, optional): Only for wanx-sketch-to-image-v1,
43
+ can be local file.
42
44
  Defaults to None.
43
45
  workspace (str): The dashscope workspace id.
44
46
  extra_input (Dict): The extra input parameters.
@@ -120,17 +122,31 @@ class ImageSynthesis(BaseAsyncApi):
120
122
  raise InputRequired('prompt is required!')
121
123
  task_group, function = _get_task_group_and_task(__name__)
122
124
  input = {PROMPT: prompt}
125
+ has_upload = False
123
126
  if negative_prompt is not None:
124
127
  input[NEGATIVE_PROMPT] = negative_prompt
125
128
  if images is not None:
126
129
  input[IMAGES] = images
127
130
  if sketch_image_url is not None and sketch_image_url:
131
+ is_upload, sketch_image_url = check_and_upload_local(
132
+ model, sketch_image_url, api_key)
133
+ if is_upload:
134
+ has_upload = True
128
135
  input['sketch_image_url'] = sketch_image_url
129
136
  if ref_img is not None and ref_img:
137
+ is_upload, ref_img = check_and_upload_local(
138
+ model, ref_img, api_key)
139
+ if is_upload:
140
+ has_upload = True
130
141
  input['ref_img'] = ref_img
131
142
  if extra_input is not None and extra_input:
132
143
  input = {**input, **extra_input}
133
144
 
145
+ if has_upload:
146
+ headers = kwargs.pop('headers', {})
147
+ headers['X-DashScope-OssResourceResolve'] = 'enable'
148
+ kwargs['headers'] = headers
149
+
134
150
  response = super().async_call(
135
151
  model=model,
136
152
  task_group=task_group,
@@ -1,8 +1,9 @@
1
1
  from .asr_phrase_manager import AsrPhraseManager
2
2
  from .recognition import Recognition, RecognitionCallback, RecognitionResult
3
3
  from .transcription import Transcription
4
+ from .vocabulary import VocabularyService, VocabularyServiceException
4
5
 
5
6
  __all__ = [
6
- Transcription, Recognition, RecognitionCallback, RecognitionResult,
7
- AsrPhraseManager
7
+ 'Transcription', 'Recognition', 'RecognitionCallback', 'RecognitionResult',
8
+ 'AsrPhraseManager', 'VocabularyServiceException', 'VocabularyService'
8
9
  ]
@@ -1,7 +1,7 @@
1
1
  import json
2
2
  import os
3
- import time
4
3
  import threading
4
+ import time
5
5
  from http import HTTPStatus
6
6
  from threading import Timer
7
7
  from typing import Any, Dict, List, Union
@@ -185,24 +185,29 @@ class Recognition(BaseApi):
185
185
  self._callback.on_complete()
186
186
  else:
187
187
  usage: Dict[str, Any] = None
188
- useags: List[Any] = None
188
+ usages: List[Any] = None
189
189
  if 'sentence' in part.output:
190
190
  if (self._first_package_timestamp < 0):
191
- self._first_package_timestamp = time.time()*1000
192
- logger.debug('first package delay {}'.format(self._first_package_timestamp - self._start_stream_timestamp))
191
+ self._first_package_timestamp = time.time() * 1000
192
+ logger.debug('first package delay {}'.format(
193
+ self._first_package_timestamp -
194
+ self._start_stream_timestamp))
193
195
  sentence = part.output['sentence']
194
- logger.debug('Recv Result :{}, isEnd: {}'.format(sentence, RecognitionResult.is_sentence_end(sentence)))
195
- if part.usage is not None:
196
+ logger.debug('Recv Result :{}, isEnd: {}'.format(
197
+ sentence,
198
+ RecognitionResult.is_sentence_end(sentence)))
199
+ if part.usage is not None:
196
200
  usage = {
197
- 'end_time': part.output['sentence']['end_time'],
201
+ 'end_time':
202
+ part.output['sentence']['end_time'],
198
203
  'usage': part.usage
199
204
  }
200
- useags = [usage]
205
+ usages = [usage]
201
206
 
202
207
  self._callback.on_event(
203
208
  RecognitionResult(
204
209
  RecognitionResponse.from_api_response(part),
205
- usages=useags))
210
+ usages=usages))
206
211
  else:
207
212
  self._running = False
208
213
  self._stream_data.clear()
@@ -318,7 +323,7 @@ class Recognition(BaseApi):
318
323
  Returns:
319
324
  RecognitionResult: The result of speech recognition.
320
325
  """
321
- self._start_stream_timestamp = time.time()*1000
326
+ self._start_stream_timestamp = time.time() * 1000
322
327
  if self._running:
323
328
  raise InvalidParameter('Speech recognition has been called.')
324
329
 
@@ -363,10 +368,14 @@ class Recognition(BaseApi):
363
368
  if part.status_code == HTTPStatus.OK:
364
369
  if 'sentence' in part.output:
365
370
  if (self._first_package_timestamp < 0):
366
- self._first_package_timestamp = time.time()*1000
367
- logger.debug('first package delay {}'.format(self._first_package_timestamp - self._start_stream_timestamp))
371
+ self._first_package_timestamp = time.time() * 1000
372
+ logger.debug('first package delay {}'.format(
373
+ self._first_package_timestamp -
374
+ self._start_stream_timestamp))
368
375
  sentence = part.output['sentence']
369
- logger.debug('Recv Result :{}, isEnd: {}'.format(sentence, RecognitionResult.is_sentence_end(sentence)))
376
+ logger.debug('Recv Result :{}, isEnd: {}'.format(
377
+ sentence,
378
+ RecognitionResult.is_sentence_end(sentence)))
370
379
  if RecognitionResult.is_sentence_end(sentence):
371
380
  sentences.append(sentence)
372
381
 
@@ -0,0 +1,270 @@
1
+ import asyncio
2
+ import os
3
+ from http import HTTPStatus
4
+ from typing import Any, Dict
5
+ from urllib.parse import urlparse
6
+
7
+ import aiohttp
8
+
9
+ from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
10
+ from dashscope.client.base_api import BaseApi
11
+ from dashscope.common.constants import ApiProtocol, HTTPMethod
12
+ from dashscope.common.error import InputRequired
13
+ from dashscope.common.utils import _get_task_group_and_task
14
+
15
+
16
+ class Transcribe(BaseApi):
17
+ """API for File Transcriber models.
18
+
19
+ """
20
+
21
+ MAX_QUERY_TRY_COUNT = 3
22
+
23
+ @classmethod
24
+ def call(cls, model: str, file: str, **kwargs) -> DashScopeAPIResponse:
25
+ """Call file transcriber model service.
26
+
27
+ Args:
28
+ model (str): The requested model, such as paraformer-16k-1
29
+ file (str): The local path or URL of the file.
30
+ channel_id (List[int], optional): The selected channel_id of audio file. # noqa: E501
31
+
32
+ Returns:
33
+ DashScopeAPIResponse: The response body.
34
+
35
+ Raises:
36
+ InputRequired: The file cannot be empty.
37
+ """
38
+ loop = asyncio.new_event_loop()
39
+ asyncio.set_event_loop(loop)
40
+ return loop.run_until_complete(cls.async_call(model, file, **kwargs))
41
+
42
+ @classmethod
43
+ async def async_call(cls, model: str, file: str,
44
+ **kwargs) -> DashScopeAPIResponse:
45
+ """Async call file transcriber model service.
46
+
47
+ Args:
48
+ model (str): The requested model, such as paraformer-16k-1
49
+ file (str): The local path or URL of the file.
50
+ channel_id (List[int], optional): The selected channel_id of audio file. # noqa: E501
51
+
52
+ Returns:
53
+ DashScopeAPIResponse: The response body.
54
+
55
+ Raises:
56
+ InputRequired: The file cannot be empty.
57
+ """
58
+ cls.is_url = cls._validate_file(file)
59
+ cls.file_name = file
60
+ cls.model_id = model
61
+
62
+ request = {'file': cls.file_name, 'is_url': cls.is_url}
63
+
64
+ # launch transcribe request, and get task info.
65
+ task = await cls._async_launch_requests(request, **kwargs)
66
+
67
+ response = await cls._async_get_result(task, **kwargs)
68
+
69
+ return response
70
+
71
+ @classmethod
72
+ async def _async_launch_requests(cls, request: Dict[str, Any], **kwargs):
73
+ """Async submit transcribe request.
74
+
75
+ Args:
76
+ inputs (Dict[str, Any]): The input parameters.
77
+
78
+ Returns:
79
+ task (Dict[str, Any]): The result of the task request.
80
+ """
81
+ inputs = {'file_link': request['file']}
82
+ task = {'file': request['file']}
83
+ local_file = None
84
+ try_count: int = 0
85
+ response = DashScopeAPIResponse(id='', code=HTTPStatus.OK, output=None)
86
+ if not request['is_url']:
87
+ try:
88
+ local_file = open(inputs['file_link'], 'rb')
89
+ except IOError as e:
90
+ raise InputRequired(f'File cannot be opened. {e}')
91
+
92
+ kwargs['form'] = {'av_file': local_file}
93
+
94
+ task_name, function = _get_task_group_and_task(__name__)
95
+ kwargs['async_request'] = True
96
+ kwargs['query'] = False
97
+
98
+ while True:
99
+ try:
100
+ response = await super().async_call(
101
+ model=cls.model_id,
102
+ task_group='audio',
103
+ task=task_name,
104
+ function=function,
105
+ input=inputs,
106
+ api_protocol=ApiProtocol.HTTP,
107
+ http_method=HTTPMethod.POST,
108
+ **kwargs)
109
+
110
+ task['request_id'] = response.id
111
+ task['code'] = response.code
112
+ task['status'] = response.status
113
+
114
+ if response.code == HTTPStatus.OK and response.output is not None: # noqa: E501
115
+ task.update(response.output)
116
+ else:
117
+ task['message'] = response.message
118
+
119
+ break
120
+
121
+ except (asyncio.TimeoutError, aiohttp.ClientConnectorError) as e:
122
+ try_count += 1
123
+ if try_count > Transcribe.MAX_QUERY_TRY_COUNT:
124
+ task['request_id'] = response.id
125
+ task['code'] = HTTPStatus.REQUEST_TIMEOUT
126
+ task['status'] = response.status
127
+ task['message'] = str(e)
128
+ break
129
+ else:
130
+ await asyncio.sleep(2)
131
+ continue
132
+ except Exception as e:
133
+ task['request_id'] = response.id
134
+ task['code'] = HTTPStatus.BAD_REQUEST
135
+ task['status'] = response.status
136
+ task['message'] = str(e)
137
+ break
138
+
139
+ if local_file is not None:
140
+ local_file.close()
141
+
142
+ return task
143
+
144
+ @classmethod
145
+ async def _async_get_result(cls, task, **kwargs):
146
+ """Async get transcribe result by polling.
147
+
148
+ Args:
149
+ task (Dict[str, Any]): The info of the task request.
150
+
151
+ Returns:
152
+ DashScopeAPIResponse: The response body.
153
+ """
154
+ request = task
155
+ responses = []
156
+ item = {}
157
+ response = DashScopeAPIResponse(id=request['request_id'],
158
+ code=request['code'],
159
+ output=None,
160
+ status=request['status'],
161
+ message=request['message'])
162
+
163
+ if request['code'] != HTTPStatus.OK:
164
+ item['file'] = request['file']
165
+ item['request_id'] = response.id
166
+ item['code'] = request['code']
167
+ item['status'] = request['status']
168
+ item['message'] = request['message']
169
+ responses.append(item)
170
+ else:
171
+ try_count: int = 0
172
+ while True:
173
+ item['file'] = request['file']
174
+ item['task_Id'] = request['task_id']
175
+
176
+ try:
177
+ inputs = {}
178
+ inputs['task_Id'] = request['task_id']
179
+ kwargs['async_request'] = True
180
+ kwargs['query'] = True
181
+
182
+ response = await super().async_call(
183
+ model=cls.model_id,
184
+ task_group=None,
185
+ task='tasks',
186
+ input=inputs,
187
+ task_id=inputs['task_Id'],
188
+ api_protocol=ApiProtocol.HTTP,
189
+ http_method=HTTPMethod.GET,
190
+ **kwargs)
191
+ except (asyncio.TimeoutError,
192
+ aiohttp.ClientConnectorError) as e:
193
+ try_count += 1
194
+ if try_count > Transcribe.MAX_QUERY_TRY_COUNT:
195
+ item['request_id'] = response.id
196
+ item['code'] = HTTPStatus.REQUEST_TIMEOUT
197
+ item['status'] = response.status
198
+ item['message'] = str(e)
199
+ responses.append(item)
200
+ break
201
+ else:
202
+ await asyncio.sleep(2)
203
+ continue
204
+ except Exception as e:
205
+ item['request_id'] = response.id
206
+ item['code'] = HTTPStatus.BAD_REQUEST
207
+ item['status'] = response.status
208
+ item['message'] = str(e)
209
+ responses.append(item)
210
+ break
211
+
212
+ try_count = 0
213
+ item['request_id'] = response.id
214
+ item['code'] = response.code
215
+ item['status'] = response.status
216
+
217
+ if response.code == HTTPStatus.OK:
218
+ if 'task_status' in response.output:
219
+ task_status = response.output['task_status']
220
+ if task_status == 'QUEUING' or task_status == 'PROCESSING': # noqa: E501
221
+ await asyncio.sleep(2)
222
+ continue
223
+
224
+ item.update(response.output)
225
+ else:
226
+ item['message'] = response.message
227
+
228
+ responses.append(item)
229
+ break
230
+
231
+ output = {}
232
+ output['results'] = responses
233
+
234
+ return DashScopeAPIResponse(id=response.id,
235
+ code=response.code,
236
+ status=response.status,
237
+ message=response.message,
238
+ output=output)
239
+
240
+ @classmethod
241
+ def _validate_file(cls, file: str):
242
+ """Check the validity of the file
243
+ and whether the file is a URL or a local path.
244
+
245
+ Args:
246
+ file (str): The local path or URL of the file.
247
+
248
+ Returns:
249
+ bool: Whether the file is a URL.
250
+ """
251
+ if file is None or len(file) == 0:
252
+ raise InputRequired(
253
+ 'Input an illegal file, please ensure that the file type is a local path or URL!' # noqa: *
254
+ )
255
+
256
+ if os.path.isfile(file):
257
+ return False
258
+ else:
259
+ result = urlparse(file)
260
+ if result.scheme is not None and len(result.scheme) > 0:
261
+ if result.scheme != 'http' and result.scheme != 'https':
262
+ raise InputRequired(
263
+ f'The URL protocol({result.scheme}) of file({file}) is not http or https.' # noqa: *
264
+ )
265
+ else:
266
+ raise InputRequired(
267
+ f'Input an illegal file({file}), maybe the file is inexistent.' # noqa: *
268
+ )
269
+
270
+ return True
@@ -3,7 +3,6 @@ import time
3
3
  from typing import List, Union
4
4
 
5
5
  import aiohttp
6
-
7
6
  from dashscope.api_entities.dashscope_response import (DashScopeAPIResponse,
8
7
  TranscriptionResponse)
9
8
  from dashscope.client.base_api import BaseAsyncApi
@@ -109,13 +108,11 @@ class Transcription(BaseAsyncApi):
109
108
  return TranscriptionResponse.from_api_response(response)
110
109
 
111
110
  @classmethod
112
- def fetch(
113
- cls,
114
- task: Union[str, TranscriptionResponse],
115
- api_key: str = None,
116
- workspace: str = None,
117
- **kwargs
118
- ) -> TranscriptionResponse:
111
+ def fetch(cls,
112
+ task: Union[str, TranscriptionResponse],
113
+ api_key: str = None,
114
+ workspace: str = None,
115
+ **kwargs) -> TranscriptionResponse:
119
116
  """Fetch the status of task, including results of batch transcription when task_status is SUCCEEDED. # noqa: E501
120
117
 
121
118
  Args:
@@ -147,13 +144,11 @@ class Transcription(BaseAsyncApi):
147
144
  return TranscriptionResponse.from_api_response(response)
148
145
 
149
146
  @classmethod
150
- def wait(
151
- cls,
152
- task: Union[str, TranscriptionResponse],
153
- api_key: str = None,
154
- workspace: str = None,
155
- **kwargs
156
- ) -> TranscriptionResponse:
147
+ def wait(cls,
148
+ task: Union[str, TranscriptionResponse],
149
+ api_key: str = None,
150
+ workspace: str = None,
151
+ **kwargs) -> TranscriptionResponse:
157
152
  """Poll task until the final results of transcription is obtained.
158
153
 
159
154
  Args:
@@ -164,7 +159,10 @@ class Transcription(BaseAsyncApi):
164
159
  Returns:
165
160
  TranscriptionResponse: The result of batch transcription.
166
161
  """
167
- response = super().wait(task, api_key=api_key, workspace=workspace, **kwargs)
162
+ response = super().wait(task,
163
+ api_key=api_key,
164
+ workspace=workspace,
165
+ **kwargs)
168
166
  return TranscriptionResponse.from_api_response(response)
169
167
 
170
168
  @classmethod
@@ -0,0 +1,173 @@
1
+ import asyncio
2
+ import time
3
+ from typing import List
4
+
5
+ import aiohttp
6
+ from dashscope.client.base_api import BaseApi
7
+ from dashscope.common.constants import ApiProtocol, HTTPMethod
8
+ from dashscope.common.logging import logger
9
+
10
+
11
+ class VocabularyServiceException(Exception):
12
+ def __init__(self, status_code: int, code: str,
13
+ error_message: str) -> None:
14
+ self._status_code = status_code
15
+ self._code = code
16
+ self._error_message = error_message
17
+
18
+ def __str__(self):
19
+ return f'Status Code: {self._status_code}, Code: {self._code}, Error Message: {self._error_message}'
20
+
21
+
22
+ class VocabularyService(BaseApi):
23
+ '''
24
+ API for asr vocabulary service
25
+ '''
26
+ MAX_QUERY_TRY_COUNT = 3
27
+
28
+ def __init__(self,
29
+ api_key=None,
30
+ workspace=None,
31
+ model=None,
32
+ **kwargs) -> None:
33
+ super().__init__()
34
+ self._api_key = api_key
35
+ self._workspace = workspace
36
+ self._kwargs = kwargs
37
+ self._last_request_id = None
38
+ self.model = model
39
+ if self.model == None:
40
+ self.model = 'speech-biasing'
41
+
42
+ def __call_with_input(self, input):
43
+ try_count = 0
44
+ while True:
45
+ try:
46
+ response = super().call(model=self.model,
47
+ task_group='audio',
48
+ task='asr',
49
+ function='customization',
50
+ input=input,
51
+ api_protocol=ApiProtocol.HTTP,
52
+ http_method=HTTPMethod.POST,
53
+ api_key=self._api_key,
54
+ workspace=self._workspace,
55
+ **self._kwargs)
56
+ except (asyncio.TimeoutError, aiohttp.ClientConnectorError) as e:
57
+ logger.error(e)
58
+ try_count += 1
59
+ if try_count <= VocabularyService.MAX_QUERY_TRY_COUNT:
60
+ time.sleep(2)
61
+ continue
62
+
63
+ break
64
+ logger.debug('>>>>recv', response)
65
+ return response
66
+
67
+ def create_vocabulary(self, target_model: str, prefix: str,
68
+ vocabulary: List[dict]) -> str:
69
+ '''
70
+ 创建热词表
71
+ param: target_model 热词表对应的语音识别模型版本
72
+ param: prefix 热词表自定义前缀,仅允许数字和小写字母,小于十个字符。
73
+ param: vocabulary 热词表字典
74
+ return: 热词表标识符 vocabulary_id
75
+ '''
76
+ response = self.__call_with_input(input={
77
+ 'action': 'create_vocabulary',
78
+ 'target_model': target_model,
79
+ 'prefix': prefix,
80
+ 'vocabulary': vocabulary,
81
+ }, )
82
+ if response.status_code == 200:
83
+ self._last_request_id = response.request_id
84
+ return response.output['vocabulary_id']
85
+ else:
86
+ raise VocabularyServiceException(response.status_code,
87
+ response.code, response.message)
88
+
89
+ def list_vocabularies(self,
90
+ prefix=None,
91
+ page_index: int = 0,
92
+ page_size: int = 10) -> List[dict]:
93
+ '''
94
+ 查询已创建的所有热词表
95
+ param: prefix 自定义前缀,如果设定则只返回指定前缀的热词表标识符列表。
96
+ param: page_index 查询的页索引
97
+ param: page_size 查询页大小
98
+ return: 热词表标识符列表
99
+ '''
100
+ if prefix:
101
+ response = self.__call_with_input(input={
102
+ 'action': 'list_vocabulary',
103
+ 'prefix': prefix,
104
+ 'page_index': page_index,
105
+ 'page_size': page_size,
106
+ }, )
107
+ else:
108
+ response = self.__call_with_input(input={
109
+ 'action': 'list_vocabulary',
110
+ 'page_index': page_index,
111
+ 'page_size': page_size,
112
+ }, )
113
+ if response.status_code == 200:
114
+ self._last_request_id = response.request_id
115
+ return response.output['vocabulary_list']
116
+ else:
117
+ raise VocabularyServiceException(response.status_code,
118
+ response.code, response.message)
119
+
120
+ def query_vocabulary(self, vocabulary_id: str) -> List[dict]:
121
+ '''
122
+ 获取热词表内容
123
+ param: vocabulary_id 热词表标识符
124
+ return: 热词表
125
+ '''
126
+ response = self.__call_with_input(input={
127
+ 'action': 'query_vocabulary',
128
+ 'vocabulary_id': vocabulary_id,
129
+ }, )
130
+ if response.status_code == 200:
131
+ self._last_request_id = response.request_id
132
+ return response.output
133
+ else:
134
+ raise VocabularyServiceException(response.status_code,
135
+ response.code, response.message)
136
+
137
+ def update_vocabulary(self, vocabulary_id: str,
138
+ vocabulary: List[dict]) -> None:
139
+ '''
140
+ 用新的热词表替换已有热词表
141
+ param: vocabulary_id 需要替换的热词表标识符
142
+ param: vocabulary 热词表
143
+ '''
144
+ response = self.__call_with_input(input={
145
+ 'action': 'update_vocabulary',
146
+ 'vocabulary_id': vocabulary_id,
147
+ 'vocabulary': vocabulary,
148
+ }, )
149
+ if response.status_code == 200:
150
+ self._last_request_id = response.request_id
151
+ return
152
+ else:
153
+ raise VocabularyServiceException(response.status_code,
154
+ response.code, response.message)
155
+
156
+ def delete_vocabulary(self, vocabulary_id: str) -> None:
157
+ '''
158
+ 删除热词表
159
+ param: vocabulary_id 需要删除的热词表标识符
160
+ '''
161
+ response = self.__call_with_input(input={
162
+ 'action': 'delete_vocabulary',
163
+ 'vocabulary_id': vocabulary_id,
164
+ }, )
165
+ if response.status_code == 200:
166
+ self._last_request_id = response.request_id
167
+ return
168
+ else:
169
+ raise VocabularyServiceException(response.status_code,
170
+ response.code, response.message)
171
+
172
+ def get_last_request_id(self):
173
+ return self._last_request_id
@@ -1,7 +1,7 @@
1
+ from .enrollment import VoiceEnrollmentException, VoiceEnrollmentService
1
2
  from .speech_synthesizer import AudioFormat, ResultCallback, SpeechSynthesizer
2
3
 
3
- __all__ = ['SpeechSynthesizer', 'ResultCallback', 'AudioFormat']
4
-
5
- # from .speech_synthesizer import (SpeechSynthesizer, ResultCallback, SpeechSynthesisResult, AudioFormat)
6
-
7
- # __all__ = ['SpeechSynthesizer', 'ResultCallback', 'SpeechSynthesisResult', 'AudioFormat']
4
+ __all__ = [
5
+ 'SpeechSynthesizer', 'ResultCallback', 'AudioFormat',
6
+ 'VoiceEnrollmentException', 'VoiceEnrollmentService'
7
+ ]
@@ -0,0 +1,170 @@
1
+ import asyncio
2
+ import time
3
+ from typing import List
4
+
5
+ import aiohttp
6
+ from dashscope.client.base_api import BaseApi
7
+ from dashscope.common.constants import ApiProtocol, HTTPMethod
8
+ from dashscope.common.logging import logger
9
+
10
+
11
+ class VoiceEnrollmentException(Exception):
12
+ def __init__(self, status_code: int, code: str,
13
+ error_message: str) -> None:
14
+ self._status_code = status_code
15
+ self._code = code
16
+ self._error_message = error_message
17
+
18
+ def __str__(self):
19
+ return f'Status Code: {self._status_code}, Code: {self._code}, Error Message: {self._error_message}'
20
+
21
+
22
+ class VoiceEnrollmentService(BaseApi):
23
+ '''
24
+ API for voice clone service
25
+ '''
26
+ MAX_QUERY_TRY_COUNT = 3
27
+
28
+ def __init__(self,
29
+ api_key=None,
30
+ workspace=None,
31
+ model=None,
32
+ **kwargs) -> None:
33
+ super().__init__()
34
+ self._api_key = api_key
35
+ self._workspace = workspace
36
+ self._kwargs = kwargs
37
+ self._last_request_id = None
38
+ self.model = model
39
+ if self.model == None:
40
+ self.model = 'voice-enrollment'
41
+
42
+ def __call_with_input(self, input):
43
+ try_count = 0
44
+ while True:
45
+ try:
46
+ response = super().call(model=self.model,
47
+ task_group='audio',
48
+ task='tts',
49
+ function='customization',
50
+ input=input,
51
+ api_protocol=ApiProtocol.HTTP,
52
+ http_method=HTTPMethod.POST,
53
+ api_key=self._api_key,
54
+ workspace=self._workspace,
55
+ **self._kwargs)
56
+ except (asyncio.TimeoutError, aiohttp.ClientConnectorError) as e:
57
+ logger.error(e)
58
+ try_count += 1
59
+ if try_count <= VoiceEnrollmentService.MAX_QUERY_TRY_COUNT:
60
+ time.sleep(2)
61
+ continue
62
+
63
+ break
64
+ logger.debug('>>>>recv', response)
65
+ return response
66
+
67
+ def create_voice(self, target_model: str, prefix: str, url: str) -> str:
68
+ '''
69
+ 创建新克隆音色
70
+ param: target_model 克隆音色对应的语音识别模型版本
71
+ param: prefix 音色自定义前缀,仅允许数字和小写字母,小于十个字符。
72
+ param: url 用于克隆的音频文件url
73
+ return: voice_id
74
+ '''
75
+ response = self.__call_with_input(input={
76
+ 'action': 'create_voice',
77
+ 'target_model': target_model,
78
+ 'prefix': prefix,
79
+ 'url': url,
80
+ }, )
81
+ if response.status_code == 200:
82
+ self._last_request_id = response.request_id
83
+ return response.output['voice_id']
84
+ else:
85
+ raise VoiceEnrollmentException(response.status_code, response.code,
86
+ response.message)
87
+
88
+ def list_voices(self,
89
+ prefix=None,
90
+ page_index: int = 0,
91
+ page_size: int = 10) -> List[dict]:
92
+ '''
93
+ 查询已创建的所有音色
94
+ param: page_index 查询的页索引
95
+ param: page_size 查询页大小
96
+ return: List[dict] 音色列表,包含每个音色的id,创建时间,修改时间,状态。
97
+ '''
98
+ if prefix:
99
+ response = self.__call_with_input(input={
100
+ 'action': 'list_voice',
101
+ 'prefix': prefix,
102
+ 'page_index': page_index,
103
+ 'page_size': page_size,
104
+ }, )
105
+ else:
106
+ response = self.__call_with_input(input={
107
+ 'action': 'list_voice',
108
+ 'page_index': page_index,
109
+ 'page_size': page_size,
110
+ }, )
111
+ if response.status_code == 200:
112
+ self._last_request_id = response.request_id
113
+ return response.output['voice_list']
114
+ else:
115
+ raise VoiceEnrollmentException(response.status_code, response.code,
116
+ response.message)
117
+
118
+ def query_voice(self, voice_id: str) -> List[str]:
119
+ '''
120
+ 查询已创建的所有音色
121
+ param: voice_id 需要查询的音色
122
+ return: bytes 注册音色使用的音频
123
+ '''
124
+ response = self.__call_with_input(input={
125
+ 'action': 'query_voice',
126
+ 'voice_id': voice_id,
127
+ }, )
128
+ if response.status_code == 200:
129
+ self._last_request_id = response.request_id
130
+ return response.output
131
+ else:
132
+ raise VoiceEnrollmentException(response.status_code, response.code,
133
+ response.message)
134
+
135
+ def update_voice(self, voice_id: str, url: str) -> None:
136
+ '''
137
+ 更新音色
138
+ param: voice_id 音色id
139
+ param: url 用于克隆的音频文件url
140
+ '''
141
+ response = self.__call_with_input(input={
142
+ 'action': 'update_voice',
143
+ 'voice_id': voice_id,
144
+ 'url': url,
145
+ }, )
146
+ if response.status_code == 200:
147
+ self._last_request_id = response.request_id
148
+ return
149
+ else:
150
+ raise VoiceEnrollmentException(response.status_code, response.code,
151
+ response.message)
152
+
153
+ def delete_voice(self, voice_id: str) -> None:
154
+ '''
155
+ 删除音色
156
+ param: voice_id 需要删除的音色
157
+ '''
158
+ response = self.__call_with_input(input={
159
+ 'action': 'delete_voice',
160
+ 'voice_id': voice_id,
161
+ }, )
162
+ if response.status_code == 200:
163
+ self._last_request_id = response.request_id
164
+ return
165
+ else:
166
+ raise VoiceEnrollmentException(response.status_code, response.code,
167
+ response.message)
168
+
169
+ def get_last_request_id(self):
170
+ return self._last_request_id
@@ -80,7 +80,7 @@ class Request:
80
80
  voice,
81
81
  format='wav',
82
82
  sample_rate=16000,
83
- volumn=50,
83
+ volume=50,
84
84
  speech_rate=1.0,
85
85
  pitch_rate=1.0,
86
86
  ):
@@ -90,7 +90,7 @@ class Request:
90
90
  self.model = model
91
91
  self.format = format
92
92
  self.sample_rate = sample_rate
93
- self.volumn = volumn
93
+ self.volume = volume
94
94
  self.speech_rate = speech_rate
95
95
  self.pitch_rate = pitch_rate
96
96
 
@@ -136,7 +136,7 @@ class Request:
136
136
  },
137
137
  'parameters': {
138
138
  'voice': self.voice,
139
- 'volume': self.volumn,
139
+ 'volume': self.volume,
140
140
  'text_type': 'PlainText',
141
141
  'sample_rate': self.sample_rate,
142
142
  'rate': self.speech_rate,
@@ -190,7 +190,7 @@ class SpeechSynthesizer:
190
190
  model,
191
191
  voice,
192
192
  format: AudioFormat = AudioFormat.DEFAULT,
193
- volumn=50,
193
+ volume=50,
194
194
  speech_rate=1.0,
195
195
  pitch_rate=1.0,
196
196
  headers=None,
@@ -253,7 +253,7 @@ class SpeechSynthesizer:
253
253
  voice=voice,
254
254
  format=format.format,
255
255
  sample_rate=format.sample_rate,
256
- volumn=volumn,
256
+ volume=volume,
257
257
  speech_rate=speech_rate,
258
258
  pitch_rate=pitch_rate,
259
259
  )
@@ -424,6 +424,8 @@ class SpeechSynthesizer:
424
424
  request = self.request.getFinishRequest()
425
425
  self.__send_str(request)
426
426
  self.close()
427
+ self.start_event.set()
428
+ self.complete_event.set()
427
429
 
428
430
  # 监听消息的回调函数
429
431
  def on_message(self, ws, message):
@@ -386,12 +386,6 @@ class Runs(CreateMixin, CancelMixin, ListObjectMixin, GetStatusMixin,
386
386
  thread_id=thread_id,
387
387
  workspace=workspace,
388
388
  api_key=api_key)
389
- import json
390
- print(
391
- json.dumps(run,
392
- default=lambda o: o.__dict__,
393
- sort_keys=True,
394
- indent=4))
395
389
  if run.status_code == HTTPStatus.OK:
396
390
  if hasattr(run, 'status'):
397
391
  if run.status in [
@@ -121,8 +121,51 @@ def upload_file(model: str, upload_path: str, api_key: str):
121
121
  return None
122
122
 
123
123
 
124
+ def check_and_upload_local(model: str, content: str, api_key: str):
125
+ """Check the content is local file path, upload and return the url
126
+
127
+ Args:
128
+ model (str): Which model to upload.
129
+ content (str): The content.
130
+ api_key (_type_): The api key.
131
+
132
+ Raises:
133
+ UploadFileException: Upload failed.
134
+ InvalidInput: The input is invalid
135
+
136
+ Returns:
137
+ _type_: if upload return True and file_url otherwise False, origin content.
138
+ """
139
+ if content.startswith(FILE_PATH_SCHEMA):
140
+ parse_result = urlparse(content)
141
+ if parse_result.netloc:
142
+ file_path = parse_result.netloc + unquote_plus(parse_result.path)
143
+ else:
144
+ file_path = unquote_plus(parse_result.path)
145
+ if os.path.exists(file_path):
146
+ file_url = OssUtils.upload(model=model,
147
+ file_path=file_path,
148
+ api_key=api_key)
149
+ if file_url is None:
150
+ raise UploadFileException('Uploading file: %s failed' %
151
+ content)
152
+ return True, file_url
153
+ else:
154
+ raise InvalidInput('The file: %s is not exists!' % file_path)
155
+ elif not content.startswith('http'):
156
+ if os.path.exists(content):
157
+ file_url = OssUtils.upload(model=model,
158
+ file_path=content,
159
+ api_key=api_key)
160
+ if file_url is None:
161
+ raise UploadFileException('Uploading file: %s failed' %
162
+ content)
163
+ return True, file_url
164
+ return False, content
165
+
166
+
124
167
  def check_and_upload(model, elem: dict, api_key):
125
- is_upload = False
168
+ has_upload = False
126
169
  for key, content in elem.items():
127
170
  # support video:[images] for qwen2-vl
128
171
  is_list = isinstance(content, list)
@@ -130,38 +173,14 @@ def check_and_upload(model, elem: dict, api_key):
130
173
 
131
174
  if key in ['image', 'video', 'audio', 'text']:
132
175
  for i, content in enumerate(contents):
133
- if content.startswith(FILE_PATH_SCHEMA):
134
- parse_result = urlparse(content)
135
- if parse_result.netloc:
136
- file_path = parse_result.netloc + unquote_plus(
137
- parse_result.path)
138
- else:
139
- file_path = unquote_plus(parse_result.path)
140
- if os.path.exists(file_path):
141
- file_url = OssUtils.upload(model=model,
142
- file_path=file_path,
143
- api_key=api_key)
144
- if file_url is None:
145
- raise UploadFileException(
146
- 'Uploading file: %s failed' % content)
147
- contents[i] = file_url
148
- is_upload = True
149
- else:
150
- raise InvalidInput('The file: %s is not exists!' %
151
- file_path)
152
- elif not content.startswith('http'):
153
- if os.path.exists(content):
154
- file_url = OssUtils.upload(model=model,
155
- file_path=content,
156
- api_key=api_key)
157
- if file_url is None:
158
- raise UploadFileException(
159
- 'Uploading file: %s failed' % content)
160
- contents[i] = file_url
161
- is_upload = True
176
+ is_upload, file_url = check_and_upload_local(
177
+ model, content, api_key)
178
+ if is_upload:
179
+ contents[i] = file_url
180
+ has_upload = True
162
181
  elem[key] = contents if is_list else contents[0]
163
182
 
164
- return is_upload
183
+ return has_upload
165
184
 
166
185
 
167
186
  def preprocess_message_element(model: str, elem: List[dict], api_key: str):
dashscope/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = '1.20.8'
1
+ __version__ = '1.20.10'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dashscope
3
- Version: 1.20.8
3
+ Version: 1.20.10
4
4
  Summary: dashscope client sdk library
5
5
  Home-page: https://dashscope.aliyun.com/
6
6
  Author: Alibaba Cloud
@@ -6,12 +6,12 @@ dashscope/files.py,sha256=QgJjwhtn9F548nCA8jD8OvE6aQEj-20hZqJgYXsUdQU,3930
6
6
  dashscope/finetune.py,sha256=_tflDUvu0KagSoCzLaf0hofpG_P8NU6PylL8CPjVhrA,6243
7
7
  dashscope/model.py,sha256=UPOn1qMYFhX-ovXi3BMxZEBk8qOK7WLJOYHMbPZwYBo,1440
8
8
  dashscope/models.py,sha256=1-bc-Ue68zurgu_y6RhfFr9uzeQMF5AZq-C32lJGMGU,1224
9
- dashscope/version.py,sha256=528qtkrW31Rybnpunug78DmLVf7Ys8Mn-aRU-fFyAdA,23
9
+ dashscope/version.py,sha256=9ivfT91xguUu9PULHzh1-7jFzb5sdLCuzbdRnbN8EGo,24
10
10
  dashscope/aigc/__init__.py,sha256=s-MCA87KYiVumYtKtJi5IMN7xelSF6TqEU3s3_7RF-Y,327
11
11
  dashscope/aigc/code_generation.py,sha256=KAJVrGp6tiNFBBg64Ovs9RfcP5SrIhrbW3wdA89NKso,10885
12
12
  dashscope/aigc/conversation.py,sha256=xRoJlCR-IXHjSdkDrK74a9ut1FJg0FZhTNXZAJC18MA,14231
13
13
  dashscope/aigc/generation.py,sha256=53oMCmN5ZbqeqAsKxmdunXlRh-XP8ZtnA7hB2id4Koo,17897
14
- dashscope/aigc/image_synthesis.py,sha256=_TGkh4L_yBNQNmoJUKlfXfljKfxX-SSyxHHQBDm1AC8,10418
14
+ dashscope/aigc/image_synthesis.py,sha256=UWHW-nvf7_aDZKr4uZDusVHjqWr9TSZjCsZI8YSWaek,11052
15
15
  dashscope/aigc/multimodal_conversation.py,sha256=SlNnnsUPV19gdx8fYJAtsMFWPNGY6vhk5IGHZ5ZczpI,5369
16
16
  dashscope/api_entities/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
17
  dashscope/api_entities/aiohttp_request.py,sha256=aE3AeWba8Ig_xHMYjrAdkq0N61l_L2VFTG6HYh912X0,10229
@@ -29,14 +29,17 @@ dashscope/assistants/assistant_types.py,sha256=1jNL30TOlrkiYhvCaB3E8jkPLG8CnQ6I3
29
29
  dashscope/assistants/assistants.py,sha256=NYahIDqhtnOcQOmnhZsjc5F5jvBUQcce8-fbrJXHVnQ,10833
30
30
  dashscope/assistants/files.py,sha256=pwLVJ_pjpRFWyfI_MRvhH7Si7FzGDj4ChzZgWTpLOhg,6699
31
31
  dashscope/audio/__init__.py,sha256=-ZRxrK-gV4QsUtlThIT-XwqB6vmyEsnhxIxdLmhCUuc,61
32
- dashscope/audio/asr/__init__.py,sha256=-s180qWn_JPSpCo1q0aDJJ5HQ3zTzD4z5yUwsRqH4aU,275
32
+ dashscope/audio/asr/__init__.py,sha256=kFdx3IYsdfGGDDlQmUjvtd2kqifuEekwlPBEOUvXvEY,406
33
33
  dashscope/audio/asr/asr_phrase_manager.py,sha256=EjtbI3zz9UQGS1qv6Yb4zzEMj4OJJVXmwkqZyIrzvEA,7642
34
- dashscope/audio/asr/recognition.py,sha256=cEooE3wGf8kKfJIVbaXEytl5X6F0hMsLe8g4Bj9Fn4w,18768
35
- dashscope/audio/asr/transcription.py,sha256=1WAg9WH89antVzRYEKXb5LQP9xylZmX4YKp7v5oMYjY,8931
34
+ dashscope/audio/asr/recognition.py,sha256=a4zIkIMiWwOEApP9k9ZC9jGDr7CP7BqB6Cy1dBVTN4g,18978
35
+ dashscope/audio/asr/transcribe.py,sha256=HfZYpvpVfvGRAIIIzX65Af33E6vsIFGd_qqhQ8LaNcM,9651
36
+ dashscope/audio/asr/transcription.py,sha256=D8CW0XDqJuEJVmNFJ6qczTysSV3Sz_rzk2C6NIKTtVc,9042
37
+ dashscope/audio/asr/vocabulary.py,sha256=880u5CGh8Ky9iWXDf_7cUuHfL5AGmw8JJRCbRThVCMI,6484
36
38
  dashscope/audio/tts/__init__.py,sha256=fbnieZX9yNFNh5BsxLpLXb63jlxzxrdCJakV3ignjlQ,194
37
39
  dashscope/audio/tts/speech_synthesizer.py,sha256=dnKx9FDDdO_ETHAjhK8zaMVaH6SfoTtN5YxXXqgY1JA,7571
38
- dashscope/audio/tts_v2/__init__.py,sha256=ve5a81qTbWDcRaSuritZtJBzryOIol2_dxzfqqdCw-k,345
39
- dashscope/audio/tts_v2/speech_synthesizer.py,sha256=bpzj9gx2D_FfOzgsjU-GBGmeWvEdewNPFd447mOgM-o,19220
40
+ dashscope/audio/tts_v2/__init__.py,sha256=5UfyDBYnuGgOy9KMxEIXA2U2ihcXutdZc1cqJudy-8M,282
41
+ dashscope/audio/tts_v2/enrollment.py,sha256=sUkOEUsP8RXREMtTkAeDTYfrQJ6lPnM_Y-DeefXB_Q4,6140
42
+ dashscope/audio/tts_v2/speech_synthesizer.py,sha256=lATasQJB8HlB_yYm90qqW6zIAE1CQFxBxhnch6xdg9s,19285
40
43
  dashscope/client/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
41
44
  dashscope/client/base_api.py,sha256=rXN97XGyDhCCaD_dz_clpFDjOJfpGjqiH7yX3LaD-GE,41233
42
45
  dashscope/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -73,7 +76,7 @@ dashscope/threads/messages/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJ
73
76
  dashscope/threads/messages/files.py,sha256=wi0nJ2zsPWOw2Jn-ZkxA3URZBIrkGxqM_uAPfXY1xv0,3820
74
77
  dashscope/threads/messages/messages.py,sha256=Zjmyf3rT1XSdn33hPrqOY6DSWUVL7pDEapG03FREPV8,8419
75
78
  dashscope/threads/runs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
76
- dashscope/threads/runs/runs.py,sha256=Cvy5FD0x1Z9c5qayYeNpoL_QIqH4yxgqdGplCk3soRw,18762
79
+ dashscope/threads/runs/runs.py,sha256=ux4VH_lxxHCw1XOqngzmsm9kwTR3jS0wX27xoAswHlY,18549
77
80
  dashscope/threads/runs/steps.py,sha256=pLNR-5g7zvYkvC-p4sZGVgYHd1jqxBerM2WFyB358H8,3638
78
81
  dashscope/tokenizers/__init__.py,sha256=Oy5FMT37Non6e1YxdHQ89U93Dy3CG1Ez0gBa771KZo0,200
79
82
  dashscope/tokenizers/qwen_tokenizer.py,sha256=dCnT9-9NrqPS85bEhjlPULUfDADVRhlleYwM_ILgCeI,4111
@@ -81,10 +84,10 @@ dashscope/tokenizers/tokenization.py,sha256=G6cSEmVLr3pjXUC3EOU9ot8MYxNnOQ4wOB2m
81
84
  dashscope/tokenizers/tokenizer.py,sha256=y6P91qTCYo__pEx_0VHAcj9YECfbUdRqZU1fdGTjF4o,1154
82
85
  dashscope/tokenizers/tokenizer_base.py,sha256=REDhzRyDT13iequ61-a6_KcTy0GFKlihQve5HkyoyRs,656
83
86
  dashscope/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
84
- dashscope/utils/oss_utils.py,sha256=TSBh7MJK4ZH40Mxd8wNEsG8nQLNhQjWRjW3itHsvoZ0,7023
85
- dashscope-1.20.8.dist-info/LICENSE,sha256=Izp5L1DF1Mbza6qojkqNNWlE_mYLnr4rmzx2EBF8YFw,11413
86
- dashscope-1.20.8.dist-info/METADATA,sha256=M4hx1_1AZmfHDck7Aw4tYEKT4Fe3Ogf_iXFlIGU_cBc,6641
87
- dashscope-1.20.8.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
88
- dashscope-1.20.8.dist-info/entry_points.txt,sha256=raEp5dOuj8whJ7yqZlDM8WQ5p2RfnGrGNo0QLQEnatY,50
89
- dashscope-1.20.8.dist-info/top_level.txt,sha256=woqavFJK9zas5xTqynmALqOtlafghjsk63Xk86powTU,10
90
- dashscope-1.20.8.dist-info/RECORD,,
87
+ dashscope/utils/oss_utils.py,sha256=7vZ2Lypxwiit8VcAqAvr3cCyhVfaLapDiNuF-H3ZCD4,7332
88
+ dashscope-1.20.10.dist-info/LICENSE,sha256=Izp5L1DF1Mbza6qojkqNNWlE_mYLnr4rmzx2EBF8YFw,11413
89
+ dashscope-1.20.10.dist-info/METADATA,sha256=tjvuGjHvDHLCC1spvuhLM9GF_ImmIffIGifJAFdya3s,6642
90
+ dashscope-1.20.10.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
91
+ dashscope-1.20.10.dist-info/entry_points.txt,sha256=raEp5dOuj8whJ7yqZlDM8WQ5p2RfnGrGNo0QLQEnatY,50
92
+ dashscope-1.20.10.dist-info/top_level.txt,sha256=woqavFJK9zas5xTqynmALqOtlafghjsk63Xk86powTU,10
93
+ dashscope-1.20.10.dist-info/RECORD,,