Example of using the API v1 for synchronous recognition
This example shows how to use the API v1 to recognize speech from an OggOpus audio file in synchronous recognition mode.
The example uses the following parameters:
- Language: Russian.
- Other parameters are left at their defaults.
Use cURL to generate and send a request to the server for recognition.
You authenticate as a Yandex or federated account with an IAM token. If using a service account, you do not have to include the folder ID in your request. Learn more about the authentication in the SpeechKit API.
Request example
Send a request to convert speech to text:
POST /speech/v1/stt:recognize?topic=general&lang=ru-RU&folderId={<folder_ID>} HTTP/1.1
Host: stt.api.cloud.yandex.net
Authorization: Bearer <IAM_token>
... (binary data of an audio file)
Where:
topic: Language model.lang: Recognition language.folderId: Folder ID.<IAM_token>: IAM token.
Send a request to convert speech to text:
export FOLDER_ID=<folder_ID>
export IAM_TOKEN=<IAM_token>
curl \
--request POST \
--header "Authorization: Bearer ${IAM_TOKEN}" \
--data-binary "@speech.ogg" \
"https://stt.api.cloud.yandex.net/speech/v1/stt:recognize?topic=general&folderId=${FOLDER_ID}"
Where:
FOLDER_ID: Folder ID.IAM_TOKEN: IAM token.--data-binary: Name of the audio file for recognition.topic: Language model.
Send a request to convert speech to text:
import urllib.request
import json
FOLDER_ID = "<folder_ID>" #: Folder ID.
IAM_TOKEN = "<IAM_token>>" #: IAM token.
with open("speech.ogg", "rb") as f:
data = f.read()
params = "&".join([
"topic=general",
"folderId=%s" % FOLDER_ID,
"lang=ru-RU"
])
url = urllib.request.Request("https://stt.api.cloud.yandex.net/speech/v1/stt:recognize?%s" % params, data=data)
# Authentication with an IAM token.
url.add_header("Authorization", "Bearer %s" % IAM_TOKEN)
responseData = urllib.request.urlopen(url).read().decode('UTF-8')
decodedData = json.loads(responseData)
if decodedData.get("error_code") is None:
print(decodedData.get("result"))
Where:
FOLDER_ID: Folder ID.IAM_TOKEN: IAM token.speech.ogg: Name of the audio file for recognition.topic: Language model.lang: Recognition language.
Send a request to convert speech to text:
<?php
$token = '<IAM_token>'; #: IAM token.
$folderId = "<folder_ID>"; #: Folder ID.
$audioFileName = "speech.ogg";
$file = fopen($audioFileName, 'rb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://stt.api.cloud.yandex.net/speech/v1/stt:recognize?lang=ru-RU&folderId=${folderId}&format=oggopus");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($audioFileName));
$res = curl_exec($ch);
curl_close($ch);
$decodedResponse = json_decode($res, true);
if (isset($decodedResponse["result"])) {
echo $decodedResponse["result"];
} else {
echo "Error code: " . $decodedResponse["error_code"] . "\r\n";
echo "Error message: " . $decodedResponse["error_message"] . "\r\n";
}
fclose($file);
Where:
Response example
HTTP/1.1 200 OK
YaCloud-Billing-Units: 15
{
"result": "your number is 212-85-06"
}
Useful links
Folder: Space containing the Yandex Cloud resources. To authenticate to AI Studio, you need a folder ID. To get it, in the AI Studio interface, hover over the folder name at the top of the screen and click . The ID will be copied to the clipboard.
