Skip to content

About

Image classifier: a FastAPI backend serving a Vision Transformer and a Streamlit app that returns the top five ImageNet classes

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ViT Image Classifier

A minimal image-identification app: a FastAPI backend serving an open-source Vision Transformer (ViT) and a Streamlit frontend where users upload a picture and get back what is in it (top-5 ImageNet classes with confidence). Classification only, no chat.

Architecture

Streamlit (frontend/app.py)  --POST /predict-->  FastAPI (backend/main.py)  -->  ViT (Hugging Face)

Model

Default: google/vit-base-patch16-224, free and open-source (Apache-2.0), pre-trained on ImageNet-21k and fine-tuned on ImageNet-1k (1,000 classes).

Swap in any other Hugging Face image-classification model via an environment variable:

MODEL_NAME=google/vit-large-patch16-224 uvicorn backend.main:app

Setup

python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install -r requirements.txt

Run

Terminal 1 (backend; the model downloads on first start):

uvicorn backend.main:app --reload --port 8000

Terminal 2 (frontend):

streamlit run frontend/app.py

Open http://localhost:8501, upload an image, click Classify.

API docs: http://localhost:8000/docs

API

Method Endpoint Description
GET /health Status, model name, device
POST /predict Multipart file -> top-K predictions

Example:

curl -X POST -F "[email protected]" http://localhost:8000/predict

Configuration

Variable Default Used by
MODEL_NAME google/vit-base-patch16-224 backend
TOP_K 5 backend
API_URL http://localhost:8000 frontend

Project structure

vit-image-classifier/
├── backend/main.py
├── frontend/app.py
├── requirements.txt
├── README.md
└── .gitignore

Limitations

  • Predictions are limited to the 1,000 ImageNet classes.
  • CPU inference works but is slower than GPU.

About

Image classifier: a FastAPI backend serving a Vision Transformer and a Streamlit app that returns the top five ImageNet classes

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages