@@ -35,24 +35,33 @@ def upload(cls,
3535 model : str ,
3636 file_path : str ,
3737 api_key : str = None ,
38- ** kwargs ) -> DashScopeAPIResponse :
38+ upload_certificate : dict = None ,
39+ ** kwargs ):
3940 """Upload file for model fine-tune or other tasks.
4041
4142 Args:
4243 file_path (str): The local file name to upload.
4344 purpose (str): The purpose of the file[fine-tune|inference]
4445 description (str, optional): The file description message.
4546 api_key (str, optional): The api key. Defaults to None.
47+ upload_certificate (dict, optional): Reusable upload
48+ certificate. Defaults to None.
4649
4750 Returns:
48- DashScopeAPIResponse: The upload information
51+ tuple: (file_url, upload_certificate) where file_url is the
52+ OSS URL and upload_certificate is the certificate used
4953 """
50- upload_info = cls .get_upload_certificate (model = model , api_key = api_key , ** kwargs )
51- if upload_info .status_code != HTTPStatus .OK :
52- raise UploadFileException (
53- 'Get upload certificate failed, code: %s, message: %s' %
54- (upload_info .code , upload_info .message ))
55- upload_info = upload_info .output
54+ if upload_certificate is None :
55+ upload_info = cls .get_upload_certificate (model = model ,
56+ api_key = api_key ,
57+ ** kwargs )
58+ if upload_info .status_code != HTTPStatus .OK :
59+ raise UploadFileException (
60+ 'Get upload certificate failed, code: %s, message: %s' %
61+ (upload_info .code , upload_info .message ))
62+ upload_info = upload_info .output
63+ else :
64+ upload_info = upload_certificate
5665 headers = {}
5766 headers = {'user-agent' : get_user_agent ()}
5867 headers ['Accept' ] = 'application/json'
@@ -77,7 +86,7 @@ def upload(cls,
7786 headers = headers ,
7887 timeout = 3600 )
7988 if response .status_code == HTTPStatus .OK :
80- return 'oss://' + form_data ['key' ]
89+ return 'oss://' + form_data ['key' ], upload_info
8190 else :
8291 msg = (
8392 'Uploading file: %s to oss failed, error: %s' %
@@ -103,17 +112,19 @@ def get_upload_certificate(cls,
103112 return super ().get (None , api_key , params = params , ** kwargs )
104113
105114
106- def upload_file (model : str , upload_path : str , api_key : str ):
115+ def upload_file (model : str , upload_path : str , api_key : str ,
116+ upload_certificate : dict = None ):
107117 if upload_path .startswith (FILE_PATH_SCHEMA ):
108118 parse_result = urlparse (upload_path )
109119 if parse_result .netloc :
110120 file_path = parse_result .netloc + unquote_plus (parse_result .path )
111121 else :
112122 file_path = unquote_plus (parse_result .path )
113123 if os .path .exists (file_path ):
114- file_url = OssUtils .upload (model = model ,
115- file_path = file_path ,
116- api_key = api_key )
124+ file_url , _ = OssUtils .upload (model = model ,
125+ file_path = file_path ,
126+ api_key = api_key ,
127+ upload_certificate = upload_certificate )
117128 if file_url is None :
118129 raise UploadFileException ('Uploading file: %s failed' %
119130 upload_path )
@@ -123,20 +134,26 @@ def upload_file(model: str, upload_path: str, api_key: str):
123134 return None
124135
125136
126- def check_and_upload_local (model : str , content : str , api_key : str ):
137+ def check_and_upload_local (model : str , content : str , api_key : str ,
138+ upload_certificate : dict = None ):
127139 """Check the content is local file path, upload and return the url
128140
129141 Args:
130142 model (str): Which model to upload.
131143 content (str): The content.
132144 api_key (_type_): The api key.
145+ upload_certificate (dict, optional): Reusable upload certificate.
146+ Defaults to None.
133147
134148 Raises:
135149 UploadFileException: Upload failed.
136150 InvalidInput: The input is invalid
137151
138152 Returns:
139- _type_: if upload return True and file_url otherwise False, origin content.
153+ tuple: (is_upload, file_url_or_content, upload_certificate)
154+ where is_upload indicates if file was uploaded, file_url_or_content
155+ is the result URL or original content, and upload_certificate
156+ is the certificate (newly obtained or passed in)
140157 """
141158 if content .startswith (FILE_PATH_SCHEMA ):
142159 parse_result = urlparse (content )
@@ -145,49 +162,82 @@ def check_and_upload_local(model: str, content: str, api_key: str):
145162 else :
146163 file_path = unquote_plus (parse_result .path )
147164 if os .path .isfile (file_path ):
148- file_url = OssUtils .upload (model = model ,
149- file_path = file_path ,
150- api_key = api_key )
165+ file_url , cert = OssUtils .upload (model = model ,
166+ file_path = file_path ,
167+ api_key = api_key ,
168+ upload_certificate = upload_certificate )
151169 if file_url is None :
152170 raise UploadFileException ('Uploading file: %s failed' %
153171 content )
154- return True , file_url
172+ return True , file_url , cert
155173 else :
156174 raise InvalidInput ('The file: %s is not exists!' % file_path )
157175 elif content .startswith ('oss://' ):
158- return True , content
176+ return True , content , upload_certificate
159177 elif not content .startswith ('http' ):
160178 content = os .path .expanduser (content )
161179 if os .path .isfile (content ):
162- file_url = OssUtils .upload (model = model ,
163- file_path = content ,
164- api_key = api_key )
180+ file_url , cert = OssUtils .upload (model = model ,
181+ file_path = content ,
182+ api_key = api_key ,
183+ upload_certificate = upload_certificate )
165184 if file_url is None :
166185 raise UploadFileException ('Uploading file: %s failed' %
167186 content )
168- return True , file_url
169- return False , content
187+ return True , file_url , cert
188+ return False , content , upload_certificate
189+
190+
191+ def check_and_upload (model , elem : dict , api_key ,
192+ upload_certificate : dict = None ):
193+ """Check and upload files in element.
170194
195+ Args:
196+ model: Model name
197+ elem: Element dict containing file references
198+ api_key: API key
199+ upload_certificate: Optional upload certificate to reuse
171200
172- def check_and_upload (model , elem : dict , api_key ):
201+ Returns:
202+ tuple: (has_upload, upload_certificate) where has_upload is bool
203+ indicating if any file was uploaded, and upload_certificate
204+ is the certificate (newly obtained or passed in)
205+ """
173206 has_upload = False
207+ obtained_certificate = upload_certificate
208+
174209 for key , content in elem .items ():
175210 # support video:[images] for qwen2-vl
176211 is_list = isinstance (content , list )
177212 contents = content if is_list else [content ]
178213
179214 if key in ['image' , 'video' , 'audio' , 'text' ]:
180215 for i , content in enumerate (contents ):
181- is_upload , file_url = check_and_upload_local (
182- model , content , api_key )
216+ is_upload , file_url , obtained_certificate = check_and_upload_local (
217+ model , content , api_key , obtained_certificate )
183218 if is_upload :
184219 contents [i ] = file_url
185220 has_upload = True
186221 elem [key ] = contents if is_list else contents [0 ]
187222
188- return has_upload
223+ return has_upload , obtained_certificate
224+
225+
226+ def preprocess_message_element (model : str , elem : dict , api_key : str ,
227+ upload_certificate : dict = None ):
228+ """Preprocess message element and upload files if needed.
189229
230+ Args:
231+ model: Model name
232+ elem: Element dict containing file references
233+ api_key: API key
234+ upload_certificate: Optional upload certificate to reuse
190235
191- def preprocess_message_element (model : str , elem : dict , api_key : str ):
192- is_upload = check_and_upload (model , elem , api_key )
193- return is_upload
236+ Returns:
237+ tuple: (is_upload, upload_certificate) where is_upload is bool
238+ indicating if any file was uploaded, and upload_certificate
239+ is the certificate (newly obtained or passed in)
240+ """
241+ is_upload , cert = check_and_upload (model , elem , api_key ,
242+ upload_certificate )
243+ return is_upload , cert
0 commit comments