<jupyter_start><jupyter_text>https://forums.fast.ai/t/training-metrics-as-notifications-on-mobile-using-callbacks/17330/4?u=hwasitiDerived from <NAME>'s basic telegram bot usage script.Utilizes this API Library:   https://github.com/python-telegram-bot/python-telegram-botTo install:   pip install python-telegram-bot --upgradeTo generate an Access Token, you have to talk to BotFather: https://t.me/botfatherand follow a few simple steps (described here: https://core.telegram.org/bots6-botfather ).For a simple example: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Introduction-to-the-API'TOKEN' should be replaced by the API token you received from @BotFather:In ~/.telegram on the machine running the job, put{"api_key": "442766545:", "chat_id": ""}For me it was:    {"api_key": "<KEY>", "chat_id": "-318454638"}        https://stackoverflow.com/a/50661601/1970830    Here's how you get an API key:   https://core.telegram.org/api/obtaining_api_idHere's how you get your chat ID: (See my Diigo highlights)   https://stackoverflow.com/a/50661601/1970830https://my.telegram.org/apps<jupyter_code>import telegram
import json
import os

def notify_me(message="Job's done!"):
    filename = os.environ['HOME'] + '/.telegram'
    with open(filename) as f:
        json_blob = f.read()
        credentials = json.loads(json_blob)

    # Initialize bot
    bot = telegram.Bot(token=credentials['api_key'])

    # Send message
    bot.send_message(chat_id=credentials['chat_id'], text=message)

learn = create_cnn(data, models.resnet34, metrics=error_rate)
notif_cb = NotificationCallback()
learn.fit_one_cycle(4, callbacks=[notif_cb])<jupyter_output><empty_output><jupyter_text>Here is an example that can be used with fastai to send Telegram messages with every epoch results<jupyter_code>@dataclass
class NotificationCallback(Callback):
        
    def on_train_begin(self, metrics_names: StrList, **kwargs: Any) -> None:
        notify_me("Epoch: train_loss , valid_loss , error_rate")

    def on_epoch_end(self,  epoch: int, smooth_loss: Tensor, last_metrics: MetricsList, **kwargs: Any) -> bool:
        super().on_epoch_end(**kwargs)
        val_loss, accuracy = last_metrics[0], last_metrics[1]
        message = str(epoch) + ": " + f"{smooth_loss.item():.4f}" + " , " + f"{val_loss:.4f}" + " , " + f"{accuracy:.4f}"
        notify_me(message)
        return False # Haider: if return true it will stop training at this point<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1306978620411456, 'fraction_numerical': 0.025413473174667203, 'mean_word_length': 4.535714285714286, 'pattern_counts': {'":': 5, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '388412', 'license': 'Apache-2.0', 'n_tokens_mistral': 850, 'n_tokens_neox': 808, 'n_words': 190, 'path': 'nbs/dl1/mobile_notifications.ipynb', 'repo_name': 'hwasiti/fastai-course-v3', 'size': '2523'}