Files
4525100cc9 Make ray an optional dependency (#3834)
* Make ray serve an optional dependency

Signed-off-by: Curtis Maddalozzo <[email protected]>
Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Unify the log configuration using kserve logger (#3577)

* Configure logging for serving runtimes

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Add pyyaml dependency

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* black format

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* fix pyproject.toml

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Rebase master

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* cleanup logger for e2e

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Modify logger format to include func name

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Log model download time.

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Rebase master

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Allow disabling logger configuration and deprecate logger related arg in model server

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Rebase master

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Resolve comments

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* pyyaml=^6.0.0 to fix build failure

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Remove logger related parameters from model server

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

---------

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* import model_server

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Fix lint

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Fix linting

Signed-off-by: Curtis Maddalozzo <[email protected]>
Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

* Rebase, minor fixes and add e2e test

Signed-off-by: Sivanantham Chinnaiyan <[email protected]>

---------

Signed-off-by: Curtis Maddalozzo <[email protected]>
Signed-off-by: Sivanantham Chinnaiyan <[email protected]>
Co-authored-by: Curtis Maddalozzo <[email protected]>
Co-authored-by: Dan Sun <[email protected]>
2024-08-11 12:08:48 -04:00

87 lines
2.7 KiB
Python

# Copyright 2024 The KServe Authors.
#
# 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.
import argparse
from torchvision import models, transforms
from typing import Dict
import torch
from PIL import Image
import base64
import io
from ray import serve
from kserve import Model, ModelServer, logging, model_server
from kserve.ray import RayModel
# the model handle name should match the model endpoint name
@serve.deployment(name="custom-model", num_replicas=1)
class AlexNetModel(Model):
def __init__(self):
self.name = "custom-model"
super().__init__(self.name)
self.model = None
self.ready = False
self.load()
def load(self):
self.model = models.alexnet(pretrained=True, progress=False)
self.model.eval()
self.ready = True
async def predict(self, payload: Dict, headers: Dict[str, str] = None) -> Dict:
inputs = payload["instances"]
# Input follows the Tensorflow V1 HTTP API for binary values
# https://www.tensorflow.org/tfx/serving/api_rest#encoding_binary_values
data = inputs[0]["image"]["b64"]
raw_img_data = base64.b64decode(data)
input_image = Image.open(io.BytesIO(raw_img_data))
preprocess = transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0)
output = self.model(input_batch)
torch.nn.functional.softmax(output, dim=1)
values, top_5 = torch.topk(output, 5)
return {"predictions": values.tolist()}
parser = argparse.ArgumentParser(parents=[model_server.parser])
args, _ = parser.parse_known_args()
if __name__ == "__main__":
if args.configure_logging:
logging.configure_logging(args.log_config_file)
app = AlexNetModel.bind()
handle = serve.run(app)
model = RayModel(name="custom-model", handle=handle)
model.load()
ModelServer().start([model])