[Hands-on] Building a Real-Time AI Voice Bot

...AssemblyAI + OpenAI + ElevenLabs.

๐Ÿ‘‰
Hey! Enjoy our free data science newsletter! Subscribe below and receive a free data science PDF (530+ pages) with 150+ core data science and machine learning lessons.

TODAY'S ISSUE

TODAYโ€™S DAILY DOSE OF DATA SCIENCE

[Hands-on] Building a Real-Time AI Voice Bot

AssemblyAIโ€‹ has always been my go-to for building speech-driven AI applications.

Itโ€™s an AI transcription platform that provides state-of-the-art AI models for any task related to speech & audio understanding.

Today, letโ€™s build the following real-time transcription apps with AssemblyAI.

0:00
/1:08

The workflow is depicted below, and the video above shows the final outcome.

  • โ€‹AssemblyAI for transcribing speech input from the user [speech-to-text].
  • OpenAI for generating response [text-to-text].
  • ElevenLabs for speech generation [text-to-speech].

The entire code is available here: โ€‹Voice Bot Demo GitHubโ€‹.

Letโ€™s build the app!

VOICE BOT APP

Prerequisites and Importsโ€‹

Start by installing the following libraries:

Next, create a file app.py and import the following libraries:

VOICE BOT APP

Implementation

Next, we define a class, initialize the clients involved in our appโ€”AssemblyAI, OpenAI, and ElevenLabs, and the interaction history:

Now think about the logical steps we would need to implement in this app:

  1. The voice bot will speak and introduce itself.
  2. Then, the user will speak, and AssemblyAI will transcribe it in real-time.
  3. The transcribed input will be sent to OpenAI to generate a text response.
  4. ElevenLabs will then verbalize the response.
  5. Back to Step 2, all while maintaining the interaction history in the self.interaction object defined in the __init__ method so that OpenAI has the entire context while producing a response in Step 3.

Thus, we need at least four more methods in AI_Assistant class:

  • generate_audio โ†’ Accepts some text and uses ElevenLabs to verbalize it:
  • generate_ai_response โ†’ Accepts the transcribed input, adds it to the interaction, and sends it to OpenAI to produce a response. Finally, it should pass this response to the generate_audio method:
  • start_transcription โ†’ Starts the microphone to record audio and transcribe it in real-time with AssemblyAI:
  1. on_data โ†’ the method to invoke upon receiving a transcript from AssemblyAI. Here, we invoke the generate_ai_response method:
  2. on_error โ†’ the method to invoke in case of an error (you can also reinvoke the start_transcription method).
  3. on_open โ†’ the method to invoke when a connection has been established with AssemblyAI.
  4. on_close โ†’ the method to invoke when closing a connection.
  5. These three methods are implemented below:
  • Lastly, we have stop_transcription โ†’ Stops the microphone and let the OpenAI generate a response using the method below.

Done!

With that, we have implemented the class.

VOICE BOT APP

Demo

Finally, we instantiate an object of this class and start the app:

Done!

This produces the output shown in the video below:

That was simple, wasnโ€™t it?

You can find all the code and instructions to run in this GitHub repo: โ€‹Voice Bot Demo GitHubโ€‹.

VOICE BOT APP

A departing note

I first used โ€‹AssemblyAIโ€‹ two years ago, and in my experience, it has the most developer-friendly and intuitive SDKs to integrate speech AI into applications.

โ€‹AssemblyAIโ€‹ first trained Universal-1 on 12.5 million hours of audio, outperforming every other model in the industry (from Google, OpenAI, etc.) across 15+ languages.

Now, they released โ€‹Universal-2โ€‹, their most advanced speech-to-text model yet.

Hereโ€™s how โ€‹Universal-2โ€‹ compares with Universal-1:

  • 24% improvement in proper nouns recognition
  • 21% improvement in alphanumeric accuracy
  • 15% better text formatting

Its performance compared to other popular models in the industry is shown below:

Isnโ€™t that impressive?

I love โ€‹AssemblyAIโ€™sโ€‹ mission of supporting developers in building next-gen voice applications in the simplest and most effective way possible.

They have already made a big dent in speech technology, and Iโ€™m eager to see how they continue from here.

Get started with:

Their API docs are available here if you want to explore their services: โ€‹AssemblyAI API docsโ€‹.

๐Ÿ™Œ Also, a big thanks to โ€‹AssemblyAIโ€‹, who very kindly partnered with us on this post and let us use their industry-leading AI transcription services.

๐Ÿ‘‰ Over to you: What would you use โ€‹AssemblyAIโ€‹ for?

THAT'S A WRAP

No-Fluff Industry ML resources to

Succeed in DS/ML roles

At the end of the day, all businesses care about impact. Thatโ€™s it!

  • Can you reduce costs?
  • Drive revenue?
  • Can you scale ML models?
  • Predict trends before they happen?

We have discussed several other topics (with implementations) in the past that align with such topics.

Here are some of them:

  • Learn sophisticated graph architectures and how to train them on graph data in this crash course.
  • So many real-world NLP systems rely on pairwise context scoring. Learn scalable approaches here.
  • Run large models on small devices using Quantization techniques.
  • Learn how to generate prediction intervals or sets with strong statistical guarantees for increasing trust using Conformal Predictions.
  • Learn how to identify causal relationships and answer business questions using causal inference in this crash course.
  • Learn how to scale and implement ML model training in this practical guide.
  • Learn 5 techniques with implementation to reliably test ML models in production.
  • Learn how to build and implement privacy-first ML systems using Federated Learning.
  • Learn 6 techniques with implementation to compress ML models.

All these resources will help you cultivate key skills that businesses and companies care about the most.

Our newsletter puts your products and services directly in front of an audience that matters โ€” thousands of leaders, senior data scientists, machine learning engineers, data analysts, etc., around the world.

Get in touch today โ†’


Join the Daily Dose of Data Science Today!

A daily column with insights, observations, tutorials, and best practices on data science.

Get Started!
Join the Daily Dose of Data Science Today!

Great! Youโ€™ve successfully signed up. Please check your email.

Welcome back! You've successfully signed in.

You've successfully subscribed to Daily Dose of Data Science.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.