├── .gitignore ├── README.md ├── callbacks ├── __init__.py └── telegram_callback.py └── requerments.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Keras callback in Telegram 2 | Telegram-bot callback for your Keras model 3 | 4 | This module allows your model to send learning history to your chat in Telegram messenger. 5 | 6 | ### Configure your Telegram Callback 7 | To start use this callback you need fo register bot in telegram and get your `telegram_id`. Follow instructions below. 8 | 9 | #### Step 1. Register your telegram bot. 10 | - Find `BotFather` in Telegram. 11 | - Follow `BotFather` instructions to register your bot in a few steps and get `token`. 12 | 13 | #### Step 2. Get your ID. 14 | - Find `@my_id_bot` bot in Telegram and get your `telegram_id`. If bot is not working get `telegram_id` manually (step 2a). 15 | 16 | #### Step 2a. Get your ID (optional). 17 | - Find your bot in telegram and send message 'hello!' 18 | - Paste in your browser `api.telegram.org/bot/getUpdates` (use token you get in previous step), if you did everything correctly you will recive a JSON where you can find your `telegram_id`. 19 | 20 | ### Example 21 | 22 | ```python 23 | from .callbacks import TelegramCallback 24 | 25 | # load data, define and compile model 26 | ... 27 | 28 | # create callback 29 | config = { 30 | 'token': '556983321:AAHO-bSWaIqcvHL91Xw12X18OWczFIpY1s0', # paste your bot token 31 | 'telegram_id': 123456789, # paste your telegram_id 32 | } 33 | 34 | tg_callback = TelegramCallback(config) 35 | 36 | # start training 37 | model.fit(x, y, batch_size=32, callbacks=[tg_callback]) 38 | ``` 39 | 40 | Congratulations! Now you will recive logs (losses, metrics, lr, etc.) in Telegram! 41 | -------------------------------------------------------------------------------- /callbacks/__init__.py: -------------------------------------------------------------------------------- 1 | from .telegram_callback import TelegramCallback -------------------------------------------------------------------------------- /callbacks/telegram_callback.py: -------------------------------------------------------------------------------- 1 | import telegram 2 | from keras.callbacks import Callback 3 | 4 | 5 | class TelegramCallback(Callback): 6 | 7 | def __init__(self, config, name=None): 8 | super(TelegramCallback, self).__init__() 9 | self.user_id = config['telegram_id'] 10 | self.bot = telegram.Bot(config['token']) 11 | if name != None: 12 | self.name = name 13 | else: 14 | self.name = self.model.name 15 | 16 | def send_message(self, text): 17 | try: 18 | self.bot.send_message(chat_id=self.user_id, text=text) 19 | except Exception as e: 20 | print('Message did not send. Error: {}.'.format(e)) 21 | 22 | def on_train_begin(self, logs={}): 23 | text = 'Start training model {}.'.format(self.name) 24 | self.send_message(text) 25 | 26 | def on_train_end(self, logs={}): 27 | text = 'Training model {} ended.'.format(self.name) 28 | self.send_message(text) 29 | 30 | def on_epoch_end(self, epoch, logs={}): 31 | text = '{}: Epoch {}.\n'.format(self.name, epoch) 32 | for k, v in logs.items(): 33 | if k != "lr": 34 | text += '{}: {:.4f}; '.format(k, v) 35 | else: 36 | text += '{}: {:.6f}; '.format(k, v) #4 decimal places too short for learning rate 37 | self.send_message(text) 38 | -------------------------------------------------------------------------------- /requerments.txt: -------------------------------------------------------------------------------- 1 | python-telegram-bot==10.0.2 2 | keras==2.1.4 3 | --------------------------------------------------------------------------------