From f5a809b5c61d71d5c128f908d030bb4d5b3b98e6 Mon Sep 17 00:00:00 2001 From: MRXLT Date: Wed, 22 Apr 2020 16:15:20 +0800 Subject: [PATCH] add run_flask && fix HTTP demo --- python/examples/bert/bert_web_service.py | 1 + .../imagenet/image_classification_service.py | 1 + .../image_classification_service_gpu.py | 3 ++- python/examples/imdb/text_classify_service.py | 1 + python/paddle_serving_server/web_service.py | 18 ++++++++++++++++++ .../paddle_serving_server_gpu/web_service.py | 18 ++++++++++++++++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/python/examples/bert/bert_web_service.py b/python/examples/bert/bert_web_service.py index e22e379d..f72694c0 100644 --- a/python/examples/bert/bert_web_service.py +++ b/python/examples/bert/bert_web_service.py @@ -36,3 +36,4 @@ bert_service.set_gpus(gpu_ids) bert_service.prepare_server( workdir="workdir", port=int(sys.argv[2]), device="gpu") bert_service.run_server() +bert_service.run_flask() diff --git a/python/examples/imagenet/image_classification_service.py b/python/examples/imagenet/image_classification_service.py index 2776eb1b..159a590a 100644 --- a/python/examples/imagenet/image_classification_service.py +++ b/python/examples/imagenet/image_classification_service.py @@ -47,3 +47,4 @@ image_service.load_model_config(sys.argv[1]) image_service.prepare_server( workdir=sys.argv[2], port=int(sys.argv[3]), device="cpu") image_service.run_server() +image_service.run_flask() diff --git a/python/examples/imagenet/image_classification_service_gpu.py b/python/examples/imagenet/image_classification_service_gpu.py index 287392e4..4f620bfd 100644 --- a/python/examples/imagenet/image_classification_service_gpu.py +++ b/python/examples/imagenet/image_classification_service_gpu.py @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle_serving_server_gpu.web_service import WebService import sys import cv2 import base64 import numpy as np from image_reader import ImageReader +from paddle_serving_server_gpu.web_service import WebService class ImageService(WebService): @@ -49,3 +49,4 @@ image_service.set_gpus("0,1") image_service.prepare_server( workdir=sys.argv[2], port=int(sys.argv[3]), device="gpu") image_service.run_server() +image_service.run_flask() diff --git a/python/examples/imdb/text_classify_service.py b/python/examples/imdb/text_classify_service.py index 50d0d1ae..5ff919eb 100755 --- a/python/examples/imdb/text_classify_service.py +++ b/python/examples/imdb/text_classify_service.py @@ -39,3 +39,4 @@ imdb_service.prepare_server( workdir=sys.argv[2], port=int(sys.argv[3]), device="cpu") imdb_service.prepare_dict({"dict_file_path": sys.argv[4]}) imdb_service.run_server() +imdb_service.run_flask() diff --git a/python/paddle_serving_server/web_service.py b/python/paddle_serving_server/web_service.py index 31b786bc..5fa9caa4 100755 --- a/python/paddle_serving_server/web_service.py +++ b/python/paddle_serving_server/web_service.py @@ -108,6 +108,24 @@ class WebService(object): p_rpc = Process(target=self._launch_rpc_service) p_rpc.start() + def run_flask(self): + app_instance = Flask(__name__) + + @app_instance.before_first_request + def init(): + self._launch_web_service() + + service_name = "/" + self.name + "/prediction" + + @app_instance.route(service_name, methods=["POST"]) + def run(): + return self.get_prediction(request) + + app_instance.run(host="0.0.0.0", + port=self.port, + threaded=False, + processes=4) + def preprocess(self, feed={}, fetch=[]): return feed, fetch diff --git a/python/paddle_serving_server_gpu/web_service.py b/python/paddle_serving_server_gpu/web_service.py index abec2b55..ab01dcc2 100644 --- a/python/paddle_serving_server_gpu/web_service.py +++ b/python/paddle_serving_server_gpu/web_service.py @@ -143,6 +143,24 @@ class WebService(object): for p in server_pros: p.start() + def run_flask(self): + app_instance = Flask(__name__) + + @app_instance.before_first_request + def init(): + self._launch_web_service() + + service_name = "/" + self.name + "/prediction" + + @app_instance.route(service_name, methods=["POST"]) + def run(): + return self.get_prediction(request) + + app_instance.run(host="0.0.0.0", + port=self.port, + threaded=False, + processes=4) + def preprocess(self, feed={}, fetch=[]): return feed, fetch -- GitLab