Skip to content

Commit 265f35e

Browse files
author
Nico Colic
committed
optimizations for running on server
1 parent 4451bc3 commit 265f35e

File tree

3 files changed

+35
-168
lines changed

3 files changed

+35
-168
lines changed

accessor.py

+17-164
Original file line numberDiff line numberDiff line change
@@ -15,162 +15,6 @@
1515

1616
TEXT_ERROR = "No 'text' to process. Use spacy_rest?text=This+is+an+example."
1717
DEFAULT_TEST_INPUT = "It is a REST service to produce annotation of syntactic parsing."
18-
TEST_JSON = """{
19-
"text": "It is a REST service to produce annotation of syntactic parsing.",
20-
"denotations": [
21-
{
22-
"id": "T0",
23-
"span": {
24-
"begin": 0,
25-
"end": 2
26-
},
27-
"obj": "PRP"
28-
},
29-
{
30-
"id": "T1",
31-
"span": {
32-
"begin": 3,
33-
"end": 5
34-
},
35-
"obj": "VB"
36-
},
37-
{
38-
"id": "T2",
39-
"span": {
40-
"begin": 6,
41-
"end": 7
42-
},
43-
"obj": "DT"
44-
},
45-
{
46-
"id": "T3",
47-
"span": {
48-
"begin": 8,
49-
"end": 12
50-
},
51-
"obj": "NN"
52-
},
53-
{
54-
"id": "T4",
55-
"span": {
56-
"begin": 13,
57-
"end": 20
58-
},
59-
"obj": "NN"
60-
},
61-
{
62-
"id": "T5",
63-
"span": {
64-
"begin": 21,
65-
"end": 23
66-
},
67-
"obj": "TO"
68-
},
69-
{
70-
"id": "T6",
71-
"span": {
72-
"begin": 24,
73-
"end": 31
74-
},
75-
"obj": "VB"
76-
},
77-
{
78-
"id": "T7",
79-
"span": {
80-
"begin": 32,
81-
"end": 42
82-
},
83-
"obj": "NN"
84-
},
85-
{
86-
"id": "T8",
87-
"span": {
88-
"begin": 43,
89-
"end": 45
90-
},
91-
"obj": "IN"
92-
},
93-
{
94-
"id": "T9",
95-
"span": {
96-
"begin": 46,
97-
"end": 55
98-
},
99-
"obj": "JJ"
100-
},
101-
{
102-
"id": "T10",
103-
"span": {
104-
"begin": 56,
105-
"end": 63
106-
},
107-
"obj": "NN"
108-
}
109-
],
110-
"relations": [
111-
{
112-
"id": "R0",
113-
"subj": "T6",
114-
"obj": "T0",
115-
"pred": "arg1Of"
116-
},
117-
{
118-
"id": "R1",
119-
"subj": "T6",
120-
"obj": "T1",
121-
"pred": "arg1Of"
122-
},
123-
{
124-
"id": "R2",
125-
"subj": "T4",
126-
"obj": "T1",
127-
"pred": "arg2Of"
128-
},
129-
{
130-
"id": "R3",
131-
"subj": "T4",
132-
"obj": "T2",
133-
"pred": "arg1Of"
134-
},
135-
{
136-
"id": "R4",
137-
"subj": "T4",
138-
"obj": "T3",
139-
"pred": "arg1Of"
140-
},
141-
{
142-
"id": "R5",
143-
"subj": "T6",
144-
"obj": "T5",
145-
"pred": "arg1Of"
146-
},
147-
{
148-
"id": "R6",
149-
"subj": "T7",
150-
"obj": "T6",
151-
"pred": "arg2Of"
152-
},
153-
{
154-
"id": "R7",
155-
"subj": "T7",
156-
"obj": "T8",
157-
"pred": "arg1Of"
158-
},
159-
{
160-
"id": "R8",
161-
"subj": "T10",
162-
"obj": "T8",
163-
"pred": "arg2Of"
164-
},
165-
{
166-
"id": "R9",
167-
"subj": "T10",
168-
"obj": "T9",
169-
"pred": "arg1Of"
170-
}
171-
]
172-
}
173-
"""
17418

17519
app = Flask(__name__)
17620

@@ -217,10 +61,12 @@ def rest():
21761
verbose("Received GET request for '{}'. Will return HTML...".format(request.args['text']))
21862
try:
21963
json_ = text_to_json(request.args['text'])
220-
pretty_json = json.dumps(json.loads(json_),sort_keys=True,indent=4)
64+
verbose(json_,type(json_))
65+
pretty_json = json.dumps(str(json.loads(json_)),sort_keys=True,indent=4)
66+
verbose(pretty_json,type(pretty_json))
22167
return(render_template('index.html',json=json_,pretty_json=pretty_json,input_text=request.args['text']))
222-
except Exception:
223-
return(error_page("Error processing GET request for '{}'".format(request.args['text'])),500)
68+
except Exception as e:
69+
return(error_page("Error processing GET request for '{}'\n{}".format(request.args['text'],e)),500)
22470

22571
# some other fantasy argument supplied
22672
if len(request.args) > 0:
@@ -269,12 +115,13 @@ def text_to_json(text):
269115

270116
try:
271117
tokens = ask_stanford(STANFORD,text)
272-
print(tokens)
118+
verbose(tokens)
273119
tokens, tags = stanford_to_lists(tokens)
274-
print(tokens,tags)
120+
verbose(tokens,tags)
275121
doc = lists_to_spacy(tokens,tags,SPACY)
276-
print(doc)
122+
verbose(doc)
277123
json_ = spacy_to_pubannotation(doc)
124+
verbose(json_)
278125
return(json_)
279126
except Exception:
280127
return('400 Bad Request (possibly an error occured when parsing due to unexpected format',400)
@@ -284,6 +131,11 @@ def json_to_response(json_):
284131

285132
# This is necessary to allow the REST be accessed from other domains
286133
response.headers['Access-Control-Allow-Origin'] = '*'
134+
135+
response.headers['Content-Type'] = 'application/json'
136+
response.headers['Content-Length'] = len(json_)
137+
response.headers['X-Content-Type-Options'] = 'nosniff'
138+
response.headers['charset'] = 'utf-8'
287139
return(response)
288140

289141

@@ -388,9 +240,10 @@ def spacy_to_pubannotation(doc):
388240
#########################
389241
# SCRIPT HELPER FUNCTIONS
390242
#########################
391-
def verbose(text):
243+
def verbose(*args):
392244
if arguments.verbose:
393-
print(text)
245+
for arg in args:
246+
print(arg)
394247
return time.time()
395248
return 0
396249

example_queries.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
curl 127.0.0.1:5000/spacy_rest?text=This+is+a+test
1+
GENERAL CURLS
2+
*************
23

3-
curl -d text="Induction of chromosome banding by trypsin/EDTA for gene mapping by in situ hybridization. We describe an easy and reproducible procedure that utilizes trypsin/EDTA for the induction of chromosome banding in conjunction with in situ hybridization. The high quality banding resolution required for grain localization is obtained on both elongated and contracted chromosomes derived from synchronized or nonsynchronized human lymphocytes or fibroblasts. This procedure can also be useful for gene localization on chromosomes from cancer cells." 127.0.0.1:5000/spacy_rest/
4+
SHORT:
5+
curl 127.0.0.1:5000/spacy_rest?text=This+is+a+test
46

7+
MEDIUM:
58
curl -H "content-type:application/json" -d '{"text":"Induction of chromosome banding by trypsin/EDTA for gene mapping by in situ hybridization."}' 127.0.0.1:5000/spacy_rest/
69

10+
LONG:
11+
curl -d text="Induction of chromosome banding by trypsin/EDTA for gene mapping by in situ hybridization. We describe an easy and reproducible procedure that utilizes trypsin/EDTA for the induction of chromosome banding in conjunction with in situ hybridization. The high quality banding resolution required for grain localization is obtained on both elongated and contracted chromosomes derived from synchronized or nonsynchronized human lymphocytes or fibroblasts. This procedure can also be useful for gene localization on chromosomes from cancer cells." http://cl26.dbcls.jp:38503/spacy_rest

static/style.css

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
/* Color Scheme
2+
* dark blue: 9BB2D7
3+
* light blue: B9D0F3
4+
* green: 99A853
5+
* light brown: A4917A
6+
* dark brown: 5A4E40 */
7+
18
body, .pre-blur-box {
29
background-image: url('background.jpg');
310
background-repeat: no-repeat;
411
background-size: cover;
512
background-position: center center;
613
background-attachment: fixed;
7-
background-color: #ccccff;
14+
background-color: #B9D0F3;
815
}
916

1017
body {
18+
border-top: solid 4px #5A4E40;
19+
margin: 0px 0px 0px 0px;
1120
background-position: center center;
1221
background-attachment: fixed;
13-
background-color: #ccccff;
22+
background-color: #B9D0F3;
1423
z-index: 1
1524
}
1625

0 commit comments

Comments
 (0)