AssemblyAI Transcription
Transcribe Audio Using the AssemblyAI API
Prerequisite You should have an API key from AssemblyAI and have it stored in a .env file.
Introduction
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.
Setup
Step 1: Install Required Packages
First, install the necessary packages using pip:
pip install python-dotenv assemblyai
Step 2: Create a .env File
Create a .env
file in the project root with your AssemblyAI API key:
ASSEMBLYAI_API_KEY=your_assemblyai_api_key
Transcribing an Audio File
Step 3: Create the Python Script
Create a Python script named assemblyai-transcription.py
with the following content:
import os
from dotenv import load_dotenv
import assemblyai as aai
# Load environment variables from .env file
load_dotenv()
# Get AssemblyAI API key from environment variables
aai.settings.api_key = os.getenv('ASSEMBLYAI_API_KEY')
# Initialize the transcriber
transcriber = aai.Transcriber()
# Define the audio URL
audio_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 configuration
config = aai.TranscriptionConfig(speaker_labels=True)
# Transcribe the audio file
transcript = transcriber.transcribe(audio_url, config)
# Print the transcription text and ID
print(transcript.text)
print(transcript.id)
# Print the speaker labels and their corresponding text
for utterance in transcript.utterances:
print(f"Speaker {utterance.speaker}: {utterance.text}")
Step 4: Run the Script
Ensure you have the .env
file in the same directory as the script. Then, execute the script:
python assemblyai-transcription.py
Conclusion
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.