speechmatics.client¶
Wrapper library to interface with Real-time ASR v2 API. Based on http://asyncio.readthedocs.io/en/latest/producer_consumer.html
- class speechmatics.client.WebsocketClient(connection_settings_or_auth_token: Optional[Union[str, speechmatics.models.ConnectionSettings]] = None)[source]¶
Manage a transcription session with the server.
The best way to interact with this library is to instantiate this client and then add a set of handlers to it. Handlers respond to particular types of messages received from the server.
- Parameters
connection_settings (speechmatics.models.ConnectionSettings) – Settings for the WebSocket connection, including the URL of the server.
- add_event_handler(event_name, event_handler)[source]¶
Add an event handler (callback function) to handle an incoming message from the server. Event handlers are passed a copy of the incoming message from the server. If event_name is set to ‘all’ then the handler will be added for every event.
For example, a simple handler that just prints out the
speechmatics.models.ServerMessageType.AddTranscript
messages received:>>> client = WebsocketClient( ConnectionSettings(url="wss://localhost:9000")) >>> handler = lambda msg: print(msg) >>> client.add_event_handler(ServerMessageType.AddTranscript, handler)
- Parameters
event_name (str) – The name of the message for which a handler is being added. Refer to
speechmatics.models.ServerMessageType
for a list of the possible message types.event_handler (Callable[[dict], None]) – A function to be called when a message of the given type is received.
- Raises
ValueError – If the given event name is not valid.
- add_middleware(event_name, middleware)[source]¶
Add a middleware to handle outgoing messages sent to the server. Middlewares are passed a reference to the outgoing message, which they may alter. If event_name is set to ‘all’ then the handler will be added for every event.
- Parameters
event_name (str) – The name of the message for which a middleware is being added. Refer to the V2 API docs for a list of the possible message types.
middleware (Callable[[dict, bool], None]) – A function to be called to process an outgoing message of the given type. The function receives the message as the first argument and a second, boolean argument indicating whether or not the message is binary data (which implies it is an AddAudio message).
- Raises
ValueError – If the given event name is not valid.
- get_language_pack_info() dict [source]¶
Get the language_pack_info which is a subset of information from the manifest in the language pack which we expose to end users.
Can be None if this field has not yet been set - i.e. if the RecognitionStarted message has not been received yet.
- async run(stream, transcription_config: speechmatics.models.TranscriptionConfig, audio_settings=AudioSettings(encoding=None, sample_rate=44100, chunk_size=4096), from_cli=False)[source]¶
Begin a new recognition session. This will run asynchronously. Most callers may prefer to use
run_synchronously()
which will block until the session is finished.- Parameters
stream (io.IOBase) – File-like object which an audio stream can be read from.
transcription_config (speechmatics.models.TranscriptionConfig) – Configuration for the transcription.
audio_settings (speechmatics.models.AudioSettings) – Configuration for the audio stream.
from_cli (bool) – Indicates whether the caller is the command-line interface or not.
- Raises
Exception – Can raise any exception returned by the consumer/producer tasks.
- run_synchronously(*args, timeout=None, **kwargs)[source]¶
Run the transcription synchronously. :raises asyncio.TimeoutError: If the given timeout is exceeded.
- stop()[source]¶
Indicates that the recognition session should be forcefully stopped. Only used in conjunction with run. You probably don’t need to call this if you’re running the client via
run_synchronously()
.
- update_transcription_config(new_transcription_config)[source]¶
Updates the transcription config used for the session. This results in a SetRecognitionConfig message sent to the server.
- Parameters
new_transcription_config (speechmatics.models.TranscriptionConfig) – The new config object.