def config_with_file(configs: dict): ''' Config `cv_module_info` and `nlp_module_info` by configs. Args: configs(dict): Module info and configs Examples: .. code-block:: python configs = {'lac': {'version': 1.0.0, 'category': nlp}} config_with_file(configs=configs) ''' for key, value in configs.items(): if "CV" == value["category"]: cv_module_info.add_module(key, {key: value}) elif "NLP" == value["category"]: nlp_module_info.add_module(key, {key: value}) v2_module_info.add_module(key, {key: value}) logger = log.get_file_logger(filename) logger.info("%s==%s" % (key, value["version"]))
def run(configs: dict = None, port: int = 8866): ''' Run flask instance for PaddleHub-Serving Args: configs(dict): module info and configs port(int): the port of the webserver Examples: .. code-block:: python configs = {'lac': {'version': 1.0.0, 'category': nlp}} run(configs=configs, port=8866) ''' logger = log.get_file_logger(filename) if configs is not None: config_with_file(configs) else: logger.error("Start failed cause of missing configuration.") return my_app = create_app(init_flag=True) my_app.run(host="0.0.0.0", port=port, debug=False, threaded=False) log.logger.info("PaddleHub-Serving has been stopped.")
def __init__(self): self.frontend = None self.backend = None filename = 'HubServing-%s.log' % time.strftime("%Y_%m_%d", time.localtime()) self.logger = log.get_file_logger(filename)
if __name__ == '__main__': argv = sys.argv modules_info = json.loads(argv[1]) gpu_index = argv[2] addr = argv[3] os.environ['CUDA_VISIBLE_DEVICES'] = gpu_index import paddlehub as hub from paddlehub.serving.http_server import package_result from paddlehub.utils import log filename = 'HubServing-%s.log' % time.strftime("%Y_%m_%d", time.localtime()) logger = log.get_file_logger(filename) logger.logger.handlers = logger.logger.handlers[0:1] modules_pred_info = {} for module_name, module_info in modules_info.items(): init_args = module_info.get('init_args', {}) init_args.update({'name': module_name}) module = hub.Module(**init_args) method_name = module.serving_func_name serving_method = getattr(module, method_name) predict_args = module_info.get('predict_args', {}) modules_pred_info.update({ module_name: { 'predict_args': predict_args, 'serving_method': serving_method }
def create_app(init_flag: bool = False, configs: dict = None): ''' Start one flask instance and ready for HTTP requests. Args: init_flag(bool): Whether the instance need to be initialized with `configs` or not configs(dict): Module configs for initializing. Returns: One flask instance. Examples: .. code-block:: python create_app(init_flag=False, configs=None) ''' if init_flag is False: if configs is None: raise RuntimeError("Lack of necessary configs.") config_with_file(configs) app_instance = Flask(__name__) app_instance.config["JSON_AS_ASCII"] = False app_instance.logger = log.get_file_logger(filename) @app_instance.route("/", methods=["GET", "POST"]) def index(): ''' Provide index page. ''' return '暂不提供可视化界面,请直接使用脚本进行请求。<br/>No visual ' \ 'interface is provided for the time being, please use the' \ ' python script to make a request directly.' @app_instance.before_request def before_request(): ''' Add id info to `request.data` before request. ''' request.data = {"id": utils.md5(request.remote_addr + str(time.time()))} @app_instance.route("/predict/<module_name>", methods=["POST"]) def predict_serving_v2(module_name: str): ''' Http api for predicting. Args: module_name(str): Module name for predicting. Returns: Result of predicting after packaging. ''' if module_name in v2_module_info.modules: module_info = v2_module_info.get_module_info(module_name) else: msg = "Module {} is not available.".format(module_name) return package_result("111", msg, "") inputs = request.json if inputs is None: results = "This usage is out of date, please use 'application/json' as content-type to post to /predict/%s. See 'https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/tutorial/serving.md' for more details." % ( module_name) return package_result("112", results, "") results = predict_v2(module_info, inputs) return results return app_instance
def create_app(client_port: int = 5559, modules_name: list = []): ''' Start one flask instance and ready for HTTP requests. Args: client_port(str): port of zmq backend address modules_name(list): the name list of modules Returns: One flask instance. Examples: .. code-block:: python create_app(client_port='5559') ''' app_instance = Flask(__name__) app_instance.config["JSON_AS_ASCII"] = False app_instance.logger = log.get_file_logger(filename) pid = os.getpid() @app_instance.route("/", methods=["GET", "POST"]) def index(): ''' Provide index page. ''' return '暂不提供可视化界面,请直接使用脚本进行请求。<br/>No visual ' \ 'interface is provided for the time being, please use the' \ ' python script to make a request directly.' @app_instance.before_request def before_request(): ''' Add id info to `request.data` before request. ''' request.data = { "id": utils.md5(request.remote_addr + str(time.time())) } @app_instance.route("/predict/<module_name>", methods=["POST"]) def predict_serving_v3(module_name: str): ''' Http api for predicting. Args: module_name(str): Module name for predicting. Returns: Result of predicting after packaging. ''' if module_name not in modules_name: msg = "Module {} is not available.".format(module_name) return package_result("111", "", msg) inputs = request.json if inputs is None: results = "This usage is out of date, please use 'application/json' as content-type to post to /predict/%s. See 'https://github.com/PaddlePaddle/PaddleHub/blob/release/v1.6/docs/tutorial/serving.md' for more details." % ( module_name) return package_result("112", results, "") inputs = {'module_name': module_name, 'inputs': inputs} port_str = 'tcp://localhost:%s' % client_port client = InferenceClientProxy.get_client(pid, port_str) results = client.send_req(inputs) return package_result("000", results, "") return app_instance