Skip to content

Commit

Permalink
[serve] Update code sample in Ray README (ray-project#18129)
Browse files Browse the repository at this point in the history
  • Loading branch information
edoakes committed Aug 26, 2021
1 parent 5c4c735 commit f16bb98
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,36 +230,38 @@ This example runs serves a scikit-learn gradient boosting classifier.

.. code-block:: python
from ray import serve
import pickle
import requests
from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier
# Train model
from ray import serve
serve.start()
# Train model.
iris_dataset = load_iris()
model = GradientBoostingClassifier()
model.fit(iris_dataset["data"], iris_dataset["target"])
# Define Ray Serve model,
@serve.deployment(route_prefix="/iris")
class BoostingModel:
def __init__(self):
def __init__(self, model):
self.model = model
self.label_list = iris_dataset["target_names"].tolist()
def __call__(self, flask_request):
payload = flask_request.json["vector"]
print("Worker: received flask request with data", payload)
async def __call__(self, request):
payload = await request.json()["vector"]
print(f"Received flask request with data {payload}")
prediction = self.model.predict([payload])[0]
human_name = self.label_list[prediction]
return {"result": human_name}
# Deploy model
client = serve.start()
client.create_backend("iris:v1", BoostingModel)
client.create_endpoint("iris_classifier", backend="iris:v1", route="/iris")
# Deploy model.
BoostingModel.deploy(model)
# Query it!
sample_request_input = {"vector": [1.2, 1.0, 1.1, 0.9]}
Expand Down

0 comments on commit f16bb98

Please sign in to comment.