{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 먼저 ping 함수가 잘 동작하는지 테스트를 진행합니다. (GET /ping)\n", "\n", "ping API는 SageMaker에서 사용할 모델이 정상적으로 로드되고 서비스가 정장적으로 동작하는지 헬스체크의 용도로 사용됩니다. 결과코드는 **200**이 리턴되어야 합니다. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import json\n", "from urllib import request\n", "\n", "base_url='http://localhost:8080'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Response code: 200\n" ] } ], "source": [ "resp = request.urlopen(\"%s/ping\" % base_url)\n", "print(\"Response code: %d\" % resp.getcode() )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 이제 예측을 실행해 봅니다. (POST /invocations)\n", "\n", "invocations API는 SageMaker를 통해 예측을 실행하는데 사용됩니다. \n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Response code: 200, Prediction: b'0.0\\n'\n", "\n", "content-type text/csv\n", "x-request-id ddaaf714-ff71-4544-a808-c0f14c83e45a\n", "Pragma no-cache\n", "Cache-Control no-cache; no-store, must-revalidate, private\n", "Expires Thu, 01 Jan 1970 00:00:00 UTC\n", "content-length 4\n", "connection keep-alive\n", "Response code: 200, Prediction: b'2.0\\n'\n", "\n", "content-type text/csv\n", "x-request-id f51fa946-45f0-4e44-9e19-e3bb67c30103\n", "Pragma no-cache\n", "Cache-Control no-cache; no-store, must-revalidate, private\n", "Expires Thu, 01 Jan 1970 00:00:00 UTC\n", "content-length 4\n", "connection keep-alive\n", "Response code: 200, Prediction: b'1.0\\n'\n", "\n", "content-type text/csv\n", "x-request-id 3127fc5f-5c9b-45cf-afac-161c27066aee\n", "Pragma no-cache\n", "Cache-Control no-cache; no-store, must-revalidate, private\n", "Expires Thu, 01 Jan 1970 00:00:00 UTC\n", "content-length 4\n", "connection keep-alive\n", "CPU times: user 1.25 s, sys: 936 ms, total: 2.18 s\n", "Wall time: 937 ms\n" ] } ], "source": [ "%%time\n", "from sagemaker.serializers import CSVSerializer\n", "csv_serializer = CSVSerializer()\n", "payloads = [\n", " [4.6, 3.1, 1.5, 0.2], # 0\n", " [7.7, 2.6, 6.9, 2.3], # 2\n", " [6.1, 2.8, 4.7, 1.2] # 1\n", "]\n", "\n", "def predict(payload):\n", " headers = {\n", " 'Content-type': 'text/csv',\n", " 'Accept': 'text/csv'\n", " }\n", " \n", " req = request.Request(\"%s/invocations\" % base_url, data=csv_serializer.serialize(payload).encode('utf-8'), headers=headers)\n", " resp = request.urlopen(req)\n", " print(\"Response code: %d, Prediction: %s\\n\" % (resp.getcode(), resp.read()))\n", " for i in resp.headers:\n", " print(i, resp.headers[i])\n", "\n", "for p in payloads:\n", " predict(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 테스트가 완료되면 1번 노트북으로 복귀하고 실행중인 셀을 멈춥니다. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }