From cf73ad603d1d306fe000ac95f8245b88a3a56208 Mon Sep 17 00:00:00 2001 From: bharath Date: Sat, 13 Oct 2018 21:27:21 +0530 Subject: [PATCH] Add create and read API calls for ml_models Change-Id: I97009b0c4309ea01011d67067f4cedb55d24aed4 --- gyanclient/common/base.py | 5 +++ gyanclient/common/httpclient.py | 17 +++++++++- gyanclient/common/utils.py | 4 +-- gyanclient/v1/models.py | 56 ++++++--------------------------- gyanclient/v1/models_shell.py | 22 +++++++------ 5 files changed, 46 insertions(+), 58 deletions(-) diff --git a/gyanclient/common/base.py b/gyanclient/common/base.py index 0f65fdb..2b3b077 100644 --- a/gyanclient/common/base.py +++ b/gyanclient/common/base.py @@ -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: diff --git a/gyanclient/common/httpclient.py b/gyanclient/common/httpclient.py index a759fa5..d752426 100644 --- a/gyanclient/common/httpclient.py +++ b/gyanclient/common/httpclient.py @@ -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): diff --git a/gyanclient/common/utils.py b/gyanclient/common/utils.py index 29b3ca4..72370f9 100644 --- a/gyanclient/common/utils.py +++ b/gyanclient/common/utils.py @@ -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) diff --git a/gyanclient/v1/models.py b/gyanclient/v1/models.py index bd12388..907e37c 100644 --- a/gyanclient/v1/models.py +++ b/gyanclient/v1/models.py @@ -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 "" % 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') \ No newline at end of file diff --git a/gyanclient/v1/models_shell.py b/gyanclient/v1/models_shell.py index c24f97b..c19c68d 100644 --- a/gyanclient/v1/models_shell.py +++ b/gyanclient/v1/models_shell.py @@ -99,18 +99,22 @@ def do_model_list(cs, args): @utils.arg('name', metavar='', help='ID or name of the model to train') -@utils.arg('--ml-file', - metavar='', - 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='', + help='Absolute path for trained models') +@utils.arg('--type', + metavar='', + 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}) \ No newline at end of file