Automatic Speech Recognition with AWS Lambda and Leopard
Tags: 100daysofcode, challenge, aws, serverless
Published: 2023-02-01 16:22:09

Day 23 is the day to show how to run an ASR (Automatic Speech Recognition) with Picovoice [Leopard Speech-to-Text](https://picovoice.ai/platform/cat/) and AWS Lambda.

**Pre-requisites**

- Register for an AWS account. We use it to access Amazon Lambda and API Gateway services via the console.
- Sign up for [Picovoice Console](https://console.picovoice.ai/) to get your AccessKey for free. We will need an AccessKey to use Leopard Speech-to-Text engine.
- Download [Postman](https://www.postman.com/downloads/). We will use this tool for testing.

**Setting up Lambda**
1. In AWS Console search for Lambda and then go to the Functions tab.
2. Press Create a Function. Then set the function name and set the runtime to Python 3.9.
3. Download the zip file from the [GitHub repository](https://github.com/Picovoice/serverless-leopard/blob/main/serverless_leopard.zip) which contains the [Lambda handler](https://github.com/Picovoice/serverless-leopard/blob/main/serverless_leopard/lambda_function.py) and packaged `[pvleopard](https://pypi.org/project/pvleopard/)` module.

The Lambda handler does the following:

- Gets and parses the data from the request.
- Saves the audio data into a temporary file.
- Transcribes and cleans up the temporary file.
- Returns the transcription.

4. Press on Upload from > .zip file and upload the zip file.
5. Once the function is uploaded, go to Configurations > General configuration tab. Press on Edit.
6. Set the memory limit to 512 MB and timeout to 30 seconds and press Save.

7. Go to Environment variables tab and press on Edit.
8. Add your `AccessKey` that you obtained from [Picovoice Console](https://console.picovoice.ai/) and press Save.
9. Press on Copy ARN (button located in the top right corner) and save your function ARN. We will need to set up API Gateway later. You may also come back later to copy your function ARN.


**Setting up API Gateway**
1. In AWS Console, go to API Gateway, and go to REST API section. Press on Build to create a new Rest API.

2. Select Rest as the protocol, New API, and give a name to your API. Press Create API to create your API.
3. Press Actions > Create Method and select POST as your method. Select Lambda Function as the integration type, tick on Proxy Integration, and copy your Lambda function ARN saved from before.
4. Once created, go to Settings tab.
5. Scroll all the way down. In Binary Media Types press on Add Binary Media Type, add multipart/form-data and press on Save Changes.
6. Go back to Resources tab. Press on Actions > Deploy API. Set [New Stage] as the deployment stage, give it a stage name and press Deploy.


You will be redirected to Stages and your API URL will be shown. Now we can test our rest API.

**Testing the API**
Use [Postman](https://www.postman.com/downloads/) to send an audio file and get the transcription.

1. Copy and paste the invoke URL from API Gateway in the request URL section and set the request type to POST.
2. In the Body tab, set the data type to form-data. Set the key name to audio_file, set the type as File, and press on Select Files and select the audio file you want to transcribe (Leopard supports the following audio formats: FLAC, MP3, Ogg, Opus, Vorbis, WAV, and WebM).
3. Once the request goes through, the result will show below.

In AWS Console, go to CloudWatch > Log groups if you would like more details on a specific API request and Lambda invocation.

**Limitations & Future Explorations**
API Gateway has a 29-second timeout and 10MB payload size limit. Lambda has a 15-minute timeout and a 6MB payload size limit.

Later, you can try to overcome these limitations:
- To transcribe audio files larger than 4.5MB, use S3 to get a pre-signed upload URL, upload your audio file, fetch it in Lambda, and process it.
- To transcribe audio files greater than 2 minutes (longer processing time), use Asynchronous Lambda to ping and get the response when the transcription finishes.

Keep in mind this is a starter code, you can always modify the [code](https://github.com/Picovoice/serverless-leopard), re-zip, and upload the zip file again to update your function.

Take a look at [Leopard GitHub Repository](https://github.com/Picovoice/leopard) or [Leopard Docs Page](https://picovoice.ai/docs/leopard/) to learn more about Leopard.

This post was originally published on [Picovoice's Medium](https://medium.com/picovoice/serverless-speech-to-text-5258e05f7031) page.