Here is a small Python script that will allow you to interact with ChatGPT using your voice instead of a keyboard. It will also voice a response using the Python pyttsx3 library. You need to install a few libraries for it to work like openai, pyaudio, and SpeechRecognition.
import openai
import pyttsx3
import speech_recognition as sr
import time
#Set your OpenAI api key
openai.api_key = ""
#Init the text-to-speech engine
engine = pyttsx3.init()
def transcribe_audio_to_text(filename):
recognizer = sr.Recognizer()
with sr.AudioFile(filename) as source:
audio = recognizer.record(source)
try:
return recognizer.recognize_google(audio)
except:
print('Skipping unknown error')
def generate_response(prompt):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=4000,
n=1,
stop=None,
temperature=0.5,
)
return response["choices"][0]["text"]
def speak_text(text):
engine.say(text)
engine.runAndWait()
def main():
while True:
#Wait for user to say "Larry", or call your assistant anything you wish
print("Say 'Larry' to start recording your question...")
with sr.Microphone() as source:
recognizer = sr.Recognizer()
audio = recognizer.listen(source)
try:
transcription = recognizer.recognize_google(audio)
if transcription.lower() == "larry":
#Record Audio
filename = "input.wav"
print("Yes human, what is your query?")
with sr.Microphone() as source:
recognizer = sr.Recognizer()
source.pause_threshold = 1
audio = recognizer.listen(source, phrase_time_limit=None, timeout=None)
with open(filename, "wb") as f:
f.write(audio.get_wav_data())
#Transcribe audio to text
text = transcribe_audio_to_text(filename)
if text:
print(f"You said: {text}")
#Generate response using GPT-3
response = generate_response(text)
print(f"Larry says: {response}")
#Read response using text-to-speech
speak_text(response)
except Exception as e:
print("An error has occurred: {}".format(e))
if __name__ == "__main__":
main()