Creating a Chatbot with Deepgram and Gro
Introduction
By the end of this tutorial, you will learn how to create a chatbot that can convert text to speech and respond to user queries using Deepgram and Gro API services. The chatbot will sound like a human, and you can integrate it with your website to provide a conversational interface for users.
Services Used
- Deepgram: A text-to-speech and speech-to-text service that is more accurate than AWS, Microsoft Azure, Whisper, Google, and Assembly AI.
- Gro API: An inferencing service for getting results quickly.
Prerequisites
- Python: Download the latest version from python.org.
- Visual Studio Code Editor: Download and install this code editor.
- API Keys: Obtain API keys from Deepgram and Gro API.
Setting Up the Environment
- Open the terminal in Visual Studio Code and create a new environment using
conda create --name envs
. - Activate the environment using
conda activate envs
. - Install the required libraries by running
pip install deepgram-sdk requests python-dotenv flask Croc
.
Configuring Environment Variables
- Create a
requirements.txt
file and install the required libraries. - Set up environment variables for Deepgram API key and Gro API key.
- Paste the API keys into the environment variables.
Code Cleanup
- Start a new Python file and import the required libraries.
- Set up the Deepgram and Gro API services using the API keys.
- Create a function to handle user input and respond with a result.
Setting up the Environment and Creating Python Files
- Create three Python files:
text_to_speech.py
,speech_to_text.py
, andGro_server.py
. - Load up the environment using
conda activate
andJarvis new
. - Install all requirements using
pip install -r requirements.txt
.
Gro Server Code
Gro_server.py
code:- Import necessary libraries:
from gro import Gro
. - Set up environment variables using
from EnV import load
. - Load environment variables using
load
. - Create a new instance of the
Gro
class. - Get the Gro API key.
- Define a new function
execute
that takes in the model, role, user, and prompt as inputs. - Return the responses obtained from the Gro API.
- Test the function by calling
execute
with the prompt "tell me a joke".
- Import necessary libraries:
Text to Speech Code
text_to_speech.py
code:- Import necessary libraries:
from deepgram import clients
. - Load environment variables using
from EnV import load
. - Define a function
text_to_speech
that takes in the text input. - Start up the Deepgram API client using the DG API key.
- Use the voice "Ora Luna" (or other available voices) to convert the text to speech.
- Import necessary libraries:
Connecting the Text to Speech and Gro Server Code
- Import the
execute
function fromGro_server.py
in themain app.py
file. - Use the
execute
function to call the Gro server and obtain the response. - Use the response obtained from the Gro server as input to the text to speech function.
- Call the
text_to_speech
function with the response as input to convert the text to speech.
Introduction to Text-to-Speech (TTS) and Speech-to-Text (STT) models
- Multiple female voices are available, including Aria, Luna, Stella, and Atlanta.
- Male voices are also available, such as Henry.
- Code snippets show how to use TTS and STT models in Python.
Using TTS models
- Import the
text2speech
library and select a voice option. - Define a function to return a wave file with the TTS output.
- Use the
name
parameter to determine whether to run the code locally or remotely.
Using STT models
- Import the
speech2text
library and load environment variables. - Set up API keys and open an audio input file.
- Convert the audio file to text and retrieve the text output.
Creating a Main App with Flask
- Import the Flask library and the request and send_file functions.
- Create an app instance using Flask.
- Define routes for the app, including a root route that returns an index.html template.
- Define a route for the speech-to-text functionality.
Conclusion
By the end of this tutorial, you will have created a chatbot that can convert text to speech and respond to user queries using Deepgram and Gro API services. This chatbot can be integrated with your website to provide a conversational interface for users.