├── Churn_model.ipynb ├── README.md ├── input.json ├── predict_setup.ipynb ├── predictor.py ├── serving.ipynb ├── setup.py └── telecom_customer_churn.csv /README.md: -------------------------------------------------------------------------------- 1 | # google_cloud_notebooks -------------------------------------------------------------------------------- /input.json: -------------------------------------------------------------------------------- 1 | {"instances":["7317-GGVPB", "Male", 0, "Yes", "No", 71, "Yes", "Yes", "Fiber optic", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "Two year", "Yes", "Credit card (automatic)", 108.6, "7690.9"]} -------------------------------------------------------------------------------- /predict_setup.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Writing predictor.py\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "%%writefile predictor.py\n", 18 | "\n", 19 | "import os\n", 20 | "\n", 21 | "import numpy as np\n", 22 | "import joblib\n", 23 | "import pandas as pd\n", 24 | "\n", 25 | "class ChurnPredictor(object):\n", 26 | "\n", 27 | " _COLUMN_NAMES=['customerID', 'gender', 'SeniorCitizen', 'Partner', 'Dependents', 'tenure', 'PhoneService', 'MultipleLines', 'InternetService', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport', 'StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling','PaymentMethod', 'MonthlyCharges', 'TotalCharges']\n", 28 | "\n", 29 | " def __init__(self, model):\n", 30 | " self._model = model\n", 31 | "\n", 32 | " def predict(self, instances, **kwargs):\n", 33 | " inputs = pd.DataFrame(data=[instances], columns=self._COLUMN_NAMES)\n", 34 | " outputs = self._model.predict(inputs)\n", 35 | " return outputs.tolist()\n", 36 | "\n", 37 | " @classmethod\n", 38 | " def from_path(cls, model_dir):\n", 39 | " model_path = os.path.join(model_dir, 'model.joblib')\n", 40 | " model = joblib.load(model_path)\n", 41 | " return cls(model)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "['No']\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "from predictor import ChurnPredictor\n", 59 | "model = ChurnPredictor.from_path('.')\n", 60 | "instance = ['7317-GGVPB', 'Male', 0, 'Yes', 'No', 71, 'Yes', 'Yes', 'Fiber optic', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Two year', 'Yes', 'Credit card (automatic)', 108.6, '7690.9']\n", 61 | "print(model.predict(instance))" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "Writing setup.py\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "%%writefile setup.py\n", 79 | "from setuptools import setup\n", 80 | "from setuptools import find_packages\n", 81 | "\n", 82 | "REQUIRED_PACKAGES = ['xgboost']\n", 83 | "\n", 84 | "setup(\n", 85 | " name='custom_predict',\n", 86 | " version='0.1',\n", 87 | " install_requires=REQUIRED_PACKAGES,\n", 88 | " packages=find_packages(),\n", 89 | " include_package_data=True,\n", 90 | " scripts=['predictor.py'])" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "running sdist\n", 103 | "running egg_info\n", 104 | "creating custom_predict.egg-info\n", 105 | "writing custom_predict.egg-info/PKG-INFO\n", 106 | "writing dependency_links to custom_predict.egg-info/dependency_links.txt\n", 107 | "writing requirements to custom_predict.egg-info/requires.txt\n", 108 | "writing top-level names to custom_predict.egg-info/top_level.txt\n", 109 | "writing manifest file 'custom_predict.egg-info/SOURCES.txt'\n", 110 | "reading manifest file 'custom_predict.egg-info/SOURCES.txt'\n", 111 | "writing manifest file 'custom_predict.egg-info/SOURCES.txt'\n", 112 | "running check\n", 113 | "warning: check: missing required meta-data: url\n", 114 | "\n", 115 | "warning: check: missing meta-data: either (author and author_email) or (maintainer and maintainer_email) must be supplied\n", 116 | "\n", 117 | "creating custom_predict-0.1\n", 118 | "creating custom_predict-0.1/custom_predict.egg-info\n", 119 | "copying files to custom_predict-0.1...\n", 120 | "copying README.md -> custom_predict-0.1\n", 121 | "copying predictor.py -> custom_predict-0.1\n", 122 | "copying setup.py -> custom_predict-0.1\n", 123 | "copying custom_predict.egg-info/PKG-INFO -> custom_predict-0.1/custom_predict.egg-info\n", 124 | "copying custom_predict.egg-info/SOURCES.txt -> custom_predict-0.1/custom_predict.egg-info\n", 125 | "copying custom_predict.egg-info/dependency_links.txt -> custom_predict-0.1/custom_predict.egg-info\n", 126 | "copying custom_predict.egg-info/requires.txt -> custom_predict-0.1/custom_predict.egg-info\n", 127 | "copying custom_predict.egg-info/top_level.txt -> custom_predict-0.1/custom_predict.egg-info\n", 128 | "Writing custom_predict-0.1/setup.cfg\n", 129 | "creating dist\n", 130 | "Creating tar archive\n", 131 | "removing 'custom_predict-0.1' (and everything under it)\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "!python setup.py sdist --formats=gztar" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 5, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "Copying file://./dist/custom_predict-0.1.tar.gz [Content-Type=application/x-tar]...\n", 149 | "/ [1 files][ 1.2 KiB/ 1.2 KiB] \n", 150 | "Operation completed over 1 objects/1.2 KiB. \n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "!gsutil cp ./dist/custom_predict-0.1.tar.gz gs://churn-model-sri/custom_predict-0.1.tar.gz" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 6, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "Using endpoint [https://ml.googleapis.com/]\n", 175 | "Created ml engine model [projects/srivatsan-project/models/ChurnPredictor].\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "!gcloud beta ai-platform models create ChurnPredictor --regions us-central1 --enable-console-logging" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 7, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "Using endpoint [https://ml.googleapis.com/]\n", 193 | "Creating version (this might take a few minutes)......done. \n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "! gcloud --quiet beta ai-platform versions create V1 \\\n", 199 | " --model ChurnPredictor \\\n", 200 | " --runtime-version 2.3 \\\n", 201 | " --python-version 3.7 \\\n", 202 | " --origin gs://churn-model-sri/ \\\n", 203 | " --package-uris gs://churn-model-sri/custom_predict-0.1.tar.gz \\\n", 204 | " --prediction-class predictor.ChurnPredictor " 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": null, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "#{\"instances\": [\"7317-GGVPB\", \"Male\", 0, \"Yes\", \"No\", 71, \"Yes\", \"Yes\", \"Fiber optic\", \"No\", \"Yes\", \"Yes\", \"Yes\", \"Yes\", \"Yes\", \"Two year\", \"Yes\", \"Credit card (automatic)\", 108.6, \"7690.9\"]}" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 8, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "Using endpoint [https://ml.googleapis.com/]\n", 226 | "['No']\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "!gcloud ai-platform predict --model ChurnPredictor --version V1 --json-request input.json" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [] 240 | } 241 | ], 242 | "metadata": { 243 | "environment": { 244 | "name": "common-cpu.m59", 245 | "type": "gcloud", 246 | "uri": "gcr.io/deeplearning-platform-release/base-cpu:m59" 247 | }, 248 | "kernelspec": { 249 | "display_name": "Python 3", 250 | "language": "python", 251 | "name": "python3" 252 | }, 253 | "language_info": { 254 | "codemirror_mode": { 255 | "name": "ipython", 256 | "version": 3 257 | }, 258 | "file_extension": ".py", 259 | "mimetype": "text/x-python", 260 | "name": "python", 261 | "nbconvert_exporter": "python", 262 | "pygments_lexer": "ipython3", 263 | "version": "3.7.8" 264 | } 265 | }, 266 | "nbformat": 4, 267 | "nbformat_minor": 4 268 | } 269 | -------------------------------------------------------------------------------- /predictor.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | 4 | import numpy as np 5 | import joblib 6 | import pandas as pd 7 | 8 | class ChurnPredictor(object): 9 | 10 | _COLUMN_NAMES=['customerID', 'gender', 'SeniorCitizen', 'Partner', 'Dependents', 'tenure', 'PhoneService', 'MultipleLines', 'InternetService', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport', 'StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling','PaymentMethod', 'MonthlyCharges', 'TotalCharges'] 11 | 12 | def __init__(self, model): 13 | self._model = model 14 | 15 | def predict(self, instances, **kwargs): 16 | inputs = pd.DataFrame(data=[instances], columns=self._COLUMN_NAMES) 17 | outputs = self._model.predict(inputs) 18 | return outputs.tolist() 19 | 20 | @classmethod 21 | def from_path(cls, model_dir): 22 | model_path = os.path.join(model_dir, 'model.joblib') 23 | model = joblib.load(model_path) 24 | return cls(model) 25 | -------------------------------------------------------------------------------- /serving.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import googleapiclient.discovery" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "instances = [\"7317-GGVPB\", \"Male\", 0, \"Yes\", \"No\", 71, \"Yes\", \"Yes\", \"Fiber optic\", \"No\", \"Yes\", \"Yes\", \"Yes\", \"Yes\", \"Yes\", \"Two year\", \"Yes\", \"Credit card (automatic)\", 108.6, \"7690.9\"]" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "service = googleapiclient.discovery.build('ml', 'v1')\n", 28 | "name = 'projects/{}/models/{}/versions/{}'.format(\"srivatsan-project\", \"ChurnPredictor\", \"V1\")" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 4, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "['No']\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "response = service.projects().predict(\n", 46 | " name=name,\n", 47 | " body={'instances': instances}\n", 48 | ").execute()\n", 49 | "\n", 50 | "if 'error' in response:\n", 51 | " raise RuntimeError(response['error'])\n", 52 | "else:\n", 53 | " print(response['predictions'])" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "#end" 63 | ] 64 | } 65 | ], 66 | "metadata": { 67 | "environment": { 68 | "name": "common-cpu.m59", 69 | "type": "gcloud", 70 | "uri": "gcr.io/deeplearning-platform-release/base-cpu:m59" 71 | }, 72 | "kernelspec": { 73 | "display_name": "Python 3", 74 | "language": "python", 75 | "name": "python3" 76 | }, 77 | "language_info": { 78 | "codemirror_mode": { 79 | "name": "ipython", 80 | "version": 3 81 | }, 82 | "file_extension": ".py", 83 | "mimetype": "text/x-python", 84 | "name": "python", 85 | "nbconvert_exporter": "python", 86 | "pygments_lexer": "ipython3", 87 | "version": "3.7.8" 88 | } 89 | }, 90 | "nbformat": 4, 91 | "nbformat_minor": 4 92 | } 93 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | from setuptools import find_packages 3 | 4 | REQUIRED_PACKAGES = ['xgboost'] 5 | 6 | setup( 7 | name='custom_predict', 8 | version='0.1', 9 | install_requires=REQUIRED_PACKAGES, 10 | packages=find_packages(), 11 | include_package_data=True, 12 | scripts=['predictor.py']) 13 | --------------------------------------------------------------------------------