-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
93 lines (67 loc) · 2.37 KB
/
server.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
# -*- coding:utf-8 -*-
from flask import Flask, render_template, request, jsonify, send_file
from werkzeug.contrib.fixers import ProxyFix
from module import classify, extractor, imageprocess, qna
import os
UPLOAD_FOLDER = "db"
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
MrLee = Flask(__name__)
MrLee.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
MrLee.wsgi_app = ProxyFix(MrLee.wsgi_app)
## Main Page
@MrLee.route("/")
def main():
return render_template("main.html")
@MrLee.route("/text")
def text():
return render_template("text.html")
@MrLee.route("/qna")
def qna():
return render_template("qna.html")
@MrLee.route("/image")
def image():
return render_template("image.html")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
## POST API
@MrLee.route("/process", methods=['POST'])
def process():
error = None
if request.method == 'POST':
if request.form['action'] == "text":
contents = request.form['contents']
result_class = classify.do(contents).split()[0]
result_keyword = extractor.extract(contents)
result_pmi = extractor.doPMI(result_class, result_keyword)
returnee = {
"class": result_class,
"keyword": result_keyword,
"pmi": result_pmi
}
return jsonify(returnee)
elif request.form['action'] == "qna":
returnee = {
"answer": qna.quest(request.form['contents'])
}
return jsonify(returnee)
elif request.form['action'] == "image":
image = request.files['image']
returnee = {}
if image and allowed_file(image.filename):
fullpath = os.path.join(MrLee.config['UPLOAD_FOLDER'], image.filename)
image.save(fullpath)
returnee = {
"uploaded": fullpath,
"inspected": imageprocess.do(os.path.abspath(fullpath))
}
return jsonify(returnee)
else:
error = "Invalid action inserted."
return error
## Get Images
@MrLee.route("/db/<filename>", methods=['GET'])
def uploaded(filename):
return send_file(os.path.join(MrLee.config['UPLOAD_FOLDER'], filename))
if __name__ == "__main__":
MrLee.run(debug=True, port=8000)