forked from padfoot18/SAHEB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
391 lines (318 loc) · 10.8 KB
/
app.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
from flask_restful import Resource, Api
from deeppavlov import build_model, configs
from nltk import word_tokenize
from flask import Flask, jsonify, render_template, request, flash, redirect, url_for, session
from flask_cors import CORS
from wtforms import Form, StringField, PasswordField, validators
from passlib.hash import sha256_crypt
from functools import wraps
from substitute_data import InsertData, UpdateData, ReadData, DeleteData
from paragraph_api import Paragraph
import sqlite3
from nltk.corpus import stopwords
from string import punctuation
import re
app = Flask(__name__)
CORS(app)
api = Api(app)
model = None
paragraph = None
values = dict()
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
@app.route('/hello/')
def hello_world():
return 'Hello, World!'
@app.before_first_request
def init_stuff():
"""
Initialize the data and the model before first request is processed.
:return: None
"""
load_data()
# load_model()
class ChatBot(Resource):
def post(self):
threshold = 45000
minimum_match = 1
question = request.form['question']
question = question.strip()
question = question
if question[-1] != "?":
question += '?'
print(question)
answer = model([paragraph], [question])
print(answer)
answer_main = answer[0][0]
keys = re.findall('zxyw[^\s.]*', answer_main)
if keys:
print(keys)
for k in keys:
answer_main = re.sub(k, values[k[4:]], answer_main)
print(answer_main)
if answer[2][0] < threshold:
question_list = removeStopWords(question)
answer_list = removeStopWords(answer_main)
print(question_list,answer_list)
count = 0
for i in question_list:
for j in answer_list:
if i == j:
count += 1
if count >= minimum_match:
return answer_main
else:
return "Sorry i didn't get that!"
else:
return answer_main
def load_model():
# load the model into memory
global model
model = build_model(configs.squad.squad, download=False)
def removeStopWords(words):
customStopWords = set(stopwords.words('english') + list(punctuation))
return [word for word in word_tokenize(words) if word not in customStopWords]
def load_data():
# DONE (3) load the paragraph and all the key-value pairs into the global variables
global paragraph
global values
para_sql = "select * from paragraph;"
values_sql = "select * from blank_data;"
try:
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
cursor.execute(para_sql)
paragraph = cursor.fetchall()[0][0]
cursor.execute(values_sql)
values_list = cursor.fetchall()
for i in values_list:
values.update({i[1]: i[2]})
print(paragraph)
print(values)
except Exception as e:
print(e)
finally:
if conn:
conn.close()
# check if user is logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
print(session['logged_in'])
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('login'))
return wrap
@app.route('/', methods=['GET', 'POST'])
def index():
if 'logged_in' in session:
if session['logged_in']:
return redirect('/para/')
else:
return render_template('login.html')
@app.route('/key_values/')
@is_logged_in
def admin_page():
return render_template('key_vals.html', js_files=['key-val.js', ])
@app.route('/read/values/')
@is_logged_in
def read_values():
formatted_data = []
try:
connection = sqlite3.connect('test.db')
c = connection.cursor()
c.execute('SELECT * FROM blank_data')
table_data = c.fetchall()
for items in table_data:
formatted_data.append(dict(id=items[0], key=items[1], value=items[2]))
connection.commit()
resp = jsonify(formatted_data)
except Exception as exception:
print(exception)
resp = jsonify(success=False)
finally:
if connection:
connection.close()
return resp
@app.route('/edit_para/', methods=['POST', 'GET'])
@is_logged_in
def edit_para():
if request.form['str']:
new_paragraph = request.form['str']
print(new_paragraph)
try:
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute('UPDATE paragraph SET para="' + new_paragraph + '";')
# conn.commit()
keys_para = re.findall('zxyw[^\s.]*]', new_paragraph)
c.execute('SELECT key FROM blank_data')
keys_db = c.fetchall()
for item in keys_para:
if item not in keys_db:
c.execute("DELETE FROM blank_data WHERE key='"+item+"';")
conn.commit()
c.execute('select * from paragraph;')
new_paragraph = c.fetchall()
except Exception as exception:
print(exception)
finally:
if conn:
conn.close()
return new_paragraph
@app.route('/update/values/', methods=['POST', ])
@is_logged_in
def update_values():
try:
connection = sqlite3.connect('test.db')
c = connection.cursor()
if request.form['id'] and request.form['value']:
i = request.form['id']
value = request.form['value']
sql = 'update blank_data set `value` = "'+value+'" where `id` = "'+i+'";'
c.execute(sql)
connection.commit()
resp = jsonify(success=True, id=i, value=value)
except Exception as exception:
print(exception)
resp = jsonify(success=False)
finally:
if connection:
connection.close()
return resp
@app.route('/insert/values/', methods=['POST', ])
@is_logged_in
def insert_values():
if request.form['key'] and request.form['value']:
key = request.form['key']
value = request.form['value']
try:
connection = sqlite3.connect('test.db')
c = connection.cursor()
sql = 'INSERT INTO blank_data (`key`, `value`) VALUES("' + key + '", "' + value + '");'
c.execute(sql)
connection.commit()
sql = 'select * from blank_data where `key` = "'+key+'";'
c.execute(sql)
data = c.fetchall()
formatted_data = {"id": data[0][0], "key": data[0][1], "value": data[0][2]}
resp = jsonify(success=True, data=formatted_data)
except sqlite3.IntegrityError as e:
print(e)
resp = jsonify(success=False, error="Key already exists!")
finally:
if connection:
connection.close()
return resp
@app.route('/delete/values', methods=['POST', ])
@is_logged_in
def delete_values():
if request.form['key']:
key = request.form['key']
try:
connection = sqlite3.connect('test.db')
c = connection.cursor()
sql = 'delete from blank_data where `key` = "'+key+'";'
c.execute(sql)
connection.commit()
resp = jsonify(success=True)
except Exception as e:
print(e)
resp = jsonify(success=False)
finally:
if connection:
connection.close()
return resp
@app.route('/para/')
@is_logged_in
def read_para():
try:
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute('select * from paragraph')
paragraph = c.fetchall()
conn.commit()
except sqlite3.IntegrityError:
return {"error"}
except Exception as exception:
print(exception)
finally:
if conn:
conn.close()
return render_template('view_para.html', para = paragraph[0][0], js_files=['para.js', ])
# register form class
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
# username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Password do not match')
])
confirm = PasswordField('Confirm Password')
# user register
@app.route('/register', methods=['GET', 'POST'])
@is_logged_in
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
# username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
try:
connection = sqlite3.connect('test.db')
cursor = connection.cursor()
cursor.execute("INSERT INTO users(name,email,password) VALUES('"+name+"','"+email+"','"+password+"')")
connection.commit()
connection.close()
return redirect(url_for('dashboard'))
except sqlite3.IntegrityError as ie:
print(ie)
except Exception as e:
print(e)
return render_template('register.html', form=form)
# User Login
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password_candidate = request.form['password']
connection = sqlite3.connect('test.db')
cursor = connection.cursor()
result = cursor.execute("SELECT * FROM users WHERE email = '"+email+"'")
print(result.fetchall())
if result.arraysize > 0:
data = result.fetchall()
print(data)
password = data[2]
if sha256_crypt.verify(password_candidate, password):
app.logger.info('PASSWORD MATCHED')
session['logged_in'] = True
session['email'] = email
flash('You are now logged in', 'success')
return redirect('/para/')
else:
app.logger.info('PASSWORD NOT MATCHED')
error = 'Incorrect Password'
return render_template('login.html', error=error)
cursor.close()
else:
app.logger.info('NO USER')
error = 'Username not found'
return render_template('login.html', error=error)
return render_template('login.html')
# logout
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out ', 'success')
return redirect(url_for('login'))
api.add_resource(ChatBot, '/chat/')
if __name__ == '__main__':
app.secret_key = 'qwertyuuiop'
app.run(host='127.0.0.1', port=5000, debug=True)