cover

KnockKnock: Automate Your Machine Learning Notifications with Ease

Introduction

Training machine learning models can take hours or even days, requiring significant computational resources. For many practitioners, this means constantly checking logs or monitoring progress to ensure scripts are running smoothly. But what happens when an error occurs hours into training, or when you need to know when a job has finished? Constant supervision is inefficient and distracting.

KnockKnock, a Python library by Hugging Face, solves this problem. By integrating with popular notification platforms like Desktop Notifications, Telegram, Email, and Slack, KnockKnock automatically informs you when your training starts, completes, or fails. This simple yet powerful tool streamlines your workflow, freeing you to focus on other tasks.


The Problem and How KnockKnock Solves It

The Challenge

Training machine learning models is resource-intensive and time-consuming. Practitioners often leave scripts running for hours or overnight, only to discover that:

  • The script failed midway due to an error.
  • Training completed hours ago, but valuable time was wasted waiting unnecessarily.
  • Teams were not updated about training progress in real-time, leading to delays.

Manually monitoring scripts is a poor use of time, and it detracts from other critical activities.

The Solution

KnockKnock simplifies the monitoring process by sending notifications directly to your preferred platform when:

  • Training Starts: Know that your script is running successfully.
  • Training Ends: Receive updates the moment your model completes training.
  • An Error Occurs: Be alerted instantly to address issues promptly.

With just a few lines of code, KnockKnock ensures you stay informed wherever you are, saving you time and effort.


Installing KnockKnock

Getting started with KnockKnock is easy. Install it using pip:

pip install knockknock

Ensure youā€™re using Python version 3.6 or higher for compatibility.


Setting Up Notifications

KnockKnock supports a range of platforms to suit different needs. Hereā€™s how to set up notifications for Desktop Notifications, Telegram, Email, and Slack.

1. Desktop Notifications

For simple on-screen updates, Desktop Notifications are an excellent choice. These are quick to set up and require no additional configuration.

Example Code:

from knockknock import desktop_sender

@desktop_sender()
def train_model():
    print("Training started...")
    import time
    time.sleep(10)  # Simulate a long-running task
    print("Training complete!")

When the script runs, notifications will appear on your desktop for both the start and end of the training process.


2. Telegram Notifications

Telegram is perfect for receiving real-time updates on your phone or desktop.

Setup Instructions:

  1. Create a bot on Telegram using BotFather.
  2. Copy the API token provided by BotFather.
  3. Retrieve your chat ID.

Example Code:

from knockknock import telegram_sender

bot_token = "your_bot_token_here"
chat_id = "your_chat_id_here"

@telegram_sender(token=bot_token, chat_id=chat_id)
def train_model():
    print("Training started...")
    import time
    time.sleep(10)  # Simulate a long-running task
    print("Training complete!")

Once configured, youā€™ll receive a Telegram message whenever training starts, finishes, or fails.


3. Email Notifications

Stay updated through your inbox with email notifications.

Setup Instructions:

  1. Install the yagmail library:
    pip install yagmail
  2. Use your Gmail account to send notifications (itā€™s recommended to create a dedicated account for this purpose).

Example Code:

from knockknock import email_sender

@email_sender(
    recipient_emails=["[email protected]"], 
    sender_email="[email protected]"
)
def train_model():
    print("Training started...")
    import time
    time.sleep(10)  # Simulate a long-running task
    print("Training complete!")

Youā€™ll receive an email when your training starts and finishes, making it a reliable option for those who rely heavily on email updates.


4. Slack Notifications

For team collaborations, Slack notifications keep everyone informed in real-time.

Setup Instructions:

  1. Create an Incoming Webhook URL for your Slack workspace.
  2. Add the webhook URL to your code.

Example Code:

from knockknock import slack_sender

webhook_url = "your_slack_webhook_url_here"

@slack_sender(webhook_url=webhook_url, channel="#your_channel")
def train_model():
    print("Training started...")
    import time
    time.sleep(10)  # Simulate a long-running task
    print("Training complete!")

Your Slack channel will receive updates whenever training begins, ends, or fails.


Benefits of Using KnockKnock

  1. Save Time: Eliminate the need to manually monitor training scripts, freeing you to focus on other tasks.
  2. Real-Time Alerts: Get instant updates, even if youā€™re away from your workstation.
  3. Team Collaboration: Keep your team in the loop with shared notifications via Slack or Telegram.
  4. Ease of Integration: Add KnockKnock to your workflow with just a few lines of code.
  5. Customizable Platforms: Choose from multiple notification methods to suit your personal or team preferences.

Conclusion

KnockKnock is a game-changer for machine learning practitioners and teams. By automating training notifications, it eliminates the inefficiencies of manual monitoring, ensuring youā€™re always updated no matter where you are. Whether you prefer desktop notifications, Telegram updates, email alerts, or Slack messages, KnockKnock integrates seamlessly into your workflow.

Ready to make your workflow more efficient? Check out KnockKnock on GitHub and start automating your notifications today!


References

Related articles:

    background

    05 December 2022

    avatar

    Francesco Di Salvo

    45 min

    30 Days of Machine Learning Engineering

    30 Days of Machine Learning Engineering

    background

    16 January 2023

    avatar

    Daniele Moltisanti

    6 min

    Advanced Data Normalization Techniques for Financial Data Analysis

    In the financial industry, data normalization is an essential step in ensuring accurate and meaningful analysis of financial data.

    background

    17 January 2023

    avatar

    Francesco Di Salvo

    10 min

    AI for breast cancer diagnosis

    Analysis of AI applications for fighting breast cancer.

    background

    18 November 2024

    avatar

    Daniele Moltisanti

    12 min

    Meet Lara: The AI Translator Revolutionizing Global Communication

    Lara is the cutting-edge AI-powered translator designed to rival professional human translations with contextual accuracy and style flexibility. Learn more!

    background

    14 November 2022

    avatar

    Francesco Di Gangi

    5 min

    Artificial Intelligence in videogames

    Artificial Intelligence is a giant world where we can find everything. Also videogames when we don't even notice...

JoinUS