-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
89 lines (65 loc) · 1.93 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
# -*- coding: utf-8 -*-
"""
@author: Chen Weiling
@software: PyCharm
@file: app.py.py
@time: 3/23/2020 1:59 PM
@comments:
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import pickle
from model import *
from utils import *
import warnings
from flask import Flask, render_template, request, make_response
from flask import jsonify
import sys
import time
import hashlib
import threading
warnings.filterwarnings("ignore")
def heartbeat():
print(time.strftime('%Y-%m-%d %H:%M:%S - heartbeat', time.localtime(time.time())))
timer = threading.Timer(60, heartbeat)
timer.start()
timer = threading.Timer(60, heartbeat)
timer.start()
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import re
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
app = Flask(__name__, static_url_path="/static")
@app.route('/message', methods=['POST'])
def reply():
req_msg = request.form['msg']
print('Message received:', req_msg)
res_msg, sim = retrieveAnswer_ann(req_msg, sent_emb, u)
if sim > threshold_ann:
res_msg = generateAnswer(req_msg, searcher, voc)
print('Message sent:', res_msg)
# 如果接受到的内容为空,则给出相应的回复
if res_msg == '':
res_msg = '你好,我现在有事不在,一会再和你联系。'
return jsonify({'text': res_msg})
@app.route("/")
def index():
return render_template("index.html")
encoder, decoder, voc, pairs, embedding = initGenModel()
# Set dropout layers to eval mode
encoder.eval()
decoder.eval()
# Initialize search module
searcher = GreedySearchDecoder(encoder, decoder)
sent_emb = pd.read_pickle(sentEmbFile)
# 结巴分词准备
init = "".join(list(jieba.cut("聊天系统初始化成功")))
# 加载annoy index
u = AnnoyIndex(hidden_size, 'angular')
u.load(annoyIdxFile)
# 启动APP
app.run()