Examples of the use of synchronous /asr/recognize API.
Note, while sync /asr/recognize methods are very simple to use, they are not best suited if fast response times are required. For fastest response time you have to use async /asr/recognize and you have to stream the audio. This way the audio can be processed while it is being spoken.
Inline Audio, Single GRXML Grammar from URL
Here is the JSON request body that we will use. Note that it has a placeholder that will be replaced by the inline base-64 encoded audio. When saving it for the script below save it to body.json file.
Zip Code grammar is hosted on a Voicegain server.
{
"audio" : {
"source": {"inline":"__INLINE_AUDIO__"}
},
"settings": {
"asr":
{ "grammars": [
{
"type":"GRXML",
"name" : "zip-codes",
"fromUrl":{
"url":"https://tracker.voicegain.ai/attachments/download/300/zip_code_simple_2.grxml"
}
}
],
"maxAlternatives" : 10
}
}
}
Here is a bash script that invokes the API passing the above JSON in the request body. Note, the -k tells curl to bypass SSL cert checking - this is to avoid sectigo bug.
#!/bin/bashYou can download the test audio sample from: https://tracker.voicegain.ai/attachments/download/82/4_8_1_6_9.wav
# convert audio to base-64 encoded string
INLINE_AUDIO="$(base64 -w 0 4_8_1_6_9.wav)"
JWT="<your JWT token here - from Settings->API Security >"
# replace the placeholder with the encoded audio
sed 's,__INLINE_AUDIO__,'"$INLINE_AUDIO"',g' body.json > body-with-audio.json
# sync recog
time curl -k -i -H "Content-Type: application/json" -H 'Accept: application/json' -H "Authorization: Bearer $JWT" -d @body-with-audio.json --verbose https://api.voicegain.ai/v1/asr/recognize
Inline Audio, Inline GRXML Grammar
Here is the JSON request body that we will use. Note that it has two placeholders that will be replaced by the inline base-64 encoded audio and grammar. When saving it for the script below save it to body2.json file.
Note: in this example we are forcing sync /asr/recognize to use the Real-Time acoustic model, by specifying the model name:
"acousticModelNonRealTime" : "VoiceGain-rt-en-US"
Currently, this is a bit of a hack -- in the 1.14.0 release we will add a parameter that will control which acoustic model is to be used for recognition in sync mode. Normally, all sync methods use the offline model, which is somewhat more accurate than the real-time model but has slower response.
{
"audio" : {
"source": {"inline":"__INLINE_AUDIO__"}
},
"settings": {
"asr": {
"grammars": [
{
"type":"GRXML",
"name" : "zip-codes",
"inline":{"data":"__INLINE_GRAMMAR__"}
}
],
"maxAlternatives" : 10,
"acousticModelNonRealTime" : "VoiceGain-rt-en-US"
}
}
}
Here is the script that invokes the API passing the above JSON in the request body. Note, the -k tells curl to bypass SSL cert checking - this is to avoid sectigo bug.
#!/bin/bash
# convert audio to base-64 encoded string
INLINE_AUDIO="$(base64 -w 0 4_8_1_6_9.wav)"
# convert grammar to base-64 encoded string
INLINE_GRAMMAR="$(base64 -w 0 zip_code_simple_2.grxml)"
JWT="<your JWT token here - from Settings->API Security >"
# replace the placeholder with the encoded audio
sed 's,__INLINE_AUDIO__,'"$INLINE_AUDIO"',g' body2.json > body2-with-audio.json
# replace the placeholder with the encoded grammar
sed 's,__INLINE_GRAMMAR__,'"$INLINE_GRAMMAR"',g' body2-with-audio.json > body-with-audio-and-grammar.json
# sync recog
time curl -k -i -H "Content-Type: application/json" -H 'Accept: application/json' -H "Authorization: Bearer $JWT" -d @body-with-audio-and-grammar.json --verbose https://api.voicegain.ai/v1/asr/recognize
Inline Audio, JJSGF Grammar
Here is the JSON request body that we will use. Note that it has a placeholder that will be replaced by the inline base-64 encoded audio. When saving it for the script below save it to body-jsgf.json file.
{
"audio" : {
"source": {"inline":"__INLINE_AUDIO__"}
},
"settings": {
"asr":
{ "grammars": [
{
"type":"JJSGF",
"grammar" : "zip-codes-jsgf",
"public": {"main": "<zip_code>"},
"rules": {
"<zip_code>": "<digit> <digit> <digit> <digit> <digit>",
"<digit>": "one | two | three | four | five | six | seven | eight | nine | zero"
}
}
]}
}
}
Here is the script that invokes the API passing the above JSON in the request body. Note, the -k tells curl to bypass SSL cert checking - this is to avoid sectigo bug.
#!/bin/bash
# convert audio to base-64 encoded string
INLINE_AUDIO="$(base64 -w 0 4_8_1_6_9.wav)"
JWT="<your JWT token here - from Settings->API Security >"
# replace the placeholder with the encoded audio
sed 's,__INLINE_AUDIO__,'"$INLINE_AUDIO"',g' body-jsgf.json > body-jsgf-with-audio.json
# sync recog
time curl -k -i -H "Content-Type: application/json" -H 'Accept: application/json' -H "Authorization: Bearer $JWT" -d @body-jsgf-with-audio.json --verbose https://api.voicegain.ai/v1/asr/recognize
Resources
Comments
0 comments
Article is closed for comments.