-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.py
38 lines (30 loc) · 891 Bytes
/
speech.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
import speech_recognition as sr
import os
import openai
import pyttsx3
import subprocess
openai.api_key = os.getenv("OPENAI_API_KEY")
# obtain audio from the microphone
r = sr.Recognizer()
mic = sr.Microphone(device_index=0)
print(sr.Microphone.list_microphone_names())
conversation = ""
while True:
with mic as source:
print("Say something!")
audio = r.listen(source, phrase_time_limit=3.0)
print("No longer listening")
try:
user_input = r.recognize_google(audio)
except:
continue
print("You said " + r.recognize_google(audio))
conversation += user_input
response = openai.Completion.create(
model="text-davinci-003",
prompt=conversation,
max_tokens=50
)
response_str = response["choices"][0]["text"].replace("\n", "")
print(response_str)
subprocess.call(["say",response_str])