dashscope 1.20.14__py3-none-any.whl → 1.22.0__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.

dashscope/__init__.py CHANGED
@@ -5,6 +5,7 @@ from dashscope.aigc.code_generation import CodeGeneration
5
5
  from dashscope.aigc.conversation import Conversation, History, HistoryItem
6
6
  from dashscope.aigc.generation import AioGeneration, Generation
7
7
  from dashscope.aigc.image_synthesis import ImageSynthesis
8
+ from dashscope.aigc.video_synthesis import VideoSynthesis
8
9
  from dashscope.aigc.multimodal_conversation import MultiModalConversation
9
10
  from dashscope.app.application import Application
10
11
  from dashscope.assistants import Assistant, AssistantList, Assistants
@@ -84,6 +85,7 @@ __all__ = [
84
85
  RunStep,
85
86
  MessageFile,
86
87
  AssistantFile,
88
+ VideoSynthesis,
87
89
  ]
88
90
 
89
91
  logging.getLogger(__name__).addHandler(NullHandler())
@@ -2,6 +2,7 @@ from .conversation import Conversation, History, HistoryItem
2
2
  from .generation import Generation
3
3
  from .image_synthesis import ImageSynthesis
4
4
  from .multimodal_conversation import MultiModalConversation
5
+ from .video_synthesis import VideoSynthesis
5
6
 
6
7
  __all__ = [
7
8
  Generation,
@@ -10,4 +11,5 @@ __all__ = [
10
11
  History,
11
12
  ImageSynthesis,
12
13
  MultiModalConversation,
14
+ VideoSynthesis,
13
15
  ]
@@ -0,0 +1,242 @@
1
+ from typing import Any, Dict, Union
2
+
3
+ from dashscope.api_entities.dashscope_response import (DashScopeAPIResponse,
4
+ VideoSynthesisResponse)
5
+ from dashscope.client.base_api import BaseAsyncApi
6
+ from dashscope.common.constants import PROMPT
7
+ from dashscope.common.error import InputRequired
8
+ from dashscope.common.utils import _get_task_group_and_task
9
+ from dashscope.utils.oss_utils import check_and_upload_local
10
+
11
+
12
+ class VideoSynthesis(BaseAsyncApi):
13
+ task = 'video-generation'
14
+ """API for video synthesis.
15
+ """
16
+ class Models:
17
+ wanx_txt2video_pro = 'wanx-txt2video-pro'
18
+ wanx_img2video_pro = 'wanx-img2video-pro'
19
+ wanx_2_1_t2v_turbo = 'wanx2.1-t2v-turbo'
20
+ wanx_2_1_t2v_plus = 'wanx2.1-t2v-plus'
21
+
22
+ @classmethod
23
+ def call(cls,
24
+ model: str,
25
+ prompt: Any,
26
+ extend_prompt: bool = True,
27
+ negative_prompt: str = None,
28
+ template: str = None,
29
+ img_url: str = None,
30
+ api_key: str = None,
31
+ extra_input: Dict = None,
32
+ workspace: str = None,
33
+ task: str = None,
34
+ **kwargs) -> VideoSynthesisResponse:
35
+ """Call video synthesis service and get result.
36
+
37
+ Args:
38
+ model (str): The model, reference ``Models``.
39
+ prompt (Any): The prompt for video synthesis.
40
+ extend_prompt (bool): The extend_prompt. Whether to enable write expansion. The default value is True.
41
+ negative_prompt (str): The negative prompt is the opposite of the prompt meaning.
42
+ template (str): LoRa input, such as gufeng, katong, etc.
43
+ img_url (str): The input image url, Generate the URL of the image referenced by the video.
44
+ api_key (str, optional): The api api_key. Defaults to None.
45
+ workspace (str): The dashscope workspace id.
46
+ extra_input (Dict): The extra input parameters.
47
+ task (str): The task of api, ref doc.
48
+ **kwargs:
49
+ size(str, `optional`): The output video size(width*height).
50
+ duration(int, optional): The duration. Duration of video generation. The default value is 5, in seconds.
51
+ seed(int, optional): The seed. The random seed for video generation. The default value is 5.
52
+
53
+ Raises:
54
+ InputRequired: The prompt cannot be empty.
55
+
56
+ Returns:
57
+ VideoSynthesisResponse: The video synthesis result.
58
+ """
59
+ return super().call(model,
60
+ prompt,
61
+ img_url=img_url,
62
+ api_key=api_key,
63
+ extend_prompt=extend_prompt,
64
+ negative_prompt=negative_prompt,
65
+ template=template,
66
+ workspace=workspace,
67
+ extra_input=extra_input,
68
+ task=task,
69
+ **kwargs)
70
+
71
+ @classmethod
72
+ def async_call(cls,
73
+ model: str,
74
+ prompt: Any,
75
+ img_url: str = None,
76
+ extend_prompt: bool = True,
77
+ negative_prompt: str = None,
78
+ template: str = None,
79
+ api_key: str = None,
80
+ extra_input: Dict = None,
81
+ workspace: str = None,
82
+ task: str = None,
83
+ **kwargs) -> VideoSynthesisResponse:
84
+ """Create a video synthesis task, and return task information.
85
+
86
+ Args:
87
+ model (str): The model, reference ``Models``.
88
+ prompt (Any): The prompt for video synthesis.
89
+ extend_prompt (bool): The extend_prompt. Whether to enable write expansion. The default value is True.
90
+ negative_prompt (str): The negative prompt is the opposite of the prompt meaning.
91
+ template (str): LoRa input, such as gufeng, katong, etc.
92
+ img_url (str): The input image url, Generate the URL of the image referenced by the video.
93
+ api_key (str, optional): The api api_key. Defaults to None.
94
+ workspace (str): The dashscope workspace id.
95
+ extra_input (Dict): The extra input parameters.
96
+ task (str): The task of api, ref doc.
97
+ **kwargs:
98
+ size(str, `optional`): The output video size(width*height).
99
+ duration(int, optional): The duration. Duration of video generation. The default value is 5, in seconds.
100
+ seed(int, optional): The seed. The random seed for video generation. The default value is 5.
101
+
102
+ Raises:
103
+ InputRequired: The prompt cannot be empty.
104
+
105
+ Returns:
106
+ DashScopeAPIResponse: The video synthesis
107
+ task id in the response.
108
+ """
109
+ if prompt is None or not prompt:
110
+ raise InputRequired('prompt is required!')
111
+ task_group, function = _get_task_group_and_task(__name__)
112
+ inputs = {PROMPT: prompt, 'extend_prompt': extend_prompt}
113
+ if negative_prompt:
114
+ inputs['negative_prompt'] = negative_prompt
115
+ if template:
116
+ inputs['template'] = template
117
+ has_upload = False
118
+ if img_url is not None and img_url:
119
+ is_upload, res_img_url = check_and_upload_local(
120
+ model, img_url, api_key)
121
+ if is_upload:
122
+ has_upload = True
123
+ inputs['img_url'] = res_img_url
124
+ if extra_input is not None and extra_input:
125
+ inputs = {**inputs, **extra_input}
126
+ if has_upload:
127
+ headers = kwargs.pop('headers', {})
128
+ headers['X-DashScope-OssResourceResolve'] = 'enable'
129
+ kwargs['headers'] = headers
130
+ response = super().async_call(
131
+ model=model,
132
+ task_group=task_group,
133
+ task=VideoSynthesis.task if task is None else task,
134
+ function=function,
135
+ api_key=api_key,
136
+ input=inputs,
137
+ workspace=workspace,
138
+ **kwargs)
139
+ return VideoSynthesisResponse.from_api_response(response)
140
+
141
+ @classmethod
142
+ def fetch(cls,
143
+ task: Union[str, VideoSynthesisResponse],
144
+ api_key: str = None,
145
+ workspace: str = None) -> VideoSynthesisResponse:
146
+ """Fetch video synthesis task status or result.
147
+
148
+ Args:
149
+ task (Union[str, VideoSynthesisResponse]): The task_id or
150
+ VideoSynthesisResponse return by async_call().
151
+ api_key (str, optional): The api api_key. Defaults to None.
152
+ workspace (str): The dashscope workspace id.
153
+
154
+ Returns:
155
+ VideoSynthesisResponse: The task status or result.
156
+ """
157
+ response = super().fetch(task, api_key=api_key, workspace=workspace)
158
+ return VideoSynthesisResponse.from_api_response(response)
159
+
160
+ @classmethod
161
+ def wait(cls,
162
+ task: Union[str, VideoSynthesisResponse],
163
+ api_key: str = None,
164
+ workspace: str = None) -> VideoSynthesisResponse:
165
+ """Wait for video synthesis task to complete, and return the result.
166
+
167
+ Args:
168
+ task (Union[str, VideoSynthesisResponse]): The task_id or
169
+ VideoSynthesisResponse return by async_call().
170
+ api_key (str, optional): The api api_key. Defaults to None.
171
+ workspace (str): The dashscope workspace id.
172
+
173
+ Returns:
174
+ VideoSynthesisResponse: The task result.
175
+ """
176
+ response = super().wait(task, api_key, workspace=workspace)
177
+ return VideoSynthesisResponse.from_api_response(response)
178
+
179
+ @classmethod
180
+ def cancel(cls,
181
+ task: Union[str, VideoSynthesisResponse],
182
+ api_key: str = None,
183
+ workspace: str = None) -> DashScopeAPIResponse:
184
+ """Cancel video synthesis task.
185
+ Only tasks whose status is PENDING can be canceled.
186
+
187
+ Args:
188
+ task (Union[str, VideoSynthesisResponse]): The task_id or
189
+ VideoSynthesisResponse return by async_call().
190
+ api_key (str, optional): The api api_key. Defaults to None.
191
+ workspace (str): The dashscope workspace id.
192
+
193
+ Returns:
194
+ DashScopeAPIResponse: The response data.
195
+ """
196
+ return super().cancel(task, api_key, workspace=workspace)
197
+
198
+ @classmethod
199
+ def list(cls,
200
+ start_time: str = None,
201
+ end_time: str = None,
202
+ model_name: str = None,
203
+ api_key_id: str = None,
204
+ region: str = None,
205
+ status: str = None,
206
+ page_no: int = 1,
207
+ page_size: int = 10,
208
+ api_key: str = None,
209
+ workspace: str = None,
210
+ **kwargs) -> DashScopeAPIResponse:
211
+ """List async tasks.
212
+
213
+ Args:
214
+ start_time (str, optional): The tasks start time,
215
+ for example: 20230420000000. Defaults to None.
216
+ end_time (str, optional): The tasks end time,
217
+ for example: 20230420000000. Defaults to None.
218
+ model_name (str, optional): The tasks model name. Defaults to None.
219
+ api_key_id (str, optional): The tasks api-key-id. Defaults to None.
220
+ region (str, optional): The service region,
221
+ for example: cn-beijing. Defaults to None.
222
+ status (str, optional): The status of tasks[PENDING,
223
+ RUNNING, SUCCEEDED, FAILED, CANCELED]. Defaults to None.
224
+ page_no (int, optional): The page number. Defaults to 1.
225
+ page_size (int, optional): The page size. Defaults to 10.
226
+ api_key (str, optional): The user api-key. Defaults to None.
227
+ workspace (str): The dashscope workspace id.
228
+
229
+ Returns:
230
+ DashScopeAPIResponse: The response data.
231
+ """
232
+ return super().list(start_time=start_time,
233
+ end_time=end_time,
234
+ model_name=model_name,
235
+ api_key_id=api_key_id,
236
+ region=region,
237
+ status=status,
238
+ page_no=page_no,
239
+ page_size=page_size,
240
+ api_key=api_key,
241
+ workspace=workspace,
242
+ **kwargs)
@@ -2,7 +2,6 @@ import json
2
2
  from http import HTTPStatus
3
3
 
4
4
  import aiohttp
5
-
6
5
  from dashscope.api_entities.base_request import AioBaseRequest
7
6
  from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
8
7
  from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
@@ -2,7 +2,6 @@ import json
2
2
  from urllib.parse import urlencode
3
3
 
4
4
  import aiohttp
5
-
6
5
  from dashscope.common.constants import ApiProtocol
7
6
  from dashscope.io.input_output import InputResolver
8
7
 
@@ -81,13 +81,15 @@ def _build_api_request(model: str,
81
81
  websocket_url = base_address
82
82
  else:
83
83
  websocket_url = dashscope.base_websocket_api_url
84
+ pre_task_id = kwargs.pop('pre_task_id', None)
84
85
  request = WebSocketRequest(url=websocket_url,
85
86
  api_key=api_key,
86
87
  stream=stream,
87
88
  ws_stream_mode=ws_stream_mode,
88
89
  is_binary_input=is_binary_input,
89
90
  timeout=request_timeout,
90
- flattened_output=flattened_output)
91
+ flattened_output=flattened_output,
92
+ pre_task_id=pre_task_id)
91
93
  else:
92
94
  raise UnsupportedApiProtocol(
93
95
  'Unsupported protocol: %s, support [http, https, websocket]' %
@@ -458,6 +458,24 @@ class ImageSynthesisOutput(DictMixin):
458
458
  **kwargs)
459
459
 
460
460
 
461
+ @dataclass(init=False)
462
+ class VideoSynthesisOutput(DictMixin):
463
+ task_id: str
464
+ task_status: str
465
+ video_url: str
466
+
467
+ def __init__(self,
468
+ task_id: str,
469
+ task_status: str,
470
+ video_url: str = '',
471
+ **kwargs):
472
+ super().__init__(self,
473
+ task_id=task_id,
474
+ task_status=task_status,
475
+ video_url=video_url,
476
+ **kwargs)
477
+
478
+
461
479
  @dataclass(init=False)
462
480
  class ImageSynthesisUsage(DictMixin):
463
481
  image_count: int
@@ -466,6 +484,14 @@ class ImageSynthesisUsage(DictMixin):
466
484
  super().__init__(image_count=image_count, **kwargs)
467
485
 
468
486
 
487
+ @dataclass(init=False)
488
+ class VideoSynthesisUsage(DictMixin):
489
+ video_count: int
490
+
491
+ def __init__(self, video_count: int = 1, **kwargs):
492
+ super().__init__(video_count=video_count, **kwargs)
493
+
494
+
469
495
  @dataclass(init=False)
470
496
  class ImageSynthesisResponse(DashScopeAPIResponse):
471
497
  output: ImageSynthesisOutput
@@ -495,6 +521,35 @@ class ImageSynthesisResponse(DashScopeAPIResponse):
495
521
  message=api_response.message)
496
522
 
497
523
 
524
+ @dataclass(init=False)
525
+ class VideoSynthesisResponse(DashScopeAPIResponse):
526
+ output: VideoSynthesisOutput
527
+ usage: VideoSynthesisUsage
528
+
529
+ @staticmethod
530
+ def from_api_response(api_response: DashScopeAPIResponse):
531
+ if api_response.status_code == HTTPStatus.OK:
532
+ output = None
533
+ usage = None
534
+ if api_response.output is not None:
535
+ output = VideoSynthesisOutput(**api_response.output)
536
+ if api_response.usage is not None:
537
+ usage = VideoSynthesisUsage(**api_response.usage)
538
+
539
+ return VideoSynthesisResponse(status_code=api_response.status_code,
540
+ request_id=api_response.request_id,
541
+ code=api_response.code,
542
+ message=api_response.message,
543
+ output=output,
544
+ usage=usage)
545
+
546
+ else:
547
+ return VideoSynthesisResponse(status_code=api_response.status_code,
548
+ request_id=api_response.request_id,
549
+ code=api_response.code,
550
+ message=api_response.message)
551
+
552
+
498
553
  @dataclass(init=False)
499
554
  class ReRankResult(DictMixin):
500
555
  index: int
@@ -3,7 +3,6 @@ from http import HTTPStatus
3
3
 
4
4
  import aiohttp
5
5
  import requests
6
-
7
6
  from dashscope.api_entities.base_request import AioBaseRequest
8
7
  from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
9
8
  from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
@@ -5,7 +5,6 @@ from http import HTTPStatus
5
5
  from typing import Tuple, Union
6
6
 
7
7
  import aiohttp
8
-
9
8
  from dashscope.api_entities.base_request import AioBaseRequest
10
9
  from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
11
10
  from dashscope.common.constants import (DEFAULT_REQUEST_TIMEOUT_SECONDS,
@@ -31,6 +30,7 @@ class WebSocketRequest(AioBaseRequest):
31
30
  is_binary_input: bool = False,
32
31
  timeout: int = DEFAULT_REQUEST_TIMEOUT_SECONDS,
33
32
  flattened_output: bool = False,
33
+ pre_task_id=None,
34
34
  ) -> None:
35
35
  super().__init__()
36
36
  """HttpRequest.
@@ -61,6 +61,7 @@ class WebSocketRequest(AioBaseRequest):
61
61
  self.task_headers = {
62
62
  'streaming': self.ws_stream_mode,
63
63
  }
64
+ self.pre_task_id = pre_task_id
64
65
 
65
66
  def add_headers(self, headers):
66
67
  self.headers = {**self.headers, **headers}
@@ -77,6 +78,10 @@ class WebSocketRequest(AioBaseRequest):
77
78
  pass
78
79
  return output
79
80
 
81
+ async def close(self):
82
+ if self.ws is not None and not self.ws.closed:
83
+ await self.ws.close()
84
+
80
85
  async def aio_call(self):
81
86
  response = self.connection_handler()
82
87
  if self.stream:
@@ -140,7 +145,11 @@ class WebSocketRequest(AioBaseRequest):
140
145
  code=e.name,
141
146
  message=e.message)
142
147
  except aiohttp.ClientConnectorError as e:
143
- raise e
148
+ logger.exception(e)
149
+ yield DashScopeAPIResponse(request_id='',
150
+ status_code=-1,
151
+ code='ClientConnectorError',
152
+ message=str(e))
144
153
  except aiohttp.WSServerHandshakeError as e:
145
154
  code = e.status
146
155
  msg = e.message
@@ -227,17 +236,22 @@ class WebSocketRequest(AioBaseRequest):
227
236
  raise error
228
237
 
229
238
  async def _start_task(self, ws):
230
- self.task_headers['task_id'] = uuid.uuid4().hex # create task id.
239
+ if self.pre_task_id is not None:
240
+ self.task_headers['task_id'] = self.pre_task_id
241
+ else:
242
+ self.task_headers['task_id'] = uuid.uuid4().hex # create task id.
231
243
  task_header = {**self.task_headers, ACTION_KEY: ActionType.START}
232
244
  # for binary data, the start action has no input, only parameters.
233
245
  start_data = self.data.get_websocket_start_data()
234
246
  message = self._build_up_message(task_header, start_data)
247
+ logger.debug('Send start task: {}'.format(message))
235
248
  await ws.send_str(message)
236
249
 
237
250
  async def _send_finished_task(self, ws):
238
251
  task_header = {**self.task_headers, ACTION_KEY: ActionType.FINISHED}
239
252
  payload = {'input': {}}
240
253
  message = self._build_up_message(task_header, payload)
254
+ logger.debug('Send finish task: {}'.format(message))
241
255
  await ws.send_str(message)
242
256
 
243
257
  async def _send_continue_task_data(self, ws):
@@ -250,12 +264,19 @@ class WebSocketRequest(AioBaseRequest):
250
264
  if len(input) > 0:
251
265
  if isinstance(input, bytes):
252
266
  await ws.send_bytes(input)
267
+ logger.debug(
268
+ 'Send continue task with bytes: {}'.format(
269
+ len(input)))
253
270
  else:
254
271
  await ws.send_bytes(list(input.values())[0])
272
+ logger.debug(
273
+ 'Send continue task with list[byte]: {}'.format(
274
+ len(input)))
255
275
  else:
256
276
  if len(input) > 0:
257
277
  message = self._build_up_message(headers=headers,
258
278
  payload=input)
279
+ logger.debug('Send continue task: {}'.format(message))
259
280
  await ws.send_str(message)
260
281
  await asyncio.sleep(0.000001)
261
282
 
@@ -1,9 +1,18 @@
1
1
  from .asr_phrase_manager import AsrPhraseManager
2
2
  from .recognition import Recognition, RecognitionCallback, RecognitionResult
3
3
  from .transcription import Transcription
4
+ from .translation_recognizer import (TranscriptionResult, Translation,
5
+ TranslationRecognizerCallback,
6
+ TranslationRecognizerChat,
7
+ TranslationRecognizerRealtime,
8
+ TranslationRecognizerResultPack,
9
+ TranslationResult)
4
10
  from .vocabulary import VocabularyService, VocabularyServiceException
5
11
 
6
12
  __all__ = [
7
13
  'Transcription', 'Recognition', 'RecognitionCallback', 'RecognitionResult',
8
- 'AsrPhraseManager', 'VocabularyServiceException', 'VocabularyService'
14
+ 'AsrPhraseManager', 'VocabularyServiceException', 'VocabularyService',
15
+ 'TranslationRecognizerRealtime', 'TranslationRecognizerChat',
16
+ 'TranslationRecognizerCallback', 'Translation', 'TranslationResult',
17
+ 'TranscriptionResult', 'TranslationRecognizerResultPack'
9
18
  ]