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

@@ -0,0 +1,160 @@
1
+ from dataclasses import dataclass
2
+ import mimetypes
3
+ import os
4
+ from datetime import datetime
5
+ from http import HTTPStatus
6
+ from time import mktime
7
+ from typing import List
8
+ from urllib.parse import unquote_plus, urlparse
9
+ from wsgiref.handlers import format_date_time
10
+ import time
11
+ import threading
12
+
13
+ import requests
14
+
15
+ from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
16
+ from dashscope.client.base_api import GetMixin
17
+ from dashscope.common.constants import (FILE_PATH_SCHEMA,
18
+ REQUEST_CONTENT_AUDIO,
19
+ REQUEST_CONTENT_IMAGE,
20
+ REQUEST_CONTENT_TEXT)
21
+ from dashscope.common.error import InvalidInput, UploadFileException
22
+ from dashscope.common.logging import logger
23
+ from dashscope.common.utils import get_user_agent
24
+
25
+ @dataclass
26
+ class UploadToken:
27
+ token: str = None
28
+ expire_time: int = None
29
+
30
+ class UploadTokenMeta(type):
31
+ def __init__(cls):
32
+ cls._tokens = {} # key model, value: token and expire_time
33
+ cls._lock = threading.Lock() # lock set
34
+
35
+ @property
36
+ def token(cls, model):
37
+ current_time = time.perf_counter()
38
+ upload_token: UploadToken = cls._tokens[model] if model in cls._tokens else None
39
+ if upload_token is not None and current_time < upload_token.expire_time:
40
+ return upload_token.token
41
+ else:
42
+ return None
43
+
44
+ @token.setter
45
+ def token(cls, model: str, upload_token: UploadToken):
46
+ cls._lock.acquire()
47
+ cls._tokens[model] = upload_token
48
+ cls._lock.release()
49
+
50
+
51
+ class OssUtils(GetMixin, metaclass=UploadTokenMeta):
52
+ SUB_PATH = 'uploads'
53
+
54
+ @classmethod
55
+ def _decode_response_error(cls, response: requests.Response):
56
+ if 'application/json' in response.headers.get('content-type', ''):
57
+ message = response.json()
58
+ else:
59
+ message = response.content.decode('utf-8')
60
+ return message
61
+
62
+ @classmethod
63
+ def upload(cls,
64
+ model: str,
65
+ file_path: str,
66
+ api_key: str = None,
67
+ **kwargs) -> DashScopeAPIResponse:
68
+ """Upload file for model fine-tune or other tasks.
69
+
70
+ Args:
71
+ file_path (str): The local file name to upload.
72
+ purpose (str): The purpose of the file[fine-tune|inference]
73
+ description (str, optional): The file description message.
74
+ api_key (str, optional): The api key. Defaults to None.
75
+
76
+ Returns:
77
+ DashScopeAPIResponse: The upload information
78
+ """
79
+ upload_token = cls.token(model)
80
+ if upload_token is None:
81
+ upload_info = cls.get_upload_certificate(model=model, api_key=api_key)
82
+ if upload_info.status_code != HTTPStatus.OK:
83
+ raise UploadFileException(
84
+ 'Get upload certificate failed, code: %s, message: %s' %
85
+ (upload_info.code, upload_info.message))
86
+ upload_info = upload_info.output
87
+ cls.token = xx
88
+
89
+ upload_info = upload_info.output
90
+ headers = {}
91
+ headers = {'user-agent': get_user_agent()}
92
+ headers['Accept'] = 'application/json'
93
+ headers['Date'] = format_date_time(mktime(datetime.now().timetuple()))
94
+ form_data = {}
95
+ form_data['OSSAccessKeyId'] = upload_info['oss_access_key_id']
96
+ form_data['Signature'] = upload_info['signature']
97
+ form_data['policy'] = upload_info['policy']
98
+ form_data['key'] = upload_info['upload_dir'] + \
99
+ '/' + os.path.basename(file_path)
100
+ form_data['x-oss-object-acl'] = upload_info['x_oss_object_acl']
101
+ form_data['x-oss-forbid-overwrite'] = upload_info[
102
+ 'x_oss_forbid_overwrite']
103
+ form_data['success_action_status'] = '200'
104
+ form_data['x-oss-content-type'] = mimetypes.guess_type(file_path)[0]
105
+ url = upload_info['upload_host']
106
+ files = {'file': open(file_path, 'rb')}
107
+ with requests.Session() as session:
108
+ response = session.post(url,
109
+ files=files,
110
+ data=form_data,
111
+ headers=headers,
112
+ timeout=3600)
113
+ if response.status_code == HTTPStatus.OK:
114
+ return 'oss://' + form_data['key']
115
+ else:
116
+ msg = (
117
+ 'Uploading file: %s to oss failed, error: %s' %
118
+ (file_path, cls._decode_response_error(response=response)))
119
+ logger.error(msg)
120
+ raise UploadFileException(msg)
121
+
122
+ @classmethod
123
+ def get_upload_certificate(cls,
124
+ model: str,
125
+ api_key: str = None,
126
+ **kwargs) -> DashScopeAPIResponse:
127
+ """Get a oss upload certificate.
128
+
129
+ Args:
130
+ api_key (str, optional): The api key. Defaults to None.
131
+
132
+ Returns:
133
+ DashScopeAPIResponse: The job info
134
+ """
135
+ params = {'action': 'getPolicy'}
136
+ params['model'] = model
137
+ return super().get(None, api_key, params=params, **kwargs)
138
+
139
+
140
+ def upload_file(model: str, upload_path: str, api_key: str):
141
+ if upload_path.startswith(FILE_PATH_SCHEMA):
142
+ parse_result = urlparse(upload_path)
143
+ if parse_result.netloc:
144
+ file_path = parse_result.netloc + unquote_plus(parse_result.path)
145
+ else:
146
+ file_path = unquote_plus(parse_result.path)
147
+ if os.path.exists(file_path):
148
+ file_url = OssUtils.upload(model=model,
149
+ file_path=file_path,
150
+ api_key=api_key)
151
+ if file_url is None:
152
+ raise UploadFileException('Uploading file: %s failed' %
153
+ upload_path)
154
+ return file_url
155
+ else:
156
+ raise InvalidInput('The file: %s is not exists!' % file_path)
157
+ return None
158
+
159
+
160
+
dashscope/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = '1.22.0'
1
+ __version__ = '1.22.1'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dashscope
3
- Version: 1.22.0
3
+ Version: 1.22.1
4
4
  Summary: dashscope client sdk library
5
5
  Home-page: https://dashscope.aliyun.com/
6
6
  Author: Alibaba Cloud
@@ -18,6 +18,7 @@ Classifier: Programming Language :: Python :: 3.10
18
18
  Classifier: Programming Language :: Python :: 3.11
19
19
  Requires-Python: >=3.8.0
20
20
  Description-Content-Type: text/markdown
21
+ License-File: LICENSE
21
22
  Requires-Dist: aiohttp
22
23
  Requires-Dist: requests
23
24
  Requires-Dist: websocket-client
@@ -221,5 +222,3 @@ Coming soon.
221
222
 
222
223
  ## License
223
224
  This project is licensed under the Apache License (Version 2.0).
224
-
225
-
@@ -1,13 +1,11 @@
1
1
  dashscope/__init__.py,sha256=_9PaKXKpYc6PXO35BjcH3R0As9sTsXGOW2iQ6R5HAVc,3011
2
2
  dashscope/cli.py,sha256=amegoTkGOs6TlHMdoo4JVOqBePo3lGs745rc7leEyrE,24020
3
- dashscope/deployment.py,sha256=ljmVi-ny6SjEs8v4oIGNWIw8UQTorE7dl5QJv7dEPIQ,5728
4
- dashscope/file.py,sha256=Dv2Fz3DLbcye2uuQxyQwRM7ky27OthouLXIpSQagQy4,3324
5
3
  dashscope/files.py,sha256=QgJjwhtn9F548nCA8jD8OvE6aQEj-20hZqJgYXsUdQU,3930
6
- dashscope/finetune.py,sha256=_tflDUvu0KagSoCzLaf0hofpG_P8NU6PylL8CPjVhrA,6243
7
4
  dashscope/model.py,sha256=UPOn1qMYFhX-ovXi3BMxZEBk8qOK7WLJOYHMbPZwYBo,1440
8
5
  dashscope/models.py,sha256=1-bc-Ue68zurgu_y6RhfFr9uzeQMF5AZq-C32lJGMGU,1224
9
- dashscope/version.py,sha256=SDK5izPcJa4KNexMqsTGqKMhzuz-w_ifY4M7gq-3Eno,23
6
+ dashscope/version.py,sha256=sUm-r83NIqFP93EJcBrI6UFP5e6fmMbnDa6TglGqS1o,23
10
7
  dashscope/aigc/__init__.py,sha256=xmdalVw7wS0cLIuU8Q0qk0q8XGw-iGk8NnQwAQZ3jAc,391
8
+ dashscope/aigc/chat_completion.py,sha256=28puJrtHkqTMZlyKLXb3UjSuICy2OPvysj9a6oNU7Vs,14585
11
9
  dashscope/aigc/code_generation.py,sha256=KAJVrGp6tiNFBBg64Ovs9RfcP5SrIhrbW3wdA89NKso,10885
12
10
  dashscope/aigc/conversation.py,sha256=xRoJlCR-IXHjSdkDrK74a9ut1FJg0FZhTNXZAJC18MA,14231
13
11
  dashscope/aigc/generation.py,sha256=53oMCmN5ZbqeqAsKxmdunXlRh-XP8ZtnA7hB2id4Koo,17897
@@ -19,9 +17,10 @@ dashscope/api_entities/aiohttp_request.py,sha256=ZFbdpJh7SwHnBzbYLhqr_FdcDVRgLVM
19
17
  dashscope/api_entities/api_request_data.py,sha256=QHGgIShcQ1jANO1szfsqUGtYix1nD2RN4LyC_fNciCM,5462
20
18
  dashscope/api_entities/api_request_factory.py,sha256=iLjOqZkbuQkn9JVduI39XPrhm1UefekAekaNqxFpNLM,5061
21
19
  dashscope/api_entities/base_request.py,sha256=cXUL7xqSV8wBr5d-1kx65AO3IsRR9A_ps6Lok-v-MKM,926
22
- dashscope/api_entities/dashscope_response.py,sha256=4a8Iya8RBUnVdzGmzoj6NvaaEeEnodGAD7evBrqEXfU,19832
20
+ dashscope/api_entities/chat_completion_types.py,sha256=U1nYR5krZYbCedPwlRufJok3hz95mm4kYn2mp20hNAc,12807
21
+ dashscope/api_entities/dashscope_response.py,sha256=FC9zGLNb2iu5fl4bJNcjLnVren_4kNGRW0i9xs4b4Q8,19839
23
22
  dashscope/api_entities/http_request.py,sha256=Pr6mr01uXELK9LwIPXrJAhNtGMkWH3gQYORXShRiQRo,13258
24
- dashscope/api_entities/websocket_request.py,sha256=h3RcvgvXpyzF8xvC4hqNUkev6HGN_YgO5emvqYaXw78,16094
23
+ dashscope/api_entities/websocket_request.py,sha256=XzLj9vsJfBAhYMyRRcbEBoJu_dAVm0HpM60k46lBqBw,16281
25
24
  dashscope/app/__init__.py,sha256=UiN_9i--z84Dw5wUehOh_Tkk_9Gq_td_Kbz1dobBEKg,62
26
25
  dashscope/app/application.py,sha256=Cnd62LFpG70XJUo4Oibry9KzXPhPNmNkKFD4R5YuGTA,9343
27
26
  dashscope/app/application_response.py,sha256=0pulI3O3z4R4h_YaDwzVimamo3XwTXGy5TiHCzysTBg,7011
@@ -33,7 +32,6 @@ dashscope/audio/__init__.py,sha256=-ZRxrK-gV4QsUtlThIT-XwqB6vmyEsnhxIxdLmhCUuc,6
33
32
  dashscope/audio/asr/__init__.py,sha256=VaWX5DRWcB81_5z2o7IPwz6Jrs9vFFJ5GEVarzVOvPY,1004
34
33
  dashscope/audio/asr/asr_phrase_manager.py,sha256=EjtbI3zz9UQGS1qv6Yb4zzEMj4OJJVXmwkqZyIrzvEA,7642
35
34
  dashscope/audio/asr/recognition.py,sha256=7ApnKJKVp1JS6e6KBZzM8y1yiMB2fEun07S6BOvqfrs,20699
36
- dashscope/audio/asr/transcribe.py,sha256=HfZYpvpVfvGRAIIIzX65Af33E6vsIFGd_qqhQ8LaNcM,9651
37
35
  dashscope/audio/asr/transcription.py,sha256=D8CW0XDqJuEJVmNFJ6qczTysSV3Sz_rzk2C6NIKTtVc,9042
38
36
  dashscope/audio/asr/translation_recognizer.py,sha256=j1oKgggSA2HDdEJAOJ4jY78_bK0FGwpfIf_AxbrAzWM,39629
39
37
  dashscope/audio/asr/vocabulary.py,sha256=2MHxeaL0ANWk-TILrHhArKSdj0d5M_YHw0cnjB-E4dY,6476
@@ -87,9 +85,10 @@ dashscope/tokenizers/tokenizer.py,sha256=y6P91qTCYo__pEx_0VHAcj9YECfbUdRqZU1fdGT
87
85
  dashscope/tokenizers/tokenizer_base.py,sha256=REDhzRyDT13iequ61-a6_KcTy0GFKlihQve5HkyoyRs,656
88
86
  dashscope/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
89
87
  dashscope/utils/oss_utils.py,sha256=TlqaMAmVRtBJIm5aIaXsrRZGKc_7cwWQ7liMB2f9Css,7331
90
- dashscope-1.22.0.dist-info/LICENSE,sha256=Izp5L1DF1Mbza6qojkqNNWlE_mYLnr4rmzx2EBF8YFw,11413
91
- dashscope-1.22.0.dist-info/METADATA,sha256=ZmeEprSXjdVtgfudY4kN8L0XSN8EughpPz7y3OyCwkM,6641
92
- dashscope-1.22.0.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
93
- dashscope-1.22.0.dist-info/entry_points.txt,sha256=raEp5dOuj8whJ7yqZlDM8WQ5p2RfnGrGNo0QLQEnatY,50
94
- dashscope-1.22.0.dist-info/top_level.txt,sha256=woqavFJK9zas5xTqynmALqOtlafghjsk63Xk86powTU,10
95
- dashscope-1.22.0.dist-info/RECORD,,
88
+ dashscope/utils/temporary_storage.py,sha256=CWBpEmuIthYg5DhU_qu63DbF2dsmqnLdKFkbWF8yX6A,6072
89
+ dashscope-1.22.1.dist-info/LICENSE,sha256=Izp5L1DF1Mbza6qojkqNNWlE_mYLnr4rmzx2EBF8YFw,11413
90
+ dashscope-1.22.1.dist-info/METADATA,sha256=xk089kHugSTlkhuy17skH7fOIT60VivJMcsYUUKAI-4,6661
91
+ dashscope-1.22.1.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
92
+ dashscope-1.22.1.dist-info/entry_points.txt,sha256=e9C3sOf9zDYL0O5ROEGX6FT8w-QK_kaGRWmPZDHAFys,49
93
+ dashscope-1.22.1.dist-info/top_level.txt,sha256=woqavFJK9zas5xTqynmALqOtlafghjsk63Xk86powTU,10
94
+ dashscope-1.22.1.dist-info/RECORD,,
@@ -1,3 +1,2 @@
1
1
  [console_scripts]
2
2
  dashscope = dashscope.cli:main
3
-
@@ -1,270 +0,0 @@
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 DELETED
@@ -1,163 +0,0 @@
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)