Speaking is the more natural way of communicating and a much better way of interacting with OpenAI ChatGPT AI engine. The following Python script enables interactive conversation with OpenAI ChatGPT by using voice commands, fully eliminating the need for typing out messages.
Demo of the Python code voice chat:
The below script imports the necessary libraries, such as OpenAI, Speech Recognition and Pyttsx3.
# Import necessary libraries
import openai
import speech_recognition as sr
import pyttsx3
It then assigns an OpenAI API key (you need to get that from https://beta.openai.com/account/api-keys)
# Assign OpenAI API key
openai.api_key = ""
Then initializes a text-to-speech engine.
# Initialize text-to-speech engine
engine = pyttsx3.init()
The rest of the code is a loop that listens for audio input from the user, by creating a Recognizer object and then records audio with a microphone.
# Create speech recognizer object
r = sr.Recognizer()
# Listen for input
with sr.Microphone() as source:
print("Speak now:")
audio = r.listen(source)
The program then attempts to recognize the audio:
try:
prompt = r.recognize_google(audio, language="en-EN", show_all=False)
print("You asked:", prompt)
, and if successful, uses OpenAI to generate a response (you can tweak the setting of OpenAI by changing the completion configuration):
# Use OpenAI to create a response
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=300
)
The OpenAI response is then converted to a string and spoken via the text-to-speech engine:
# Get the response text
response_text = str(response['choices'][0]['text']).strip('\n\n')
print(response_text)
# Speak the response
engine.say(response_text)
engine.runAndWait()
print()
If the speech recognition fails, the program will display an error message.
except:
response_text = "Sorry, I didn't get that!"
print(response_text)
engine.say(response_text)
engine.runAndWait()
print()
Experience the power of AI with this easy-to-use script, here is the full Python code.
Also available on GitHub: https://github.com/JozefJarosciak/OpenAI-ChatGPT-Voice-Chat
# Import necessary libraries
import openai
import speech_recognition as sr
import pyttsx3
# Assign OpenAI API key
openai.api_key = ""
# Initialize text-to-speech engine
engine = pyttsx3.init()
# Loop to listen for audio input
while True:
# Create speech recognizer object
r = sr.Recognizer()
# Listen for input
with sr.Microphone() as source:
print("Speak now:")
audio = r.listen(source)
# Try to recognize the audio
try:
prompt = r.recognize_google(audio, language="en-EN", show_all=False)
print("You asked:", prompt)
# Use OpenAI to create a response
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=300
)
# Get the response text
response_text = str(response['choices'][0]['text']).strip('\n\n')
print(response_text)
# Speak the response
engine.say(response_text)
engine.runAndWait()
print()
# Catch if recognition fails
except:
response_text = "Sorry, I didn't get that!"
print(response_text)
engine.say(response_text)
engine.runAndWait()
print()