Official Python client for Zaban API - AI4Bharat services for Indian languages
Project description
Zaban Python Client
Official Python client for Zaban API - providing access to AI4Bharat 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
- Sign up at [Zaban Dashboard]
- Navigate to API Keys section
- Create a new API key
- Copy the key (starts with
sk-) - Use it in your client initialization
📚 Examples
Check out the examples/ directory for more usage examples:
basic_usage.py- Basic usage examplesasync_usage.py- Async/await examplesbatch_translation.py- Batch processing
🤝 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
- GitHub Issues: Report bugs or request features
- Email: support@zaban.ai
🙏 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zaban-0.1.1.tar.gz.
File metadata
- Download URL: zaban-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc34ef639de488d090c66038525c116f06155b877715d1979b7fe2e628040562
|
|
| MD5 |
9c00b69d645e437837af6f0618d90169
|
|
| BLAKE2b-256 |
3a177ac621fff95e9d077b3d2c804a7ebe9f26d9475033bff30cd09e54205d81
|
File details
Details for the file zaban-0.1.1-py3-none-any.whl.
File metadata
- Download URL: zaban-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a613e740aa3ed4effc32556c246df5b34d3b791af762808f9e3929076d86c5f
|
|
| MD5 |
82269f4efe2ed1baf34ac2286391f881
|
|
| BLAKE2b-256 |
d9c68abcc560068dad584b8f351b47531384c6b808e29e92e0a635bdce9fbc16
|