def publish_model(self, args): """发布模型到稳定版""" message = args.message project = get_active_project() config_path = self.get_model_config_path() config = get_config_parser(config_path) section_name = DEFAULT_MODEL_SECTION model_id = config_get(config.get, section_name, 'model_id') oauth2_section = DEFAULT_OAUTH2_SECTION token_section = DEFAULT_TOKEN_SECTION config = read_config(project) access_token = config_get(config.get, token_section, 'access_token') endpoint = config_get(config.get, oauth2_section, 'endpoint') api = API(access_token, endpoint=endpoint, timeout=DEFAULT_MODEL_TIMEOUT) try: result = api.publish_model(model_id, message) except APIError as e: output('[red]模型发布失败:[/red]') output_json(e.result) sys.exit(1) output('[green]模型 {} 稳定版已成功发布。\n版本号:{}。[/green]'.format( model_id, result['version']))
def run_model(self, args): params = {} if args.file: filepath = args.file if not exists(filepath): output('文件不存在:{}'.format(filepath)) sys.exit(1) if isdir(filepath): output('存在文件夹:{}'.format(filepath)) sys.exit(1) with open(filepath, 'r') as fp: params = json.load(fp) elif args.args: params = dict(args.args) draft = args.draft test = args.test project = get_active_project() config_path = self.get_model_config_path() config = get_config_parser(config_path) section_name = DEFAULT_MODEL_SECTION model_id = config_get(config.get, section_name, 'model_id') oauth2_section = DEFAULT_OAUTH2_SECTION token_section = DEFAULT_TOKEN_SECTION config = read_config(project) access_token = config_get(config.get, token_section, 'access_token') endpoint = config_get(config.get, oauth2_section, 'endpoint') api = API(access_token, endpoint=endpoint, timeout=DEFAULT_MODEL_TIMEOUT) try: if draft is True: result = api.invoke_draft_model(model_id, **params) elif test is True: port = args.port api = API(access_token, endpoint='{}:{}'.format(TEST_SERVER_ENDPOINT, port), timeout=DEFAULT_MODEL_TIMEOUT) result = api.invoke_model(model_id, **params) else: result = api.invoke_model(model_id, **params) except APIError as e: output('[red]模型运行失败:[/red]') output_json(e.result) sys.exit(1) output('[green]模型返回值:[/green]') output_json(result.json())
def deploy_model(self, args): """部署模型""" ignore_source = args.ignore_source home = get_home() project = get_active_project() config_path = self.get_model_config_path() config = get_config_parser(config_path) section_name = DEFAULT_MODEL_SECTION model_id = config_get(config.get, section_name, 'model_id') timestr = datetime.now().strftime('%Y%m%d%H%M%S%f') randstr = uuid4().hex zip_filename = '{}.{}.{}.zip'.format(model_id, timestr, randstr) params = {} params['runtime'] = config_get(config.get, section_name, 'runtime') params['memory_size'] = config_get(config.getint, section_name, 'memory_size') params['timeout'] = config_get(config.getint, section_name, 'timeout') oauth2_section = DEFAULT_OAUTH2_SECTION token_section = DEFAULT_TOKEN_SECTION config = read_config(project) access_token = config_get(config.get, token_section, 'access_token') endpoint = config_get(config.get, oauth2_section, 'endpoint') api = API(access_token, endpoint=endpoint, timeout=DEFAULT_MODEL_TIMEOUT) object_name = None if not ignore_source: ignore_path = join(home, BGE_IGNORE_FILE) if not exists(ignore_path): output('未发现 .bgeignore 文件,初始化 {} ...'.format(ignore_path)) open(ignore_path, 'w').write(BGEIGNORE_TEMPLATE) minify_path = join(home, BGE_MINIFY_FILE) if not exists(minify_path): output('未发现 .bgeminify 文件,初始化 {} ...'.format(minify_path)) open(minify_path, 'w').write(BGEMINIFY_TEMPLATE) output('开始打包模型源码...') zip_tmpdir = join(home, '.bge', 'tmp') if not exists(zip_tmpdir): os.makedirs(zip_tmpdir) with tempfile.NamedTemporaryFile(suffix='.zip', prefix='model-', dir=zip_tmpdir, delete=False) as tmp: with zipfile.ZipFile(tmp.name, 'w', ZIP_COMPRESSION) as zf: self._zip_codedir(home, zf) tmp.flush() tmp.seek(0, 2) size = tmp.tell() tmp.seek(0) human_size = human_byte(size) if size > 100 * 1024 * 1024: output('打包后 zip 文件大小为 {},最大限制 100MB'.format(human_size)) exit(1) output('打包成功:{}'.format(tmp.name)) output('文件大小:{}'.format(human_size)) output('开始上传模型源码...') try: object_name = api.upload(zip_filename, tmp) except APIError as e: output('[red]上传模型源码失败:[/red]') output_json(e.result) sys.exit(1) output('[green]上传成功[green]') with console.status('模型部署中...', spinner='earth'): try: result = api.deploy_model(model_id, object_name=object_name, **params) except APIError as e: output('[red]部署模型失败:[/red]') output_json(e.result) sys.exit(1) task_id = result.task_id output('模型部署任务:{}'.format(task_id)) task_path = join(home, '.bge', 'task_id') with open(task_path, 'w') as f: f.write(task_id) output('模型部署任务返回结果:') progress = self._wait_model_task(api, task_id, task_path) if 'SUCCESS' == progress: output('[green]模型 {} 灰度部署成功。'.format(model_id)) elif 'FAILURE' == progress: output('[red]模型 {} 灰度部署失败。任务结果:{}'.format(model_id, result)) elif 'REVOKED' == progress: output('[white]模型 {} 灰度部署任务已被撤销。'.format(model_id))