-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
76 lines (59 loc) · 2.16 KB
/
Copy pathserver.py
File metadata and controls
76 lines (59 loc) · 2.16 KB
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
from flask import Flask, request, jsonify
import os
from pico.pico_span_robot import PICOSpanRobot
from pico.pico_robot import PICORobot
app = Flask(__name__)
PICO_TIAB_BOT = PICOSpanRobot()
PICO_TEXT_BOT = PICORobot()
# this is a guess, based on typical abstract length, of what should run in the span of a typical web request
MAX_REQUEST_SIZE = 500
def validate_length(*argv):
if len(argv) == 0:
return False
all_lists = [type(x) is list for x in argv]
if not all_lists:
return False
if len(argv) == 1:
return len(argv[0]) <= MAX_REQUEST_SIZE
else:
n = len(argv[0])
all_equal_length = all(len(x) == n for x in argv)
return all_equal_length and n <= MAX_REQUEST_SIZE
@app.route('/pico', methods=['POST'])
def get_pico():
body = request.get_json()
if not body:
return jsonify({
"message": "Must supply application/json data type"
}), 400
results = []
if 'abstract' in body and 'title' in body:
if validate_length(body['title'], body['abstract']):
for title, abstract in zip(body["title"], body["abstract"]):
annotation = PICO_TIAB_BOT.annotate(abstract, title)
results.append(annotation)
else:
return jsonify({
"message": "'abstract' and 'title' must be lists of equal length, less than or equal to %d" % (MAX_REQUEST_SIZE,)
}), 400
elif 'full_text' in body:
if validate_length(body['full_text']):
for text in body['full_text']:
results.append(PICO_TEXT_BOT.annotate(text))
else:
return jsonify({
"message": "'full_text' must be a list with length less than or equal to %d" % (MAX_REQUEST_SIZE, )
}), 400
else:
return jsonify({
"message": "Must supply {'abstract', title'} or {'full_text'}"
}), 400
return jsonify(results)
if __name__ == "__main__":
# this entry point should only be used in dev
app.run(
host=os.environ.get("RP_HOST", "localhost"),
debug=True,
port=os.environ.get("RP_PORT", 5050),
threaded=True
)