Voice Activated ChaptGPT with Python

This test shows allows you to talk to your computer, have ChatGPT answer you, and then have that spoken by your computer.

sudo apt install gTTS

sudo apt install python3-pyaudio

pip3 install SpeechRecognition

pip3 install openai


from gtts import gTTS
import os
import openai

openai.api_key = 'APIKEY'

import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:                
    audio = r.listen(source)                   

    query = r.recognize_google(audio)

except LookupError:                           
    print("Could not understand audio")

print(f'query: {query}')

response = openai.ChatCompletion.create(
    {"role": "system", "content": "you are a teacher"},
    {"role": "assistant", "content": "answer in fewer than  100 words"},
    {"role": "user", "content": query}

reply = response["choices"][0]["message"]["content"]

print(f'reply {reply}')

language = 'en'

myobj = gTTS(text=reply, lang=language, slow=False)


os.system("mpg321 reply.mp3")

Be the first to comment

Leave a Reply