Skip to content

Instantly share code, notes, and snippets.

@philschmid
Created May 6, 2022 05:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philschmid/a8bc466fa20a7bd2044d92f7aeb38888 to your computer and use it in GitHub Desktop.
Save philschmid/a8bc466fa20a7bd2044d92f7aeb38888 to your computer and use it in GitHub Desktop.
from sagemaker.huggingface.model import HuggingFaceModel
from sagemaker.serverless import ServerlessInferenceConfig
from sagemaker.serializers import DataSerializer
import sagemaker
import boto3
try:
role = sagemaker.get_execution_role()
except ValueError:
iam = boto3.client('iam')
role = iam.get_role(RoleName='sagemaker_execution_role')['Role']['Arn']
# Hub Model configuration. <https://huggingface.co/models>
hub = {
'HF_MODEL_ID':'facebook/wav2vec2-base-960h',
'HF_TASK':'automatic-speech-recognition',
}
# create Hugging Face Model Class
huggingface_model_sls = HuggingFaceModel(
env=hub, # configuration for loading model from Hub
role=role, # iam role with permissions to create an Endpoint
transformers_version="4.17", # transformers version used
pytorch_version="1.10", # pytorch version used
py_version='py38', # python version used
)
# deploy the endpoint endpoint
predictor_sls = huggingface_model_sls.deploy(
serverless_inference_config=ServerlessInferenceConfig(memory_size_in_mb=4096, max_concurrency=10),
serializer=DataSerializer(content_type='audio/x-audio'), # serializer for our audio data.
)
# download sample audio file
# !wget https://cdn-media.huggingface.co/speech_samples/sample1.flac
# call inference with path to audio file
res = predictor_sls.predict(data="sample1.flac")
print(res)
# {'text': "GOING ALONG SLUSHY COUNTRY ROADS AND SPEAKING TO DAMP AUDIENCES IN DRAUGHTY SCHOOL ROOMS DAY AFTER DAY FOR A FORTNIGHT HE'LL HAVE TO PUT IN AN APPEARANCE AT SOME PLACE OF WORSHIP ON SUNDAY MORNING AND HE CAN COME TO US IMMEDIATELY AFTERWARDS"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment