This guide demonstrates how to transcribe an audio file and ask questions using the AssemblyAI Lemur model. You’ll learn how to set up your environment, configure your API key, transcribe an audio file, and ask questions on the transcript.
assemblyai-lemur-example.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 a Transcriber object to handle transcription taskstranscriber = aai.Transcriber()# To transcribe a new audio file, uncomment the next line and provide the file URL# transcript = transcriber.transcribe("https://example.org/customer.mp3")# If you already have a transcript ID, you can retrieve the transcript directly# Replace the ID with your actual transcript IDtranscript = aai.Transcript.get_by_id("6frp2s8rav-72b6-4b3a-8605-60c5ef112ef8")print(transcript)# Print the transcript to verify its content# Define a list of questions to ask on the transcript using the Lemur modelquestions =[ aai.LemurQuestion( question="What kind of equipment does Cassidy use?", answer_format="Provide a Summary in a paragraph format."), aai.LemurQuestion( question="Who are the speakers?", answer_format="Short sentence"), aai.LemurQuestion( question="Are there any recommendations or tips for people getting started with live streaming and handling fears and jitters?", answer_format="Bullet list")]# Use the Lemur model to ask the defined questions on the transcriptresult = transcript.lemur.question(questions)# Loop through each question and its corresponding answer, and print themfor q in result.response:print(f"Question: {q.question}")print(f"Answer: {q.answer}")
Create a Python script named assemblyai-lemur-example.py with the following content:
assemblyai-lemur-example.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 a Transcriber object to handle transcription taskstranscriber = aai.Transcriber()# To transcribe a new audio file, uncomment the next line and provide the file URL# transcript = transcriber.transcribe("https://example.org/customer.mp3")# If you already have a transcript ID, you can retrieve the transcript directly# Replace the ID with your actual transcript IDtranscript = aai.Transcript.get_by_id("6frp2s8rav-72b6-4b3a-8605-60c5ef112ef8")print(transcript)# Print the transcript to verify its content# Define a list of questions to ask on the transcript using the Lemur modelquestions =[ aai.LemurQuestion( question="What kind of equipment does Cassidy use?", answer_format="Provide a Summary in a paragraph format."), aai.LemurQuestion( question="Who are the speakers?", answer_format="Short sentence"), aai.LemurQuestion( question="Are there any recommendations or tips for people getting started with live streaming and handling fears and jitters?", answer_format="Bullet list")]# Use the Lemur model to ask the defined questions on the transcriptresult = transcript.lemur.question(questions)# Loop through each question and its corresponding answer, and print themfor q in result.response:print(f"Question: {q.question}")print(f"Answer: {q.answer}")
You have successfully transcribed an audio file and asked questions using the AssemblyAI Lemur model! This guide provided a basic example to get you started. You can now expand on this by customizing the questions and handling different types of audio content.