DEV Community

ashish
ashish

Posted on

Using telegram bot for Image Classification

training the model:

Pick any of your comfortable framework PyTorch / Tensorflow. And then train your image classifier /or/ use any trained model.

Training example: tensorflow | pytorch

I'm using tflite model as it serves best for edge and low computing devices.

model conversion:

If you want to convert from pytorch to tflite you can go through this code from omerferhatt

It converts from torch to ONNX and then TF2 >>> tflite

Torch->ONNX->TF2->TFLite

Put the Inference code for this model in a '.py' file and place it along with model file and labels.txt (for tensorflow).

telegram bot:

easy steps:

1) connect with bot father

2) create your bot and get token

I used python-telegram-bot library for building the bot. Just used the simple echo-bot template example and changed according to my need.

import logging
import os

from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
from model import get_predictions  # calling model func

# Enable logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)

logger = logging.getLogger(__name__)


# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    update.message.reply_text('Hi send an image to classify!')


def help_command(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /help is issued."""
    update.message.reply_text('Help!')


def photo(update: Update, context: CallbackContext) -> int:
    user = update.message.from_user
    photo_file = update.message.photo[-1].get_file()
    photo_file.download('user_photo.jpg')
    logger.info("Photo of %s: %s", user.first_name, 'user_photo.jpg')
    update.message.reply_text(
        'Okay now wait a few seconds!!!'
    )
    update.message.reply_text(get_prediction('user_photo.jpg'))


def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    TOKEN = " " # place your token here
    updater = Updater(TOKEN, use_context=True)
    PORT = int(os.environ.get('PORT', '8443'))

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # on different commands - answer in Telegram
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))

    # on noncommand i.e message - echo the message on Telegram
    dispatcher.add_handler(MessageHandler(Filters.photo & ~Filters.command, photo))

    updater.start_webhook(listen="0.0.0.0",
                      port=PORT,
                      url_path=TOKEN)
    updater.bot.set_webhook("https://yourapp.herokuapp.com/" + TOKEN)
    updater.idle()

if __name__ == '__main__':
    main()

Enter fullscreen mode Exit fullscreen mode

In above code, update.message.reply_text(get_prediction('user_photo.jpg')) I'm passing the result predictions to user by calling the model, you can replace it according to your inferencing model code.

Final touch

I choose Heroku platform for deploying my bot. For more hosting ideas visit this link

Before moving forward, you need to create requirements file and Procfile.

For tflite inference you don't need whole tensorflow library, just install the tflite interpretor. See this guide for full info.

Put these lines in Procfile according to your need:

web: python bot.py
Enter fullscreen mode Exit fullscreen mode

Now my folder structure looks like this:

└── img_classify_bot
    ├── model.py
    └── bot.py
    └── model.tflite
    └── labels.txt
    └── bot.py
    └── requirements.txt
    └── Procfile

Enter fullscreen mode Exit fullscreen mode

finally, use heroku-cli for deploying the bot.(Your bot Token is sensitive info, don't reveal it publicly)

classifier-bot

Top comments (1)

Collapse
 
pometwoacc profile image
PomeTwoAcc • Edited

Hi! Thank you for your tutorial, it helps me a lot to finally deploy my Telegram bot!
Could you please provide an example of what the model.py has to be?
I'm facing some problems reading models from the folder..
Thanks!