@@ -374,7 +374,7 @@ def add_model_content(cls, model, file, name, role=None, content_type=None):
374
374
model : str or dict
375
375
The name or id of the model, or a dictionary representation of
376
376
the model.
377
- file : str or bytes
377
+ file : str, dict, or bytes
378
378
A file related to the model, such as the model code.
379
379
name : str
380
380
Name of the file related to the model.
@@ -399,22 +399,24 @@ def add_model_content(cls, model, file, name, role=None, content_type=None):
399
399
400
400
if content_type is None and isinstance (file , bytes ):
401
401
content_type = "application/octet-stream"
402
+ elif isinstance (file , dict ):
403
+ import json
404
+ file = json .dumps (file )
402
405
403
- if content_type is not None :
404
- files = {name : (name , file , content_type )}
405
- else :
406
- files = {name : file }
407
-
408
- metadata = {"role" : role , "name" : name }
409
-
410
- # return cls.post('/models/{}/contents'.format(id_), files=files, data=metadata)
406
+ files = {"files" : (name , file , content_type )}
411
407
412
- # if the file already exists, a 409 error will be returned
408
+ if role is None :
409
+ params = {}
410
+ else :
411
+ params = {"role" : role }
412
+ params = "&" .join ("{}={}" .format (k , v ) for k , v in params .items ())
413
+
414
+ # If the file already exists, a 409 error will be returned
413
415
try :
414
416
return cls .post (
415
- "/models/{}/contents" .format (id_ ), files = files , data = metadata
417
+ "/models/{}/contents" .format (id_ ), files = files , params = params
416
418
)
417
- # delete the older duplicate model and rerun the API call
419
+ # Delete the older duplicate model and rerun the API call
418
420
except HTTPError as e :
419
421
if e .code == 409 :
420
422
model_contents = cls .get_model_contents (id_ )
@@ -424,7 +426,7 @@ def add_model_content(cls, model, file, name, role=None, content_type=None):
424
426
return cls .post (
425
427
"/models/{}/contents" .format (id_ ),
426
428
files = files ,
427
- data = metadata ,
429
+ params = params ,
428
430
)
429
431
else :
430
432
raise e
0 commit comments