Route to Default Language if Language Confidence is Low
This guide will show you how to use AssemblyAI’s API to resubmit a request using a default language if the Automatic Language Detection’s language_confidence
is below a certain threshold.
Get Started
Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for an AssemblyAI account and get your API key from your dashboard.
Step-by-Step Instructions
Install the SDK:
Import the assemblyai
package and set the API key.
Define a default_language
, which should be set to the language code that will be used to rerun the transcript if language detection runs with low language_confidence
.
Define an audio_url
that is set to a link to the audio file. Define and set the parameters audio: audioUrl
and language_detection: true
. We also need to define our language_confidence_threshold
. For the purposes of this example, we’ll set it to 0.8, representing 80% confidence.
If a transcript ends up with a language_confidence
below this value, the transcript will error out and will return the transcript using the default_language
.
You can handle the error safely by checking the error message and rerunning the transcript with the language_code
set to the default_language
.
The error handling flow works as follows:
- If there is no error, the transcript ID and text are printed
- If there is an error:
- Check if it’s related to
language_confidence
being below threshold - If so:
- Print message about rerunning with default language
- Create new transcript with
default_language
as thelanguage_code
- Print new transcript ID and text
- If not:
- Print the error message
- Check if it’s related to
When rerunning with the default language, the configuration is updated to:
- Turn off
language_detection
- Remove
language_confidence_threshold
- Set
language_code
to thedefault_language
You will not be charged for the first transcript if there is an error. You will only be charged for the transcript that processes successfully.