Skip to main content

Official Python client for Zaban API - Zaban services for Indian languages

Project description

Zaban Python Client

PyPI version Python Support License: MIT

Official Python client for Zaban API - providing access to Zaban services for Indian languages including translation, text-to-speech, speech-to-text, and transliteration.

🌟 Features

  • Translation: Powered by IndicTrans2, supporting 22 Indian languages
  • Text-to-Speech (TTS): Convert text to natural-sounding speech
  • Speech-to-Text (STT): Transcribe audio to text
  • Transliteration: Convert text between different scripts
  • Async Support: Full async/await support for concurrent operations
  • Type Safe: Fully typed with Pydantic models
  • Simple API: Clean, intuitive interface similar to OpenAI's client

📦 Installation

pip install zaban

🚀 Quick Start

from zaban import Zaban

# Initialize client with API key
client = Zaban(api_key="sk-your-api-key")

# Or use environment variable ZABAN_API_KEY
client = Zaban()

# Translate text
result = client.translation.create(
    text="Hello, how are you?",
    source_lang="eng_Latn",
    target_lang="hin_Deva"
)
print(result.translated_text)  # "आप कैसे हैं?"

📖 Usage Examples

Translation

# English to Hindi
result = client.translation.create(
    text="Hello, how are you?",
    source_lang="eng_Latn",
    target_lang="hin_Deva"
)
print(result.translated_text)  # "आप कैसे हैं?"

# Auto-detect source language
result = client.translation.create(
    text="Hello",
    target_lang="hin_Deva",
    auto_detect=True
)

# Using convenience method
result = client.translation.translate(
    "Hello",
    to="hin_Deva",
    from_="eng_Latn"
)

Text-to-Speech (TTS)

# Generate speech from text
audio = client.audio.speech.create(
    text="नमस्ते दुनिया",
    lang="hi",
    speaker="female",
    format="wav"
)

# Save to file
audio.save("output.wav")

# Or get bytes directly
audio_bytes = audio.content

Speech-to-Text (STT)

# Transcribe from file path
transcription = client.audio.transcriptions.create(
    audio="audio.wav",
    lang="hi"
)
print(transcription.text)

# From file object
with open("audio.wav", "rb") as f:
    transcription = client.audio.transcriptions.create(
        audio=f,
        lang="hi"
    )

# From audio URL
transcription = client.audio.transcriptions.create(
    audio_url="https://example.com/audio.wav",
    lang="hi"
)

Transliteration

# Transliterate from Latin to Devanagari
result = client.transliteration.create(
    text="namaste",
    source_script="latn",
    target_script="deva",
    lang="hi",
    topk=3
)
print(result.top)  # "नमस्ते"
print(result.results)  # ["नमस्ते", "नमस्ते", "नमस्ते"]

⚡ Async Support

import asyncio
from zaban import AsyncZaban

async def main():
    # Initialize async client
    async with AsyncZaban(api_key="sk-your-api-key") as client:
        # Single translation
        result = await client.translation.create(
            text="Hello",
            target_lang="hin_Deva",
            auto_detect=True
        )
        print(result.translated_text)
        
        # Batch translations (concurrent)
        texts = ["Hello", "Goodbye", "Thank you"]
        tasks = [
            client.translation.create(text=t, target_lang="hin_Deva", auto_detect=True)
            for t in texts
        ]
        results = await asyncio.gather(*tasks)
        
        for result in results:
            print(result.translated_text)

asyncio.run(main())

🌐 Supported Languages

The Zaban API supports 22 Indian languages plus English using BCP-47 format with script:

Language Code Script
English eng_Latn Latin
Hindi hin_Deva Devanagari
Bengali ben_Beng Bengali
Tamil tam_Taml Tamil
Telugu tel_Telu Telugu
Gujarati guj_Gujr Gujarati
Kannada kan_Knda Kannada
Malayalam mal_Mlym Malayalam
Marathi mar_Deva Devanagari
Punjabi pan_Guru Gurmukhi
Oriya ory_Orya Oriya
Assamese asm_Beng Bengali
Urdu urd_Arab Arabic

[See full list in documentation]

🔧 Configuration

Environment Variables

# Set API key
export ZABAN_API_KEY="sk-your-api-key"

# Alternative
export ZABAN_KEY="sk-your-api-key"

Client Options

client = Zaban(
    api_key="sk-your-api-key",       # API key
    base_url="http://localhost:8000/api/v1",  # API base URL
    timeout=30.0,                     # Request timeout in seconds
    max_retries=2,                    # Max retries for failed requests
)

🛡️ Error Handling

from zaban import (
    Zaban,
    ZabanError,
    AuthenticationError,
    RateLimitError,
    ValidationError,
    APIError,
    TimeoutError,
    ConnectionError,
)

try:
    result = client.translation.create(
        text="Hello",
        target_lang="hin_Deva"
    )
except AuthenticationError as e:
    print(f"Invalid API key: {e}")
except RateLimitError as e:
    print(f"Rate limit exceeded: {e}")
except ValidationError as e:
    print(f"Invalid request: {e}")
except TimeoutError as e:
    print(f"Request timed out: {e}")
except ConnectionError as e:
    print(f"Connection failed: {e}")
except ZabanError as e:
    print(f"API error: {e}")

🔑 Getting an API Key

  1. Sign up at [Zaban Dashboard]
  2. Navigate to API Keys section
  3. Create a new API key
  4. Copy the key (starts with sk-)
  5. Use it in your client initialization

📚 Examples

Check out the examples/ directory for more usage examples:

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

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

🔗 Links

💬 Support

🙏 Acknowledgments

Built with ❤️ using AI4Bharat's amazing open-source models:

  • IndicTrans2 for translation
  • AI4Bharat TTS/STT services for speech processing

Note: This is an unofficial client. For official API documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zaban-0.1.2.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zaban-0.1.2-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file zaban-0.1.2.tar.gz.

File metadata

  • Download URL: zaban-0.1.2.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for zaban-0.1.2.tar.gz
Algorithm Hash digest
SHA256 24a628ece92929d83cda58f8f1a33fdf1785f7af5cdb236e8622ab9914c2eed4
MD5 d937dd0097172741b265b797cd0487c3
BLAKE2b-256 bcdddd5de237e3fdd1c4c8a36408b63227a749cd28646c7a30a1077165b92b5e

See more details on using hashes here.

File details

Details for the file zaban-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: zaban-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for zaban-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fb3d981df6b33a9e3f72d0619111722372d2963f2b1dc8ade289204cc7fd3934
MD5 ce3c7eb9cb1812559a0e609db3f14ec0
BLAKE2b-256 78cec201349d3b005660e8ab330e10e8b86b9a064670260352d6321ce562cc19

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page