Skip to main content

Documentation Index

Fetch the complete documentation index at: https://assemblyai.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

In this guide, we’ll show you how to plot a speaker timeline with matplotlib, using results from the speaker diarization model.

Quickstart

import assemblyai as aai
import matplotlib.pyplot as plt

aai.settings.api_key = "YOUR_API_KEY"

config = aai.TranscriptionConfig(speaker_labels=True, speech_models=["universal-3-pro", "universal-2"])
transcriber = aai.Transcriber()
transcript = transcriber.transcribe("./my-audio.mp3", config)
utterances = transcript.utterances

def plot_speaker_timeline(utterances):
    fig, ax = plt.subplots(figsize=(12, 4))
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
    speaker_colors = {}

    for utterance in utterances:
        start = utterance.start / 60000 # in minutes
        end = utterance.end / 60000 # in minutes
        speaker = utterance.speaker

        if speaker not in speaker_colors:
            speaker_colors[speaker] = colors[len(speaker_colors) % len(colors)] # set a colour for each new speaker

        ax.barh(speaker, end - start, left=start, color=speaker_colors[speaker], height=0.4) # create horizontal bar plot

    ax.set_xlabel('Time (mins)')
    ax.set_ylabel('Speakers')
    ax.set_title('Speaker Timeline')
    ax.grid(True, which='both', linestyle='--', linewidth=0.5)
    plt.show()

plot_speaker_timeline(utterances)

Get Started

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.

Step-by-Step Instructions

Install the SDK.
pip install -U assemblyai
!pip install -U matplotlib
Import the assemblyai package and set the API key.
import assemblyai as aai

aai.settings.api_key = "YOUR_API_KEY"
Create a TranscriptionConfig object and set speaker labels to True.
config = aai.TranscriptionConfig(speaker_labels=True, speech_models=["universal-3-pro", "universal-2"])
Create a Transcriber object.
transcriber = aai.Transcriber()
Use the Transcriber object’s transcribe method and pass in the audio file’s path and config object as parameters. The transcribe method saves the results of the transcription to the Transcriber object’s transcript attribute.
transcript = transcriber.transcribe("./my-audio.mp3", config)
Alternatively, you can use an audio URL available on the internet.
Extract the utterances from the transcript and set this to utterances.
utterances = transcript.utterances
Import the matplotlib.pyplot library. Then use the following plot_speaker_timeline function which results in a plot image of the speaker timeline. This function extracts the start and end timestamps of each utterance per speaker and plots the data onto the horizontal bar chart. The X and Y axis are labelled accordingly.
import matplotlib.pyplot as plt

def plot_speaker_timeline(utterances):
    fig, ax = plt.subplots(figsize=(12, 4))
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
    speaker_colors = {}

    for utterance in utterances:
        start = utterance.start / 60000 # in minutes
        end = utterance.end / 60000 # in minutes
        speaker = utterance.speaker

        if speaker not in speaker_colors:
            speaker_colors[speaker] = colors[len(speaker_colors) % len(colors)] # set a colour for each new speaker

        ax.barh(speaker, end - start, left=start, color=speaker_colors[speaker], height=0.4) # create horizontal bar plot

    ax.set_xlabel('Time (mins)')
    ax.set_ylabel('Speakers')
    ax.set_title('Speaker Timeline')
    ax.grid(True, which='both', linestyle='--', linewidth=0.5)
    plt.show()
Finally, call the plot_speaker_timeline function passing utterances as a parameter to see the plot image result.
plot_speaker_timeline(utterances)