-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterate_service.py
executable file
·189 lines (163 loc) · 5.51 KB
/
iterate_service.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# import python libs
from flask import Flask
from flask import jsonify
from flask import request
from pprint import pprint as pp
import argparse
import json
import logging
import socket
import requests
import threading
# import project libs
import sys
sys.path.append('lib')
import ner_pipeline
import iteration_processing
import merge_processing
# defining globals & constants
global app
global args
app = Flask(__name__)
# Flask routes
@app.route('/', methods=['GET'])
def who_are_you():
logging.info('who are you request; respond with JSON')
message = {
'services': [
{
'role': 'merge',
'route': '/merge'
},
{
'role': 'iterate',
'route': '/iterate'
}
]
}
return create_json_response_from(message)
@app.route('/iterate', methods=['GET'])
def iterate_who_are_you():
message = {
'role': 'iterate',
'title': 'MaxEnt NER Iterator',
'description': 'Using NLTK\'s MaxEnt chunker for german NER.',
'version': 0.3,
'problem_id': 'ner',
'interface_types': [ 'ner_complete', 'questionnaire' ]
}
return create_json_response_from(message)
@app.route('/iterate', methods=['POST'])
def iterate():
if args.async:
logging.info('iterate request (async)')
data = request.json
threading.Thread(target=async_iteration_processing, args=(data,)).start()
return create_json_response_from({ 'status': 'async' })
else:
logging.info('iterate request (request)')
(documents, statistics) = iteration_processing.process_iteration(request.json['raw_data'])
return create_json_response_from({
'annotation_documents': documents,
'statistics': statistics
})
def async_iteration_processing(data):
(documents, statistics) = iteration_processing.process_iteration(data['raw_data'])
annotation_documents = { 'annotation_documents': documents }
res = requests.post(
data['callback_urls'][0],
data=json.dumps(annotation_documents),
headers={ 'Content-Type': 'application/json' }
)
statistics = { 'statistics': statistics }
res = requests.post(
data['callback_urls'][1],
data=json.dumps(statistics),
headers={ 'Content-Type': 'application/json' }
)
@app.route('/merge', methods=['GET'])
def merge_who_are_you():
message = {
'role': 'merge',
'title': 'RawDatum replacer',
'description': 'Creates new raw data from annotation documents. ' \
'Existing Raw datum will be deleted.',
'version': 0.1,
'problem_id': 'ner'
}
return create_json_response_from(message)
@app.route('/merge', methods=['POST'])
def merge():
if args.async:
logging.info('merge request (async)')
data = request.json
threading.Thread(target=async_merge_processing, args=(data,)).start()
return create_json_response_from({ 'status': 'async' })
else:
logging.info('merge request (request)')
(raw_datum_id, annotation_documents) = merge_processing.decode_post_data(request.json)
logging.info('received %s documents as parts of raw datum #%s' % (len(annotation_documents), raw_datum_id))
raw_datum = merge_processing.create_new_raw_datum(raw_datum_id, annotation_documents)
return create_json_response_from(raw_datum)
def async_merge_processing(data):
(raw_datum_id, annotation_documents) = merge_processing.decode_post_data(data)
logging.info('received %s documents as parts of raw datum #%s' % (len(annotation_documents), raw_datum_id))
raw_datum = merge_processing.create_new_raw_datum(raw_datum_id, annotation_documents)
res = requests.patch(
data['callback_url'],
data=json.dumps(raw_datum),
headers={ 'Content-Type': 'application/json' }
)
# helpers
def create_json_response_from(hash):
response = jsonify(hash)
response.status_code = 200
return response
# entry point as a stand alone script
if __name__ == '__main__':
usePort = 5001
useHost = 'localhost'
parser = argparse.ArgumentParser(
description='Dalphi Iterate Service; 13.10.16 Robert Greinacher')
parser.add_argument(
'-a',
'--async',
action='store_true',
dest='async',
help='communicates asynchronous and non-blocking with DALPHI')
parser.add_argument(
'-d',
'--daemon',
action='store_true',
dest='daemon',
help='enables daemon mode')
parser.add_argument(
'-l',
'--localhost',
action='store_true',
dest='localhost',
help='use "localhost" instead of current network IP')
parser.add_argument(
'-p',
'--port',
type=int,
help='set the network port number')
parser.add_argument(
'-v',
'--verbose',
action='store_true',
dest='verbose',
help='enables verbose mode')
args = parser.parse_args()
if args.port:
usePort = args.port
if not args.localhost:
hostename = socket.gethostname()
useHost = socket.gethostbyname(hostename)
logging.basicConfig(filename='service.log', level=logging.INFO)
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logging.getLogger().addHandler(logging.StreamHandler())
logging.info('start running flask app')
app.run(useHost, usePort, args.verbose)