Skip to main content

indictrans2-ai4bharat

A Python package for Indian language translation using AI4Bharat's IndicTrans2 API service. This package provides an easy-to-use interface for translating text between English and various Indian languages, with automatic language detection.

Acknowledgment

This package uses the IndicTrans2 translation service provided by AI4Bharat. All translation capabilities are powered by AI4Bharat's API. Visit AI4Bharat's website for more information about their services.

Features

  • Support for 23 Indian languages
  • Automatic language detection
  • Language information utilities
  • User agent rotation to avoid rate limits
  • Automatic session refresh
  • Retry mechanism for failed requests
  • Error handling and input validation
  • Easy to integrate into existing projects

Installation

You can install the package using pip:

pip install indictrans2-ai4bharat

Or install from source:

git clone https://github.com/yourusername/indictrans2-ai4bharat
cd indictrans2-ai4bharat
pip install -e .

Quick Start

from indictrans2 import IndicTranslator

# Create translator instance
translator = IndicTranslator()

# Auto-detect language and translate
text = "नमस्ते, कैसे हैं आप?"
translation = translator.translate(text, target_lang="en")
print(translation)  # Output: Hello, how are you?

# Get language information
languages = translator.get_supported_languages()
print(languages)

Language Detection and Information

# Detect language
text = "வணக்கம், எப்படி இருக்கிறீர்கள்?"
detected = translator.detect_language(text)
print(detected)  # Output: ta

# Get language name
lang_name = translator.get_language_name("ta")
print(lang_name)  # Output: Tamil

# Get confidence scores
confidence = translator.detect_language_confidence(text)
for lang, score in confidence:
    print(f"{lang}: {score:.2%}")

# Get language code from name
code = translator.get_language_code("Hindi")
print(code)  # Output: hi

# List all supported languages
codes = translator.get_supported_language_codes()
names = translator.get_supported_language_names()

Advanced Translation

# Create translator with custom settings
translator = IndicTranslator(
    max_retries=3,              # Number of retries for failed requests
    session_refresh_interval=3600  # Refresh session every hour
)

# Batch translation with auto-detection
texts = [
    "Hello world!",
    "नमस्ते दुनिया!",
    "வணக்கம் உலகம்!"
]

for text in texts:
    # Source language will be auto-detected
    translation = translator.translate(text, target_lang="hi")
    print(f"Original: {text}")
    print(f"Hindi: {translation}\n")

Supported Languages

Language Code Language Code
Assamese as Malayalam ml
Bengali bn Manipuri mni
Bodo brx Marathi mr
Dogri doi Nepali ne
English en Odia or
Gujarati gu Punjabi pa
Hindi hi Sanskrit sa
Kannada kn Santali sat
Kashmiri ks Sindhi sd
Konkani kok Tamil ta
Maithili mai Telugu te
Urdu ur

API Reference

IndicTranslator

class IndicTranslator:
    def translate(self, text, target_lang, source_lang=None):
        """
        Translate text with optional auto-detection
        
        Args:
            text: Text to translate
            target_lang: Target language code
            source_lang: Source language code (if None, will be auto-detected)
            
        Returns:
            Translated text if successful, None otherwise
        """
    
    def detect_language(self, text):
        """
        Detect the language of input text
        
        Returns:
            Detected language code
        """
    
    def detect_language_confidence(self, text):
        """
        Detect language with confidence scores
        
        Returns:
            List of (language_code, confidence_score) tuples
        """
    
    def get_language_name(self, lang_code):
        """Get language name from code"""
    
    def get_language_code(self, language_name):
        """Get language code from name"""
    
    def get_supported_languages(self):
        """Get dictionary of supported languages"""
    
    def get_supported_language_codes(self):
        """Get list of supported language codes"""
    
    def get_supported_language_names(self):
        """Get list of supported language names"""

Examples

See the examples directory for more detailed examples:

  • basic_translation.py: Basic translation usage
  • batch_translation.py: Batch translation with rate limit handling
  • language_detection.py: Language detection and information features

Contributing

Contributions are welcome! Please feel free to submit pull requests.

License

This package is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This is an unofficial package that uses AI4Bharat's IndicTrans2 API service. Users must ensure their usage complies with AI4Bharat's terms of service and guidelines. This package is not affiliated with or endorsed by AI4Bharat.

Credits

Translation service provided by AI4Bharat.

Metadata

Release files for indictrans2-ai4bharat 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for indictrans2-ai4bharat 0.1.0
File Size Uploaded Provenance
indictrans2_ai4bharat-0.1.0.tar.gz 11.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for indictrans2-ai4bharat 0.1.0
File Interpreter ABI Platform
indictrans2_ai4bharat-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.4 kB

Release files / indictrans2_ai4bharat-0.1.0.tar.gz

Download URL indictrans2_ai4bharat-0.1.0.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f36a22649b5f3111b1276713dd36335a722130d23feed3682aa8e3d8fb7f7d33
BLAKE2b-256 checksum
How to use checksums
486306d4d18e290573721e77520190b65e702ff61743469924435db2333b578a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Release files / indictrans2_ai4bharat-0.1.0-py3-none-any.whl

Download URL indictrans2_ai4bharat-0.1.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f059635f0d7438de60c710460a3e397a497f95eab935fd7be9336d7688e310a3
BLAKE2b-256 checksum
How to use checksums
9436a65a94c287ffd1260f5f6eb960a25aa8a76c746687b5f3d41c5a135fea70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Security for indictrans2-ai4bharat 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page