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.
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 pltdef 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.