Using telegram bot for Image Classification
Tags: machinelearning, tensorflow
Published: 2020-11-16 11:37:27

## training the model:

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

<img src="https://i.imgflip.com/4mjmc9.jpg" style="zoom:33%;" />

Training example: [tensorflow](https://www.tensorflow.org/tutorials/images/classification) |  [pytorch](https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html)

> 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](https://github.com/omerferhatt/torch2tflite)

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](https://t.me/botfather)  <img src="https://core.telegram.org/file/811140763/1/PihKNbjT8UE/03b57814e13713da37" style="zoom: 25%;" />

2) create your bot and get token



I used [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) library for building the bot. Just used the simple echo-bot template example and changed according to my need.



```python
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()

```



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](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Where-to-host-Telegram-Bots)

<img src="https://www3.assets.heroku.com/assets/home/hero/focus-647c57d2effb7d2dfb5871161afab3cf097de6339c02e85d84ea14747800fcb0.png" style="zoom:50%;" />

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](https://www.tensorflow.org/lite/guide/python) for full info. 

Put these lines in Procfile according to your need:

```reStructuredText
web: python bot.py
```

Now my folder structure looks like this:

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



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



​                                

|                        classifier-bot                        |                                                              |
| :----------------------------------------------------------: | :----------------------------------------------------------: |
| <img src="https://live.staticflickr.com/65535/50607753803_a1da92fc79_o_d.gif" style="zoom:67%;" /> | ![](https://media.tenor.com/images/0421e70227a98b91bce6e146bf9edaae/tenor.gif) |