Transcribing a Local Audio File using AssemblyAI
Learn how to transcribe a local audio file using AssembyAI.
We'll cover the following
Transcribing mp3 files
Before building the UI, we need a few helper functions which we can use to upload the file to AssemblyAI’s server for the model to work and return the transcribed text.
The helper functions code is written in the transcribe.py
file.
Import the required modules
The following piece of code should be present at the beginning of the transcribe.py
file.
import os
from dotenv import load_dotenv
import requests
Helper function 1: Uploading a local audio file to AssemblyAI
The first function we need to write is a method to upload an audio file. This function should be present inside the transcribe.py
file.
The AssemblyAI model expects the file to be accessible via a URL
. Therefore, we’ll need to upload the audio file to blob storage to make it accessible via a URL
. Fortunately, AssemblyAI provides a quick and easy way to do this.
We need to make a POST
request to the following AssemblyAI API endpoint: https://api.assemblyai.com/v2/upload
The response will contain a temporary URL
to the file, we can pass this URL
back to the AssemblyAI transcript
API endpoint. The URL
is a private URL
accessible only to the AssemblyAI servers.
All the uploads are immediately deleted after transcription and never stored.
We will use Python’s request
library that we installed earlier to make the POST
request.
def get_url(token,data):
'''
Parameter:
token: The API key
data : The File Object to upload
Return Value:
url : Url to uploaded file
'''
headers = {‘authorization’: token}
response = requests.post(‘https://api.assemblyai.com/v2/upload',
headers=headers,
data=data)
url = response.json()[“upload_url”]
print(“Uploaded File and got temporary URL to file”)
return URL
Explanation
-
The function accepts the API
token
and the fileobject
as parameters -
We make a
POST
request to the AssemblyAI Upload API endpoint mentioned above and include the API token and the file object as a part of the request body. -
The response object contains the
URL
to the uploaded file. ThisURL
is returned by the function.
Get hands-on with 1400+ tech skills courses.