-
Notifications
You must be signed in to change notification settings - Fork 0
/
assistanttt.py
58 lines (43 loc) · 1.26 KB
/
assistanttt.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
# !pip install SpeechRecognition==3.10.0
# !pip install pyttsx3==2.90
# !pip install openai==1.3.7
import speech_recognition as sr
import pyttsx3
import openai
openai.api_key = ""
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voices', voices[1].id)
r = sr.Recognizer()
mic = sr.Microphone()
conversation = ""
user_name = "Melanee"
bot_name = "Melanee's assistant"
while True:
with mic as source:
print("\n Listening...")
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
print("no longer listening")
try:
user_input = r.recognize_google(audio)
except:
continue
prompt = user_name+":"+user_input + "\n"+bot_name+":"
conversation += prompt
response = openai.Completion.create(
model="text-davinci-003",
prompt=conversation,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
response_str = response["choices"][0]["text"].replace("\n", "")
response_str =response_str.split(
user_name + ":" ,1)[0].split(bot_name+ ":",1)[0]
conversation+= response_str +"\n"
print(response_str)
engine.say(response_str)
engine.runAndWait()