Metadata-Version: 2.5
Name: vaakini
Version: 0.1.0
Summary: Python SDK for Vaakini (https://vaakini.com): text to speech in 12 Indian languages
Project-URL: Homepage, https://vaakini.com
Project-URL: Documentation, https://docs.vaakini.com/python
Author: Oggnai Technologies Private Limited
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: ai,audio,hindi,indian-languages,text-to-speech,tts,vaakini,voice
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Multimedia :: Sound/Audio
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: httpx<1,>=0.27.0
Requires-Dist: websockets>=15.0
Description-Content-Type: text/markdown

# vaakini

The Python SDK for [Vaakini](https://vaakini.com): text to speech in 12 Indian languages with one
model, `vaakini-tts-1`.

Full documentation: [docs.vaakini.com/python](https://docs.vaakini.com/python).

## Install

```bash
pip install vaakini
```

## Usage

```python
from vaakini import Vaakini

client = Vaakini()  # reads VAAKINI_API_KEY; or Vaakini(api_key="vk_…")

audio = client.speech.create(input="नमस्ते, आप कैसे हैं?", voice="Monika", language="hi")
with open("hello.mp3", "wb") as f:
    f.write(audio)
```

Your API key is in the dashboard at [vaakini.com](https://vaakini.com). `AsyncVaakini` is the same
client for asyncio: every call below is awaited.

### Speech

```python
# The audio as it arrives: the first of it follows the text's first sentence.
for chunk in client.speech.stream(input=text, voice="Monika", response_format="pcm"):
    play(chunk)
```

`speech.create()` and `speech.stream()` take:

| Argument | Default | |
|---|---|---|
| `input` | required | the text, up to 20,000 characters |
| `voice` | required | a Vaakini voice's name, in any case (`"Monika"`), or the id of one of your org's custom voices (`"cv_…"`) |
| `language` | `None` | `"as"`, `"bn"`, `"en"`, `"gu"`, `"hi"`, `"kn"`, `"ml"`, `"mr"`, `"or"`, `"pa"`, `"ta"` or `"te"`. Left out, the model picks it from the text's script |
| `response_format` | `"mp3"` | `"mp3"`, `"pcm"` (16-bit little-endian mono, 24000 Hz) or `"wav"` |
| `model` | `"vaakini-tts-1"` | the model; this is the only one |
| `normalize` | on | numbers, currency, dates, times, phone numbers, URLs and emails spoken as words |
| `temperature`, `top_k`, `top_p`, `repetition_penalty` | the model's | sampling settings |

### Voices and models

```python
for voice in client.voices.list():
    print(voice.id, voice.category, voice.gender, voice.languages)

model = client.models.list()[0]
print(model.id, model.languages, model.formats)
```

### Streamed text

For text that arrives as it is written, such as an LLM's reply, `client.stream()` opens a
connection: text goes in piece by piece, and each sentence's audio comes back as soon as the
sentence is complete.

```python
with client.stream(voice="Monika", language="hi") as stream:
    for token in llm_tokens:
        stream.send_text(token)
    stream.flush()

    for event in stream:
        if event.type == "audio":
            play(event.data)  # 16-bit mono PCM, 24000 Hz
        elif event.type == "sentence":
            print(event.index, event.text)
        elif event.type == "done":
            print(event.sentences, "sentences,", event.characters, "characters")
```

A reply is one segment: `send_text()` any number of times, then `flush()`. Its events are a
`SentenceEvent` before each sentence's `AudioEvent`s, and a `DoneEvent` at the end, where the
iteration stops. The next `send_text()` starts the next segment on the same connection; iterate
again for its events.

- `stream(...)` takes `voice`, `language`, `model` and the options that `speech.create()` takes,
  and `format`: `"pcm"` (the default) or `"mp3"`.
- `stream.cancel()` ends a segment early, for a barge-in: its remaining audio is dropped, and its
  iteration stops at a `CancelledEvent`. The next segment's text can be sent at once.
- To play audio while the text is still arriving, send on one thread and iterate on another, once
  the segment's first `send_text()` has been called. With `AsyncVaakini`, use two tasks:

```python
async with client.stream(voice="Monika") as stream:
    await stream.send_text(first_token)
    sender = asyncio.create_task(send_rest_and_flush(stream))
    async for event in stream:
        ...
```

- Vaakini closes a connection that sends nothing for 30 minutes, and when one of its servers
  restarts. A closed connection raises `APIConnectionError`: open a new one.

### Custom voices

Your org's own voices, each cloned from one reference recording. They are off until Vaakini
enables them for your org: write to hello@vaakini.com.

```python
voice = client.custom_voices.create(
    name="Priya (support)",
    audio="priya.wav",  # a path, bytes or an open binary file: WAV, FLAC or Ogg, 0.5 to 30 seconds
    transcript="नमस्ते, मैं प्रिया बोल रही हूँ।",
    language="hi",
)
client.speech.create(input="आपका स्वागत है।", voice=voice.id)

client.custom_voices.list()  # .enabled, .limit, .voices
client.custom_voices.get(voice.id)
client.custom_voices.update(voice.id, name="Priya", gender="female")
client.custom_voices.replace_recording(voice.id, audio="priya2.wav", transcript="…", language="hi")
client.custom_voices.delete(voice.id)
```

### Errors

Every error is a `vaakini.VaakiniError`.

| Error | `code` | |
|---|---|---|
| `BadRequestError` | `bad_request` | the request is invalid; the message names the field |
| `AuthenticationError` | `unauthorized` | the API key is missing or invalid |
| `InsufficientCreditsError` | `insufficient_credits` | the org is out of credit |
| `NotFoundError` | `not_found` | no such custom voice in your org |
| `CustomVoicesNotEnabledError` | `custom_voices_not_enabled` | custom voices aren't enabled for your org |
| `CustomVoiceLimitError` | `custom_voice_limit` | your org holds as many custom voices as it can |
| `RateLimitError` | `rate_limited` | the model is at capacity; try again |
| `UnavailableError` | `unavailable` | the model is unavailable; try again |
| `ModelTimeoutError` | `timeout` | the model sent no audio in time; try again |
| `InternalServerError` | `internal` | the request failed on Vaakini's side; try again |
| `APIConnectionError` | | the API could not be reached, or the connection ended early |
| `APITimeoutError` | | the API did not answer within the client's `timeout` |

The first ten are `APIError`s, with `code`, `message`, `status_code` (None on a `stream()`
connection) and `retryable`. If `speech.stream()` raises `APIConnectionError` in the middle of the
audio, the take failed: treat the request as failed.

### Client options

`Vaakini(api_key=None, base_url="https://api.vaakini.com", timeout=60.0)`: `timeout` is the
seconds to wait for a connection, and for each piece of a response. Close a client with
`client.close()`, or use it in a `with` statement.

## License

Apache-2.0.
