-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
390 lines (328 loc) · 14.2 KB
/
server.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import os
import json
import re
from urllib.request import urlopen
from urllib.parse import urlencode
from sqlalchemy import create_engine
from sqlalchemy import Integer, String, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from flask import Flask, send_from_directory, send_file, redirect, render_template, Response, request
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
import pandas as pd
debug_mode = os.name == 'nt'
config_file = "config_git_ignore.json" if os.path.exists("config_git_ignore.json") else "config.json"
with open(config_file, "r") as f:
config = json.load(f)
if debug_mode:
db_string = config["debug_connection_string"]
else: # production
db_string = config["prod_connection_string"]
db = create_engine(db_string)
chat2dataset = {}
chat2last_example = {}
Base = declarative_base()
Session = sessionmaker(bind=db)
class Dataset(Base):
__tablename__ = 'datasets'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String)
description = Column(String)
def __repr__(self):
return str({"id": self.id, "name": self.name, "description": self.description})
class Class(Base):
__tablename__ = 'classes'
id = Column(Integer, primary_key=True, autoincrement=True)
dataset = Column(Integer)
name = Column(String)
def __repr__(self):
return str({"id": self.id, "name": self.name, "dataset": self.dataset})
class Example(Base):
__tablename__ = 'examples'
id = Column(Integer, primary_key=True, autoincrement=True)
dataset = Column(Integer)
name = Column(String)
value = Column(String)
def __repr__(self):
return str({"id": self.id, "name": self.name, "value": self.value, "dataset": self.dataset})
class Annotation(Base):
__tablename__ = 'annotations'
id = Column(Integer, primary_key=True, autoincrement=True)
dataset = Column(Integer)
chat_id = Column(Integer)
example = Column(Integer)
class_id = Column(Integer)
def __repr__(self):
return str({"id": self.id, "chat_id": self.chat_id, "class_id": self.class_id, "example": self.example, "dataset": self.dataset})
Base.metadata.create_all(db)
app = Flask("annotbot")
# --------------- BOT ---------------------- #
def chunks(lst, n):
"""Yield successive n-sized chunks from lst"""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def expand_regex_classes(classes, txt):
"""Replaces regex class names, with their corresponding matches"""
new_classes = []
for cls in classes:
if not cls.name.startswith('/'):
new_classes.append(cls)
continue
pattern, match_index = cls.name[1:].rsplit('/', 1)
match_index = int(match_index)
rgx = re.compile(pattern)
matches = rgx.findall(txt)
if match_index<len(matches):
cls.name = matches[match_index]
new_classes.append(cls)
return new_classes
def send_annotation_request(chat_id):
"""Queries a datapoint, and sends annotation request via telegram"""
dataset = chat2dataset.get(chat_id)
if type(dataset)!=int:
raise SystemError(f"No dataset for chat_id={chat_id}")
data_point = db.execute(
f"""
select E.id, E.value from
(select * from examples where dataset={dataset}) E
left join
(Select * from annotations where chat_id={chat_id} and dataset={dataset}) A
on A.example=E.id
where A.class_id is NULL
order by random()
""").first()
if data_point is None:
bot.sendMessage(chat_id, config["done_message"])
return
data_point_index, data_point_value = data_point
session = Session()
classes = session.query(Class).filter(Class.dataset==dataset).all()
classes = expand_regex_classes(classes, data_point_value)
classes_keys = [InlineKeyboardButton(text=cls.name, callback_data=f"{dataset}:{data_point_index}:{cls.id}") for cls in classes]
control_keys = [InlineKeyboardButton(text=config["skip_text"], callback_data=config["skip_text"])]
if 0<len(classes)<4: # horizontally
keyboard = InlineKeyboardMarkup(inline_keyboard=[classes_keys, control_keys])
else: # vertically
keyboard = InlineKeyboardMarkup(inline_keyboard=list(chunks(classes_keys, 2)) + [control_keys])
# Cache the last example id that was sent to this use
chat2last_example[chat_id] = data_point_index
if data_point_value.startswith("http://") or data_point_value.startswith("https://"):
bot.sendPhoto(chat_id, data_point_value, reply_markup=keyboard)
else:
bot.sendMessage(chat_id, data_point_value, reply_markup=keyboard)
def telegram_chat_message(msg):
"""Callback for an incoming textual message"""
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id)
if content_type == 'text':
if debug_mode:
chat2dataset[chat_id] = 1
send_annotation_request(chat_id)
return
session = Session()
dataset = session.query(Dataset).filter(Dataset.name==msg['text'].strip().lower()).first()
if dataset is None:
available_datasets = "\n" + ",".join([d.name for d in session.query(Dataset).all()])
bot.sendMessage(chat_id, config["dataset_not_found_message"]+available_datasets)
notify_dev(f"{chat_id} was asked to select a dataset")
else:
chat2dataset[chat_id] = dataset.id
notify_dev(f"{chat_id} selected "+msg['text'].strip().lower())
send_annotation_request(chat_id)
def telegram_callback_query(msg):
"""Callback for button click"""
query_id, chat_id, query_data = telepot.glance(msg, flavor='callback_query')
print('Callback Query:', query_id, chat_id, query_data)
if query_data==config["skip_text"]: #Skip
send_annotation_request(chat_id)
return
dataset, example, cls = tuple(map(int, query_data.split(":", 2)))
new_annotation = Annotation(dataset=dataset, chat_id=chat_id, example=example, class_id=cls)
if debug_mode:
print(new_annotation)
else:
session = Session()
session.add(new_annotation)
session.commit()
bot.answerCallbackQuery(query_id, text=f"Got {cls}")
last_example = chat2last_example.get(chat_id, -1)
if example==last_example:
send_annotation_request(chat_id)
def telegram_outbound_text(token, chat_id, text):
"""Send a telegram message"""
url = ("https://api.telegram.org/bot"+token+"/sendMessage?" + urlencode({"text": text, "chat_id": chat_id}))
handler = urlopen(url)
return handler.read().decode('utf-8')
def notify_dev(text):
"""Send a telegram message to dev"""
return telegram_outbound_text(config["debug_token"], config["debug_chat_id"], text)
# ------------- Server --------------------#
@app.route('/remind/<dataset_name>')
def remind(dataset_name):
ret = []
session = Session()
dataset = session.query(Dataset).filter(Dataset.name == dataset_name).first()
if dataset is None:
return "Not found"
token = config["debug_token"] if debug_mode else config["prod_token"]
chats = [chat_id for chat_id, dataset_id in chat2dataset.items() if dataset_id == dataset.id]
for chat_id in chats:
text = f"I still have many questions about {dataset_name}, could you please help ?"
telegram_outbound_text(token, chat_id, text)
ret.append(f"Reminded {chat_id} about {dataset_name}")
if any(chats):
notify_dev("\n".join(ret))
return '<br />'.join(ret)
@app.route('/data/<dataset_name>')
def get_data_file(dataset_name):
"""Queries the DB and returns the dataset as csv"""
session = Session()
dataset = session.query(Dataset).filter(Dataset.name==dataset_name).first()
if dataset is None:
available_datasets = "<br />" + "<br />".join([d.name for d in session.query(Dataset).all()])
return config["dataset_not_found_message"]+available_datasets
data = session.query(Example).filter(Example.dataset==dataset.id).all()
csv = pd.DataFrame([(d.name, d.value) for d in data], columns=["key", "value"])\
.to_csv()
return Response(
csv,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=data.csv"})
@app.route('/classes/<dataset_name>')
def get_classes_file(dataset_name):
"""Queries the DB and returns the classes as csv"""
session = Session()
dataset = session.query(Dataset).filter(Dataset.name==dataset_name).first()
if dataset is None:
available_datasets = "<br />" + "<br />".join([d.name for d in session.query(Dataset).all()])
return config["dataset_not_found_message"]+available_datasets
data = session.query(Class).filter(Class.dataset==dataset.id).all()
csv = '\n'.join([d.name for d in data])
return Response(
csv,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=classes.csv"})
@app.route('/annotated/<dataset_name>')
def annotated(dataset_name):
"""Queries the DB and returns the annotated dataset as csv"""
session = Session()
dataset = session.query(Dataset).filter(Dataset.name==dataset_name).first()
if dataset is None:
available_datasets = "<br />" + "<br />".join([d.name for d in session.query(Dataset).all()])
return config["dataset_not_found_message"]+available_datasets
data = db.execute(
f"""
select E.name as example,C.name as cls,COUNT(DISTINCT A.chat_id) as cnt
from examples E inner join annotations A on E.dataset=A.dataset and A.example=E.id inner join classes C on C.dataset=A.dataset and C.id=A.class_id
where A.dataset={dataset.id}
GROUP BY 1,2
""")
csv = pd.DataFrame(list(data), columns=["example", "cls", "cnt"])\
.pivot_table(values="cnt", aggfunc=sum, index="example", columns="cls", fill_value=0)\
.to_csv()
return Response(
csv,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=annotated.csv"})
@app.route('/bot_exists/<dataset_name>')
def bot_exists(dataset_name):
"""Endpoint to check if a bot name exists"""
session = Session()
dataset = session.query(Dataset).filter(Dataset.name==dataset_name).first()
return "0" if dataset is None else "1"
@app.route('/favicon.ico')
def serve_favicon():
return send_file('static/favicon.ico')
@app.route('/static/<path:path>')
def serve_static(path):
return send_from_directory('static', path)
@app.route('/')
@app.route('/home')
def home():
return render_template("home.html",
pages=["Home","New Bot","View Annotations"],
active_page="Home")
@app.route('/new_bot')
def new_dataset():
return render_template("new_bot.html",
pages=["Home","New Bot","View Annotations"],
active_page="New Bot",
bot_url=config["bot_url"])
@app.route('/view_annotations')
def view_datasets():
session = Session()
return render_template("view_annotations.html",
pages=["Home","New Bot","View Annotations"],
active_page="View Annotations",
datasets=session.query(Dataset).all())
def parse_inputs(form: dict):
"""Transforms form data to python objects"""
bot_name = form.get("txt_botname", "").lower().strip()
bot_desc = form.get("txt_desc", "").strip()
try:
classes = json.loads(form.get("txt_classes", ""))
assert type(classes) == list
except (AssertionError, json.JSONDecodeError):
classes = [c.strip() for c in form.get("txt_classes", "").split('\n') if any(c.strip())]
# Regex classes
for i in range(len(classes)-1,-1,-1):
cls = classes[i]
if cls.startswith('/') and cls.endswith('/'):
try:
rgx = re.compile(cls[1:-1])
assert rgx.groups < 2
except re.error:
print (f"invalid regex {cls}")
continue
except AssertionError:
print("Regex Groups are not supported")
continue
del classes[i]
for j in range(config["regex_class_limit"]):
classes.append("/{p}/{n}".format(n=j,p=rgx.pattern))
try:
data = json.loads(form.get("txt_data", ""))
assert type(data) == dict
except (AssertionError, json.JSONDecodeError):
data = {}
for datum in form.get("txt_data", "").split('\n'):
if datum.find(',')<0:
continue
key, val = datum.split(',', 1)
data[key] = val.replace("\\n", "\n")
assert 2 < len(bot_name) <= 50
assert 5 < len(bot_desc)
assert any(data) and any(classes)
return (bot_name, bot_desc, classes, data)
@app.route('/submit_dataset', methods=['GET', 'POST'])
def submit_dataset():
"""Creates a new dataset for the bot"""
bot_name, bot_desc, classes, data = parse_inputs(request.form)
session = Session()
assert session.query(Dataset).filter(Dataset.name == bot_name).first() is None
session.add(Dataset(name=bot_name, description=bot_desc))
session.commit()
dataset_id = session.query(Dataset).filter(Dataset.name==bot_name).first().id
session.add_all([Class(dataset=dataset_id, name=cls) for cls in classes])
session.add_all([Example(dataset=dataset_id, name=key, value=val) for key, val in data.items()])
session.commit()
notify_dev("New bot created: " + bot_name)
return render_template("view_annotations.html",
pages=["Home","New Bot","View Annotations"],
active_page="View Annotations",
datasets=session.query(Dataset).all())
if __name__ == "__main__":
if debug_mode:
bot = telepot.Bot(config["debug_token"])
else:
bot = telepot.Bot(config["prod_token"])
MessageLoop(bot, {
'chat': telegram_chat_message,
'callback_query': telegram_callback_query
}).run_as_thread()
app.run(port=80, host='0.0.0.0')