How ajitsingh98/cattle-breed-classifier-webapp works
Purpose and Entry Points
This repository delivers image classification for 26 indigenous Indian cattle and buffalo breeds, with an ML training/evaluation pipeline and a containerized React + FastAPI deployment. Runtime API entry point: backend/app/main.py; browser entry point: frontend/src/main.jsx; hosted deployment is built from root Dockerfile.
Client Application
The React/Vite SPA provides prediction, breed exploration, and informational views. frontend/src/App.jsx routes/views compose frontend/src/pages/PredictPage.jsx and frontend/src/pages/BreedExplorerPage.jsx. The API boundary is centralized in frontend/src/services/api.js; local prediction history is managed by frontend/src/hooks/usePredictionHistory.js.
Backend API and Serving Boundary
FastAPI owns health, inference, and metadata endpoints through backend/app/main.py and route modules: backend/app/api/routes/predict.py, backend/app/api/routes/metadata.py, and backend/app/api/routes/health.py. In unified deployment it also serves the compiled frontend, making FastAPI the public application boundary rather than requiring a separate web server.
Inference and Domain Metadata
Prediction requests cross from uploaded image handling into model execution via backend/app/services/image_loader.py and backend/app/services/inference.py. Typed request/response contracts reside in backend/app/schemas/predict.py and backend/app/schemas/breed.py. Breed descriptions and associated metadata come from backend/app/services/breed_info.py, backed by data/breed_metadata.csv. Model selection/path are environment-configured in backend/app/core/config.py (notably MODEL_PATH and MODEL_NAME).
ML Data and Model Lifecycle
The source image corpus is organized by breed in Cattle_Resized/; preparation creates train/validation/test manifests through ml/src/data/prepare_dataset.py, with transforms in ml/src/data/transforms.py. Training supports MLP, custom CNN, ResNet transfer learning, and ViT transfer learning, defined in ml/src/models/ and orchestrated by ml/src/training/trainer.py / ml/src/training/engine.py. Configuration is model-specific under ml/configs/.
Evaluation and Deployment Model Choice
Evaluation computes metrics and compares architectures through ml/src/evaluation/metrics.py and ml/src/evaluation/compare_models.py; durable reports/manifests are under ml/artifacts/. Runtime preprocessing/prediction logic is independently available in ml/src/inference/preprocess.py and ml/src/inference/predictor.py. The deployment expects an externally available .pth model artifact; the README identifies ViT or ResNet as deployment candidates.
Runtime and Infrastructure
Python 3.11, PyTorch 2.x, and FastAPI implement backend/model serving; Node 20+, React 18, and Vite build the client. Docker is the primary packaging mechanism: root Dockerfile builds the unified application, while backend/Dockerfile, frontend/Dockerfile, and docker-compose.yml support split/local arrangements. Procfile supports process-platform startup. Hugging Face Spaces is the stated production hosting target, with deploy.py acting as the deployment automation integration point.
Main entry points: main.py FastAPI application · predict.py Prediction endpoint · inference.py Serving inference · trainer.py Training orchestration
Components 20
React Client
- React/Vite SPA · Prediction, exploration, and informational views.
frontend/src/App.jsx - API client
frontend/src/services/api.js - Prediction history · Stores local prediction history.
frontend/src/hooks/usePredictionHistory.js
FastAPI Serving
- FastAPI application · Hosts API endpoints and the compiled SPA in unified deployment.
backend/app/main.py - Prediction endpoint
backend/app/api/routes/predict.py - Metadata endpoint
backend/app/api/routes/metadata.py - Prediction & breed contracts
backend/app/schemas/predict.py - Image loader
backend/app/services/image_loader.py - Serving inference
backend/app/services/inference.py - Breed information
backend/app/services/breed_info.py - Breed metadata
data/breed_metadata.csv - Runtime model config · Selects MODEL_PATH and MODEL_NAME.
backend/app/core/config.py
ML Lifecycle
- Breed image corpus · Images are organized by breed.
- Dataset preparation · Creates train, validation, and test manifests.
ml/src/data/prepare_dataset.py - Training orchestration · Trains MLP, custom CNN, ResNet, and ViT variants.
ml/src/training/trainer.py - Model architectures
- Model evaluation · Computes metrics and compares architectures.
ml/src/evaluation/compare_models.py - ML artifacts · Provides durable evaluation outputs and deployable model artifacts.
Packaging & Hosting
- Unified Docker build · Builds the hosted React and FastAPI application.
Dockerfile - Hugging Face Spaces
deploy.py
Connections 19
- React/Vite SPA → API client: calls
- React/Vite SPA → Prediction history: records results
- API client → FastAPI application: HTTP API
- FastAPI application → Prediction endpoint: routes
- FastAPI application → Metadata endpoint: routes
- Prediction endpoint → Prediction & breed contracts: validates
- Prediction endpoint → Image loader: loads upload
- Image loader → Serving inference: prepared image
- Serving inference → Runtime model config: selects model
- Metadata endpoint → Breed information: queries
- Breed information → Breed metadata: reads
- Breed image corpus → Dataset preparation: source images
- Dataset preparation → Training orchestration: dataset manifests
- Model architectures → Training orchestration: architectures
- Training orchestration → Model evaluation: trained models
- Model evaluation → ML artifacts: reports and candidates
- ML artifacts → Serving inference: selected .pth artifact
- Unified Docker build → FastAPI application: packages backend and SPA
- Unified Docker build → Hugging Face Spaces: deploys