forked from RuiXilin0530/annotation_platform_mmqa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsd_annotation_backend.py
138 lines (114 loc) · 3.97 KB
/
msd_annotation_backend.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
import os
import json
import sys
from flask import Flask, jsonify, request, abort
import time
import logging
sys.path.append('/Users/lizijun/Documents/uiuc/platform/Annotation front+back/')
from task_distributor import DynamicTaskManager, Message
timestamp = time.strftime('%Y%m%d_%H%M', time.localtime())
# Load config file
with open('config.json', encoding = 'utf8') as f:
config = json.load(f)
# Config logging
if not os.path.exists('./log'):
os.mkdir('./log')
logging.basicConfig(
#format = '%(asctime)s - %(message)s',
#datefmt = '%Y-%m-%d %H:%M:%S',
format = '%(name)s: %(message)s',
level = logging.DEBUG,
filename='./log/log_{}.txt'.format(timestamp),
filemode='w')
logging.root.addHandler(logging.StreamHandler(sys.stdout))
logging.info(json.dumps(config, indent = 4, sort_keys = True, ensure_ascii = False))
# Load all data
with open(config['data_fn'], encoding='utf8') as f:
all_data = json.load(f)
# all_data = [json.loads(k) for k in f]
all_data = all_data['data'][0]["paragraphs"]
logging.info('Original data: {}'.format(len(all_data)))
# if("id" not in all_data[0]):
# for i in range(len(all_data)):
# all_data[i]['id'] = str(i)
id_key = config['id_key']
# Load finish id list
finish_list = set()
if len(config['finish_fn']) > 0:
logging.info('Find finished samples')
for fin_fn in config['finish_fn']:
with open(fin_fn, encoding='utf8') as f:
cur_fin = set([json.loads(k)[id_key] for k in f])
logging.info('{}: {}'.format(fin_fn, len(cur_fin)))
finish_list.update(cur_fin)
logging.info('Total finished samples: {}'.format(len(finish_list)))
# Filter data
all_data = [d for d in all_data if d[id_key] not in finish_list]
print(all_data[0])
logging.info('After filtering: {} samples'.format(len(all_data)))
save_fn = './anno_data/labeled_data_{}.txt'.format(timestamp)
pw = None if config['pw_fn'] is None else json.load(open(config['pw_fn'], encoding='utf8'))
# Get task manager object
my_task = DynamicTaskManager(all_data, save_fn, pw,
id_key, config['num_worker'], config['timeout'])
app = Flask(__name__)
@app.route('/')
def info_page():
with open('./home.html', 'r', encoding='utf8') as f:
web = f.read()
return web
@app.route('/get-data', methods = ['POST'])
def get_data():
body = request.json
u = body.get('user', '')
p = body.get('password', '')
message = my_task.get_user_data(u, p)
return jsonify(message)
@app.route('/send-data', methods = ['POST'])
def send_data():
body = request.json
u = body.get('user', '')
p = body.get('password', '')
pid = body.get(id_key, '')
result = body.get('label_result')
message = my_task.save_label_result(u, pid, result, p)
return jsonify(message)
@app.route('/authentication', methods = ['POST'])
def authenticate():
body = request.json
u = body.get('user', '')
p = body.get('password', '')
message = my_task.auth(u, p)
if message is None:
message = Message(100, 'Successful')
return jsonify(message)
@app.route('/web/<fn>')
def get_web(fn):
path = os.path.join('./web', fn)
if os.path.exists(path):
with open(path, encoding='utf8') as f:
cont = f.read()
return cont
else:
abort(404)
#-------------------
# Administrator
#-------------------
@app.route('/admin/user')
def admin_web():
his = my_task.info_user_history()
return jsonify(his)
@app.route('/admin/reload-user-pw')
def reload_pw():
my_task.passwd_table = json.load(open(config['pw_fn'], encoding='utf8'))
return '0'
@app.route('/admin/cache')
def cache():
res = my_task.info_working_task()
return jsonify(res)
@app.route('/admin/cache/verbose')
def cache_verbose():
res = my_task.info_working_task(True)
return jsonify(res)
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = config['port'], debug = False)