-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
174 lines (142 loc) · 4.7 KB
/
app.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
from flask import Flask, render_template, redirect, url_for
from flask import request, jsonify
from flask_apscheduler import APScheduler
import random
import json
import os
import subprocess
import sys
from collections import Counter
from data_pool import Datasets
try:
from datasets import load_dataset
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", 'datasets'])
finally:
from datasets import load_dataset
dataset = Datasets().get_data()
# set configuration values
class Config:
SCHEDULER_API_ENABLED = True
app = Flask(__name__)
app.config.from_object(Config())
scheduler = APScheduler()
class Config:
SCHEDULER_API_ENABLED = True
scheduler.init_app(app)
def save_json(entry):
data = []
if os.path.exists('static/data/dataset.json'):
with open('static/data/dataset.json', encoding="utf-8") as f:
data = json.load(f)
new_entry = {}
for key in entry:
if key in ['instruction', 'output', 'id', 'reviewer']:
new_entry[key] = entry[key]
data.append(new_entry)
with open('static/data/dataset.json', 'w', encoding="utf-8") as f:
json.dump(data, f, ensure_ascii = False, indent=2)
def get_next_id():
with open('static/data/dataset.json', encoding="utf-8") as f:
data = json.load(f)
return max([-1]+[int(ex['id'].split('-')[-1]) for ex in data if 'cidar-v2' in ex['id']])
@app.route('/api/submit',methods = ['POST', 'GET'])
def submit():
if request.method == 'POST':
element = {k:request.form[k] for k in request.form}
if element['id'] == 'cidar-v2-empty':
element['id'] = f'cidar-v2-{get_next_id() + 1}'
save_json(element)
return render_template('index.html')
@app.route('/api/data')
def send_data():
return jsonify(random.choice(dataset))
@app.route('/api/getConNames')
def get_cont_names():
with open('static/data/dataset.json', encoding="utf-8") as f:
data = json.load(f)
return jsonify(Counter([elm['reviewer'].strip().split(' ')[0].strip() for elm in data]))
@app.route('/api/getCon', methods = ['POST', 'GET'])
def get_cont():
print(request.form)
name = request.form['reviewer']
with open('static/data/dataset.json', encoding="utf-8" ) as f:
data = json.load(f)
return jsonify({
"num_cont":len([elm for elm in data if elm['reviewer'] == name])
})
@app.route('/api/getById', methods = ['POST', 'GET'])
def get_by_id():
id = request.form['id']
with open('static/data/dataset.json', encoding="utf-8") as f:
data = json.load(f)
element = [ex for ex in data if ex['id'] == id]
print(element)
if len(element):
element = element[0]
else:
element = {
"instruction":'',
"output" :'',
"id":-1
}
return jsonify(element)
def get_max_idx():
with open('static/data/dataset.json', encoding="utf-8") as f:
data = json.load(f)
return max([int(ex['index']) for ex in data])
@app.route('/api/empty')
def send_empty_data():
element = {
"instruction":'',
"output" :'',
"id":'cidar-v2-empty'
}
return jsonify(element)
@app.route('/api/saved')
def send_saved_data():
element = {
"instruction":'',
"output" :'',
"id":-1
}
with open('static/data/dataset.json', encoding="utf-8") as f:
data = json.load(f)
if len(data):
saved_indices = list(range(len(data)))
index = random.choice(saved_indices)
element = data[index]
return jsonify(element)
@scheduler.task('interval', id='do_push_hf', hours=1)
def push_hub():
TOKEN = os.environ.get('HF_TOKEN')
subprocess.run(["huggingface-cli", "login", "--token", TOKEN])
with open('static/data/dataset.json', encoding="utf-8") as f:
data = json.load(f)
if len(data):
dataset = load_dataset("json", data_files="static/data/dataset.json", download_mode = "force_redownload")
dataset.push_to_hub('arbml/cidarv2')
def init_dataset():
os.makedirs('static/data', exist_ok=True)
saved_data = None
try:
data = []
for i, ex in enumerate(load_dataset('arbml/cidarv2')):
data.append(ex)
except:
data = []
with open('static/data/dataset.json', 'w', encoding="utf-8") as f:
json.dump(data, f, ensure_ascii = False, indent=2)
@app.route('/explore')
def explore():
return render_template('explore.html')
@app.route('/review')
def review():
return render_template('review.html')
@app.route('/')
def index():
return render_template('index.html')
init_dataset()
scheduler.start()
if __name__ == '__main__':
app.run(port=5000)