-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_callback.py
35 lines (28 loc) · 1.24 KB
/
telegram_callback.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
from keras.callbacks import Callback
import requests
class TelegramCallback(Callback):
def __init__(self, username):
super(TelegramCallback, self).__init__()
self.token = 'your bot token'
res = requests.get("https://api.telegram.org/bot{}/getUpdates".format(self.token))
jsn = res.json()['result']
for updt in jsn:
if updt['message']['from']['username'] == username:
self.chat_id = updt['message']['chat']['id']
def send_info(self, text):
requests.post("https://api.telegram.org/bot{}/sendMessage".format(self.token),
data={'chat_id': self.chat_id, 'text': text})
def on_train_begin(self, logs=None):
if requests is None:
raise ImportError('Notification requires the requests library.')
text = 'Training successfully started.'
self.send_info(text)
def on_train_end(self, logs=None):
text = 'Training successfully ended.'
self.send_info(text)
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
text = 'Epoch {}: '.format(epoch + 1)
for metric in self.params['metrics']:
text += '{}: {:.3f} '.format(metric, logs[metric])
self.send_info(text)