Use LeMUR with Streaming Speech-to-Text (STT)

This script is modified to contain a global variable conversation_data that accumulates the transcribed text in the on_data function. Once the transcription session is closed, the on_close function sends conversation_data to LeMUR for analysis using LeMUR’s input_text parameter.

Quickstart

1import assemblyai as aai
2
3aai.settings.api_key = "YOUR-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_data(transcript: aai.RealtimeTranscript):
11 "This function is called when a new transcript has been received."
12
13 global conversation_data
14
15 if not transcript.text:
16 return
17
18 if isinstance(transcript, aai.RealtimeFinalTranscript):
19 print(transcript.text, end="\r\n")
20 conversation_data += f"{transcript.text} \n"
21 else:
22 print(transcript.text, end="\r")
23
24def on_error(error: aai.RealtimeError):
25 "This function is called when the connection has been closed."
26
27 print("An error occured:", error)
28
29def on_close():
30 "This function is called when the connection has been closed."
31 global conversation_data
32 print("Closing Session")
33 result = aai.Lemur().task(
34 "You are a helpful coach. Provide an analysis of the transcript "
35 "and offer areas to improve with exact quotes. Include no preamble. "
36 "Start with an overall summary then get into the examples with feedback.",
37 input_text=conversation_data
38 )
39
40 print(result.response)
41
42# Create the Streaming Speech-to-Text transcriber
43transcriber = aai.RealtimeTranscriber(
44 on_data=on_data,
45 on_error=on_error,
46 sample_rate=44_100,
47 on_open=on_open, # optional
48 on_close=on_close, # optional
49)
50
51conversation_data = ""
52
53# Start the connection
54transcriber.connect()
55
56# Open a microphone stream
57microphone_stream = aai.extras.MicrophoneStream()
58
59# Press CTRL+C to abort
60transcriber.stream(microphone_stream)
61
62transcriber.close()

Step-by-Step Instructions

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.

$brew install portaudio
>
>pip 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)

In our on_data function, we append the transcript text to the conversation_data variable.

1def on_data(transcript: aai.RealtimeTranscript):
2 "This function is called when a new transcript has been received."
3
4 global conversation_data
5
6 if not transcript.text:
7 return
8
9 if isinstance(transcript, aai.RealtimeFinalTranscript):
10 print(transcript.text, end="\r\n")
11 conversation_data += f"{transcript.text} \n"
12 else:
13 print(transcript.text, end="\r")

In our on_close function, we send the accumulated conversation_data to LeMUR for analysis.

1def on_close():
2 "This function is called when the connection has been closed."
3 global conversation_data
4 print("Closing Session")
5 result = aai.Lemur().task(
6 "You are a helpful coach. Provide an analysis of the transcript "
7 "and offer areas to improve with exact quotes. Include no preamble. "
8 "Start with an overall summary then get into the examples with feedback.",
9 input_text=conversation_data
10 )
11
12 print(result.response)

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,
6 on_close=on_close,
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()