Skip to content

Commit

Permalink
Client library - simplify make_client signature (openvinotoolkit#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzegla committed Nov 18, 2021
1 parent 1bb0127 commit a5c751e
Show file tree
Hide file tree
Showing 20 changed files with 707 additions and 720 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,9 @@ test_functional: venv
# This fact is used in test_client_lib, where make build runs in .venv Python 3 environment
test_client_lib:
@cd client/python/lib && \
make style && \
. .venv/bin/activate; make build && \
make test && \
make style || exit 1 && \
. .venv/bin/activate; make build || exit 1 && \
make test || exit 1 && \
make clean

tools_get_deps:
Expand Down
2 changes: 1 addition & 1 deletion client/python/lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ VIRTUALENV_EXE := python3 -m virtualenv -p python3
VIRTUALENV_DIR := .venv
ACTIVATE="$(VIRTUALENV_DIR)/bin/activate"

PACKAGE_PATH := dist/ovmsclient-0.1-py3-none-any.whl
PACKAGE_PATH := dist/ovmsclient-0.2-py3-none-any.whl

.PHONY: build-deps build-package build test clean

Expand Down
4 changes: 2 additions & 2 deletions client/python/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Assuming you have TFS API built, you can use `make build-package` target to buil

**To install the package run:**

`pip install dist/ovmsclient-0.1-py3-none-any.whl`
`pip install dist/ovmsclient-0.2-py3-none-any.whl`

*Note*: For development purposes you may want to repeatedly reinstall the package.
For that consider using `pip install` with `--force-reinstall` and `--no-deps` options.
Expand All @@ -42,7 +42,7 @@ Apart from `make build`, there are also other targets available:
- `make build-package` - builds only `ovmsclient` package (requires TFS API protos compiled)
- `make test` - runs tests on `ovmsclient` package. By default the package located in `dist/` directory is used. To specify custom package path pass `PACKAGE_PATH` option like:

`make test PACKAGE_PATH=/opt/packages/ovmsclient-0.1-py3-none-any.whl`
`make test PACKAGE_PATH=/opt/packages/ovmsclient-0.2-py3-none-any.whl`

- `make clean` - removes all intermediate files generated while building the package

Expand Down
39 changes: 19 additions & 20 deletions client/python/lib/ovmsclient/tfs_compat/base/serving_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,40 +107,39 @@ def _open_private_key(cls, key_path):
key = f.read()
return key

@classmethod
def _check_config(cls, config):

if 'address' not in config or 'port' not in config:
raise ValueError('The minimal config must contain address and port')

cls._check_address(config['address'])

cls._check_port(config['port'])

if 'tls_config' in config:
cls._check_tls_config(config['tls_config'])

@classmethod
def _check_address(cls, address):

if not isinstance(address, str):
raise TypeError(f'address type should be string, but is {type(address).__name__}')

if address != "localhost" and not ipv4(address) and not domain(address):
raise ValueError('address is not valid')

@classmethod
def _check_port(cls, port):

if not isinstance(port, int):
raise TypeError(f'port type should be int, but is type {type(port).__name__}')
try:
port = int(port)
except Exception:
raise TypeError('port should be of type int')

if port.bit_length() > 16 or port < 0:
raise ValueError(f'port should be in range <0, {2**16-1}>')

@classmethod
def _check_url(cls, url):
if isinstance(url, str):
try:
[address, port] = url.split(":", 1)
except ValueError:
raise ValueError("url must be a string in format <address>:<port>")
else:
raise TypeError("url must be a string in format <address>:<port>")
cls._check_address(address)
cls._check_port(port)

@classmethod
def _check_tls_config(cls, tls_config):

if not isinstance(tls_config, dict):
raise TypeError('tls_config should be of type dict')

if 'server_cert_path' not in tls_config:
raise ValueError('server_cert_path is not defined in tls_config')

Expand Down
26 changes: 12 additions & 14 deletions client/python/lib/ovmsclient/tfs_compat/grpc/serving_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,22 @@ def get_model_status(self, request):
return GrpcModelStatusResponse(raw_response)

@classmethod
def _build(cls, config):
def _build(cls, url, tls_config):

ServingClient._check_config(config)
ServingClient._check_url(url)

grpc_address = f"{config['address']}:{config['port']}"

if 'tls_config' in config:
if tls_config is not None:
ServingClient._check_tls_config(tls_config)
server_cert, client_cert, client_key = ServingClient._prepare_certs(
config['tls_config'].get('server_cert_path'),
config['tls_config'].get('client_cert_path'),
config['tls_config'].get('client_key_path')
tls_config.get('server_cert_path'),
tls_config.get('client_cert_path'),
tls_config.get('client_key_path')
)

creds = ssl_channel_credentials(root_certificates=server_cert,
private_key=client_key, certificate_chain=client_cert)

channel = secure_channel(grpc_address, creds)
channel = secure_channel(url, creds)
else:
channel = insecure_channel(grpc_address)
channel = insecure_channel(url)

prediction_service_stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
model_service_stub = model_service_pb2_grpc.ModelServiceStub(channel)
Expand Down Expand Up @@ -235,7 +232,7 @@ def _check_predict_request(cls, request):


@ovmsclient_export("make_grpc_client", grpcclient="make_client")
def make_grpc_client(config):
def make_grpc_client(url, tls_config=None):
'''
Create GrpcClient object.
Expand Down Expand Up @@ -297,4 +294,5 @@ def make_grpc_client(config):
>>> client = make_grpc_client(config)
>>> print(client)
'''
return GrpcClient._build(config)

return GrpcClient._build(url, tls_config)
27 changes: 12 additions & 15 deletions client/python/lib/ovmsclient/tfs_compat/http/serving_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@

class HttpClient(ServingClient):

def __init__(self, address, port, session, client_key=None, server_cert=None):
self.address = address
self.port = port
def __init__(self, url, session, client_key=None, server_cert=None):
self.url = url
self.session = session
self.client_key = client_key
self.server_cert = server_cert
Expand Down Expand Up @@ -65,7 +64,7 @@ def predict(self, request):

raw_response = None
try:
raw_response = self.session.post(f"http://{self.address}:{self.port}"
raw_response = self.session.post(f"http://{self.url}"
f"/v1/models/{request.model_name}"
f"/versions/{request.model_version}:predict",
data=request.parsed_inputs,
Expand Down Expand Up @@ -106,7 +105,7 @@ def get_model_metadata(self, request):

raw_response = None
try:
raw_response = self.session.get(f"http://{self.address}:{self.port}"
raw_response = self.session.get(f"http://{self.url}"
f"/v1/models/{request.model_name}"
f"/versions/{request.model_version}"
f"/metadata",
Expand Down Expand Up @@ -147,7 +146,7 @@ def get_model_status(self, request):

raw_response = None
try:
raw_response = self.session.get(f"http://{self.address}:{self.port}"
raw_response = self.session.get(f"http://{self.url}"
f"/v1/models/{request.model_name}"
f"/versions/{request.model_version}",
cert=self.client_key, verify=self.server_cert)
Expand All @@ -158,19 +157,17 @@ def get_model_status(self, request):
return HttpModelStatusResponse(raw_response)

@classmethod
def _build(cls, config):
ServingClient._check_config(config)
address = config.get("address")
port = config.get("port")
def _build(cls, url, tls_config):
ServingClient._check_url(url)
client_cert = None
server_cert = None
if "tls_config" in config:
tls_config = config["tls_config"]
if tls_config is not None:
ServingClient._check_tls_config(tls_config)
if "client_cert_path" in tls_config and "client_key_path" in tls_config:
client_cert = (tls_config["client_cert_path"], tls_config["client_key_path"])
server_cert = tls_config.get('server_cert_path', None),
session = requests.Session()
return cls(address, port, session, client_cert, server_cert)
return cls(url, session, client_cert, server_cert)

@classmethod
def _check_model_status_request(cls, request):
Expand All @@ -192,7 +189,7 @@ def _check_predict_request(cls, request):


@ovmsclient_export("make_http_client", httpclient="make_client")
def make_http_client(config):
def make_http_client(url, tls_config=None):
'''
Create HttpClient object.
Expand Down Expand Up @@ -254,4 +251,4 @@ def make_http_client(config):
>>> client = make_http_client(config)
>>> print(client)
'''
return HttpClient._build(config)
return HttpClient._build(url, tls_config)
2 changes: 2 additions & 0 deletions client/python/lib/scripts/build_ovmsclient_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
# limitations under the License.
#

set -e

python scripts/generate_api.py
mv __init__.py ovmsclient/
2 changes: 2 additions & 0 deletions client/python/lib/scripts/build_tfs_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# limitations under the License.
#

set -e

git clone --branch v2.5.0 --depth 1 https://github.com/tensorflow/tensorflow.git tf
git clone --branch 2.5.1 --depth 1 https://github.com/tensorflow/serving.git tfs

Expand Down
6 changes: 3 additions & 3 deletions client/python/lib/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ def finalize_options(self):
...

def build_tfs_api(self):
subprocess.run(["sh", "./scripts/build_tfs_api.sh"])
subprocess.run(["sh", "./scripts/build_tfs_api.sh"], check=True)

def build_ovmsclient_api(self):
subprocess.run(["sh", "./scripts/build_ovmsclient_api.sh"])
subprocess.run(["sh", "./scripts/build_ovmsclient_api.sh"], check=True)

def run(self):
self.build_tfs_api()


setuptools.setup(
name='ovmsclient',
version='0.1',
version='0.2',
scripts=[] ,
author="Intel",
author_email="[email protected]",
Expand Down
Loading

0 comments on commit a5c751e

Please sign in to comment.