{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "fdad1f55", "metadata": {}, "outputs": [], "source": [ "from sagemaker.huggingface import HuggingFaceModel\n", "import sagemaker\n", "\n", "role = sagemaker.get_execution_role()" ] }, { "cell_type": "code", "execution_count": 3, "id": "8a691203", "metadata": {}, "outputs": [], "source": [ "# Hub Model configuration. https://huggingface.co/models\n", "hub = {\n", " 'HF_MODEL_ID':'distilbert-base-uncased',\n", " 'HF_TASK':'text-classification'\n", "}\n", "\n", "# create Hugging Face Model Class\n", "huggingface_model = HuggingFaceModel(\n", " transformers_version='4.17.0',\n", " pytorch_version='1.10.2',\n", " py_version='py38',\n", " env=hub,\n", " role=role, \n", ")" ] }, { "cell_type": "code", "execution_count": 4, "id": "b9624bf4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-----!" ] } ], "source": [ "# deploy model to SageMaker Inference\n", "predictor = huggingface_model.deploy(\n", " initial_instance_count=1, # number of instances\n", " instance_type='ml.m5.12xlarge' # ec2 instance type\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "id": "2b24b113", "metadata": {}, "outputs": [], "source": [ "endpoint_name = predictor.endpoint_name" ] }, { "cell_type": "code", "execution_count": 8, "id": "5a01dd78", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'label': 'LABEL_0', 'score': 0.528241753578186}]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "predictor.predict({\n", " 'inputs': \"I am super happy right now.\"\n", "})" ] }, { "cell_type": "code", "execution_count": 9, "id": "1270b5dd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'[{\"label\":\"LABEL_1\",\"score\":0.507586658000946}]'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import boto3\n", "import json\n", "client = boto3.client('sagemaker-runtime')\n", "content_type = \"application/json\"\n", "request_body = {'inputs': \"I am super happy right now.\"}\n", "data = json.loads(json.dumps(request_body))\n", "payload = json.dumps(data)\n", "response = client.invoke_endpoint(\n", " EndpointName=predictor.endpoint_name,\n", " ContentType=content_type,\n", " Body=payload)\n", "result = response['Body'].read()\n", "result" ] }, { "cell_type": "code", "execution_count": 10, "id": "10122976", "metadata": {}, "outputs": [], "source": [ "#!pip install locust" ] }, { "cell_type": "code", "execution_count": 15, "id": "4c405d54", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/ec2-user/anaconda3/envs/python3/bin/locust\n" ] } ], "source": [ "!which locust" ] }, { "cell_type": "code", "execution_count": null, "id": "4aeee5b8", "metadata": {}, "outputs": [], "source": [ "%%bash -s \"$endpoint_name\"\n", "cd locust\n", "./distributed.sh $1" ] }, { "cell_type": "code", "execution_count": null, "id": "fcd93aa5", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "locust_data = pd.read_csv('locust/results_stats.csv')\n", "for index, row in locust_data.head(n=2).iterrows():\n", " print(index, row)" ] } ], "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.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }