def verify_password(token, password): # first try to authenticate by token user = None s = Serializer(app.config['SECRET_KEY']) try: data = s.loads(token) rows = connectDB.db_select( "select id, username from users where id = %s ;", (data['id'], )) user = {'id': rows[0][0], 'username': rows[0][1].strip()} except SignatureExpired: print("SignatureExpired") # valid token, but expired except BadSignature: print("BadSignature") # invalid token if not user: # try to authenticate with username/password rows = connectDB.db_select( "select id, username, password from users where username = %s limit 1;", (token, )) # verify password if len(rows) == 0 or not pwd_context.verify(password, rows[0][2].strip()): print("no user " + token) return False user = {'id': rows[0][0], 'username': rows[0][1].strip()} g.user = user s = Serializer(app.config['SECRET_KEY'], expires_in=3600) return jsonify({ 'user': user['username'], 'duration': 3600, 'token': s.dumps({ 'id': user['id'] }).decode('ascii') })
def WordIntentList(): # list all intent i = request.args.get('i') if i is None or i == "": rows = connectDB.db_select( "select word, intent from word_intent order by intent ", []) else: rows = connectDB.db_select( "select word, intent from word_intent where intent = %s ", [i]) # store to dict i_dict = {} for row in rows: word = row[0].strip() intent = row[1].strip() if intent not in i_dict: i_dict[intent] = [] i_dict[intent].append(word) # reconstruct print(i_dict) ret = [] for intent in i_dict: ii = {'intent': intent, 'words': []} ii['words'] = i_dict[intent] ret.append(ii) return jsonify(ret)
def retest(): global classifier global tmp_training_file print("testing trained data...") # write training file rows = connectDB.db_select( "select message, keywords, intent from train_set;", []) count = 0 total = 0 for row in rows: m = row[0].strip() i = row[2].strip() res = test(m, False) r_i = res['intent'] r_f = res['found'] r_c = res['confidence'] print("m: '" + m + "' i: '" + i + "' r: '" + r_i + "' f: '" + r_f + "' c: " + str(r_c)) total += 1 if i == r_i: count += 1 else: print("wrong! expexted: " + i + " got: " + r_i) print("-------------------------------") rate = (count / total) * 100 print("result, total: " + str(total) + " correct: " + str(count) + " percent: " + str(rate)) result = {'total': str(total), 'correct': str(count), 'percent': str(rate)} return result
def searchWord(m): rows = connectDB.db_select( "select distinct intent from word_intent where word = %s ", [m]) if len(rows) > 0: return (rows) else: return ("empty")
def WordIntent(w): # get intent i for word w rows = connectDB.db_select( "select word, intent from word_intent where word = %s ", [w]) intent = [] for row in rows: intent.append(row[1].strip()) return jsonify(intent)
def trainWordList(q): result = [] ql = '%' + q + '%' rows = connectDB.db_select( 'select message, keywords, intent from train_set where message like %s or intent like %s', [ql, ql]) for row in rows: message = row[0].strip() keywords = row[1].strip() intent = row[2].strip() result.append({ 'message': message, 'intent': intent, 'keyword': keywords }) return jsonify(result)
def load_classifier(): global classifier global tmp_training_file epoch = 10 # write training file rows = connectDB.db_select( "select message, keywords, intent from train_set;", []) count = 0 with open(tmp_training_file, "w") as t_f: for e in range(0, epoch): for row in rows: l = '__label__' + \ row[2].strip().replace(" ", "_;") + \ ' __label__' + row[1].strip() + "\n" t_f.write(l) count += 1 # reload classifier if count > 0: #classifier = fasttext.train_supervised(tmp_training_file, 'class', label_prefix='__label__', pretrained_vectors='model.vec') classifier = fasttext.train_supervised(input="tmp_train_2.txt", lr=1) classifier.save_model("model.bin")