MicroSmart is an AI-powered microscope assistant delivered via a Telegram bot. This tool is designed to provide fast, preliminary analysis of microscopic samples.
Version 1.0 is the Blood Analyst. It can receive a photo of a blood smear and return a preliminary analysis, including cell counts (RBC, WBC, Platelets) and potential flags for abnormalities.
The system is built on a simple, scalable microservice architecture:
- User (Telegram): A user sends a photo to the MicroSmart Telegram Bot.
- Telegram Bot (
telegram_bot.py): This Python script (usingpython-telegram-bot) receives the image. It does not do any analysis. - Backend API (
api_server.py): The bot securely forwards the image to a FastAPI backend server. - AI Model (
model/best.pt): The API server loads a pre-trained YOLOv8 model, performs object detection on the image, and counts the detected cells. - JSON Report: The API server returns a
JSONobject with the counts and flags back to the bot. - Report (Telegram): The bot formats this JSON into a user-friendly Markdown report and sends it back to the user in the chat.
- AI Model: Python,
ultralytics(YOLOv8), Google Colab (for training). - Backend: Python,
FastAPI,uvicorn. - Bot Frontend: Python,
python-telegram-bot. - Image Processing:
opencv-python-headless. - Environment:
python-dotenvfor secure key management. - Deployment: Currently running in GitHub Codespaces.
Here's how to set up and run the MicroSmart v1.0 from scratch.
- A trained YOLOv8 model file (
best.pt). - A Telegram Bot Token from
@BotFather. - Python 3.10+
-
Clone the repository:
git clone [https://github.com/your-username/Microsmart.git](https://github.com/your-username/Microsmart.git) cd Microsmart -
Install System Dependencies: OpenCV requires an underlying system library.
sudo apt-get update && sudo apt-get install -y libgl1 -
Install Python Libraries:
pip install -r requirements.txt
-
Add Your Model: Place your trained
best.ptfile into the/modeldirectory. -
Create Environment File: Create a file named
.envin the root directory. Add your secret keys here:TELEGRAM_TOKEN=YOUR_BOT_TOKEN_FROM_@BOTFATHER API_URL=[http://127.0.0.1:8000/analyze/blood](http://127.0.0.1:8000/analyze/blood)
(Note: This
API_URLis for running both services on your local machine. See the Codespaces note below for cloud deployment.)
This system requires two terminals to run simultaneously.
-
Terminal 1: Start the API Server
python api_server.py
You should see
Uvicorn running on http://0.0.0.0:8000. -
Terminal 2: Start the Telegram Bot
python telegram_bot.py
You should see
Bot is polling for messages....
Now, you can go to your Telegram bot and send it an image to analyze.
This project is set up to run in GitHub Codespaces. The instructions are almost identical, with two key differences:
- Port Visibility: When you run
api_server.py, Codespaces will forward port 8000. You must go to the "PORTS" tab, right-click the8000port, and set "Port Visibility" toPublic. .envFile: TheAPI_URLin your.envfile must be the public Codespace URL provided in the "PORTS" tab. It will look like this:API_URL=[https://your-codespace-name-8000.preview.app.github.dev/analyze/blood](https://your-codespace-name-8000.preview.app.github.dev/analyze/blood)
- Phase 2: Stool Sample Analysis
- Train a new model to detect parasite eggs (Ascaris, Giardia, etc.).
- Add a new
/analyze/stoolendpoint to the API. - Add a
/stoolcommand to the bot.
- Phase 3: Urine Sample Analysis
- Train a new model for urine sediment (crystals, casts, cells).
- Add a new
/analyze/urineendpoint to the API. - Add a
/urinecommand to the bot.