Translate API using Mayura and Sarvam-Translate Models
Overview
This tutorial demonstrates how to use the Sarvam translate API to translate texts/paragraphs from one language to another. The API supports additional features such as transliteration(a type of conversion of a text from one script to another that involves swapping letters), output_Script and gender .
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_AI_API_KEY” with your actual API key.
3. Understanding the Parameters
🔹 The API takes several key parameters:
language_code
(String) – Newly added languages. Supported values:
"as-IN"
(Assamese - India)"brx-IN"
(Bodo- India)"doi-IN"
(Dogri - India)"kok-IN"
(Konkani - India)"ks-IN"
(Kashmiri - India)"mai-IN"
(Maithili - India)"mni-IN"
(Manipuri (Meiteilon) - India)"ne-IN"
(Nepali - India)"sa-IN"
(Sanskrit - India)"sat-IN"
(Santali - India)"sd-IN"
(Sindhi - India)"ur-IN"
(Urdu - India)
4. Basic Usage
4.1: Read the Document
We have two sample documents under the data
folder:
4.2: Split the input text into chunks based on model limits
For Mayura:v1, the API has a maximum chunk size of 1000 characters per request.
For Sarvam-Translate:v1, the API has a maximum chunk size of 2000 characters per request.
we need to split the text accordingly.
4.3: Sample Hindi to Sanskrit Translation using Sarvam-Translate:v1
sarvam-translate:v1: Supports all 22 scheduled languages of India, formal mode only.
4.4: Setting up the API Endpoint using Sarvam-Translate model
There are three main types of translations we support:
1️⃣ English to Indic 🏛 → Translating from English to Indian languages (e.g., “Invoice total is 3,450.75 है।”)
2️⃣ Indic to English 🌍 → Converting Indian languages to English (e.g., “आपका ऑर्डर सफलतापूर्वक दर्ज किया गया है।” → “Your order has been successfully placed.”)
3️⃣ Indic to Indic 🔄 → Translating between Indian languages (e.g., Hindi → Tamil, Bengali → Marathi).
English to Indic Translation
Indic to English Translation
Indic to Indic Translation
5. Advanced Features
5.1: Translation Modes & Differences
1️⃣ Formal – Highly professional, uses pure Hindi (e.g., “कुल राशि”, “देय है”). Suitable for official documents, legal papers, and corporate communication.
2️⃣ Classic-Colloquial – Balanced mix of Hindi & English, slightly informal (e.g., “कुल जोड़”, “देना होगा”). Ideal for business emails, customer support, and semi-formal communication.
3️⃣ Modern-Colloquial – Hinglish, casual, and direct (e.g., “Invoice total”, “due है”, “contact करो”). Best for chatbots, social media, and casual conversations.
📌 Rule of Thumb:
- Use Formal for official content 🏛
- Use Classic-Colloquial for general communication 💬
- Use Modern-Colloquial for everyday conversations 🚀
Formal
Supported by both Mayura:v1 and Sarvam-Translate:v1.
Classic Colloquial
Supported only by Mayura:v1 model.
Modern Colloquial
Supported only by Mayura:v1 model.
5.2: Speaker Gender
The translation model supports Male and Female speaker options, which impact the tone and style of the output.
1️⃣ Male Voice 🔵
2️⃣ Female Voice 🔴
Female
Male
5.3: Numerals Format Feature
The numerals_format
parameter controls how numbers appear in the translation. It has two options:
1️⃣ International (Default) 🌍 → Uses standard 0-9 numerals.
✅ Example: “मेरा phone number है: 9840950950.”
✅ Best for universally understood content, technical documents, and modern usage.
2️⃣ Native 🔡 → Uses language-specific numerals.
✅ Example: “मेरा phone number है: ९८४०९५०९५०.”
✅ Ideal for traditional texts, cultural adaptation, and regional content.
📌 When to Use What?
- Use International for wider readability and digital content 📱
- Use Native for localized, heritage-focused, and print media content 📖
Native
International
5.4: Numerals Format Feature
The output_script
parameter controls how the translated text is transliterated, i.e., how it appears in different scripts while keeping pronunciation intact.
Transliteration Options for Mayura:
1️⃣ Default (null) – No transliteration applied.
✅ Example: “आपका Rs. 3000 का EMI pending है।”
✅ Best for modern, mixed-language content.
2️⃣ Roman – Converts the output into Romanized Hindi.
✅ Example: “aapka Rs. 3000 ka EMI pending hai.”
✅ Ideal for users who can speak but not read native scripts.
3️⃣ Fully-Native – Uses formal native script transliteration.
✅ Example: “आपका रु. 3000 का ई.एम.ऐ. पेंडिंग है।”
✅ Best for official documents and structured writing.
4️⃣ Spoken-Form-in-Native – Uses native script but mimics spoken style.
✅ Example: “आपका थ्री थाउजेंड रूपीस का ईएमअइ पेंडिंग है।”
✅ Ideal for voice assistants, conversational AI, and informal speech.
📌 When to Use What?
- Default – For natural, mixed-language modern writing ✍️
- Roman – For users unfamiliar with native scripts 🔤
- Fully-Native – For formal, structured translations 🏛
- Spoken-Form-in-Native – For casual speech and voice applications 🎙
🚫 Note: For sarvam-translate:v1 - Transliteration is not supported
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.
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! 🚀