Add create and read API calls for ml_models

Change-Id: I97009b0c4309ea01011d67067f4cedb55d24aed4
This commit is contained in:
bharath 2018-10-13 21:27:21 +05:30
parent 04c2e42647
commit cf73ad603d
5 changed files with 46 additions and 58 deletions

View File

@ -49,6 +49,11 @@ class Manager(object):
if body:
return self.resource_class(self, body)
def _create_and_upload(self, url, data):
resp, body = self.api.raw_request('POST', url, data=data)
if body:
return self.resource_class(self, body)
def _format_body_data(self, body, response_key):
if response_key:
try:

View File

@ -378,7 +378,22 @@ class SessionClient(adapter.LegacyJsonAdapter):
kwargs.setdefault('headers', {})
kwargs['headers'].setdefault('Content-Type',
'application/octet-stream')
return self._http_request(url, method, **kwargs)
kwargs['headers'].setdefault('Accept', 'application/json')
resp = self._http_request(url, method, **kwargs)
body = resp.content
content_type = resp.headers.get('content-type', None)
status = resp.status_code
if status == 204 or status == 205 or content_type is None:
return resp, list()
if 'application/json' in content_type:
try:
body = resp.json()
except ValueError:
LOG.error('Could not decode response body as JSON')
else:
body = None
return resp, body
class ResponseBodyIterator(object):

View File

@ -131,8 +131,8 @@ def list_hosts(hosts):
def list_models(models):
columns = ('uuid', 'name', 'type', 'status', 'state', 'deployed_url',
'deployed_on')
columns = ('id', 'name', 'status', 'ml_type', 'url',
'deployed')
utils.print_list(models, columns,
{'versions': print_list_field('versions')},
sortby_index=None)

View File

@ -18,15 +18,6 @@ from gyanclient.common import utils
from gyanclient import exceptions
CREATION_ATTRIBUTES = ['name', 'image', 'command', 'cpu', 'memory',
'environment', 'workdir', 'labels', 'image_pull_policy',
'restart_policy', 'interactive', 'image_driver',
'security_groups', 'hints', 'nets', 'auto_remove',
'runtime', 'hostname', 'mounts', 'disk',
'availability_zone', 'auto_heal', 'privileged',
'exposed_ports', 'healthcheck']
class Model(base.Resource):
def __repr__(self):
return "<Model %s>" % self._info
@ -39,9 +30,9 @@ class ModelManager(base.Manager):
def _path(id=None):
if id:
return '/v1/ml-models/%s' % id
return '/v1/ml_models/%s' % id
else:
return '/v1/ml-models'
return '/v1/ml_models'
def list_models(self, **kwargs):
"""Retrieve a list of Models.
@ -51,7 +42,7 @@ class ModelManager(base.Manager):
"""
return self._list_pagination(self._path(''),
"models")
"ml_models")
def get(self, id):
try:
@ -59,11 +50,13 @@ class ModelManager(base.Manager):
except IndexError:
return None
def model_train(self, **kwargs):
def model_create(self, **kwargs):
new = {}
new['name'] = kwargs["name"]
new['ml_file'] = kwargs["ml_file"]
return self._create(self._path(), new)
new["name"] = kwargs["name"]
new["type"] = kwargs["type"]
model = self._create(self._path(), new)
upload_trained_model = kwargs['trained_model']
return self._create_and_upload(self._path(model.id)+'/upload_trained_model', upload_trained_model)
def delete_model(self, id):
return self._delete(self._path(id))
@ -80,33 +73,4 @@ class ModelManager(base.Manager):
return self._action(id, '/deploy')
def undeploy_model(self, id):
return self._action(id, '/unstop')
def rebuild(self, id, **kwargs):
return self._action(id, '/rebuild',
qparams=kwargs)
def restart(self, id, timeout):
return self._action(id, '/reboot',
qparams={'timeout': timeout})
def pause(self, id):
return self._action(id, '/pause')
def unpause(self, id):
return self._action(id, '/unpause')
def logs(self, id, **kwargs):
if kwargs['stdout'] is False and kwargs['stderr'] is False:
kwargs['stdout'] = True
kwargs['stderr'] = True
return self._action(id, '/logs', method='GET',
qparams=kwargs)[1]
def execute(self, id, **kwargs):
return self._action(id, '/execute',
qparams=kwargs)[1]
def execute_resize(self, id, exec_id, width, height):
self._action(id, '/execute_resize',
qparams={'exec_id': exec_id, 'w': width, 'h': height})[1]
return self._action(id, '/unstop')

View File

@ -99,18 +99,22 @@ def do_model_list(cs, args):
@utils.arg('name',
metavar='<name>',
help='ID or name of the model to train')
@utils.arg('--ml-file',
metavar='<ml_file>',
help='The ML model file to be trained')
def do_train_model(cs, args):
"""Remove security group for specified model."""
@utils.arg('--trained-model',
metavar='<trained_model>',
help='Absolute path for trained models')
@utils.arg('--type',
metavar='<type>',
help='Type of the ML model')
def do_create_model(cs, args):
"""Upload and create a trained model"""
opts = {}
opts['name'] = args.name
opts['type'] = args.type
opts = gyan_utils.remove_null_parms(**opts)
try:
opts['ml_file'] = yaml.load(open(args.ml_file))
models = cs.models.model_train(**opts)
gyan_utils.list_models(models)
opts['trained_model'] = open(args.trained_model, 'rb').read()
models = cs.models.model_create(**opts)
gyan_utils.list_models([models])
except Exception as e:
print("Creation of model %(model)s "
"failed: %(e)s" % {'model': args.name, 'e': e})
"failed: %(e)s" % {'model': args.name, 'e': e})