Share your V2 projects here

Hi everyone, I don’t know whether this belongs here, but sharing it anyway.
I made a NotifierCallback that notifies you through almost any Instant Messaging(Telegram/Slack/ and more…), SMS, Email, Push Notification service at each Epoch’s end.
Callback Code:

class NotifierCallback(Callback):
    "Notifies you the losses and the metrics"
    def __init__(self, service_addrs):
        self.service_addrs = L(service_addrs)
        try:
            import apprise
        except:
            raise ModuleNotFoundError("Apprise module not found, Install it to use this Callback.")
        self.apobj = apprise.Apprise()
        for addrs in self.service_addrs:
            self.apobj.add(addrs)

    def begin_fit(self):
        "Replace default logger with _notify"
        self.old_logger,self.learn.logger = self.logger,self._notify

    def _notify(self, log):
        "Notifies all services and call the old logger."
        msg_body = ""
        for data in zip(self.recorder.metric_names,log):
          msg_body += f"{data[0]}: {str(data[1])}\n"
        self.apobj.notify(title="",body=msg_body)      
        self.old_logger(log)

    def after_fit(self):
        "Restore old logger"
        self.learn.logger = self.old_logger

This is made possible by the awesome Apprise library.

Example:

telegram_addrs = f'"tgram://{bot_token}/{chat_id}" #Telegram Notification
windows = "windows://"  #Windows Desktop Notification
service_addrs=[telegram_addrs,windows]
learn.fit_one_cycle(25, max_lr=1e-3,callbacks=[NotifierCallback(services_addrs)])

There is a huge list of services supported by Apprise which you can view at Apprise Github Page

This is useful if you’re training a model that takes a long time to train and you need to do other work instead of checking often how your model is performing.

23 Likes