Identify Speaker Names From the Transcript Using LeMUR

In this guide, you’ll learn how to use AssemblyAI’s API to transcribe audio, identify speakers, and infer their names using LeMUR. We’ll walk through the process of configuring the transcriber, submitting a transcript to LeMUR with speaker labels, and generating a mapping of speaker names from the transcript.

This workflow will enable you to have speaker labels with the speaker’s name in your transcripts instead of Speaker A/B.

Before you begin

To complete this tutorial, you need:

$pip install assemblyai

Import the assemblyai and re packages and set your API key:

1import assemblyai as aai
2import re
3
4aai.settings.api_key = "YOUR-API-KEY"

Define a Transcriber, a TranscriptionConfig with speaker_labels set to True. Then, create a transcript.

1transcriber = aai.Transcriber()
2
3audio_url = (
4 "https://www.listennotes.com/e/p/accd617c94a24787b2e0800f264b7a5e/"
5)
6
7config = aai.TranscriptionConfig(speaker_labels=True)
8transcript = transcriber.transcribe(audio_url, config)

Process the transcript with speaker labels:

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

Count the unique speakers, then create a LemurQuestion for each speaker. Lastly, ask LeMUR the questions, specifying text_with_speaker_labels as the input_text.

1# Count the number of unique speaker labels
2unique_speakers = set(utterance.speaker for utterance in transcript.utterances)
3
4questions = []
5for speaker in unique_speakers:
6 questions.append(
7 aai.LemurQuestion(
8 question=f"Who is speaker {speaker}?",
9 answer_format="<First Name> <Last Name (if applicable)>")
10
11 )
12
13result = aai.Lemur().question(
14 questions,
15 input_text=text_with_speaker_labels,
16 final_model=aai.LemurModel.claude3_5_sonnet,
17 context="Your task is to infer the speaker's name from the speaker-labelled transcript"
18)

Map the speaker alphabets to their names from LeMUR:

1speaker_mapping = {}
2
3for qa_response in result.response:
4 pattern = r"Who is speaker (\w)\?"
5 match = re.search(pattern, qa_response.question)
6 if match and match.group(1) not in speaker_mapping.keys():
7 speaker_mapping.update({match.group(1): qa_response.answer})

Print the Transcript with Speaker Names:

1for utterance in transcript.utterances[:10]:
2 speaker_name = speaker_mapping[utterance.speaker]
3 print(f"{speaker_name}: {utterance.text[:50]}...")