Translate Streaming STT Transcripts with LeMUR

In this guide, you’ll learn how to implement real-time translation using AssemblyAI’s Streaming model and LeMUR framework.

Quickstart

1import assemblyai as aai
2
3aai.settings.api_key = "API_KEY"
4
5def on_open(session_opened: aai.RealtimeSessionOpened):
6 "This function is called when the connection has been established."
7
8 print("Session ID:", session_opened.session_id)
9
10def on_error(error: aai.RealtimeError):
11 "This function is called when an error has been thrown."
12
13 print("An error occurred:", error)
14
15def on_close():
16 "This function is called when the connection has been closed."
17
18 print("Closing Session")
19
20
21def on_data(transcript: aai.RealtimeTranscript):
22 "This function is called when a new transcript has been received."
23
24 if not transcript.text:
25 return
26
27 if isinstance(transcript, aai.RealtimeFinalTranscript):
28 result = aai.Lemur().task("Translate the following text into Spanish. Do not write a preamble. Just return the translated text.", input_text=transcript.text)
29 print(result.response, end="\r\n")
30
31transcriber = aai.RealtimeTranscriber(
32 on_data=on_data,
33 on_error=on_error,
34 sample_rate=44_100,
35 on_open=on_open,
36 on_close=on_close,
37)
38
39# Start the connection
40transcriber.connect()
41
42# Open a microphone stream
43microphone_stream = aai.extras.MicrophoneStream()
44
45# Press Ctrl+C to abort
46transcriber.stream(microphone_stream)
47
48transcriber.close()

Step-by-Step Instructions

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for a free account and get your API key from your dashboard. Please note that this feature is available for paid accounts only. If you’re on the free plan, you’ll need to upgrade.

Install the AssemblyAI Python SDK. To use the microphone stream, you need to install the extras for this SDK. Mac and Linux users also need to install portaudio before installing the extras.

1# (Mac)
2brew install portaudio
3
4# (Debian/Ubuntu)
5apt install portaudio19-dev
6
7pip install "assemblyai[extras]"

Import the assemblyai package and set the API key.

1import assemblyai as aai
2
3aai.settings.api_key = "API_KEY"

Create functions to handle different events during transcription.

1def on_open(session_opened: aai.RealtimeSessionOpened):
2 "This function is called when the connection has been established."
3
4 print("Session ID:", session_opened.session_id)
5
6def on_error(error: aai.RealtimeError):
7 "This function is called when an error has been thrown."
8
9 print("An error occurred:", error)
10
11def on_close():
12 "This function is called when the connection has been closed."
13
14 print("Closing Session")

In our on_data function, we’re going to call LeMUR to perform real-time translation using the Claude 3 Haiku model. Other models work just as well (if not better, in the case of 3.5 Sonnet), but Haiku has proven to be the fastest at translation tasks. Note that LeMUR does only officially support English, so your mileage may vary with how well the translations turn out.

1def on_data(transcript: aai.RealtimeTranscript):
2 "This function is called when a new transcript has been received."
3
4 if not transcript.text:
5 return
6
7 if isinstance(transcript, aai.RealtimeFinalTranscript):
8 result = aai.Lemur().task(
9 "Translate the following text into Spanish. Do not write a preamble. Just return the translated text.",
10 input_text=transcript.text,
11 final_model=aai.LemurModel.claude3_haiku)
12 print(result.response, end="\r\n")

Create a RealtimeTranscriber to set up the Streaming STT configuration.

1transcriber = aai.RealtimeTranscriber(
2 on_data=on_data,
3 on_error=on_error,
4 sample_rate=44_100,
5 on_open=on_open, # Optional
6 on_close=on_close, # Optional
7)

Begin the Streaming STT transcription process.

1# Start the connection
2transcriber.connect()
3
4# Open a microphone stream
5microphone_stream = aai.extras.MicrophoneStream()
6
7# Press Ctrl+C to abort
8transcriber.stream(microphone_stream)
9
10transcriber.close()