-
Notifications
You must be signed in to change notification settings - Fork 2
/
speech_assistance.py
139 lines (98 loc) · 3.35 KB
/
speech_assistance.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from neuralintents import GenericAssistant
from datetime import date, datetime
import speech_recognition as sr
import pyttsx3 as tts
import sys
r = sr.Recognizer()
speaker = tts.init()
speaker.setProperty('rate', 130)
todo_list = ['Go Shopping', 'Clean Room']
def create_note():
global r
speaker.say("What do you want to write onto your note")
speaker.runAndWait()
done = False
while not done:
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
note = r.recognize_google(audio)
note = note.lower()
speaker.say("Choose a filename")
speaker.runAndWait()
print("Chose Filename")
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
filename = r.recognize_google(audio)
filename = filename.lower()
with open(filename, 'w') as file:
file.write(note)
done = True
speaker.say(f"I successfully created the note {filename}")
speaker.runAndWait()
except sr.UnknownValueError:
r = sr.Recognizer()
speaker.say("I did not understand you! Please try again")
speaker.runAndWait()
print("try again")
def add_todo():
global r
speaker.say("What todo do you want me to add")
speaker.runAndWait()
done = False
while not done:
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
item = r.recognize_google(audio)
item = item.lower()
todo_list.append(item)
done = True
speaker.say(f"Added {item} to to do list")
speaker.runAndWait()
except sr.UnknownValueError:
r = sr.Recognizer()
speaker.say("I did not understand you! Please try again")
speaker.runAndWait()
def show_todo():
speaker.say("The following items are on your todo list")
for item in todo_list:
speaker.say(item)
speaker.runAndWait()
def current_time():
now = datetime.now()
current_time = now.strftime("%H %M %S")
speaker.say(f"The current time is {current_time}")
def greeting():
speaker.say("Hello What can i do for you")
speaker.runAndWait()
def quit():
speaker.say("Bye")
speaker.runAndWait()
sys.exit(0)
mappings = {
"greeting": greeting,
"create_note": create_note,
"add_todo": add_todo,
"show_todo": show_todo,
"current_time": current_time,
"exit": quit
}
assistance = GenericAssistant('intents.json', intent_methods=mappings)
assistance.train_model()
while True:
try:
with sr.Microphone() as source:
print("speech assistance started")
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
message = r.recognize_google(audio)
message = message.lower()
assistance.request(message)
print(message)
except sr.UnknownValueError:
r = sr.Recognizer()
speaker.say("Sorry Could Not Understand")
speaker.runAndWait()