LID API Tutorial
🔗 Overview
This notebook demonstrates how to use the Language Identification API to detect the language and script codes. We will also explore how language identification can be used in translation and transliteration tasks to automatically detect the source language and perform the appropriate transformations.
1. Installation
Before you begin, ensure you have the necessary Python libraries installed. Run the following commands to install the required packages:
2. Authentication
To use the API, you need an API subscription key. Follow these steps to set up your API key:
- Obtain your API key: If you don’t have an API key, sign up on the Sarvam AI Dashboard to get one.
- Replace the placeholder key: In the code below, replace “YOUR_SARVAM_API_KEY” with your actual API key.
3. Basic Usage
The API requires a single input parameter:
⚠️ Note: If the API cannot confidently detect the language or script, it will return null
for one or both fields.
Response Parameters
-
language_code
(String) – The detected language in BCP-47 format. Supported values:"en-IN"
(English - India)"en-US"
(English - United States)"bn-IN"
(Bengali - India)"gu-IN"
(Gujarati - India)"hi-IN"
(Hindi - India)"kn-IN"
(Kannada - India)"ml-IN"
(Malayalam - India)"mr-IN"
(Marathi - India)"od-IN"
(Odia - India)"pa-IN"
(Punjabi - India)"ta-IN"
(Tamil - India)"te-IN"
(Telugu - India)"ur-IN"
(Urdu - India)
-
script_code
(String) – The detected writing script in ISO-15924 format.Supported values:"Latn"
→ Latin (Roman script)"Beng"
→ Bengali script"Gujr"
→ Gujarati script"Deva"
→ Devanagari script"Knda"
→ Kannada script"Mlym"
→ Malayalam script"Orya"
→ Odia script"Guru"
→ Gurmukhi (Punjabi) script"Taml"
→ Tamil script"Telu"
→ Telugu script"Arab"
→ Arabic script
4. Language Detection Usage
1.Initialize the Client
2.Define Input Text
3.Detect Language and Script
4.Try Another Input
5. Auto Detection
To enable automatic language detection, pass "auto"
as the source_language_code
.
The API will return the transliterated/translated text along with the detected source language code.
🚫 Note: In case of detection failure, manually specify the source_language_code
with one of the supported language codes.
If the API is unable to detect the language, the response will include an error message:
Auto Detection in Transliterate
Auto Detection in Translate
6. Error Handling
You may encounter these errors while using the API:
-
403 Forbidden (
invalid_api_key_error
)- Cause: Invalid API key.
- Solution: Use a valid API key from the Sarvam AI Dashboard.
-
429 Too Many Requests (
insufficient_quota_error
)- Cause: Exceeded API quota.
- Solution: Check your usage, upgrade if needed, or implement exponential backoff when retrying.
-
500 Internal Server Error (
internal_server_error
)- Cause: Issue on our servers.
- Solution: Try again later. If persistent, contact support.
-
400 Bad Request (
invalid_request_error
)- Cause: Incorrect request formatting.
- Solution: Verify your request structure, and parameters.
-
422 Unprocessable Entity Request (
unprocessable_entity_error
)- Cause: Unable to detect the language of the input text.
- Solution: Explicitly pass the source_language_code parameter with a supported language.
7. Additional Resources
For more details, refer to the our official documentation and we are always there to support and help you on our Discord Server:
- Documentation: docs.sarvam.ai
- Community: Join the Discord Community
8. Final Notes
- Keep your API key secure.
- Use clear audio for best results.
- Explore advanced features like diarization and translation.
Keep Building! 🚀