-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktopAssistant.py
191 lines (161 loc) · 5.45 KB
/
desktopAssistant.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import pyttsx3
import speech_recognition as sr
import webbrowser
import wikipedia
import os
import requests
engine = pyttsx3.init('sapi5')
voice = engine.getProperty('voices')
engine.setProperty('volume',1.0)
engine.setProperty('voice',voice[1].id)
engine.setProperty('rate',180)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def command():
r = sr.Recognizer()
with sr.Microphone() as source_voice:
print('Listening...')
r.pause_threshold = .7
r.energy_threshold = 500
audio = r.listen(source=source_voice)
try:
print('Recognizing...')
query = r.recognize_google(audio,language='en-in')
print(f'{query}\n')
except Exception:
print('Sorry. Couldn\'t recognize')
return 'NONE'
return query
def sum():
speak('Tell me the numbers you want to add by saying and after every number')
print('Tell me the numbers you want to add by saying and after every number')
numbers = command()
lst = []
for i in numbers.split():
try:
number = int(i)
lst.append(number)
except ValueError:
pass
sum = 0
for j in lst:
sum+=j
speak(f'The sum is {sum}')
print(f'The sum is {sum}')
def multiply():
speak('Tell me the numbers you want to multiply by saying and after every number')
print('Tell me the numbers you want to multiply by saying and after every number')
numbers = command()
lst = []
for i in numbers.split():
try:
number = int(i)
lst.append(number)
except ValueError:
pass
res = 1
for j in lst:
res*=j
speak(f'The resultant is {res}')
print(f'The resultant is {res}')
def power():
speak('Tell me two numbers ')
print('Tell me two numbers ')
numbers = command()
lst = []
for i in numbers.split():
try:
number = int(i)
lst.append(number)
except ValueError:
pass
res = lst[0]**lst[1]
speak(f'The resultant is {res}')
print(f'The resultant is {res}')
speak('Hey, I"m your assistant. What is your name')
name = command()
speak(name + 'If you want to skip the introduction, say skip')
query = command()
if 'skip' not in query:
speak('Hey '+ name +'. What do you want me to do? I can add, subtract, multiply, raise a number to another number. I can also read news and create or read a file')
if __name__=='__main__':
while(1):
clear = lambda: os.system('cls')
clear()
query = command().lower()
if 'youtube' in query:
webbrowser.open('youtube.com')
elif 'google' in query:
webbrowser.open('google.com')
elif 'amazon' in query:
webbrowser.open('amazon.in')
elif 'wikipedia' in query:
print('Searching...')
query = query.replace('wikipedia','')
result = wikipedia.summary(query,sentences = 2)
speak(result)
print(result)
elif ('create' or 'note') in query:
speak('What should it\'s name be?')
name = command()
speak('What should I write?')
text = command()
file = open(f'{name}.txt','a')
txt = file.write(text)
file.close()
elif ('open') in query:
speak('What is the file name?')
name = command()
name = f'{name}.txt'
if name in os.listdir():
file = open(name,'r')
z = file.read()
speak(z)
speak('Do you want to print it?')
while True:
printing = str(command().lower())
if 'yes' in printing:
print(z)
speak('OK. Here you go.')
file.close()
break
elif 'no' in printing:
speak('OK')
file.close()
break
else:
speak('Sorry. I did not understand')
else:
speak('Sorry. This file is not in this directory')
elif 'news' in query:
url = ('https://newsapi.org/v2/top-headlines?'
'country=in&'
'sortBy=popularity&'
'apiKey=YOUR API KEY FROM NEWSAPI.COM'
)
response = requests.get(url)
x = response.json()
y = x['articles']
for i in y:
speak(i['title'])
print(i['title'])
speak('DO YOU WANT DESCRIPTION: ')
z = command().lower()
if 'yes' in z:
speak(i['description'])
print(i['description'])
elif 'quit' or 'enough' or 'stop' in z:
break
else:
speak('OK')
speak('MOVING ON TO THE NEXT NEWS')
elif 'add' in query:
sum()
elif 'multiply' in query:
multiply()
elif 'power' in query:
power()
elif 'terminate' in query:
speak('Bye, Have a great time')
break