Transcribe MP3 Audio Using AssemblyAI with Streamlit
Set up a Streamlit app to transcribe MP3 audio files using AssemblyAI
Prerequisite You should have an API key from AssemblyAI and have it stored in a .env file.
Introduction
This guide demonstrates how to set up a Streamlit app to transcribe MP3 audio files using AssemblyAI. You’ll learn how to set up your environment, configure your API key, and create a simple app to transcribe audio from a given URL.
Setup
Step 1: Install Required Packages
First, install the necessary packages using pip:
pip install streamlit assemblyai python-dotenv
Step 2: Create a .env File
Create a .env
file in the project root with your AssemblyAI API key:
ASSEMBLY_AI_API_KEY=your_assemblyai_api_key
Step 3: Create the Python Script
Create a Python script named main.py
with the following content:
import streamlit as st
import assemblyai as aai
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Get API key from environment variables
ASSEMBLYAI_API_KEY = os.getenv('ASSEMBLYAI_API_KEY')
aai.settings.api_key = ASSEMBLYAI_API_KEY
def transcribe_audio(audio_url):
# Create a Transcriber object
transcriber = aai.Transcriber()
# Transcribe the audio file from the URL
transcript = transcriber.transcribe(audio_url)
paragraphs = transcript.get_paragraphs()
return paragraphs
st.title('MP3 Transcription using AssemblyAI')
# MP3 URL Input
mp3_url = st.text_input('Enter MP3 URL')
if st.button('Transcribe'):
if not mp3_url:
st.error('Please enter a valid MP3 URL.')
else:
with st.spinner('Transcribing...'):
# Transcribe the audio file using the URL
paragraphs = transcribe_audio(mp3_url)
for paragraph in paragraphs:
st.write(paragraph.text)
Step 4: Run the Streamlit App
Ensure you have the .env
file in the same directory as the script. Then, execute the Streamlit app:
streamlit run main.py
Conclusion
You have successfully set up a Streamlit app to transcribe MP3 audio files using AssemblyAI! This guide provided a basic example to get you started. You can now expand on this by customizing the transcription process and enhancing the app’s functionality.