Skip to content

Commit

Permalink
Update PyFunc notebook examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Arief Rahmansyah committed Jan 5, 2024
1 parent e8aa3cb commit 867119a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
50 changes: 44 additions & 6 deletions examples/pyfunc/Pyfunc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@
"outputs": [],
"source": [
"with merlin.new_model_version() as v: \n",
" merlin.log_pyfunc_model(model_instance=EnsembleModel(), \n",
" conda_env=\"env.yaml\", \n",
" artifacts={\"xgb_model\": model_1_path, \"sklearn_model\": model_2_path})"
" merlin.log_pyfunc_model(\n",
" model_instance=EnsembleModel(), \n",
" conda_env=\"env.yaml\", \n",
" artifacts={\"xgb_model\": model_1_path, \"sklearn_model\": model_2_path},\n",
" )"
]
},
{
Expand Down Expand Up @@ -386,17 +388,53 @@
"merlin.undeploy(v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4. Testing PyFunc Model Server Locally"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Up until this point, you have successfully built and run your PyFunc model on the Merlin server. To have faster development iteration, you can also simulate running the PyFunc model server in your local machine, by calling `merlin.run_pyfunc_model` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"merlin.run_pyfunc_model(\n",
" model_instance=EnsembleModel(), \n",
" conda_env=\"env.yaml\", \n",
" artifacts={\"xgb_model\": model_1_path, \"sklearn_model\": model_2_path},\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once the PyFunc model server is running, you can send the requests using curl from your terminal:\n",
"\n",
"```\n",
"curl -X POST \"http://localhost:8080/v1/models/ensemblemodel-dev:predict\" -d '{\n",
" \"instances\": [\n",
" [2.8, 1.0, 6.8, 0.4],\n",
" [3.1, 1.4, 4.5, 1.6]\n",
" ]\n",
"}'\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "merlin-sdk",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -410,7 +448,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
1 change: 0 additions & 1 deletion python/sdk/merlin/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
VersionApi,
)
from docker import APIClient
from docker.errors import BuildError
from docker.models.containers import Container
from merlin import pyfunc
from merlin.autoscaling import (
Expand Down
12 changes: 9 additions & 3 deletions python/sdk/merlin/pyfunc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import re
import shutil
from abc import abstractmethod
from dataclasses import dataclass
Expand All @@ -12,7 +11,6 @@
import pandas
from caraml.upi.v1 import upi_pb2
from docker import APIClient
from docker.errors import BuildError
from git import Repo
from merlin.docker.docker import copy_pyfunc_dockerfile, wait_build_complete
from merlin.protocol import Protocol
Expand Down Expand Up @@ -543,6 +541,15 @@ def _build_image(


def _run_container(image_tag, model_name, model_version, model_full_name, port):
docker_client = docker.from_env()

# Stop all previous containers to avoid port conflict
started_containers = docker_client.containers.list(
filters={"name": model_full_name}
)
for started_container in started_containers:
started_container.remove(force=True)

try:
env_vars = {}
env_vars["CARAML_HTTP_PORT"] = "8080"
Expand All @@ -551,7 +558,6 @@ def _run_container(image_tag, model_name, model_version, model_full_name, port):
env_vars["CARAML_MODEL_FULL_NAME"] = model_full_name
env_vars["WORKERS"] = "1"

docker_client = docker.from_env()
container = docker_client.containers.run(
image=image_tag,
name=model_name,
Expand Down

0 comments on commit 867119a

Please sign in to comment.