7
7
"""The Model Repository service supports registering and managing models."""
8
8
9
9
from warnings import warn
10
- from io import StringIO
11
- import json
12
10
13
11
from .service import Service
14
12
from ..core import current_session , get , delete , sasctl_command , HTTPError
@@ -368,24 +366,22 @@ def create_model(
368
366
)
369
367
370
368
@classmethod
371
- def add_model_content (cls , model , file , name , content_type = "multipart/form-data" , role = None ):
372
- """Add additional files to the model. Additional files can come in the form of
373
- a bytes-like, string, or dict object. String and dict objects will be converted to
374
- a bytes-like object for upload.
369
+ def add_model_content (cls , model , file , name , role = None , content_type = None ):
370
+ """Add additional files to the model.
375
371
376
372
Parameters
377
373
----------
378
374
model : str or dict
379
375
The name or id of the model, or a dictionary representation of
380
376
the model.
381
- file : str, dict, or bytes
377
+ file : str or bytes
382
378
A file related to the model, such as the model code.
383
379
name : str
384
380
Name of the file related to the model.
385
- content_type : str, optional
386
- An HTTP Content-Type value. Default value is multipart/form-data.
387
- role : str, optional
388
- Role of the model file, such as 'Python pickle'. Default value is None.
381
+ role : str
382
+ Role of the model file, such as 'Python pickle'.
383
+ content_type : str
384
+ an HTTP Content-Type value
389
385
390
386
Returns
391
387
-------
@@ -400,45 +396,38 @@ def add_model_content(cls, model, file, name, content_type="multipart/form-data"
400
396
else :
401
397
model = cls .get_model (model )
402
398
id_ = model ["id" ]
403
-
404
- # Convert string file representations to bytes-like object
405
- if isinstance (file , str ):
406
- file = StringIO (file )
407
- content_type = "multipart/form-data"
408
- # Convert dict file representations to bytes-like object
409
- elif isinstance (file , dict ):
410
- file = StringIO (json .dumps (file ))
411
- content_type = "multipart/form-data"
412
-
413
- files = {"files" : (name , file , content_type )}
414
-
415
- if role is None :
416
- params = {}
399
+
400
+ if content_type is None and isinstance (file , bytes ):
401
+ content_type = "application/octet-stream"
402
+
403
+ if content_type is not None :
404
+ files = {name : (name , file , content_type )}
417
405
else :
418
- params = {"role" : role }
419
- params = "&" .join ("{}={}" .format (k , v ) for k , v in params .items ())
406
+ files = {name : file }
407
+
408
+ metadata = {"role" : role , "name" : name }
420
409
421
- # If the file already exists, a 409 error will be returned
410
+ # return cls.post('/models/{}/contents'.format(id_), files=files, data=metadata)
411
+
412
+ # if the file already exists, a 409 error will be returned
422
413
try :
423
414
return cls .post (
424
- "/models/{}/contents" .format (id_ ),
425
- files = files ,
426
- params = params ,
415
+ "/models/{}/contents" .format (id_ ), files = files , data = metadata
427
416
)
428
- except HTTPError as err :
429
- # Delete the older duplicate model file and rerun the API call
430
- if err .code == 409 :
417
+ # delete the older duplicate model and rerun the API call
418
+ except HTTPError as e :
419
+ if e .code == 409 :
431
420
model_contents = cls .get_model_contents (id_ )
432
421
for item in model_contents :
433
422
if item .name == name :
434
423
cls .delete ("/models/{}/contents/{}" .format (id_ , item .id ))
435
424
return cls .post (
436
425
"/models/{}/contents" .format (id_ ),
437
426
files = files ,
438
- params = params ,
427
+ data = metadata ,
439
428
)
440
429
else :
441
- raise err
430
+ raise e
442
431
443
432
@classmethod
444
433
def default_repository (cls ):
0 commit comments