Skip to main content

Speechly Public Protobuf Stubs

Project description

Python Speechly API

See the generic Speechly gRPC stubs documentation for more information about using the API.

Install

Install the latest package using pip:

pip install speechly-api

Note that the minimum python version supported is 3.6.

Using Python Stubs

The stubs are generated for the default grpcio python package, and the examples are using asyncio.

Creating a Channel

In python, the default authority of the channel needs to be overridden, as it defaults to a string containing the port number. This will not work with the API, so we set the DNS name manually:

channel = grpc.aio.secure_channel(
    target='api.speechly.com:443',
    credentials=grpc.ssl_channel_credentials(),
    options=[('grpc.default_authority', 'api.speechly.com')]
)

IdentityAPI

Login with speechly.identity.v2.IdentityAPI using an app_id:

async def login(channel, device_id, app_id=None, project_id=None):
    assert device_id, 'UUID device_is required'
    assert (app_id or project_id), 'app_id or project_id is required'
    identity_api = IdentityAPIStub(channel)
    req = LoginRequest(device_id=device_id)
    if app_id:
        # if a token with a single app_id is required:
        req.application.app_id = app_id
    else:
        # get a token that is usable for all apps in project:
        req.project.project_id = project_id
    response = await identity_api.Login(req)
    token = response.token
    expires = datetime.fromisoformat(response.expires_at)
    return token, expires

SLU

Open a bidirectional stream to speechly.slu.v1.SLU/Stream and send audio from a source generator to the API. The following example assumes that the audio_stream is an iterator that yields audio with 1 channel and sample rate 16KHz, in bytes chunks:

async def stream_speech(channel, token, audio_stream, app_id=None):
    auth = ('authorization', f'Bearer {token}')

    async def read_responses(stream):
        transcript = []
        intent = ''
        entities = []
        resp = await stream.read()
        while resp != grpc.aio.EOF:
            if resp.HasField('started'):
                print(f'audioContext {resp.audio_context} started')
            elif resp.HasField('transcript'):
                transcript.append(resp.transcript.word)
            elif resp.HasField('entity'):
                entities.append(resp.entity.entity)
            elif resp.HasField('intent'):
                intent = resp.intent.intent
            elif resp.HasField('finished'):
                print(f'audioContext {resp.audio_context} finished')
            resp = await stream.read()
        return intent, entities, transcript

    async def send_audio(stream, source):
        await stream.write(SLURequest(event=SLUEvent(event='START', app_id=app_id)))
        for chunk in source:
            await stream.write(SLURequest(audio=chunk))
        await stream.write(SLURequest(event=SLUEvent(event='STOP')))
        await stream.done_writing()

    async with channel:
        slu = SLUStub(channel)
        try:
            stream = slu.Stream(metadata=[auth])
            config = SLUConfig(channels=1, sample_rate_hertz=16000)
            await stream.write(SLURequest(config=config))
            recv = read_responses(stream)
            send = send_audio(stream, audio_stream)
            r = await asyncio.gather(recv, send)
            intent, entities, transcript = r[0]
            print('Intent:', intent)
            print('Entities:', ', '.join(entities))
            print('Transcript:', ' '.join(transcript))
        except grpc.aio.AioRpcError as e:
            print('Error in SLU', str(e.code()), e.details())

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

speechly_api-0.4.5.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

speechly_api-0.4.5-py3-none-any.whl (34.9 kB view details)

Uploaded Python 3

File details

Details for the file speechly_api-0.4.5.tar.gz.

File metadata

  • Download URL: speechly_api-0.4.5.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for speechly_api-0.4.5.tar.gz
Algorithm Hash digest
SHA256 fa267ecc0857241d6e5a68e4efb43d79a3fe62cd9d9c38b80d0af3843a3881db
MD5 d07f10278e895cd2985f0acccf56652c
BLAKE2b-256 261fbbe1602449bad60754208f62819798f6db6b58ddde34fd2cb1c7e138dd66

See more details on using hashes here.

File details

Details for the file speechly_api-0.4.5-py3-none-any.whl.

File metadata

  • Download URL: speechly_api-0.4.5-py3-none-any.whl
  • Upload date:
  • Size: 34.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6

File hashes

Hashes for speechly_api-0.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 42506f3c85ffef9c7e96befd9bf61f6655c0f4871c47a9e479ef48ab9bc8e7cc
MD5 3bf4f9a61f415a8b401b4dd8428440d6
BLAKE2b-256 07ec89484b6c0ba863c860ae8580b72b8c538771fa580b8e665d27838cf29955

See more details on using hashes here.

Supported by

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