-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
98 lines (68 loc) · 2.25 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
# import the Flask class from the flask module
from flask import Flask, render_template, request, send_from_directory, make_response
import os
import db_manager
import json
from db_manager import DB
import config
import pickle
from inference import infer
import re
import string
import nltk
current_dir = os.curdir
# create the application object
app = Flask(__name__)
app.config['DATABASE_PATH'] = os.path.join(os.curdir, 'db/db.sqlite')
global kmeans
global vectorizer
stopword = nltk.corpus.stopwords.words('english')
def clean_text(text):
text_nopunct = "".join([char for char in text if char not in string.punctuation])
tokens = re.split('\W+', text_nopunct)
text = [word for word in tokens if word not in stopword]
return text
kmeans = pickle.load(open("kmeans.pkl", "rb"))
vectorizer = pickle.load(open('tfidf.pickle', "rb"))
def init():
global manager
data_dir = os.path.join(os.curdir, 'db')
if not os.path.exists(data_dir):
os.mkdir(data_dir)
manager = DB(app.config['DATABASE_PATH'])
if not os.path.exists(app.config['DATABASE_PATH']):
manager.create_table(db_manager.CREATE_TABLE_STATEMENT)
print("REQUIRED TABLES CREATED..")
print("VECTORIZER LOADED")
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db/test.db'
@app.route('/<path:path>')
def send_js(path):
return send_from_directory('static', path)
# use decorators to link the function to a url
@app.route('/')
def home():
# return "Hello, World!" # return a string
return render_template('search.html')
'''
API DEVELOPMENT
'''
@app.route('/get_relevant', methods=['POST'])
def get_invite():
result = {"error": 1}
try:
query = json.loads(request.data.decode('utf-8'))['query']
group = infer(query, kmeans, vectorizer)
resultDf = manager.get_emails(group[0])
data = []
for index, row in resultDf.iterrows():
data.append(dict(row))
result['data'] = data
result["error"] = 0
return make_response(json.dumps(result), 200)
except Exception as err:
result["message"] = str(err)
return make_response(json.dumps(result), 404)
# start the server with the 'run()' method
if __name__ == '__main__':
init()
app.run(debug=True)