dashscope 1.20.7__py3-none-any.whl → 1.20.9__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/aigc/image_synthesis.py +17 -1
- dashscope/audio/asr/transcribe.py +270 -0
- dashscope/deployment.py +163 -0
- dashscope/file.py +94 -0
- dashscope/finetune.py +175 -0
- dashscope/resources/qwen.tiktoken +151643 -0
- dashscope/threads/runs/runs.py +0 -6
- dashscope/utils/oss_utils.py +50 -31
- dashscope/version.py +1 -1
- {dashscope-1.20.7.dist-info → dashscope-1.20.9.dist-info}/METADATA +3 -2
- {dashscope-1.20.7.dist-info → dashscope-1.20.9.dist-info}/RECORD +15 -10
- {dashscope-1.20.7.dist-info → dashscope-1.20.9.dist-info}/entry_points.txt +1 -0
- {dashscope-1.20.7.dist-info → dashscope-1.20.9.dist-info}/LICENSE +0 -0
- {dashscope-1.20.7.dist-info → dashscope-1.20.9.dist-info}/WHEEL +0 -0
- {dashscope-1.20.7.dist-info → dashscope-1.20.9.dist-info}/top_level.txt +0 -0
|
@@ -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,
|
|
@@ -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
|
dashscope/deployment.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
2
|
+
from dashscope.client.base_api import (CreateMixin, DeleteMixin, GetMixin,
|
|
3
|
+
ListMixin, PutMixin, StreamEventMixin)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Deployment(CreateMixin, DeleteMixin, ListMixin, GetMixin,
|
|
7
|
+
StreamEventMixin, PutMixin):
|
|
8
|
+
SUB_PATH = 'deployments'
|
|
9
|
+
"""Deploy a model.
|
|
10
|
+
"""
|
|
11
|
+
@classmethod
|
|
12
|
+
def call(cls,
|
|
13
|
+
model: str,
|
|
14
|
+
capacity: int,
|
|
15
|
+
version: str = None,
|
|
16
|
+
suffix: str = None,
|
|
17
|
+
api_key: str = None,
|
|
18
|
+
workspace: str = None,
|
|
19
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
20
|
+
"""Call to deployment a model service.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
model (str): The model name.
|
|
24
|
+
version (str, optional): The model version, unnecessary
|
|
25
|
+
for fine-tuned model. Defaults to None.
|
|
26
|
+
suffix (str, optional): The name suffix of the model deployment,
|
|
27
|
+
If specified, the final model name will be model_suffix.
|
|
28
|
+
Defaults to None.
|
|
29
|
+
capacity (int, optional): The model service capacity.
|
|
30
|
+
api_key (str, optional): The api-key. Defaults to None.
|
|
31
|
+
workspace (str): The dashscope workspace id.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
DashScopeAPIResponse: _description_
|
|
35
|
+
"""
|
|
36
|
+
req = {'model_name': model, 'capacity': capacity}
|
|
37
|
+
|
|
38
|
+
if version is not None:
|
|
39
|
+
req['model_version'] = version
|
|
40
|
+
if suffix is not None:
|
|
41
|
+
req['suffix'] = suffix
|
|
42
|
+
return super().call(req,
|
|
43
|
+
api_key=api_key,
|
|
44
|
+
workspace=workspace,
|
|
45
|
+
**kwargs)
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def list(cls,
|
|
49
|
+
page_no=1,
|
|
50
|
+
page_size=10,
|
|
51
|
+
api_key: str = None,
|
|
52
|
+
workspace: str = None,
|
|
53
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
54
|
+
"""List deployments.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
api_key (str, optional): The api api_key, if not present,
|
|
58
|
+
will get by default rule(TODO: api key doc). Defaults to None.
|
|
59
|
+
page_no (int, optional): Page number. Defaults to 1.
|
|
60
|
+
page_size (int, optional): Items per page. Defaults to 10.
|
|
61
|
+
workspace (str): The dashscope workspace id.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
DashScopeAPIResponse: The deployment list.
|
|
65
|
+
"""
|
|
66
|
+
return super().list(page_no,
|
|
67
|
+
page_size,
|
|
68
|
+
api_key,
|
|
69
|
+
workspace=workspace,
|
|
70
|
+
**kwargs)
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def get(cls,
|
|
74
|
+
deployed_model: str,
|
|
75
|
+
api_key: str = None,
|
|
76
|
+
workspace: str = None,
|
|
77
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
78
|
+
"""Get model deployment information.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
deployed_model (str): The deployment_id.
|
|
82
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
83
|
+
workspace (str): The dashscope workspace id.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
DashScopeAPIResponse: The deployment information.
|
|
87
|
+
"""
|
|
88
|
+
return super().get(deployed_model,
|
|
89
|
+
api_key,
|
|
90
|
+
workspace=workspace,
|
|
91
|
+
**kwargs)
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def delete(cls,
|
|
95
|
+
deployment_id: str,
|
|
96
|
+
api_key: str = None,
|
|
97
|
+
workspace: str = None,
|
|
98
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
99
|
+
"""Delete model deployment.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
deployment_id (str): The deployment id.
|
|
103
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
104
|
+
workspace (str): The dashscope workspace id.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
DashScopeAPIResponse: The delete result.
|
|
108
|
+
"""
|
|
109
|
+
return super().delete(deployment_id,
|
|
110
|
+
api_key,
|
|
111
|
+
workspace=workspace,
|
|
112
|
+
**kwargs)
|
|
113
|
+
|
|
114
|
+
@classmethod
|
|
115
|
+
def update(cls,
|
|
116
|
+
deployment_id: str,
|
|
117
|
+
version: str,
|
|
118
|
+
api_key: str = None,
|
|
119
|
+
workspace: str = None,
|
|
120
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
121
|
+
"""Update model deployment.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
deployment_id (str): The deployment id.
|
|
125
|
+
version (str): The target model version.
|
|
126
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
127
|
+
workspace (str): The dashscope workspace id.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
DashScopeAPIResponse: The delete result.
|
|
131
|
+
"""
|
|
132
|
+
req = {'deployment_model': deployment_id, 'model_version': version}
|
|
133
|
+
return super().put(deployment_id,
|
|
134
|
+
req,
|
|
135
|
+
api_key,
|
|
136
|
+
workspace=workspace,
|
|
137
|
+
**kwargs)
|
|
138
|
+
|
|
139
|
+
@classmethod
|
|
140
|
+
def scale(cls,
|
|
141
|
+
deployment_id: str,
|
|
142
|
+
capacity: int,
|
|
143
|
+
api_key: str = None,
|
|
144
|
+
workspace: str = None,
|
|
145
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
146
|
+
"""Scaling model deployment.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
deployment_id (str): The deployment id.
|
|
150
|
+
capacity (int): The target service capacity.
|
|
151
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
DashScopeAPIResponse: The delete result.
|
|
155
|
+
"""
|
|
156
|
+
req = {'deployed_model': deployment_id, 'capacity': capacity}
|
|
157
|
+
path = '%s/%s/scale' % (cls.SUB_PATH.lower(), deployment_id)
|
|
158
|
+
return super().put(deployment_id,
|
|
159
|
+
req,
|
|
160
|
+
path=path,
|
|
161
|
+
api_key=api_key,
|
|
162
|
+
workspace=workspace,
|
|
163
|
+
**kwargs)
|
dashscope/file.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
4
|
+
from dashscope.client.base_api import (DeleteMixin, FileUploadMixin, GetMixin,
|
|
5
|
+
ListMixin)
|
|
6
|
+
from dashscope.common.constants import FilePurpose
|
|
7
|
+
from dashscope.common.error import InvalidFileFormat
|
|
8
|
+
from dashscope.common.utils import is_validate_fine_tune_file
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class File(FileUploadMixin, ListMixin, DeleteMixin, GetMixin):
|
|
12
|
+
SUB_PATH = 'files'
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def upload(cls,
|
|
16
|
+
file_path: str,
|
|
17
|
+
purpose: str = FilePurpose.fine_tune,
|
|
18
|
+
description: str = None,
|
|
19
|
+
api_key: str = None,
|
|
20
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
21
|
+
"""Upload file for model fine-tune or other tasks.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
file_path (str): The local file name to upload.
|
|
25
|
+
purpose (str): The purpose of the file[fine-tune|inference]
|
|
26
|
+
description (str, optional): The file description message.
|
|
27
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
DashScopeAPIResponse: The upload information
|
|
31
|
+
"""
|
|
32
|
+
if purpose == FilePurpose.fine_tune:
|
|
33
|
+
if not is_validate_fine_tune_file(file_path):
|
|
34
|
+
raise InvalidFileFormat(
|
|
35
|
+
'The file %s is not in valid jsonl format' % file_path)
|
|
36
|
+
with open(file_path, 'rb') as f:
|
|
37
|
+
return super().upload(files=[('files', (os.path.basename(f.name),
|
|
38
|
+
f, None))],
|
|
39
|
+
descriptions=[description]
|
|
40
|
+
if description is not None else None,
|
|
41
|
+
api_key=api_key,
|
|
42
|
+
**kwargs)
|
|
43
|
+
|
|
44
|
+
@classmethod
|
|
45
|
+
def list(cls,
|
|
46
|
+
page=1,
|
|
47
|
+
page_size=10,
|
|
48
|
+
api_key: str = None,
|
|
49
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
50
|
+
"""List uploaded files.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
api_key (str, optional):
|
|
54
|
+
The api api_key, can be None,
|
|
55
|
+
if None, will get by default rule(TODO: api key doc).
|
|
56
|
+
page (int, optional): Page number. Defaults to 1.
|
|
57
|
+
page_size (int, optional): Items per page. Defaults to 10.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
DashScopeAPIResponse: The fine-tune jobs in the result.
|
|
61
|
+
"""
|
|
62
|
+
return super().list(page, page_size, api_key, **kwargs)
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def get(cls,
|
|
66
|
+
file_id: str,
|
|
67
|
+
api_key: str = None,
|
|
68
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
69
|
+
"""Get the file info.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
file_id (str): The file id.
|
|
73
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
DashScopeAPIResponse: The job info
|
|
77
|
+
"""
|
|
78
|
+
return super().get(file_id, api_key, **kwargs)
|
|
79
|
+
|
|
80
|
+
@classmethod
|
|
81
|
+
def delete(cls,
|
|
82
|
+
file_id: str,
|
|
83
|
+
api_key: str = None,
|
|
84
|
+
**kwargs) -> DashScopeAPIResponse:
|
|
85
|
+
"""Delete uploaded file.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
file_id (str): The file id want to delete.
|
|
89
|
+
api_key (str, optional): The api key. Defaults to None.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
DashScopeAPIResponse: Delete result.
|
|
93
|
+
"""
|
|
94
|
+
return super().delete(file_id, api_key, **kwargs)
|