提交 ff034d3f 编写于 作者: H HexToString

update http_client

上级 594603d8
...@@ -23,11 +23,9 @@ args = benchmark_args() ...@@ -23,11 +23,9 @@ args = benchmark_args()
reader = ChineseBertReader({"max_seq_len": 128}) reader = ChineseBertReader({"max_seq_len": 128})
fetch = ["pooled_output"] fetch = ["pooled_output"]
endpoint_list = ['127.0.0.1:9292']
client = HttpClient(ip='127.0.0.1', port='9292') client = HttpClient()
client.load_client_config(args.model) client.load_client_config(args.model)
#client.set_ip('127.0.0.1')
#client.set_port('9292')
''' '''
if you want use GRPC-client, set_use_grpc_client(True) if you want use GRPC-client, set_use_grpc_client(True)
or you can directly use client.grpc_client_predict(...) or you can directly use client.grpc_client_predict(...)
...@@ -49,6 +47,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default) ...@@ -49,6 +47,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default)
if you want use JSON data format in HTTP-body, set False if you want use JSON data format in HTTP-body, set False
''' '''
#client.set_http_proto(True) #client.set_http_proto(True)
client.connect(endpoint_list)
for line in sys.stdin: for line in sys.stdin:
feed_dict = reader.process(line) feed_dict = reader.process(line)
......
...@@ -20,8 +20,6 @@ import time ...@@ -20,8 +20,6 @@ import time
client = HttpClient() client = HttpClient()
client.load_client_config(sys.argv[1]) client.load_client_config(sys.argv[1])
#client.set_ip('127.0.0.1')
#client.set_port('9393')
''' '''
if you want use GRPC-client, set_use_grpc_client(True) if you want use GRPC-client, set_use_grpc_client(True)
or you can directly use client.grpc_client_predict(...) or you can directly use client.grpc_client_predict(...)
...@@ -43,13 +41,14 @@ we recommend use Proto data format in HTTP-body, set True(which is default) ...@@ -43,13 +41,14 @@ we recommend use Proto data format in HTTP-body, set True(which is default)
if you want use JSON data format in HTTP-body, set False if you want use JSON data format in HTTP-body, set False
''' '''
#client.set_http_proto(True) #client.set_http_proto(True)
client.connect(["127.0.0.1:9393"])
fetch_list = client.get_fetch_names()
import paddle import paddle
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.uci_housing.test(), buf_size=500), paddle.dataset.uci_housing.test(), buf_size=500),
batch_size=1) batch_size=1)
fetch_list = client.get_fetch_names()
for data in test_reader(): for data in test_reader():
new_data = np.zeros((1, 13)).astype("float32") new_data = np.zeros((1, 13)).astype("float32")
new_data[0] = data[0][0] new_data[0] = data[0][0]
......
...@@ -18,10 +18,8 @@ from paddle_serving_app.reader import Sequential, URL2Image, Resize ...@@ -18,10 +18,8 @@ from paddle_serving_app.reader import Sequential, URL2Image, Resize
from paddle_serving_app.reader import CenterCrop, RGB2BGR, Transpose, Div, Normalize from paddle_serving_app.reader import CenterCrop, RGB2BGR, Transpose, Div, Normalize
import time import time
client = HttpClient(ip='127.0.0.1', port='9696') client = HttpClient()
client.load_client_config(sys.argv[1]) client.load_client_config(sys.argv[1])
#client.set_ip('127.0.0.1')
#client.set_port('9292')
''' '''
if you want use GRPC-client, set_use_grpc_client(True) if you want use GRPC-client, set_use_grpc_client(True)
or you can directly use client.grpc_client_predict(...) or you can directly use client.grpc_client_predict(...)
...@@ -43,6 +41,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default) ...@@ -43,6 +41,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default)
if you want use JSON data format in HTTP-body, set False if you want use JSON data format in HTTP-body, set False
''' '''
#client.set_http_proto(True) #client.set_http_proto(True)
client.connect(["127.0.0.1:9696"])
label_dict = {} label_dict = {}
label_idx = 0 label_idx = 0
......
...@@ -17,10 +17,8 @@ from paddle_serving_app.reader.imdb_reader import IMDBDataset ...@@ -17,10 +17,8 @@ from paddle_serving_app.reader.imdb_reader import IMDBDataset
import sys import sys
import numpy as np import numpy as np
client = HttpClient(ip='127.0.0.1', port='9292') client = HttpClient()
client.load_client_config(sys.argv[1]) client.load_client_config(sys.argv[1])
#client.set_ip('127.0.0.1')
#client.set_port('9292')
''' '''
if you want use GRPC-client, set_use_grpc_client(True) if you want use GRPC-client, set_use_grpc_client(True)
or you can directly use client.grpc_client_predict(...) or you can directly use client.grpc_client_predict(...)
...@@ -42,6 +40,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default) ...@@ -42,6 +40,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default)
if you want use JSON data format in HTTP-body, set False if you want use JSON data format in HTTP-body, set False
''' '''
#client.set_http_proto(True) #client.set_http_proto(True)
client.connect(["127.0.0.1:9292"])
# you can define any english sentence or dataset here # you can define any english sentence or dataset here
# This example reuses imdb reader in training, you # This example reuses imdb reader in training, you
......
...@@ -21,10 +21,8 @@ import os ...@@ -21,10 +21,8 @@ import os
import io import io
import numpy as np import numpy as np
client = HttpClient(ip='127.0.0.1', port='9292') client = HttpClient()
client.load_client_config(sys.argv[1]) client.load_client_config(sys.argv[1])
#client.set_ip('127.0.0.1')
#client.set_port('9292')
''' '''
if you want use GRPC-client, set_use_grpc_client(True) if you want use GRPC-client, set_use_grpc_client(True)
or you can directly use client.grpc_client_predict(...) or you can directly use client.grpc_client_predict(...)
...@@ -46,6 +44,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default) ...@@ -46,6 +44,7 @@ we recommend use Proto data format in HTTP-body, set True(which is default)
if you want use JSON data format in HTTP-body, set False if you want use JSON data format in HTTP-body, set False
''' '''
#client.set_http_proto(True) #client.set_http_proto(True)
client.connect(["127.0.0.1:9292"])
reader = LACReader() reader = LACReader()
for line in sys.stdin: for line in sys.stdin:
......
# coding=utf-8
# Copyright (c) 2020 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.
# pylint: disable=doc-string-missing
from paddle_serving_server.web_service import WebService
from paddle_serving_app.reader import ChineseBertReader
import sys
import os
import numpy as np
class BertService(WebService):
def load(self):
self.reader = ChineseBertReader({
"vocab_file": "vocab.txt",
"max_seq_len": 128
})
def preprocess(self, feed=[], fetch=[]):
feed_res = []
is_batch = False
for ins in feed:
feed_dict = self.reader.process(ins["words"].encode("utf-8"))
for key in feed_dict.keys():
feed_dict[key] = np.array(feed_dict[key]).reshape(
(len(feed_dict[key]), 1))
feed_res.append(feed_dict)
return feed_res, fetch, is_batch
bert_service = BertService(name="bert")
bert_service.load()
bert_service.load_model_config(sys.argv[1])
bert_service.prepare_server(
workdir="workdir", port=int(sys.argv[2]), use_lite=True, use_xpu=True, ir_optim=True)
bert_service.run_rpc_service()
bert_service.run_web_service()
# coding=utf-8
# Copyright (c) 2020 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.
# pylint: disable=doc-string-missing
from paddle_serving_server.web_service import WebService
from paddle_serving_app.reader import ChineseBertReader
import sys
import os
import numpy as np
class BertService(WebService):
def load(self):
self.reader = ChineseBertReader({
"vocab_file": "vocab.txt",
"max_seq_len": 128
})
def preprocess(self, feed=[], fetch=[]):
feed_res = []
is_batch = False
for ins in feed:
feed_dict = self.reader.process(ins["words"].encode("utf-8"))
for key in feed_dict.keys():
feed_dict[key] = np.array(feed_dict[key]).reshape(
(len(feed_dict[key]), 1))
feed_res.append(feed_dict)
return feed_res, fetch, is_batch
bert_service = BertService(name="bert")
bert_service.load()
bert_service.load_model_config(sys.argv[1])
bert_service.prepare_server(
workdir="workdir", port=int(sys.argv[2]), use_lite=True, use_xpu=True, ir_optim=True)
bert_service.run_rpc_service()
bert_service.run_web_service()
...@@ -23,18 +23,3 @@ The `paddlepaddle` package is used in `test_client.py`, and you may need to down ...@@ -23,18 +23,3 @@ The `paddlepaddle` package is used in `test_client.py`, and you may need to down
``` shell ``` shell
python3 test_client.py uci_housing_client/serving_client_conf.prototxt python3 test_client.py uci_housing_client/serving_client_conf.prototxt
``` ```
## HTTP service
### Start server
Start a web service with default web service hosting modules:
``` shell
python3 -m paddle_serving_server.serve --model uci_housing_model --thread 10 --port 9393 --use_lite --use_xpu --ir_optim --name uci
```
### Client prediction
``` shell
curl -H "Content-Type:application/json" -X POST -d '{"feed":[{"x": [0.0137, -0.1136, 0.2553, -0.0692, 0.0582, -0.0727, -0.1583, -0.0584, 0.6283, 0.4919, 0.1856, 0.0795, -0.0332]}], "fetch":["price"]}' http://127.0.0.1:9393/uci/prediction
```
...@@ -31,19 +31,3 @@ python3 -m paddle_serving_server.serve --model uci_housing_model --thread 10 --p ...@@ -31,19 +31,3 @@ python3 -m paddle_serving_server.serve --model uci_housing_model --thread 10 --p
``` shell ``` shell
python3 test_client.py uci_housing_client/serving_client_conf.prototxt python3 test_client.py uci_housing_client/serving_client_conf.prototxt
``` ```
## HTTP服务
### 开启服务端
通过下面的一行代码开启默认web服务:
``` shell
python3 -m paddle_serving_server.serve --model uci_housing_model --thread 10 --port 9393 --use_lite --use_xpu --ir_optim --name uci
```
### 客户端预测
``` shell
curl -H "Content-Type:application/json" -X POST -d '{"feed":[{"x": [0.0137, -0.1136, 0.2553, -0.0692, 0.0582, -0.0727, -0.1583, -0.0584, 0.6283, 0.4919, 0.1856, 0.0795, -0.0332]}], "fetch":["price"]}' http://127.0.0.1:9393/uci/prediction
```
...@@ -22,6 +22,7 @@ import gzip ...@@ -22,6 +22,7 @@ import gzip
from collections import Iterable from collections import Iterable
import base64 import base64
import sys import sys
import re
import grpc import grpc
from .proto import general_model_service_pb2 from .proto import general_model_service_pb2
...@@ -98,7 +99,7 @@ class HttpClient(object): ...@@ -98,7 +99,7 @@ class HttpClient(object):
self.headers["Content-Type"] = "application/proto" self.headers["Content-Type"] = "application/proto"
self.max_body_size = 512 * 1024 * 1024 self.max_body_size = 512 * 1024 * 1024
self.use_grpc_client = False self.use_grpc_client = False
self.url = None self.http_s = "http://"
# 使用连接池能够不用反复建立连接 # 使用连接池能够不用反复建立连接
self.requests_session = requests.session() self.requests_session = requests.session()
...@@ -170,7 +171,6 @@ class HttpClient(object): ...@@ -170,7 +171,6 @@ class HttpClient(object):
def set_max_body_size(self, max_body_size): def set_max_body_size(self, max_body_size):
self.max_body_size = max_body_size self.max_body_size = max_body_size
self.init_grpc_stub()
def set_timeout_ms(self, timeout_ms): def set_timeout_ms(self, timeout_ms):
if not isinstance(timeout_ms, int): if not isinstance(timeout_ms, int):
...@@ -183,25 +183,46 @@ class HttpClient(object): ...@@ -183,25 +183,46 @@ class HttpClient(object):
raise ValueError("retry_times must be int type.") raise ValueError("retry_times must be int type.")
else: else:
self.requests_session.mount( self.requests_session.mount(
'http://', HTTPAdapter(max_retries=retry_times)) self.http_s, HTTPAdapter(max_retries=retry_times))
def set_ip(self, ip):
self.ip = ip
self.init_grpc_stub()
def set_service_name(self, service_name): def set_service_name(self, service_name):
self.service_name = service_name self.service_name = service_name
def set_port(self, port): def connect(self, url=None, encryption=False):
self.port = port if isinstance(url, (list, tuple)):
self.server_port = port if len(url) > 1:
self.init_grpc_stub() raise ValueError("HttpClient only support 1 endpoint")
else:
def set_url(self, url): url = url[0]
if isinstance(url, str): if isinstance(url, str):
self.url = url if url.startswith("https://"):
url = url[8:]
self.http_s = "https://"
if url.startswith("http://"):
url = url[7:]
self.http_s = "http://"
url_parts = url.split(':')
if len(url_parts) != 2 or check_ip(url_parts[0]) == False:
raise ValueError(
"url not right, it should be like 127.0.0.1:9393 or http://127.0.0.1:9393"
)
else:
self.ip = url_parts[0]
self.port = url_parts[1]
self.server_port = url_parts[1]
if encryption:
self.get_serving_port()
if self.use_grpc_client:
self.init_grpc_stub()
def check_ip(ipAddr):
compile_ip = re.compile(
'^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$'
)
if compile_ip.match(ipAddr):
return True
else: else:
print("url must be str") return False
def add_http_headers(self, headers): def add_http_headers(self, headers):
if isinstance(headers, dict): if isinstance(headers, dict):
...@@ -229,10 +250,9 @@ class HttpClient(object): ...@@ -229,10 +250,9 @@ class HttpClient(object):
def use_key(self, key_filename): def use_key(self, key_filename):
with open(key_filename, "rb") as f: with open(key_filename, "rb") as f:
self.key = f.read() self.key = f.read()
self.get_serving_port()
def get_serving_port(self): def get_serving_port(self):
encrypt_url = "http://" + str(self.ip) + ":" + str(self.port) encrypt_url = self.http_s + str(self.ip) + ":" + str(self.port)
if self.key is not None: if self.key is not None:
req = json.dumps({"key": base64.b64encode(self.key).decode()}) req = json.dumps({"key": base64.b64encode(self.key).decode()})
else: else:
...@@ -481,13 +501,7 @@ class HttpClient(object): ...@@ -481,13 +501,7 @@ class HttpClient(object):
postData = self.process_json_data(feed_dict, fetch_list, batch, postData = self.process_json_data(feed_dict, fetch_list, batch,
log_id) log_id)
web_url = "http://" + self.ip + ":" + self.server_port + self.service_name web_url = self.http_s + self.ip + ":" + self.server_port + self.service_name
if self.url != None:
if "http" not in self.url:
self.url = "http://" + self.url
if "self.service_name" not in self.url:
self.url = self.url + self.service_name
web_url = self.url
# 当数据区长度大于512字节时才压缩. # 当数据区长度大于512字节时才压缩.
self.headers.pop("Content-Encoding", "nokey") self.headers.pop("Content-Encoding", "nokey")
try: try:
......
...@@ -228,7 +228,8 @@ class Server(object): ...@@ -228,7 +228,8 @@ class Server(object):
engine.batch_infer_size = self.op_max_batch[index % engine.batch_infer_size = self.op_max_batch[index %
len(self.op_max_batch)] len(self.op_max_batch)]
engine.enable_overrun = 1 engine.enable_overrun = False
engine.allow_split_request = True
engine.model_dir = model_config_path engine.model_dir = model_config_path
engine.enable_memory_optimization = self.memory_optimization engine.enable_memory_optimization = self.memory_optimization
engine.enable_ir_optimization = self.ir_optimization engine.enable_ir_optimization = self.ir_optimization
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册