Skip to content

Instantly share code, notes, and snippets.

@yashika51
Created July 26, 2020 01:14
Show Gist options
  • Save yashika51/7635e3120a92869658611d410f33e83e to your computer and use it in GitHub Desktop.
Save yashika51/7635e3120a92869658611d410f33e83e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting iris_classifier.py\n"
]
}
],
"source": [
"%%writefile iris_classifier.py\n",
"#writing script for BentoML Service Class\n",
"from bentoml import env,artifacts,api, BentoService\n",
"from bentoml.artifact import SklearnModelArtifact\n",
"from bentoml.adapters import DataframeInput\n",
"\n",
"@env(auto_pip_dependencies=True,\n",
" conda_dependencies=['bentoml'])\n",
"@artifacts([SklearnModelArtifact(\"model\")])\n",
"class IrisClassifier(BentoService):\n",
" @api(input=DataframeInput())\n",
" \n",
" def predict(self,df):\n",
" return self.artifact.model.predict(df)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting main.py\n"
]
}
],
"source": [
"%%writefile main.py\n",
"\n",
"from sklearn.svm import SVC\n",
"from sklearn.datasets import load_iris\n",
"from iris_classifier import IrisClassifier \n",
"\n",
"if __name__==\"__main__\":\n",
" #load data\n",
" iris=load_iris()\n",
" X,y=iris.data, iris.target\n",
"\n",
" #train model\n",
" clf=SVC()\n",
" clf.fit(X,y)\n",
"\n",
" #create an iris classifier service instance \n",
" service=IrisClassifier()\n",
"\n",
" #pack the newly trained service classifier\n",
" service.pack(\"model\",clf)\n",
"\n",
" # Save the prediction service to disk for model serving\n",
" saved_path=service.save()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2020-07-26 06:42:24,979] INFO - BentoService bundle 'IrisClassifier:20200726064151_392FB8' saved to: C:\\Users\\Yashika Sharma/bentoml/repository/IrisClassifier\\20200726064151_392FB8\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\Yashika Sharma\\anaconda3\\envs\\bento\\lib\\site-packages\\setuptools\\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.\n",
" warnings.warn(\n",
"C:\\Users\\Yashika Sharma\\AppData\\Roaming\\Python\\Python38\\site-packages\\traitlets\\config\\loader.py:795: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
" if len(key) is 1:\n",
"C:\\Users\\Yashika Sharma\\AppData\\Roaming\\Python\\Python38\\site-packages\\traitlets\\config\\loader.py:804: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n",
" if len(key) is 1:\n"
]
}
],
"source": [
"!python main.py\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\Yashika Sharma\\anaconda3\\envs\\bento\\lib\\site-packages\\setuptools\\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.\n",
" warnings.warn(\n"
]
},
{
"ename": "NameError",
"evalue": "name 'saved_path' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-4-c8234edf732c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mbentoml\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mloaded_iris\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mbentoml\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msaved_path\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'saved_path' is not defined"
]
}
],
"source": [
"import bentoml\n",
"loaded_iris=bentoml.load(saved_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!bentoml get IrisClassifier:latest"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!bentoml get --print-location IrisClassifier:latest"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!bentoml serve IrisClassifier:latest"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!cd {\"saved_path\"} && docker build --quiet -t iris-classifier-server ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.3 64-bit ('bento': conda)",
"language": "python",
"name": "python38364bitbentoconda77f3f14f5fe54a59bff1798af3e158d7"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@chienhsiang-hung
Copy link

thank you for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment