-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
195 lines (170 loc) · 6.06 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
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
from flask import Flask, redirect, url_for, request, jsonify
import argparse
import os
import json
from modules.pdf import PDFParser
from modules.database import Database
from modules.configs import ModelConfig
app = Flask(__name__)
UPLOAD_FOLDER = './uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
ALLOWED_EXTENSIONS = {'pdf'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
database = Database()
configs = ModelConfig()
def get_args():
parser = argparse.ArgumentParser(description='Document Processing API')
parser.add_argument('-p', '--port', type=str, default="12345",
help='Port number', nargs='+')
args = parser.parse_args()
port = args.port[0]
return port
port_code = get_args()
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['POST'])
def upload_document():
try:
if 'file' not in request.files:
print('No file part')
file = request.files['file']
metadata = json.loads(file.filename)
id = metadata["id"]
exists = database.document_exists(id)
if not exists:
if 'category' in metadata:
categories = metadata['category'].split('/')
else:
categories = ['unclassified']
categories.append(metadata["id"])
parser = PDFParser(metadata["id"], app.config['UPLOAD_FOLDER'], categories, file)
parser.parse()
message = "Submitted job for Document ID %s." % id
response = jsonify({"message": message})
else:
message = "Document ID %s already exists." % id
response = jsonify({"message": message})
response.status_code = 500
except Exception as e:
message = str(e)
response = jsonify({"message": message})
response.status_code = 404
return response
@app.route('/fetch', methods=['GET'])
def fetch_document():
id = request.args.get('id')
if id is not None:
content, status = database.fetch_document(id)
if content is None:
message = 'Document ID %s is being processed.' % id
response = jsonify({"message": message})
response.status_code = 500
return response
else:
message = content
json_path = os.path.join(message['processed_path'], message['id'] + '.json')
with open(json_path, 'r') as fi:
message['content'] = json.loads(fi.read())
return jsonify(message)
else:
response = jsonify({"message": "id not provided"})
response.status_code = 404
return response
@app.route('/tag', methods=['POST'])
def tag_document():
data = request.get_json(force=True)
if "id" in data and "categories" in data:
categories = data["categories"]
id = data["id"]
if id is not None:
content, status = database.fetch_document(id)
if content is None:
message = 'Document ID %s does not exist.' % id
response = jsonify({"message": message})
response.status_code = 500
return response
else:
database.update_category(id, categories, content, app.config['UPLOAD_FOLDER'])
message = {"message": "OK"}
return jsonify(message)
else:
response = jsonify({"message": "id/categories not provided"})
response.status_code = 404
return response
@app.route('/models', methods=['POST'])
def create_model_config():
data = request.get_json(force=True)
if 'name' not in data:
response = jsonify({"message": "Name is required."})
response.status_code = 500
return response
name = data["name"]
if 'params' not in data:
response = jsonify({"message": "Model parameters are required."})
response.status_code = 500
return response
params = data["params"]
status, message = configs.create(name, params)
if status:
return jsonify({"message": "Created config: %s" % name})
else:
response = jsonify({"message": message})
response.status_code = 500
return response
@app.route('/train', methods=['POST'])
def train_model():
data = request.get_json(force=True)
if 'name' not in data:
response = jsonify({"message": "Name is required."})
response.status_code = 500
return response
name = data["name"]
status, message = configs.train(name)
if status:
return jsonify({"message": "Submitted model %s for training." % name})
else:
response = jsonify({"message": message})
response.status_code = 500
return response
@app.route('/train_status', methods=['POST'])
def train_status():
data = request.get_json(force=True)
if 'name' not in data:
response = jsonify({"message": "Name is required."})
response.status_code = 500
return response
name = data["name"]
status, message = configs.check_status(name)
if status:
return jsonify({"message": message})
else:
response = jsonify({"message": message})
response.status_code = 500
return response
@app.route('/predict', methods=['POST'])
def get_prediction():
data = request.get_json(force=True)
if 'name' not in data:
response = jsonify({"message": "Name is required."})
response.status_code = 500
return response
name = data["name"]
if 'id' not in data:
response = jsonify({"message": "Document ID is required."})
response.status_code = 500
return response
id = data["id"]
status, message = configs.predict(id, name)
if status:
return jsonify({"message": message})
else:
response = jsonify({"message": message})
response.status_code = 500
return response
if __name__ == '__main__':
try:
app.run(debug=True, port=int(port_code))
except Exception:
app.run(debug=True, port=12345)