Last active
May 20, 2022 17:09
-
-
Save philschmid/bef900bf481929613df0b5dbe9484d5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Development Environment and Permissions\n", | |
"\n", | |
"### Installation \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "69c59d90", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%pip install sagemaker --upgrade\n", | |
"import sagemaker\n", | |
"\n", | |
"assert sagemaker.__version__ >= \"2.75.0\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ce0ef431", | |
"metadata": {}, | |
"source": [ | |
"Install `git` and `git-lfs`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "96d8dfea", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# For notebook instances (Amazon Linux)\n", | |
"!sudo yum update -y \n", | |
"!curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | sudo bash\n", | |
"!sudo yum install git-lfs git -y\n", | |
"# For other environments (Ubuntu)\n", | |
"!sudo apt-get update -y \n", | |
"!curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash\n", | |
"!sudo apt-get install git-lfs git -y" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9e4386d9", | |
"metadata": {}, | |
"source": [ | |
"### Permissions\n", | |
"\n", | |
"_If you are going to use Sagemaker in a local environment (not SageMaker Studio or Notebook Instances). You need access to an IAM Role with the required permissions for Sagemaker. You can find [here](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html) more about it._" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "1c22e8d5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Couldn't call 'get_role' to get Role ARN from role name philippschmid to get Role path.\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"sagemaker role arn: arn:aws:iam::558105141721:role/sagemaker_execution_role\n", | |
"sagemaker bucket: sagemaker-us-east-1-558105141721\n", | |
"sagemaker session region: us-east-1\n" | |
] | |
} | |
], | |
"source": [ | |
"import sagemaker\n", | |
"import boto3\n", | |
"sess = sagemaker.Session()\n", | |
"# sagemaker session bucket -> used for uploading data, models and logs\n", | |
"# sagemaker will automatically create this bucket if it not exists\n", | |
"sagemaker_session_bucket=None\n", | |
"if sagemaker_session_bucket is None and sess is not None:\n", | |
" # set to default bucket if a bucket name is not given\n", | |
" sagemaker_session_bucket = sess.default_bucket()\n", | |
"\n", | |
"try:\n", | |
" role = sagemaker.get_execution_role()\n", | |
"except ValueError:\n", | |
" iam = boto3.client('iam')\n", | |
" role = iam.get_role(RoleName='sagemaker_execution_role')['Role']['Arn']\n", | |
"\n", | |
"sess = sagemaker.Session(default_bucket=sagemaker_session_bucket)\n", | |
"\n", | |
"print(f\"sagemaker role arn: {role}\")\n", | |
"print(f\"sagemaker bucket: {sess.default_bucket()}\")\n", | |
"print(f\"sagemaker session region: {sess.boto_region_name}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "952983b5", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"repository = \"jonatasgrosman/wav2vec2-large-xlsr-53-english\"\n", | |
"model_id=repository.split(\"/\")[-1]\n", | |
"s3_location=f\"s3://{sess.default_bucket()}/custom_inference/{model_id}/model.tar.gz\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "b8452981", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Updated git hooks.\n", | |
"Git LFS initialized.\n", | |
"Cloning into 'wav2vec2-large-xlsr-53-english'...\n", | |
"remote: Enumerating objects: 93, done.\u001b[K\n", | |
"remote: Counting objects: 100% (93/93), done.\u001b[K\n", | |
"remote: Compressing objects: 100% (88/88), done.\u001b[K\n", | |
"remote: Total 93 (delta 44), reused 0 (delta 0), pack-reused 0\u001b[K\n", | |
"Unpacking objects: 100% (93/93), 745.20 KiB | 1.57 MiB/s, done.\n", | |
"Filtering content: 100% (2/2), 2.35 GiB | 19.68 MiB/s, done.\n" | |
] | |
} | |
], | |
"source": [ | |
"!git lfs install\n", | |
"!git clone https://huggingface.co/$repository" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "6146af09", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"!cp -r code/ $model_id/code/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "e65fd56e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[Errno 2] No such file or directory: 'wav2vec2-large-xlsr-53-english'\n", | |
"/Users/philipp/Projects/huggingface/notebooks/sagemaker/17_custom_inference_script/wav2vec2-large-xlsr-53-english\n", | |
"a README.md\n", | |
"a code\n", | |
"a code/inference.py\n", | |
"a config.json\n", | |
"a preprocessor_config.json\n", | |
"a pytorch_model.bin\n", | |
"a special_tokens_map.json\n", | |
"a vocab.json\n" | |
] | |
} | |
], | |
"source": [ | |
"%cd $model_id\n", | |
"!tar zcvf model.tar.gz *" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "c581bc40", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"upload: ./model.tar.gz to s3://sagemaker-us-east-1-558105141721/custom_inference/wav2vec2-large-xlsr-53-english/model.tar.gz\n" | |
] | |
} | |
], | |
"source": [ | |
"!aws s3 cp model.tar.gz $s3_location\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "fc75b436", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"!mkdir code" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3965a632", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%%writefile code/inference.py\n", | |
"\n", | |
"from transformers import pipeline\n", | |
"\n", | |
"\n", | |
"def model_fn(model_dir):\n", | |
" pipe = pipeline(\"automatic-speech-recognition\", model=model_dir, chunk_length_s=10)\n", | |
" return pipe\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"-----!" | |
] | |
} | |
], | |
"source": [ | |
"from sagemaker.huggingface.model import HuggingFaceModel\n", | |
"from sagemaker.serializers import DataSerializer\n", | |
"from sagemaker.serverless import ServerlessInferenceConfig\n", | |
"\n", | |
"# Hub Model configuration. <https://huggingface.co/models>\n", | |
"hub = {\n", | |
" 'HF_MODEL_ID':'distilbert-base-uncased-finetuned-sst-2-english',\n", | |
" 'HF_TASK':'text-classification'\n", | |
"}\n", | |
"\n", | |
"# create Hugging Face Model Class\n", | |
"huggingface_model = HuggingFaceModel(\n", | |
" model_data=s3_location, # path to your trained sagemaker model\n", | |
" role=role, # iam role with permissions to create an Endpoint\n", | |
" transformers_version=\"4.17\", # transformers version used\n", | |
" pytorch_version=\"1.10\", # pytorch version used\n", | |
" py_version='py38', # python version used\n", | |
")\n", | |
"\n", | |
"# Specify MemorySizeInMB and MaxConcurrency in the serverless config object\n", | |
"serverless_config = ServerlessInferenceConfig(\n", | |
" memory_size_in_mb=4096, max_concurrency=10,\n", | |
")\n", | |
"\n", | |
"# create a serializer for the data\n", | |
"audio_serializer = DataSerializer(content_type='audio/x-audio') # using x-audio to support multiple audio formats\n", | |
"\n", | |
"# deploy the endpoint endpoint\n", | |
"predictor = huggingface_model.deploy(\n", | |
"serverless_inference_config=serverless_config,\n", | |
" serializer=audio_serializer, # serializer for our audio data.\n", | |
"\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b6b3812f", | |
"metadata": {}, | |
"source": [ | |
"## Request Inference Endpoint using the `HuggingfacePredictor`\n", | |
"\n", | |
"The `.deploy()` returns an `HuggingFacePredictor` object which can be used to request inference." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "b72ff640", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"--2022-05-20 19:06:10-- https://cdn-media.huggingface.co/speech_samples/sample1.flac\n", | |
"Auflösen des Hostnamens cdn-media.huggingface.co (cdn-media.huggingface.co)… 13.227.153.89, 13.227.153.26, 13.227.153.12, ...\n", | |
"Verbindungsaufbau zu cdn-media.huggingface.co (cdn-media.huggingface.co)|13.227.153.89|:443 … verbunden.\n", | |
"HTTP-Anforderung gesendet, auf Antwort wird gewartet … 200 OK\n", | |
"Länge: 282378 (276K) [audio/flac]\n", | |
"Wird in »sample1.flac« gespeichert.\n", | |
"\n", | |
"sample1.flac 100%[===================>] 275,76K --.-KB/s in 0,05s \n", | |
"\n", | |
"2022-05-20 19:06:10 (5,60 MB/s) - »sample1.flac« gespeichert [282378/282378]\n", | |
"\n", | |
"{'text': \"going along slashy country roads and speaking to damp audiences in drafty school rooms day-after-day for a fortnighthe'll have to put in an appearance at some place of worship on sunday morning and he can come\"}\n" | |
] | |
} | |
], | |
"source": [ | |
"!wget https://cdn-media.huggingface.co/speech_samples/sample1.flac\n", | |
"audio_path = \"sample1.flac\"\n", | |
"\n", | |
"res = predictor.predict(data=audio_path)\n", | |
"print(res)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "cb10007d", | |
"metadata": {}, | |
"source": [ | |
"### Delete model and endpoint\n", | |
"\n", | |
"To clean up, we can delete the model and endpoint." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"id": "1e6fb7b8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"predictor.delete_model()\n", | |
"predictor.delete_endpoint()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "bb862af7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"interpreter": { | |
"hash": "c281c456f1b8161c8906f4af2c08ed2c40c50136979eaae69688b01f70e9f4a9" | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment