From d6901933802dff0a67b57d28b0090d7c604a311e Mon Sep 17 00:00:00 2001 From: Jiawei Wang Date: Mon, 30 Aug 2021 10:02:09 +0000 Subject: [PATCH] fix web service config --- python/examples/pipeline/ocr/web_service.py | 53 ++++++++++++++++++++- python/paddle_serving_server/web_service.py | 4 +- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/python/examples/pipeline/ocr/web_service.py b/python/examples/pipeline/ocr/web_service.py index 67244158..484a0936 100644 --- a/python/examples/pipeline/ocr/web_service.py +++ b/python/examples/pipeline/ocr/web_service.py @@ -22,7 +22,55 @@ from paddle_serving_app.reader import Div, Normalize, Transpose from paddle_serving_app.reader import DBPostProcess, FilterBoxes, GetRotateCropImage, SortedBoxes _LOGGER = logging.getLogger() - +import yaml +from argparse import ArgumentParser,RawDescriptionHelpFormatter +class ArgsParser(ArgumentParser): + def __init__(self): + super(ArgsParser, self).__init__( + formatter_class=RawDescriptionHelpFormatter) + self.add_argument("-c", "--config", help="configuration file to use") + self.add_argument( + "-o", "--opt", nargs='+', help="set configuration options") + + def parse_args(self, argv=None): + args = super(ArgsParser, self).parse_args(argv) + assert args.config is not None, \ + "Please specify --config=configure_file_path." + args.conf_dict = self._parse_opt(args.opt, args.config) + return args + + def _parse_helper(self, v): + if v.isnumeric(): + if "." in v: + v = float(v) + else: + v = int(v) + elif v == "True" or v == "False": + v = (v == "True") + return v + + def _parse_opt(self, opts, conf_path): + f = open(conf_path) + config = yaml.load(f, Loader=yaml.Loader) + if not opts: + return config + for s in opts: + s = s.strip() + k, v = s.split('=') + v = self._parse_helper(v) + print(k,v, type(v)) + cur = config + parent = cur + for kk in k.split("."): + if kk not in cur: + cur[kk] = {} + parent = cur + cur = cur[kk] + else: + parent = cur + cur = cur[kk] + parent[k.split(".")[-1]] = v + return config class DetOp(Op): def init_op(self): @@ -176,5 +224,6 @@ class OcrService(WebService): ocr_service = OcrService(name="ocr") -ocr_service.prepare_pipeline_config("config.yml") +FLAGS = ArgsParser().parse_args() +ocr_service.prepare_pipeline_config(yml_dict=FLAGS.conf_dict) ocr_service.run_service() diff --git a/python/paddle_serving_server/web_service.py b/python/paddle_serving_server/web_service.py index 3db3b7bc..de5bf379 100755 --- a/python/paddle_serving_server/web_service.py +++ b/python/paddle_serving_server/web_service.py @@ -50,7 +50,7 @@ class WebService(object): def get_pipeline_response(self, read_op): return None - def prepare_pipeline_config(self, yaml_file): + def prepare_pipeline_config(self, yml_file=None, yml_dict=None): # build dag read_op = pipeline.RequestOp() last_op = self.get_pipeline_response(read_op) @@ -60,7 +60,7 @@ class WebService(object): "`get_pipeline_response`.") response_op = pipeline.ResponseOp(input_ops=[last_op]) self._server.set_response_op(response_op) - self._server.prepare_server(yaml_file) + self._server.prepare_server(yml_file=yml_file, yml_dict=yml_dict) def run_service(self): self._server.run_server() -- GitLab