Skip to main content

Python SDK

Set Up

  1. Create an account on the Speechmatics On-Demand Portal here.
  2. Navigate to Manage > API Keys page in the Speechmatics On-Demand Portal.
  3. Enter a name for your API key and store your API key somewhere safe.

The Speechmatics Flow library and CLI can found on GitHub and installed using pip:

pip3 install speechmatics-flow

Code example

Speak with Flow with this Python code example. Just copy in your API key and file name to get started!

1import asyncio
2import io
3import ssl
4import sys
5
6import pyaudio
7
8from speechmatics_flow.client import WebsocketClient
9from speechmatics_flow.models import (
10    ConnectionSettings,
11    Interaction,
12    AudioSettings,
13    ConversationConfig,
14    ServerMessageType,
15)
16
17AUTH_TOKEN = "Place your auth token here"
18
19# Create a websocket client
20client = WebsocketClient(
21    ConnectionSettings(
22        url="wss://flow.api.speechmatics.com/v1/flow",
23        auth_token=AUTH_TOKEN,
24    )
25)
26
27# Create a buffer to store binary messages sent from the server
28audio_buffer = io.BytesIO()
29
30
31# Create callback function which adds binary messages to audio buffer
32def binary_msg_handler(msg: bytes):
33    if isinstance(msg, (bytes, bytearray)):
34        audio_buffer.write(msg)
35
36
37# Register the callback which will be called
38# when the client receives an audio message from the server
39client.add_event_handler(ServerMessageType.AddAudio, binary_msg_handler)
40
41
42async def audio_playback(buffer):
43    """Read from buffer and play audio back to the user"""
44    p = pyaudio.PyAudio()
45    stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, output=True)
46    try:
47        while True:
48            # Get the current value from the buffer
49            audio_to_play = buffer.getvalue()
50            # Only proceed if there is audio data to play
51            if audio_to_play:
52                # Write the audio to the stream
53                stream.write(audio_to_play)
54                buffer.seek(0)
55                buffer.truncate(0)
56            # Pause briefly before checking the buffer again
57            await asyncio.sleep(0.05)
58    finally:
59        stream.close()
60        stream.stop_stream()
61        p.terminate()
62
63
64async def main():
65    tasks = [
66        # Use the websocket to connect to Flow Service and start a conversation
67        asyncio.create_task(
68            client.run(
69                interactions=[Interaction(sys.stdin.buffer)],
70                audio_settings=AudioSettings(),
71                conversation_config=ConversationConfig(),
72            )
73        ),
74        # Run audio playback handler which streams audio from audio buffer
75        asyncio.create_task(audio_playback(audio_buffer)),
76    ]
77
78    await asyncio.gather(*tasks)
79
80
81asyncio.run(main())
82