-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
169 lines (133 loc) · 4.47 KB
/
app.py
File metadata and controls
169 lines (133 loc) · 4.47 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
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
from flask import Flask, jsonify, request, json
from flask_cors import CORS
import nltk, csv
from random import sample
import os.path
from dataloader import fileformatter
# configuration
DEBUG = True
# instantiate the app
app = Flask(__name__)
app.config.from_object(__name__)
# enable CORS
CORS(app, resources={r'/*': {'origins': '*'}})
if not os.path.exists('data.json'):
fileformatter.run()
# Load all data at runtime
with open('data.json') as json_file:
LABELS = json.load(json_file)
with open('spandata.json') as json_file:
SPANS = json.load(json_file)
with open('completed.csv') as csvfile:
COMPLETED = [int(item) for item in list(csv.reader(csvfile,delimiter=','))[0]]
def saveProgress():
with open('data.json','w') as fp:
json.dump(LABELS, fp)
with open('spandata.json', 'w') as fp:
json.dump(SPANS, fp)
with open('completed.csv','w') as fp:
wr = csv.writer(fp, quoting=csv.QUOTE_ALL)
wr.writerow(COMPLETED)
def getspanvalueslist(idx):
span_values = []
for key in SPANS[COMPLETED[idx]].keys():
if key != 'id':
for value in SPANS[COMPLETED[idx]][key].values():
span_values.extend(value)
return span_values
@app.route('/getnextdocument', methods=['GET'])
def getnextdocument():
# Here we implement active learning
return jsonify({
'status': 'success',
'labels': LABELS[COMPLETED[-1]],
'spans': SPANS[COMPLETED[-1]],
'span_values': getspanvalueslist(-1)
})
@app.route('/getpreviousdocument', methods=['GET'])
def getpreviousdocument():
return jsonify({
'status': 'success',
'labels': LABELS[COMPLETED[-1]]
})
@app.route('/updateSpans', methods=['POST'])
def updateSpans():
response_object = {'status': 'success'}
req_data = request.get_json()
spans = req_data['newspans']
category = req_data['category']
label = req_data['label']
id = req_data['id']
combinedspans = list(set(spans + SPANS[id][category][label]))
print('updating span:', 'id:', id, ',label:', label, ',category:', category)
SPANS[id][category][label] = combinedspans
saveProgress()
return response_object
@app.route('/changelabel', methods=['POST'])
def changelabel():
req_data = request.get_json()
category = req_data['category']
label = req_data['label']
id = req_data['id']
print('changing labels:', 'id:', id, ',label:', label, ',category:', category)
LABELS[id][category][label] = not LABELS[id][category][label]
SPANS[id][category][label] = []
response_object = {'status': 'success',
'new_value': LABELS[id][category][label]}
saveProgress()
return response_object
@app.route('/addlabel', methods=['POST'])
def addlabel():
response_object = {'status': 'success'}
req_data = request.get_json()
newlabel = req_data['newlabel']
category = req_data['category']
for idx, span in enumerate(SPANS):
span[category][newlabel] = []
LABELS[idx][category][newlabel] = False
saveProgress()
return response_object
@app.route('/changecurrentid', methods=['POST'])
def changecurrentid():
response_object = {'status': 'success'}
req_data = request.get_json()
new_id = int(req_data['newid'])
COMPLETED.append(new_id)
return response_object
@app.route('/getCurrentId', methods=['GET'])
def getcurrentid():
return jsonify({
'status': 'success',
'currentid': COMPLETED[-1]
})
@app.route('/getHistory', methods=['GET'])
def gethistory():
print('returning history')
return jsonify({
'status': 'success',
'history': COMPLETED
})
@app.route('/getDocumentviaIndex', methods=['POST'])
def getdocumentviaindex():
print('returning document using idx')
req_data = request.get_json()
req_idx = int(req_data['newIdx'])
return jsonify({
'status': 'success',
'labels': LABELS[COMPLETED[req_idx]],
'spans': SPANS[COMPLETED[req_idx]],
'span_values': getspanvalueslist(req_idx)
})
@app.route('/sampleNewDocument', methods=['GET'])
def samplenewdocument():
all_documents= set(list(range(len(LABELS))))
incomplete = list(all_documents.difference(set(COMPLETED)))
#random sampling one atm
sampled_id = sample(incomplete,1)[0]
COMPLETED.append(sampled_id)
saveProgress()
return jsonify({
'status': 'success'
})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0' ,port='5001')