Search⌘ K
AI Features

Prerequisites and Required Libraries

Explore how to set up the necessary project environment, including Python libraries like Streamlit, Requests, and Python-dotenv, to build a web app that transcribes audio using AssemblyAI's REST API. Learn to create a virtual environment, structure your project directory, and safely manage your API credentials.

In this chapter, we’ll build a web app that can transcribe audio using AssemblyAI and Streamlit.

Transcription

Transcription is the process of converting audio to text. Although we can implement a machine learning model to get the text from audio, it is extremely inconvenient. If we try to train a machine learning model for transcription, we’ll have to deal with the following:

  • Extensive knowledge of audio signal processing is needed to extract features from an audio signal.
  • A large amount of data will have to be mined/scraped from various sources.
  • Knowledge of machine learning libraries such as PyTorch or TensorFlow is required.

Fortunately, AssemblyAI has a free tier version available that lets us transcribe audio by making a few requests.

Requirements

  • An AssemblyAI account (Sign up for free here)

  • An AssemblyAI API key (You can find one here)

  • Basic knowledge of Python 3.5+ (We’ll use Python 3.9 for this chapter.)

  • Although it’s not required, familiarity with the Requests library in Python will be helpful.

Libraries

  • AssemblyAI is used to convert audio to text. It provides a REST API that can be used in any language that makes a REST API call, such as JavaScript, PHP, Python, and so on. We’ll use Python to make requests to the API.

  • We’ll use the Requests library to make requests to AssemblyAI’s REST API.

  • We’ll use the Python-dotenv library to read variables from .env files.

Setting up the project directory

Create a new folder/directory using the command line.

mkdir ASSEMBLYAI

To keep secret credentials secret, it’s good practice to store credentials inside a .env file. We can then use the Python-dotenv library to read the credentials from the .env file. We can also store them in environment variables if preferred.

Inside the new directory you created, ASSEMBLYAI, create two new Python files and a .env file

If you’re using Windows, use the following statement:

New-Item main.py, transcribe.py, .env

If you’re using macOS or Linux, use the following statement:

touch main.py && touch transcribe.py && touch .env

The file main.py will contain all the code related to the Streamlit UI while the file transcribe.py will contain the helper functions and the code which interacts with AssemblyAI’s API.

Setting up the project environment

Ensure that you’re in the ASSEMBLYAI directory. In case you aren’t there already, use the cd command to change the directory.

cd ASSEMBLYAI

Commands for windows users

  • To install the virtualenv, write the following command:
    python -m pip install — user virtualenv
    
  • To create the virtual environment, write the following command:
    python -m venv venv
    
  • To activate the venv, write the following command:
    venv/Scripts/activate
    

Commands for macOS or Linux users

  • To install the virtualenv, write the following command:
    python3 -m pip install — user virtualenv
    
  • To create the virtual environment, write the following command:
    Python3 -m venv venv
    
  • To activate the venv, write the following command:
    source venv/bin/activate
    

To install the Requests, Steamlit and Python-dotenv libraries respectively, we can enter this one line on the command line:

pip install streamlit, requests, python-dotenv

This will install the up-to-date libraries required.

The file structure should look like as shown in the figure below.

Add the API key to the .env file

  • Open the .env file that you created in the “Setting up the project environment” section.

  • Add the following line: API_TOKEN = “Your API Key”.

  • Replace the string “Your API Key” with the API key given to you by Assembly AI.