Create a ChatGPT Voice Assistant in 8 Minutes (Python Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
ever since chat GPT was released I have the constant urge to ask Siri a question that only chat GPT can answer so instead I decided to create a gpt3 powered Voice Assistant with python I'm going to show how you can do the same and at the end we will give some ideas on how to take this program and make it into a software as a service business we will be diving into the code step by step and explaining what each line of code is doing so even if you're new to Python and AI you'll still be able to follow along first open your python environment and create a new python file we will Begin by importing the openai library which will allow us to access the gpt3 API next we'll import the pi TTS X3 Library which will allow us to convert text to speech we will also be using the speech underscore recognition library to transcribe audio to text so let's import that as well now that we have imported the necessary libraries we can set up our open AI API key this will allow us to access the gpt3 API in this line of code replace the dummy API key with your own open AI API key which you you can get from the openai website for free with our libraries imported and our API key setup let's set up our text to speech engine here we create an instance of the text-to-speech engine using the init method this instance is stored in the engine variable which we will use later to generate speech from text now let's create a python function to transcribe our voice commands into text for our Python program to understand we will transcribe audio to text using the python speech recognition Library the speech underscore recognition Library provides a convenient way to transcribe audio to text in Python start by defining the transcribe audio to text function this line defines a new function called transcribe audio to text and specifies that it takes a single argument file name which represents the name of the audio file that we want to transcribe now let's create an instance of the recognizer class from the SR speech recognition module this object is a requirement to perform speech recognition on the audio file next we use the with statement to open the audio file specified by file name using the audio file class from the SR module then we record the audio using the record method of the recognizer object finally let's transcribe the recorded audio to text using the recognize underscore Google method of the recognizer object if an error occurs during transcription an exception will be raised and a message indicating that there was an error and the program is trying again will be printed this handles unknown speech errors from Google if our program records unintelligible sounds and that's it this function takes an audio file as input and Returns the transcribed text moving forward we will create a function to generate responses from the gpt3 API this line defines a new function called generate underscore response and specifies that it takes a single argument prompt The Prompt argument represents the input text that we want to use as a starting point for generating a response using the open AI gpt3 API now let's use the open AI completion create method to generate a response based on the given prompt as shown in the openai documentation for gpt3 in this line we pass several arguments to the open AI completion create method to specify the parameters of the response for example we specify the engine as the gpt3 model by inserting text DaVinci 3 then we set the max tokens variable to control the number of characters a response from gpt3 is limited to 4000 is the max we can do with the DaVinci 3 engine we can set the limit lower if desired for development to increase query speed and then adjust it back to 4000 when we have finished developing and are ready to publish our program for now let's just set it to 4000 to see what the API is capable of the temperature variable in the open AI API is a parameter used to control the creativity or randomness of the generated text a temperature of 0.5 is often a good starting point as it balances the trade-off between predictability and creativity in the generated text here we return the generated response from the gpt3 API and that's all it we now have a function to establish connection with open AI so we can later call upon this function to communicate with the gpt3 API let's create a simple function for speaking our responses from the assistant so it is 100 voice Interactive first we will Define a function called speak text which takes a text argument this function will convert the text argument to speech using the pi ttsx3 library in this function we use the engine say method to specify the text to be spoken and the engine run and wait method to play the speech now that we have set up all the prerequisite functions let's start structuring the logic of how we want python to run this script let's create a main function to do so next we'll add a while loop that will run continuously until the program is stopped the while true statement means that the loop will run until we force the program to stop in terminal this will allow our program to listen answer and then continue listening now let's add a message that will be displayed on the screen to instruct the user to say genius this line prints a message on the screen that instructs the user to say genius to start recording their question genius will be our prompt command to make our program start recording our question we'll use the SR microphone class to access the microphone and record audio let's create an instance of the SR recognizer class that we can use to transcribe audio to text let's now record audio using the listen method of the recognizer object now we'll transcribe the recorded audio to text using the recognize Google method this line checks if the transcribed text is genius the lower method is used to convert the text to lowercase to make the comparison case insensitive if the transcribed text is genius let's record more audio this code block records more audio and saves it to a file named input.wave The Print function is used to display a message instructing the user to say their question next let's transcribe the recorded audio to text this line transcribes the recorded audio to text using the transcribe audio to text function now let's check if the transcription was successful if the transcription was successful the text variable will contain the transcribed text next let's print the transcribed text now let's generate a response using the generate response function next let's print the response finally let's read the response using text to speech Let's wrap up our triloop with this exception message except statements are used in Python to handle errors and give us the control of how we want our program to respond to a given error without accept statements errors will simply cause a python script to stop running the basic idea behind using a try except block is to attempt to execute a block of code and if any errors occurs during the execution of our try statement python will respond how we tell it to in the accept statement overall using try accept statements can help make our python programs more robust resilient and easier to debug and that's it this is the complete main function that runs the voice assistant finally we add the following code to the end of the script to run the main function with these additions our personal assistant is complete and ready to use now let's test out the script and see how it works genius I just created a super dank Voice Assistant with python and gpt3 how can I make the Python program into a website so everyone can access it to make your Python program into a website you'll need to use a web framework such as flask or Django these Frameworks will allow you to create a web application that can be hosted on the internet and accessed by anyone with an internet connection you'll also need to create a web interface for your voice assistant so that users can interact with it additionally you'll need to set up a server to host your application once you have all of these components in place you'll be able to make your Python program into a website genius my python code just returned no Pi TTS X3 module found why did it do that it is possible that the python code was looking for a specific module that was not found it could also be that the module you were looking for was not installed correctly or was not compatible with your version of python genius write a diss song about how my subscribers are superior to pancakes that sounds like nle Choppa my subscribers they're always down to show some love they don't need Sarah [Applause]
Info
Channel: Ai Austin
Views: 310,818
Rating: undefined out of 5
Keywords: python, gpt3, chatgpt, programming, saas, software as a service, gpt, online business, how to make money online, money, business, finance, bitcoin, crypto, cryptocurrency, currency, ai, artificial intelligence, entrepreneurship, entrepreneur
Id: 8z8Cobsvc9k
Channel Id: undefined
Length: 8min 30sec (510 seconds)
Published: Fri Feb 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.