Have you ever wondered how to do live voice-to-text transcription with Python? We'll use Flask 2.0 and Deepgram to achieve our goal in this article.
Flask 2.0 is a familiar, lightweight, micro web framework that is very flexible. It doesn't make decisions for us, meaning we are free to choose which database, templating engine, etc., to use without lacking functionality. Deepgram uses AI speech recognition to do real-time audio transcription, and we'll be using our Python SDK.
The final code for this project is located here in Github, if you want to jump ahead.
Getting Started
Before we start, it's essential to generate a Deepgram API key to use in our project. We can go here. For this tutorial, we'll be using Python 3.10, but Deepgram supports some earlier versions of Python as well. Since we're using async in Flask, you'll need to have Python 3.7 or higher. We'll also need to set up a virtual environment to hold our project. We can read more about those here and how to create one.
Install Dependencies
Create a folder directory to store all of our project files, and inside of it, create a virtual environment. Ensure our virtual environment is activated, as described in the article in the previous section. Make sure that all of the dependencies get installed inside that environment.
For a quick reference, here are the commands we need to create and activate our virtual environment:
We need to install the following dependencies from our terminal:
The latest version of Flask
The Deepgram Python SDK
The dotenv library, which helps us work with our environment variables
The aiohttp-wsgi, which allows us to work with WebSockets in our WSGI application
Create a Flask Application
Let's get a starter Flask application up and running that renders an HTML page so that we can progress on our live transcription project.
Create a file called main.py inside our project and a templates folder with an HTML file called index.html.
The main.py file will hold our Python code.
Lastly, we'll store our HTML file inside the templates folder and hold our HTML markup here.
We have to export it into an environment variable to run the application. In our terminal, type the following:
If we start our development server from the terminal to run the project using flask run, the index.html page renders in the browser.
Add Deepgram API Key
Our API Key will allow access to use Deepgram. Let's create a .env file that will store our key. When we push our code to Github, hide our key, make sure to add this to our .gitignore file.
In our file, add the following environment variable with our Deepgram API key, which we can grab here:
The below code shows how to load our key into the project and access it in main.py:
Get Mic Data From Browser
Our next step is to get the microphone data from the browser, which will require a little JavaScript.
Use this code inside the <script></script> tag in index.html to access data from the user's microphone.
If you want to learn more about working with the mic in the browser, please check out this post.
Websocket Connection Between Server and Browser
We'll need to work with WebSockets in our project. We can think of WebSockets as a connection between a server and a client that stays open and allows sending continuous messages back and forth.
The first WebSocket connection is between our Python server that holds our Flask application and our browser client. In this project, we'll use aiohttp to handle the WebSocket server.
We need to create a WebSocket endpoint that listens to our Flask web server code for client connections. In the main.py file, add the following code:
This code accepts a WebSocket connection between the server and the client. As long as the connection stays open, we will receive bytes and wait until we get a message from the client. We're defining a variable called deepgram_socket, which calls a function process_audio and opens the connection to Deepgram. In this user-defined method, we'll also connect to Deepgram. While the server and browser connection stays open, we'll wait for messages and send data.
In index.html, this code listens for a client connection then connects to the client like so:
Websocket Connection Between Server and Deepgram
We need to establish a connection between our central Flask server and Deepgram to get the audio and do our real-time transcription. Add this code to our main.py file.
This code adds a route to the endpoint listen to the socket function. The equivalent of this is app.route in Flask.
Next, let's create our functions to process the audio, get the transcript from that audio and connect to Deepgram. In our main.py, add this code.
The process_audio function takes fast_socket as an argument, which will keep the connection open between the client and the Flask server. We also connect to Deepgram and pass in the get_transcript function. This function gets the transcript and sends it back to the client.
The connect_to_deepgram function creates a socket connection to deepgram, listens for the connection to close, and gets incoming transcription objects.
Lastly, in our index.html, we need to receive and obtain data with the below events. Notice they are getting logged to our console. If you want to know more about what these events do, check out this blog post.
Let's start our application and start getting real-time transcriptions. From our terminal, run python main.py and pull up our localhost on port 5555, http://127.0.0.1:5555/. If we haven't already, allow access to our microphone. Start speaking, and we should see a transcript like the one below:
Congratulations on building a real-time transcription project with Flask and Deepgram. You can find the code here with instructions on how to run the project.
Learn more about Deepgram
Sign up for a Deepgram account and get $200 in Free Credit (up to 45,000 minutes), absolutely free. No credit card needed!
We encourage you to explore Deepgram by checking out the following resources:
Unlock language AI at scale with an API call.
Get conversational intelligence with transcription and understanding on the world's best speech AI platform.