-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
84 lines (70 loc) · 2.92 KB
/
test.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
import threading
import datetime
import random
import speech_recognition
import pyttsx3 as tts
from neuralintents import GenericAssistant
class Nova:
def __init__(self):
self.recognizer = speech_recognition.Recognizer()
self.speaker = tts.init(driverName='sapi5')
self.speaker.setProperty("rate", 150)
self.speaker.setProperty(
"voice", self.speaker.getProperty('voices')[1].id)
self.assistant = GenericAssistant(
"datasets/intents.json", intent_methods={"time": self.get_the_time})
self.assistant.train_model()
# threading.Thread(target=self.run_assistant).start()
self.run_assistant()
def get_the_time(self):
responses = [
"The time is %%.",
"It is currently %%.",
"The current time is %%.",
"It's approximately %%.",
"Let me check... the time now is %%.",
"I have the time, it's %%."
]
# Get the current time
now = datetime.datetime.now().strftime("%H:%M")
response = random.choice(responses).replace("%%", now)
self.speaker.say(response)
self.speaker.runAndWait()
def run_assistant(self):
while True:
try:
with speech_recognition.Microphone() as mic:
print("Mic active.")
self.recognizer.pause_threshold = 0.5
self.recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = self.recognizer.listen(mic)
print("audio captured")
text = self.recognizer.recognize_google(audio)
text = text.lower()
print("before awake word")
# Nova
if "nova" in text:
print("nova activated. Speak now...")
audio = self.recognizer.listen(mic)
print("audio captured after nova activated")
text = self.recognizer.recognize_google(audio)
text = text.lower()
if text == "stop":
self.speaker.say("Bye!")
self.speaker.runAndWait()
self.speaker.stop()
exit()
else:
if text is not None:
response = self.assistant.request(text)
if response is not None:
print(response)
self.speaker.say(response)
self.speaker.runAndWait()
else:
print("Text: ", text)
print("after awake word")
except Exception as e:
print("An error occurred: ", str(e))
continue
Nova()