-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSASHA.py
139 lines (112 loc) · 3.82 KB
/
SASHA.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
import pyttsx3
import speech_recognition as sr
import datetime
import tkinter as tk
import wikipedia
import webbrowser
engine = pyttsx3.init('espeak')
voices = engine.getProperty('voices')
# print(voices[0].id)
engine.setProperty('voices', voices[0].id)
def speak(audio):
engine.setProperty('rate', 150)
engine.say(audio)
engine.runAndWait()
def wishMe():
try:
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("good morning!!" + user)
elif hour >= 12 and hour < 18:
speak("good afternoon!!" + user)
else:
speak("goog evening!!" + user)
speak(
" i am sasha " + user + " " + " please tell me how may i help you. to start and let me listen your command press listen")
except Exception as p:
speak("please first enter your name")
def takeCommand():
# it takes microphone input from user and returns string output
try:
print(user)
a = 0
except NameError:
a = 1
speak("please enter name first")
if a == 0:
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening..")
speak("listening...")
r.pause_threshold = 1
r.energy_threshold = 1000
audio = r.listen(source)
try:
global query
print("Recognizing....")
query = r.recognize_google(audio, language='en-in')
# print("user said: ",query)
speak(user + "said")
speak(query)
query = query.lower()
q = tk.Label(ra, text="user said : " + query)
q.pack()
if 'wikipedia' in query:
speak("searching wikipedia...")
result = wikipedia.summary(query, sentences=1)
speak("according to wikipedia")
b = tk.Label(ra, text="according to wikipedia : " + result)
b.pack()
speak(result)
ra.mainloop()
elif "open youtube" in query:
try:
webbrowser.get("firefox").open("https://www.youtube.com")
except:
webrowser.open("https://www.youtube.com")
elif "open google" in query:
try:
webbrowser.get("firefox").open("https://www.google.com")
except:
webrowser.open("https://www.google.com")
else:
speak("sorry " + user + query + "it is not answerable")
c = tk.Label(ra, text="sorry " + user + " " + "your query " + query + " " + " is not answerable")
c.pack()
print(" ")
except Exception as e:
print(e)
print("say that again please...")
speak("say that again please...")
return "None"
return query
def fun():
try:
w = tk.Label(ra,
text=' i am sasha ' + user + 'please tell me how may i help you. to start and let me listen your command press listen')
w.pack()
wishMe()
ra.mainloop()
except:
speak("please first enter your name")
def us():
global user
user = str(e1.get("1.0", "end-1c"))
# print(user)
return user
if __name__ == "__main__":
ra = tk.Tk()
ra.title('sasha voice assistant')
m2 = tk.Label(ra, text='enter your name')
e1 = tk.Text(ra, height=2, width=10)
m2.pack()
e1.pack()
c = tk.Button(ra, text='submit name', command=lambda: us())
m1 = tk.Label(ra, text=' PLEASE PRESS START')
button = tk.Button(ra, text='start', command=lambda: fun())
button2 = tk.Button(ra, text='listen', width=15, command=lambda: takeCommand())
c.pack()
m1.pack()
button.pack()
button2.pack()
ra.mainloop()