From 3f787a82c7ab34989dc03d3909b5858d5c2f8c62 Mon Sep 17 00:00:00 2001 From: haoyuying <35907364+haoyuying@users.noreply.github.com> Date: Wed, 21 Oct 2020 19:54:07 +0800 Subject: [PATCH] add convert and config --- paddlehub/commands/config.py | 102 +++++++++++++++ paddlehub/commands/convert.py | 132 +++++++++++++++++++ paddlehub/commands/tmpl/init_py.tmpl | 0 paddlehub/commands/tmpl/serving_demo.tmpl | 25 ++++ paddlehub/commands/tmpl/x_model.tmpl | 147 ++++++++++++++++++++++ paddlehub/utils/utils.py | 11 -- paddlehub/utils/xarfile.py | 2 +- 7 files changed, 407 insertions(+), 12 deletions(-) create mode 100644 paddlehub/commands/tmpl/init_py.tmpl create mode 100644 paddlehub/commands/tmpl/serving_demo.tmpl create mode 100644 paddlehub/commands/tmpl/x_model.tmpl diff --git a/paddlehub/commands/config.py b/paddlehub/commands/config.py index e69de29b..f97186c1 100644 --- a/paddlehub/commands/config.py +++ b/paddlehub/commands/config.py @@ -0,0 +1,102 @@ +#coding:utf-8 +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License" +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import json +import os +import re +import hashlib +import uuid +import time + +from paddlehub.env import CONF_HOME +from paddlehub.commands import register +from paddlehub.utils.utils import md5 + +default_server_config = { + "server_url": ["http://paddlepaddle.org.cn/paddlehub"], + "resource_storage_server_url": "https://bj.bcebos.com/paddlehub-data/", + "debug": False, + "log_level": "DEBUG", + "hub_name": md5(str(uuid.uuid1())[-12:]) + "-" + str(int(time.time())) +} + + +@register(name='hub.config', description='Configure PaddleHub.') +class ConfigCommand: + @staticmethod + def show_config(): + print("The current configuration is shown below.") + with open(os.path.join(CONF_HOME, "config.json"), "r") as fp: + print(json.dumps(json.load(fp), indent=4)) + + @staticmethod + def set_server_url(server_url): + with open(os.path.join(CONF_HOME, "config.json"), "r") as fp: + config = json.load(fp) + re_str = r"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$" + if re.match(re_str, server_url) is not None: + config["server_url"] = list([server_url]) + ConfigCommand.set_config(config) + else: + print("The format of the input url is invalid.") + + @staticmethod + def set_config(config): + with open(os.path.join(CONF_HOME, "config.json"), "w") as fp: + fp.write(json.dumps(config)) + print("Set success! The current configuration is shown below.") + print(json.dumps(config, indent=4)) + + @staticmethod + def set_log_level(level): + level = str(level).upper() + if level not in ["NOLOG", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]: + print("Allowed values include: " "NOLOG, DEBUG, INFO, WARNING, ERROR, CRITICAL") + return + with open(os.path.join(CONF_HOME, "config.json"), "r") as fp: + current_config = json.load(fp) + with open(os.path.join(CONF_HOME, "config.json"), "w") as fp: + current_config["log_level"] = level + fp.write(json.dumps(current_config)) + print("Set success! The current configuration is shown below.") + print(json.dumps(current_config, indent=4)) + + @staticmethod + def show_help(): + str = "config