[Yandex Cloud documentation](../../index.md) > [Tutorials](../index.md) > [Serverless technologies](index.md) > Serverless-based workflows and automation > Recognizing audio files from Object Storage on a regular basis

# Recognizing audio files from Yandex Object Storage on a regular basis

# Recognizing audio files from Yandex Object Storage on a regular basis


The SpeechKit [asynchronous recognition API](https://aistudio.yandex.ru/docs/en/speechkit/stt/api/transcribation-api) is integrated with Yandex Object Storage. Therefore, you can set up automatic recognition of audio files of [supported formats](https://aistudio.yandex.ru/docs/en/speechkit/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 recognition of audio files using SpeechKit:

1. [Create a cloud function](#create-function) to read files from your Object Storage bucket, send them to the API, and check the file recognition status.
1. [Create a trigger](#create-trigger) to regularly invoke your cloud function.
1. [Test the function](#check-function).

## Getting started {#before-you-begin}

1. [Create](../../iam/operations/sa/create.md) a service account named `asr-batch-sa`.
1. [Assign](../../iam/operations/sa/assign-role-for-sa.md) the `storage.editor`, `functions.functionInvoker`, and `ai.speechkit-stt.user` roles to the service account for the folder in which it was created.
1. [Create](../../iam/operations/authentication/manage-access-keys.md#create-access-key) a static access key for the service account.


1. [Create](../../iam/operations/authentication/manage-api-keys.md#create-api-key) an API key for the service account.
1. [Create](../../storage/operations/buckets/create.md) an Object Storage bucket named `asr-batch-bucket` in the service account folder.
1. Open `asr-batch-bucket`, click **Create folder**, and specify `input` in the **Folder name** field.
1. [Upload](../../storage/operations/objects/upload.md#simple) the `config.json` file with the specified [recognition language](https://aistudio.yandex.ru/docs/en/speechkit/stt/models#languages) to the bucket's `input` folder. The file only contains one setting:

   ```json
   {
     "lang": "<language_code>"
   }
   ```

   {% note info %}

   If there is no `config.json` file in the bucket, the recognition language will be Russian.

   {% endnote %}

## Create a cloud function {#create-function}

1. In the [management console](https://console.yandex.cloud), navigate to the folder with the new service account.
1. Select **Cloud Functions**.
1. Click **Create function** and specify `asr-batch-function` as the function name.
1. Click **Create**.
1. Under **Editor**, select the `Python` `3.8` runtime environment and click **Continue**.
1. Download the [script file](https://github.com/yandex-cloud-examples/yc-speechkit-async-recognizer/blob/main/examples/asr-batch-function/functions/main.py) from the Yandex Cloud repository.
1. Under **Function code**, clear the contents of the `index.py` file and paste the downloaded script.
1. Under **Function code**, create a file named `requirements.txt` and add the following code to it:

   ```text
   boto3
   botocore
   requests
   ```

1. Specify the function run settings:
   * **Entry point**: `index.handler`
   * **Timeout**: `60`
   * **Service account**: `asr-batch-sa`
1. Add these environment variables:
   * `S3_BUCKET`: `asr-batch-bucket`
   * `S3_PREFIX`: `input`
   * `S3_PREFIX_LOG`: `log`
   * `S3_PREFIX_OUT`: `out`
   * `S3_KEY`: Static access key ID
   * `S3_SECRET`: Static access key secret
   * `API_KEY`: API key ID
   * `API_SECRET`: API key secret

1. Click **Save changes**.

## Create a trigger {#create-trigger}

1. In the [management console](https://console.yandex.cloud), navigate to the folder the function was created in.
1. Navigate to **Cloud Functions**.
1. Select **Triggers**.
1. Click **Create trigger**.
1. Specify the trigger settings:
   * **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`.
1. Click **Create trigger**.

The trigger you created will fire once a minute and invoke the [cloud function](#create-function).

## Test the function {#check-function}

1. In the [management console](https://console.yandex.cloud), navigate to the folder the function was created in.
1. Navigate to **Object Storage** and open `asr-batch-bucket`.
1. [Upload](../../storage/operations/objects/upload.md#simple) audio files of any [supported format](https://aistudio.yandex.ru/docs/en/speechkit/formats) to the `input` folder.
1. Wait a few minutes and make sure the bucket now contains the `log` and `out` folders.
1. Check the recognition status in the `log` folder. The status of each audio file sent for recognition is saved to an auxiliary file named `<audio_file_name>.json`, e.g., `audio.mp3.json`. The `"done": "false"` parameter in the file indicates that the recognition process is not completed.
1. Check the recognition result in the `out` folder. The result is saved to a JSON file named `<audio_file_name>.json`, e.g., `audio.mp3.json`. To learn more about the recognition result format, see [Asynchronous recognition API](https://aistudio.yandex.ru/docs/en/speechkit/stt/api/transcribation-api#get-result-response).

{% note info %}

You can monitor the progress of the script in the [logs](../../functions/operations/function/function-logs.md) of `asr-batch-function`.

{% endnote %}


#### See also {#see-also}


* [Asynchronous recognition API v2](https://aistudio.yandex.ru/docs/en/speechkit/stt/api/transcribation-api)
* [Asynchronous recognition of LPCM audio files using the API v2](https://aistudio.yandex.ru/docs/en/speechkit/stt/api/transcribation-lpcm)
* [Asynchronous recognition of OggOpus audio files using the API v2](https://aistudio.yandex.ru/docs/en/speechkit/stt/api/transcribation-ogg)