Regular recognition of audio files from Yandex Object Storage
The SpeechKit asynchronous recognition API is integrated with Yandex Object Storage. This enables you to set up automatic recognition of audio files of supported formats that are regularly uploaded to an Object Storage bucket. A cloud function in Yandex Cloud Functions regularly checks the bucket for audio files and sends them to the SpeechKit API for recognition. The recognition result and status are saved to the same Object Storage bucket.
To set up automatic audio file recognition using SpeechKit:
- Create a cloud function to read files from your Object Storage bucket, send them to the API, and check the file recognition status.
- Create a trigger to regularly invoke your cloud function.
- Test the function.
Getting started
-
Create a service account named
asr-batch-sa
. -
Assign to the service account the
storage.editor
,functions.functionInvoker
, andai.speechkit-stt.user
roles for the folder in which it was created. -
Create a static access key for the service account.
-
Create an API key to access the service account.
-
Create an Object Storage bucket named
asr-batch-bucket
in the service account folder. -
Open
asr-batch-bucket
, click Create folder, and set the Folder name field toinput
. -
Upload a
config.json
file with a set recognition language to the bucket'sinput
folder. The file only contains one parameter:{ "lang": "<language_code>" }
Note
If there is no
config.json
file in the bucket, the recognition language is Russian.
Create a cloud function
-
In the management console
, navigate to the folder with the new service account. -
Select Cloud Functions.
-
Click Create function and specify
asr-batch-function
as the function name. -
Click Create.
-
Under Editor, select
Python
3.8
as the runtime environment and click Continue. -
Download a script file
from the Yandex Cloud repository. -
Under Function code, clear the contents of the
index.py
file and paste the downloaded script. -
Under Function code, create a file named
requirements.txt
and add the following code to it:boto3 botocore requests
-
Specify the function run parameters:
- Entry point:
index.handler
- Timeout, sec:
60
- Service account:
asr-batch-sa
- Entry point:
-
Add environment variables:
S3_BUCKET
:asr-batch-bucket
S3_PREFIX
:input
S3_PREFIX_LOG
:log
S3_PREFIX_OUT
:out
S3_KEY
: ID of the static access keyS3_SECRET
: Secret of the static access keyAPI_KEY
: ID of the API keyAPI_SECRET
: Secret of the API key
-
Click Save changes.
Create a trigger
- In the management console, select Cloud Functions.
- Select Triggers.
- Click Create trigger.
- Specify the trigger parameters:
- Name:
asr-batch-cron
- Type:
Timer
- Launched resource:
Function
- Cron expression:
Every minute
- Function:
asr-batch-function
- Function version tag:
$latest
- Service account:
asr-batch-sa
- Name:
- Click Create trigger.
The trigger you created will fire once a minute and invoke the cloud function.
Test the function
- In the management console, select Object Storage and open
asr-batch-bucket
. - Upload audio files of any supported format to the
input
folder. - Wait a few minutes and make sure the bucket contains the
log
andout
folders. - Check the recognition status in the
log
folder. The status of each audio file sent for recognition is saved to an<audio_file_name>.json
auxiliary file (such asaudio.mp3.json
). If the file contains the"done": "false"
parameter, the recognition process is not completed. - Check the recognition result in the
out
folder. The result is saved to an<audio_file_name>.json
file (such asaudio.mp3.json
). For more information about the recognition result format, see Asynchronous recognition API.
Note
You can monitor the script running progress by viewing logs of your asr-batch-function
.