Process Speaker Labels with LeMURs Custom Text Input Parameter

In this guide, we’ll show you how to use AssemblyAI’s LeMUR (Leveraging Large Language Models to Understand Recognized Speech) framework to process Speaker Labels from your transcript using the input_text parameter. The input_text option allows you to modify the transcripts before processing it using LeMUR, and in this example, format Speaker Labels in your LeMUR request.

Quickstart

1import assemblyai as aai
2
3aai.settings.api_key = "YOUR_API_KEY"
4config = aai.TranscriptionConfig(speaker_labels=True)
5transcriber = aai.Transcriber()
6
7transcript = transcriber.transcribe("https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3", config=config)
8
9text = ""
10
11for utt in transcript.utterances:
12 text += f"Speaker {utt.speaker}:\n{utt.text}\n"
13
14result = aai.Lemur().task(
15 '''
16 This is a speaker-labeled transcript of a podcast episode between Michel Martin, NPR host, and Peter DeCarlo, a professor at Johns Hopkins University.
17 Please answer the following questions:
18 1) Who is Speaker A and Speaker B?
19 2) What questions did the podcast host ask?
20 3) What were the main concerns of the podcast guest?"
21 ''',
22 input_text=text,
23 final_model=aai.LemurModel.claude3_5_sonnet,
24)
25
26print(result.response)

Calling LeMUR using transcript_ids is preferred as default. Depending on your use case, you can alternatively use the input_text parameter to call LeMUR with custom formatted transcript data including edited transcripts, speaker-labelled transcripts and more.

Get Started

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for an account and get your API key from your dashboard.

LeMUR features are currently only available to paid users, at two pricing tiers: LeMUR and LeMUR Basic. See pricing for more details.

Step-by-Step Instructions

In this guide, we will include Speaker Labels to the conversation by using the input_text parameter. We will use the Custom Task endpoint to prompt LeMUR. You can use any LeMUR Endpoint and adjust the prompt parameters to suit your project’s needs.

Install the SDK.

1pip install -U assemblyai

Import the assemblyai package and set your API key.

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

Create a TranscriptionConfig with speaker_labels set to True.

1config = aai.TranscriptionConfig(speaker_labels=True)

Use the Transcriber object’s transcribe method and parse the configuratin and the audio file’s path as a parameter . The transcribe method will save the results of the transcription to the Transcriber object’s transcript attribute.

1transcriber = aai.Transcriber()
2
3transcript = transcriber.transcribe("https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3", config=config)

Create an empty string variable text to store the output. Iterate through each utterance in the transcript to append the formatted Speaker Labels to be used as the input_text LeMUR parameter.

1text = ""
2
3for utt in transcript.utterances:
4 text += f"Speaker {utt.speaker}:\n{utt.text}\n"

Run Lemur’s task method on your transcript and parse the prompt and input_text parameters. The text variable contains the formatted Speaker Labels and Text response from your transcript.

The result is stored in response as a string.

1result = aai.Lemur().task(
2 '''
3 This is a speaker-labeled transcript of a podcast episode between Michel Martin, NPR host, and Peter DeCarlo, a professor at Johns Hopkins University.
4 Please answer the following questions:
5 1) Who is Speaker A and Speaker B?
6 2) What questions did the podcast host ask?
7 3) What were the main concerns of the podcast guest?"
8 ''',
9 input_text=text,
10 final_model=aai.LemurModel.claude3_5_sonnet,
11)
12
13print(result.response)

The output will look similar to the example below.