Skip to main content

The official Python SDK for aiOla API - Speech-to-Text and Text-to-Speech with built-in microphone support

Project description

aiOla Python SDK

The official Python SDK for the aiOla API, designed to work seamlessly in both synchronous and asynchronous environments.

Installation

Basic Installation

pip install aiola
# or
uv add aiola

With Microphone Support

For microphone streaming functionality, install with the mic extra:

pip install 'aiola[mic]'
# or
uv add 'aiola[mic]'

Usage

Authentication

The aiOla SDK uses a two-step authentication process:

  1. Generate Access Token: Use your API key to create a temporary access token, save it for later use
  2. Create Client: Use the access token to instantiate the client

Step 1: Generate Access Token

from aiola import AiolaClient

result = AiolaClient.grant_token(
    api_key='your-api-key'
)

access_token = result['accessToken'] 
session_id = result['sessionId']

Step 2: Create Client

client = AiolaClient(
    access_token=access_token
)

Complete Example

import os
from aiola import AiolaClient

def example():
    try:
        # Step 1: Generate access token
        result = AiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY')
        )
        
        # Step 2: Create client
        client = AiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Use client for API calls
        with open('./audio.wav', 'rb') as audio_file:
            transcript = client.stt.transcribe_file(
                file=audio_file,
                language='en'
            )
        
        print('Transcript:', transcript)
        
    except Exception as error:
        print('Error:', error)

example()

Session Management

Close Session:

# Terminates the session
result = AiolaClient.close_session(access_token)
print(f"Session closed at: {result['deletedAt']}")

Custom base URL (enterprises)

result = AiolaClient.grant_token(
    api_key='your-api-key',
    auth_base_url='https://mycompany.auth.aiola.ai'
)

client = AiolaClient(
    access_token=result['accessToken'],
    base_url='https://mycompany.api.aiola.ai'
)

Speech-to-Text – transcribe file

import os
from aiola import AiolaClient

def transcribe_file():
    try:
        # Step 1: Generate access token
        result = AiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY')
        )
        
        # Step 2: Create client
        client = AiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Transcribe file
        with open('path/to/your/audio.wav', 'rb') as audio_file:
            transcript = client.stt.transcribe_file(
                file=audio_file,
                language="en"
            )

        print(transcript)
    except Exception as error:
        print('Error transcribing file:', error)

transcribe_file()

Speech-to-Text – live streaming

import os
from aiola import AiolaClient, MicrophoneStream
from aiola.types import LiveEvents

def live_streaming():
    try:
        # Step 1: Generate access token, save it
        result = AiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY') or 'YOUR_API_KEY'
        )
        
        # Step 2: Create client using the access token
        client = AiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Start streaming
        connection = client.stt.stream(
            lang_code='en'
        )

        @connection.on(LiveEvents.Transcript)
        def on_transcript(data):
            print('Transcript:', data.get('transcript', data))

        @connection.on(LiveEvents.Connect)
        def on_connect():
            print('Connected to streaming service')

        @connection.on(LiveEvents.Disconnect)
        def on_disconnect():
            print('Disconnected from streaming service')

        @connection.on(LiveEvents.Error)
        def on_error(error):
            print('Streaming error:', error)

        connection.connect()

        try:
            # Capture audio from microphone using the SDK's MicrophoneStream
            with MicrophoneStream(
                channels=1,
                samplerate=16000,
                blocksize=4096,
            ) as mic:
                mic.stream_to(connection)
                
                # Keep the main thread alive
                while True:
                    try:
                        import time
                        time.sleep(0.1)
                    except KeyboardInterrupt:
                        print('Keyboard interrupt')
                        break
                
        except KeyboardInterrupt:
            print('Keyboard interrupt')
        finally:
            connection.disconnect()
        
    except Exception as error:
        print('Error:', error)

if __name__ == "__main__":
    live_streaming()

Text-to-Speech

import os
from aiola import AiolaClient

def create_file():
    try:
        # Step 1: Generate access token
        result = AiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY')
        )
        
        # Step 2: Create client
        client = AiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Generate audio
        audio = client.tts.synthesize(
            text='Hello, how can I help you today?',
            voice='jess',
            language='en'
        )

        with open('./audio.wav', 'wb') as f:
            for chunk in audio:
                f.write(chunk)
        
        print('Audio file created successfully')
    except Exception as error:
        print('Error creating audio file:', error)

create_file()

Text-to-Speech – streaming

import os
from aiola import AiolaClient

def stream_tts():
    try:
        # Step 1: Generate access token
        result = AiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY')
        )
        
        # Step 2: Create client
        client = AiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Stream audio
        stream = client.tts.stream(
            text='Hello, how can I help you today?',
            voice='jess',
            language='en'
        )

        audio_chunks = []
        for chunk in stream:
            audio_chunks.append(chunk)
        
        print('Audio chunks received:', len(audio_chunks))
    except Exception as error:
        print('Error streaming TTS:', error)

stream_tts()

Async Client

For asynchronous operations, use the AsyncAiolaClient:

Async Speech-to-Text – file transcription

import asyncio
import os
from aiola import AsyncAiolaClient

async def transcribe_file():
    try:
        # Step 1: Generate access token
        result = await AsyncAiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY')
        )
        
        # Step 2: Create client
        client = AsyncAiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Transcribe file
        with open('path/to/your/audio.wav', 'rb') as audio_file:
            transcript = await client.stt.transcribe_file(
                file=audio_file,
                language="en"
            )

        print(transcript)
    except Exception as error:
        print('Error transcribing file:', error)

asyncio.run(transcribe_file())

Async Text-to-Speech

import asyncio
import os
from aiola import AsyncAiolaClient

async def create_audio_file():
    try:
        # Step 1: Generate access token
        result = await AsyncAiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY')
        )
        
        # Step 2: Create client
        client = AsyncAiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Generate audio
        audio = client.tts.synthesize(
            text='Hello, how can I help you today?',
            voice='jess',
            language='en'
        )

        with open('./audio.wav', 'wb') as f:
            async for chunk in audio:
                f.write(chunk)
        
        print('Audio file created successfully')
    except Exception as error:
        print('Error creating audio file:', error)

asyncio.run(create_audio_file())

Async Text-to-Speech – streaming

import asyncio
import os
from aiola import AsyncAiolaClient

async def stream_tts():
    try:
        # Step 1: Generate access token
        result = await AsyncAiolaClient.grant_token(
            api_key=os.getenv('AIOLA_API_KEY')
        )
        
        # Step 2: Create client
        client = AsyncAiolaClient(
            access_token=result['accessToken']
        )
        
        # Step 3: Stream audio
        stream = client.tts.stream(
            text='Hello, how can I help you today?',
            voice='jess',
            language='en'
        )

        audio_chunks = []
        async for chunk in stream:
            audio_chunks.append(chunk)
        
        print('Audio chunks received:', len(audio_chunks))
    except Exception as error:
        print('Error streaming TTS:', error)

asyncio.run(stream_tts())

Requirements

  • Python 3.10+
  • For microphone streaming functionality: Install with pip install 'aiola[mic]'

Examples

The SDK includes several example scripts in the examples/ directory.

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

aiola-0.0.1.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

aiola-0.0.1-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file aiola-0.0.1.tar.gz.

File metadata

  • Download URL: aiola-0.0.1.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.9

File hashes

Hashes for aiola-0.0.1.tar.gz
Algorithm Hash digest
SHA256 609d11202ff366cd114b44feac6cd9ef26c94fae7222291fbafc4d7414f355ff
MD5 a40e725a2c9e5da1b9d5d753c607f43f
BLAKE2b-256 bb1e6d38381e08e5046f1aa32f21c9b45728d175bde21be65078656b36880a04

See more details on using hashes here.

File details

Details for the file aiola-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiola-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.9

File hashes

Hashes for aiola-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce24727ec93987a64dc4e324188bd9db755aa2d2791fc1f54f4fa01afb5b04ad
MD5 942c34085ef5482bd033b7eb04aa990a
BLAKE2b-256 e3bd8ecee0ed3fe533c39bef08d23113a36e501235da05d9f71a6579ce27a651

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