-
Notifications
You must be signed in to change notification settings - Fork 7
/
voicechatgpt.py
60 lines (49 loc) · 1.56 KB
/
voicechatgpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import openai
import speech_recognition as sr
import pyttsx3
# OpenAI API key
openai.api_key = "API KEY"
# Text-to-speech engine
engine = pyttsx3.init()
def listen_and_respond():
"""
Listen for audio input, recognize it and respond using OpenAI
"""
# Create speech recognizer object
r = sr.Recognizer()
# Listen for input
with sr.Microphone() as source:
print("Listening...")
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 sr.UnknownValueError:
response_text = "Sorry, I didn't understand what you said"
print(response_text)
engine.say(response_text)
engine.runAndWait()
print()
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
def main():
while True:
listen_and_respond()
if __name__ == "__main__":
main()