This guide demonstrates how to transcribe an audio file using the AssemblyAI API. You’ll learn how to set up your environment, configure your API key, and transcribe an audio file.
assemblyai-transcription.py
import osfrom dotenv import load_dotenvimport assemblyai as aai# Load environment variables from .env fileload_dotenv()# Get AssemblyAI API key from environment variablesaai.settings.api_key = os.getenv('ASSEMBLYAI_API_KEY')# Initialize the transcribertranscriber = aai.Transcriber()# Define the audio URLaudio_url =("https://cdn.simplecast.com/audio/3fa5a74f-0522-4a05-a00e-d880fe923dac/episodes/6eb635c7-2622-4902-a268-8d41a258c9c8/audio/d70e31a6-19d5-4ad7-ba83-3aee66923b88/default_tc.mp3")# Set the transcription configurationconfig = aai.TranscriptionConfig(speaker_labels=True)# Transcribe the audio filetranscript = transcriber.transcribe(audio_url, config)# Print the transcription text and IDprint(transcript.text)print(transcript.id)# Print the speaker labels and their corresponding textfor utterance in transcript.utterances:print(f"Speaker {utterance.speaker}: {utterance.text}")
Create a Python script named assemblyai-transcription.py with the following content:
assemblyai-transcription.py
import osfrom dotenv import load_dotenvimport assemblyai as aai# Load environment variables from .env fileload_dotenv()# Get AssemblyAI API key from environment variablesaai.settings.api_key = os.getenv('ASSEMBLYAI_API_KEY')# Initialize the transcribertranscriber = aai.Transcriber()# Define the audio URLaudio_url =("https://cdn.simplecast.com/audio/3fa5a74f-0522-4a05-a00e-d880fe923dac/episodes/6eb635c7-2622-4902-a268-8d41a258c9c8/audio/d70e31a6-19d5-4ad7-ba83-3aee66923b88/default_tc.mp3")# Set the transcription configurationconfig = aai.TranscriptionConfig(speaker_labels=True)# Transcribe the audio filetranscript = transcriber.transcribe(audio_url, config)# Print the transcription text and IDprint(transcript.text)print(transcript.id)# Print the speaker labels and their corresponding textfor utterance in transcript.utterances:print(f"Speaker {utterance.speaker}: {utterance.text}")
You have successfully transcribed an audio file using the AssemblyAI API! This guide provided a basic example to get you started. You can now expand on this by customizing the transcription configuration and handling different audio files.