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
ai-speech.py
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)
try:
query = r.recognize_google(audio)
except LookupError:
print("Could not understand audio")
print(f'query: {query}')
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"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)
myobj.save("reply.mp3")
os.system("mpg321 reply.mp3")
Be the first to comment