-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementMain.py
438 lines (349 loc) · 15.4 KB
/
implementMain.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import random
import json
import torch
from model import NeuralNet
from tokenizationAndStemming import BagOfWords, tokenize
from voice import listener
import dominate
import pyttsx3
import os.path
from dominate.tags import *
engine = pyttsx3.init()
engine.setProperty('rate', 130)
from specialHandlers import handleSrc, handleTable, handleList, handleSelect
# Tokenize, stem the spoken word and shape them to fit on the model
def tokenizeAndStemSpoken(sentence, allWords):
sentence = tokenize(sentence)
x = BagOfWords(sentence, allWords)
x = x.reshape(1, x.shape[0])
x = torch.from_numpy(x)
return x
# open the intents file
with open("intents.json", "r") as f:
intents = json.load(f)
with open("attributes.json", "r") as f:
attributes = json.load(f)
# Load the creadential saved during training
FILE = "data.pth"
data = torch.load(FILE)
# load all the saved values
inputSize = data["input_size"]
outputSize = data["output_size"]
hiddenSize = data["hidden_size"]
allWords = data["allWords_size"]
tags = data["tags"]
modelState = data["modelState"]
model = NeuralNet(inputSize, hiddenSize, outputSize)
# synthesize next Html tag or attribute provided
def synthesizeTag(sentence, botName, recType):
# load the statedictionary
model.load_state_dict(modelState)
model.eval()
# tokenize find BOG predict the class for new sentence
x = tokenizeAndStemSpoken(sentence, allWords)
# find the predicted output
output = model(x)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
# apply softmax to find probability of predicted class
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
# loop for pattern to see if any pattern matches
if prob.item() > 0.85:
# now we have to decide are we looking for tag or attribute
if recType == 1:
for intent in intents['intents']:
if tag == intent["tags"]:
return tag
else:
for attribute in attributes['attributes']:
if tag == attribute['attr']:
return tag
else:
engine.say('I do not understand')
engine.runAndWait()
print(f"{botName}: I do not understand...")
# listen to user for tag or atrribute
def listenUser(recType):
# dont be confuse this while is for the case when tag is not recognized
while True:
# create the bot
botName = "Jony"
if recType == 1:
engine.say('Lets hear tag')
engine.runAndWait()
print("Let's hear tag!")
else:
engine.say('Lets hear tag')
print("Let's hear atriibute! ")
# listen to voice command
sentence = listener()
engine.say('I hear')
# engine.say(sentence)
# engine.runAndWait()
print('Jony: I hear>', sentence)
# see if the user say quit to end the tag
if 'quit' in sentence:
return 'quit'
elif 'finish' in sentence:
return 'quit'
else:
print(f'you: {sentence}')
# if command is understandable synthesize the tag
tag = synthesizeTag(sentence, botName, recType)
print(f'jony: {tag}')
return tag
def listenTag():
# listen the tag
innerElement = []
tag = listenUser(1)
# we have to handle some special tag like tabe,list,inputoption etc
if tag == 'table':
innerElement = handleTable()
if tag == 'ol' or tag == 'ul':
innerElement = handleList()
if tag == "form":
innerElement = handleForm()
if tag == 'select':
innerElement = handleSelect()
return tag, innerElement
# it listen to the attribute for special tag
def listenAttribute(tag):
attribute = []
# For each tag there can be multiple attributes listen to the attributes
while True:
listenedAttribute = listenUser(2)
if listenedAttribute is not None:
if 'quit' in listenedAttribute:
return attribute
# we have to handle some special attribute like src,values of option
if listenedAttribute == "src":
value = handleSrc()
else:
print(f'jony: speak {listenedAttribute} value')
value = listener()
attrValue = {
"attr": listenedAttribute,
"value": value
}
attribute.append(attrValue)
def completeListener():
# empty lists to contain the atriibute and
tag = []
attribute = []
innerElement = []
innerText = []
# listen to various tag and their inner nested tags
tag, innerElement = listenTag()
# some tag like img,vedio,input,href need atrribute like src,type,href
mustHaveAtrribute = ['img', 'input', 'vedio', 'a']
# listen to associated attribute
if tag in mustHaveAtrribute:
attribute = listenAttribute(tag)
# now we have to listen to the innertext if there is any
engine.say('Is there any inner text associated say yes to have it')
engine.runAndWait()
print(
f'is there any inner text associated with {tag} ? ... Say "Yes" to have it..')
openion = listener()
if 'yes' in openion:
innerText = listener()
# After tag and attributes are clear make appropriate data to pass to react
data = {
"element": tag,
"innerText": innerText,
"attributes": attribute,
"innerElement": innerElement
}
return data
def handleForm():
tags = []
while True:
tag = completeListener()
tags.append(tag)
engine.say('Is there more tag inside form')
engine.runAndWait()
print('is there more tag inside form?')
engine.say('Till now')
engine.runAndWait()
print('till now ', tags)
openion = listener()
if 'yes' not in openion:
return tags
# dictionary to hold the command given
commandTags = {
"tags": []
}
def main():
# listen to user, predict the command, and gve appropriate response
data = completeListener()
# save the command to dictionary
commandTags["tags"].append(data)
print("commandTags:", commandTags)
if os.path.isfile('main_index.html'):
print('file exist')
else:
file_object = open('main_index.html', 'a')
file_object.write('<html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" /><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script><title>Document</title></head><body><div class="container"> ')
file_object.close()
# doc = dominate.document(title='Dominate your HTML')
for tag in commandTags['tags']:
print(tag['element'], tag['innerText'])
if(tag['element'] == 'p'):
file_object = open('main_index.html', 'a')
file_object.write('<p>'+tag['innerText']+'</p>'+'<br>')
file_object.close()
elif(tag['element'] == 'button'):
file_object = open('main_index.html', 'a')
file_object.write('<button class="btn btn-primary">'+tag['innerText']+'</button>'+'<br>')
file_object.close()
elif(tag['element'] == 'header'):
file_object = open('main_index.html', 'a')
file_object.write('<h1>'+tag['innerText']+'</h1>'+'<br>')
file_object.close()
elif(tag['element'] == 'sub_header'):
file_object = open('main_index.html', 'a')
file_object.write('<h3>'+tag['innerText']+'</h3>'+'<br>')
file_object.close()
elif(tag['element'] == 'anchor'):
file_object = open('main_index.html', 'a')
file_object.write('<a>'+tag['innerText']+'</a>'+'<br>')
file_object.close()
elif(tag['element'] == 'ol'):
file_object = open('main_index.html', 'a')
file_object.write('<ol>')
file_object.close()
for data in tag['innerElement']:
# print(data['value'])
file_object = open('main_index.html', 'a')
file_object.write('<li style="line-height: 50px;background-color: black;color: white;font-size: 24px;width: 50%;padding-left: 50px;">'+data['value']+'</li>')
file_object.close()
file_object = open('main_index.html', 'a')
file_object.write('</ol>'+'<br>')
file_object.close()
elif(tag['element'] == 'ul'):
file_object = open('main_index.html', 'a')
file_object.write('<ul>')
file_object.close()
for data in tag['innerElement']:
# print(data['value'])
file_object = open('main_index.html', 'a')
file_object.write('<li style="line-height: 50px;background-color: black;color: white;font-size: 24px;width: 50%;padding-left: 50px;">'+data['value']+'</li>')
file_object.close()
file_object = open('main_index.html', 'a')
file_object.write('</ul>'+'<br>')
file_object.close()
elif(tag['element'] == 'form'):
file_object = open('main_index.html', 'a')
file_object.write('<form>')
file_object.close()
for data in tag['innerElement']:
# print(data['value'])
file_object = open('main_index.html', 'a')
if(data['element']=='label'):
file_object.write('<label style="font-size: 16px;width: 100%;">'+data['innerText']+'</label>'+'<br>')
elif(data['element']=='input'):
for inputdata in data['attributes']:
file_object.write('<input style="font-size: 16px;width: 100%;"'+'type="'+inputdata['value']+'"'+'<br>')
file_object.close()
file_object = open('main_index.html', 'a')
file_object.write('</form>'+'<br>')
file_object.close()
elif(tag['element'] == 'li'):
file_object = open('main_index.html', 'a')
file_object.write('<li style="line-height: 50px;background-color: black;color: white;font-size: 24px;width: 50%;padding-left: 50px;">'+tag['innerText']+'</li>')
file_object.close()
elif(tag['element'] == 'select'):
file_object = open('main_index.html', 'a')
file_object.write('<select style="width: 50%;height: 50px;font-size: 20px;padding-left: 50px;">')
file_object.close()
for data in tag['innerElement']:
# print(data['value'])
file_object = open('main_index.html', 'a')
file_object.write('<option>'+data['value']+'</option>')
file_object.close()
file_object = open('main_index.html', 'a')
file_object.write('</select>'+'<br>')
file_object.close()
elif(tag['element'] == 'table'):
file_object = open('main_index.html', 'a')
file_object.write('<table style="font-family: arial, sans-serif;border-collapse: collapse;width: 100%;">')
file_object.close()
for data in tag['innerElement']:
val=1
file_object = open('main_index.html', 'a')
file_object.write('<tr>')
file_object.close()
if(val==1):
for tabledata in data['innerElement']:
file_object = open('main_index.html', 'a')
file_object.write('<th style="border: 1px solid #dddddd;text-align: left;padding: 8px;">'+tabledata['value']+'</th>')
file_object.close()
val=2
elif(val>1):
for tabledata in data['innerElement']:
file_object = open('main_index.html', 'a')
file_object.write('<td style="border: 1px solid #dddddd;text-align: left;padding: 8px;">'+tabledata['value']+'</td>')
file_object.close()
val=2
file_object = open('main_index.html', 'a')
file_object.write('</tr>')
file_object.close()
file_object = open('main_index.html', 'a')
file_object.write('</table>'+'<br>')
file_object.close()
elif(tag['element'] == 'caption'):
file_object = open('main_index.html', 'a')
file_object.write('<caption>'+tag['innerText']+'</caption>'+'<br>')
file_object.close()
elif(tag['element'] == 'img'):
for data in tag['attributes']:
# for imagedata in
file_object = open('main_index.html', 'a')
file_object.write('<img style="width:100%;height:400px;" '+'src="'+'F:\FinalYear_Project\imgAndVedio/'+data['value']+'"'+'>'+'</img>'+'<br>')
file_object.close()
elif(tag['element'] == 'i'):
file_object = open('main_index.html', 'a')
file_object.write('<i>'+tag['innerText']+'</i>'+'<br>')
file_object.close()
elif(tag['element'] == 'strong'):
file_object = open('main_index.html', 'a')
file_object.write('<strong>'+tag['innerText']+'</strong>'+'<br>')
file_object.close()
elif(tag['element'] == 'b'):
file_object = open('main_index.html', 'a')
file_object.write('<b>'+tag['innerText']+'</b>'+'<br>')
file_object.close()
elif(tag['element'] == 'dialog'):
file_object = open('main_index.html', 'a')
file_object.write('<dialog open>'+tag['innerText']+'</dialog>'+'<br>')
file_object.close()
elif(tag['element'] == 'textarea'):
file_object = open('main_index.html', 'a')
file_object.write('<textarea style="width: 100%;height: 30%;">'+tag['innerText']+'</textarea>'+'<br>')
file_object.close()
elif(tag['element'] == 'fieldset'):
file_object = open('main_index.html', 'a')
file_object.write('<fieldset>'+tag['innerText']+'</fieldset>'+'<br>')
file_object.close()
elif(tag['element'] == 'br'):
file_object = open('main_index.html', 'a')
file_object.write('<br>')
file_object.close()
elif(tag['element'] == 'hr'):
file_object = open('main_index.html', 'a')
file_object.write('<hr style="border-top: 5px solid #eee;">'+'<br>')
file_object.close()
elif(tag['element'] == 'address'):
file_object = open('main_index.html', 'a')
file_object.write('<address>'+tag['innerText']+'</address>'+'<br>')
file_object.close()
elif(tag['element'] == 'footer'):
file_object = open('main_index.html', 'a')
file_object.write('<footer style="background-color: gray;">')
file_object.write('<p style="padding-left: 15px;padding-top: 15px;">'+tag['innerText']+'</p>'+'<br>')
file_object.write('<a style="padding-left: 15px;" href="mailto:[email protected]">'+'[email protected]'+'</a>'+'<br>')
file_object.write('</footer>'+'<br>'+'<br>')
file_object.close()
if __name__ == "__main__":
main()