DEV Community


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


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
    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."""

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

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.command, photo))

                      url_path=TOKEN)"" + TOKEN)

if __name__ == '__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
Enter fullscreen mode Exit fullscreen mode

Now my folder structure looks like this:

└── img_classify_bot
    └── model.tflite
    └── labels.txt
    └── 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)


Top comments (1)

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 has to be?
I'm facing some problems reading models from the folder..