-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaphire.py
342 lines (279 loc) · 10.7 KB
/
saphire.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import geocoder as gc
import gtts
import nltk
from nltk.stem.lancaster import LancasterStemmer
from nltk.util import pr
from numpy.lib import type_check
stemmer = LancasterStemmer()
import random
import webbrowser as wb
import numpy
import tflearn
import json
import keyboard
from datetime import datetime
import pickle
import speech_recognition as sr
from gtts import gTTS
from playsound import playsound
import os
import requests
from os import path
import time
from calculator.simple import SimpleCalculator
cal = SimpleCalculator()
import os.path
bad_responce = ['I could not understand the question','I dont know how to respond', "I do not know the answer to that"]
first_run = True
with open('intents.json') as file:
data = json.load(file)
try:
with open('data.pickle', 'rb') as f:
words, lables, training, output = pickle.load(f)
first_run = False
except:
words = []
lables = []
docs_x = []
docs_y = []
training = []
output = []
for intent in data['intents']:
for pattern in intent["patterns"]:
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
docs_x.append(wrds)
docs_y.append(intent['tag'])
if intent['tag'] not in lables:
lables.append(intent['tag'])
print(lables)
words = [stemmer.stem(w.lower()) for w in words if w not in '?']
words = sorted(list(set(words)))
lables = sorted(lables)
out_empty = [0 for _ in range(len(lables))]
for x, doc in enumerate(docs_x):
bag = []
wrds =[stemmer.stem(w) for w in doc]
#for p in wrds :
#p = str(p)
#if len(p) > 4:
#x = random.randrange(0,len(p))
#wrds = res_str = p[:x-1] + p[x+1:]
for w in words:
if w in wrds:
i = 1
#u = len(w)
#print(u)
#if w in wrds:
#i = 1
#i = i + 1
else:
i = 0
#u = 0
bag.append(i)
#bag.append(u)
output_row = out_empty[:]
output_row[lables.index(docs_y[x])] = 1
training.append(bag)
output.append(output_row)
training = numpy.array(training)
output = numpy.array(output)
with open('data.pickle', 'wb') as f:
pickle.dump((words, lables, training, output), f)
net = tflearn.input_data(shape=[None, len(training[0])])
#net = tflearn.fully_connected(net, 512)
#net = tflearn.fully_connected(net, 448)
#net = tflearn.fully_connected(net, 384)
#net = tflearn.fully_connected(net, 320)
net = tflearn.fully_connected(net, 16)
net = tflearn.fully_connected(net, 16)
net = tflearn.fully_connected(net, len(output[0]), activation='softmax')
net = tflearn.regression(net)
model = tflearn.DNN(net)
try:
model.load('model.tflearn.TFL')
except:
model.fit(training, output, n_epoch=10000, batch_size=1024 ,show_metric=True)
model.save('model.tflearn.TFL')
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
def bag_of_words(s, words):
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
for se in s_words:
for i,w in enumerate(words):
u = 0
if w == se:
bag[i] = 1
#bag[i+1] = len(w)
#for w in se:
#u = u + 1
#bag[i] = u
return numpy.array(bag)
def chat(inp):
if inp == None:
x = 'could not under stand the questoin'
else:
bow = bag_of_words(inp, words)
if 1 in bow:
results = model.predict([bag_of_words(inp, words)])
#print(results)
results_index = numpy.argmax(results)
tag = lables[results_index]
for tg in data['intents']:
if tg['tag'] == tag:
responses = tg['responses']
x = random.choice(responses)
else:
x = random.choice(bad_responce)
if x == 'show_weather':
g = gc.ip('me')
lat = g.lat
lon = g.lng
url = 'http://api.openweathermap.org/data/2.5/weather?lat='+str(lat) + '&lon='+ str(lon) + '&appid=22592c15c610c3a1c10c7e1f104615a5&units=imperial&mode=json'
response = requests.get(url)
weather_json = json.loads(response.text)
main_weather = weather_json["main"]
temp = str(main_weather["temp"])
low = str(main_weather["temp_min"])
high = str(main_weather["temp_max"])
city = str(weather_json["name"])
x = ('in '+ str(city) + " it is " + str(temp) + ' degrees, with a low of ' + str(low), " degrees ,and a high of " + str(high) + " degrees")
if x == 'time':
now = datetime.now()
x = now.strftime("%H:%M")
if x == "math":
try:
cal.run(inp)
x = str(cal.lcd)
except:
x = 'math is still a beta function and we could not understand the question'
if x == 'take_note':
tts = gTTS('what do you want your note to be')
tts.save('responce.mp3')
playsound('responce.mp3')
os.remove('responce.mp3')
with sr.Microphone() as source:
audio = r.listen(source)
try:
note_num = 1
note = r.recognize_google(audio)
while True:
if path.exists("notes/note" + str(note_num) + ".txt") == True:
note_num = note_num + 1
else:
break
f = open('notes/note'+ str(note_num) + '.txt' , 'x')
f.write(note)
f.close
f = 'notes/note' + str(note_num) + 'txt'
print('note saved at:', os.path.abspath(f))
x = 'note saved'
except:
x = 'an error occurred'
tts = gTTS(str(x))
print(x)
tts.save('responce.mp3')
playsound('responce.mp3')
os.remove('responce.mp3')
#print(random.choice(responses))
if first_run == True:
tts = gTTS('Hello welcome to Sapphire 1.2 We have added many new commands. first off we have added typing just say type followed by what you want to type. secondly we have added the ability to look things up on google and youtube say look up to look something up and say look up blank on youtube to look something up on youtube. lastly we have added the ability to open programs just say open followed by the program name to pen the program')
else:
tts = gTTS('Welcome Back')
tts.save('responce.mp3')
playsound('responce.mp3')
os.remove('responce.mp3')
print('start')
timer = 0
while True:
if round(int(time.time())) == timer or round(int(time.time())) + 1 == timer or round(int(time.time())) - 1 == timer:
playsound("alarm.mp3")
if keyboard.is_pressed(('ctrl','f1')):
run = True
inp = ''
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
inp = r.recognize_google(audio)
inp = inp.lower()
print(inp)
if r.recognize_google(audio) == 'quit':
break
if str(inp)[0:5] == 'type ' or str(inp)[0:5] == 'Type ' :
inp = str(inp)[5:]
keyboard.write(inp)
run = False
if str(inp)[0:5] == 'open ' or str(inp)[0:5] == 'Open ':
inp = inp[5:]
try:
try:
os.startfile('C:/ProgramData/Microsoft/Windows/Start Menu/Programs/'+ str(inp)+ '.lnk')
except:
homedir = os.path.expanduser("~")
os.startfile(str(homedir) + '/desktop/' + str(inp)+ '.lnk')
except:
tts = gTTS('Sapphire could not luanch the desiered program')
tts.save('responce.mp3')
playsound('responce.mp3')
os.remove('responce.mp3')
run = False
if "timer" in str(inp):
x = inp.split(" ")
index = 0
for idx,i in enumerate(x):
try:
y = int(i)
index = idx
break
except:
pass
if x[index] =="minute" or "minutes":
timer = round(int(time.time())) + (y *60)
print(timer)
run = False
if 'look up' in str(inp)[:7]:
inp = str(inp)
if 'on YouTube' in str(inp):
list = str(inp).split(' ')
list.remove('look')
list.remove('up')
list.remove('on')
list.remove('YouTube')
inp = ' '.join(list)
wb.open("https://www.youtube.com/results?search_query="+ str(inp))
run = False
else:
list = str(inp).split(' ')
list.remove('look')
list.remove('up')
inp = ' '.join(list)
wb.open("https://www.google.com/search?q="+ str(inp))
run = False
if "define" in str(inp):
inp = inp.split(" ")
inp = str(inp[1])
url = f"https://api.dictionaryapi.dev/api/v2/entries/en_US/{inp}"
response = requests.get(url)
dictonary = json.loads(response.text)
x = dictonary[0]["meanings"][0]["definitions"][0]["definition"]
tts = gTTS(x)
tts.save('responce.mp3')
playsound('responce.mp3')
os.remove('responce.mp3')
run = False
if 'nevermind' in str(inp):
tts = gTTS('Ok')
tts.save('responce.mp3')
playsound('responce.mp3')
os.remove('responce.mp3')
if run == True:
chat(inp)
except:
pass