PythonGPT: My Voice Assistant in Python

🔵🟡 Can You Have a Voice Assistant Using the GPT Language Model? 🟡🔵

 

Ever since I created a post about building a chatbot using GPT3, I’ve been wondering: “Can I use this language model for a voice assistant?”

 

To find out, I did the most obvious thing - I opened the documentation of the APIs released by OpenAI, hoping there was already something about it. (Spoiler: there wasn’t.)

 

💡 That’s when I had an epiphany: Utilize two Python libraries to convert text to audio and vice versa. This way, the APIs would interpret only text, output a text string with the response, and then it would be translated into a vocal message.

 

👤 How would you use a voice assistant that leverages GPT-3? Let me know in the Linkedin Post comments!

 

If you’re interested in the code, you can find it below

Code Analysis

This section of the code imports several libraries that will be used throughout the script. It imports sys, json, and openai for handling file paths, JSON data, and interacting with OpenAI’s API, respectively. The pyttsx3 library is used for text-to-speech conversion, while speech_recognition is imported as sr for converting audio to text.

The script then loads the OpenAI API key from a secrets.json file located in the parent directory. This API key is necessary for making requests to the OpenAI service.

The listen function captures audio input from the user through the microphone and converts it into text. It first initializes an error variable and a Recognizer object from the speech_recognition library. The microphone is set as the audio source, and the script prompts the user to speak. The recognizer.listen() method captures the audio, which is then processed using Google’s speech recognition service to convert the audio to text.

The language is set to Italian ('it-IT'), but it can easily be changed to another language. If the conversion fails, an error message is printed, and the error variable is set to 1.

The openaiCompletion function takes the text output from the previous step and sends it to OpenAI’s text-davinci-003 model. The model processes the input based on the parameters provided:

  • temperature: Controls the creativity of the response. Higher values mean more creative outputs.
  • max_tokens: Limits the length of the generated response.
  • top_p and best_of: Influence the sampling technique for generating responses.
  • frequency_penalty and presence_penalty: Adjust the model’s behavior regarding repeating phrases.

The response from the model is printed and then returned for further use.

This function converts the text response generated by the OpenAI model into speech. The pyttsx3 library is used to initialize a text-to-speech engine. The speaking rate and volume are set before the text is converted to speech and spoken aloud by the engine.

The main script ties all the functions together. It starts by calling the listen function to capture and convert the user’s audio input into text. If no error occurs during this process, the script sends the text to the OpenAI API for processing using the openaiCompletion function. Depending on the result, the appropriate message is returned. Finally, the response is converted back to speech using the textToAudio function, completing the interaction loop.