{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## First, let's test the ping method (GET /ping)\n", "\n", "This method will be used by Sagemaker for health check our model. It must return a code **200**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "from urllib import request\n", "\n", "base_url='http://localhost:8080'\n", "\n", "resp = request.urlopen(\"%s/ping\" % base_url)\n", "print(\"Response code: %d\" % resp.getcode() )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Then we can the predictions (POST /invocations)\n", "\n", "This method will be used by Sagemaker for the predictions. Here we're simulating the header parameter related to the CustomAttributes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "import pandas as pd\n", "\n", "def predict(test_df, content_type='text/csv', accept='text/csv'):\n", " headers = {\n", " 'Content-type': content_type,\n", " 'Accept': accept\n", " }\n", " \n", " if content_type == 'text/csv':\n", " payload = test_df.to_csv(index=False).encode('utf-8')\n", " else:\n", " payload = test_df.to_json().encode('utf-8')\n", " \n", " req = request.Request(\"%s/invocations\" % base_url, data=payload, headers=headers)\n", " resp = request.urlopen(req)\n", " body = resp.read().decode('utf-8')\n", " print(\"Response code: %d, Prediction: %s\\n\" % (resp.getcode(), body))\n", " for i in resp.headers:\n", " print(i, resp.headers[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the first 10 records from test dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get the first 10 records\n", "test_df = pd.read_csv('test.csv').head(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test all the different input and output formats" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "predict(test_df, 'text/csv', 'text/csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "predict(test_df, 'text/csv', 'application/json')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "predict(test_df, 'application/json', 'application/json')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "predict(test_df, 'application/json', 'text/csv')" ] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_python3" }, "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.6.10" } }, "nbformat": 4, "nbformat_minor": 4 }