# ajitsingh98/cattle-breed-classifier-webapp architecture

> Architecture diagram of the GitHub repository ajitsingh98/cattle-breed-classifier-webapp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ajitsingh98/cattle-breed-classifier-webapp
- Repository: https://github.com/ajitsingh98/cattle-breed-classifier-webapp

## Overview

#### 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: [`backend/app/main.py`](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/main.py) (FastAPI application), [`backend/app/api/routes/predict.py`](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/api/routes/predict.py) (Prediction endpoint), [`backend/app/services/inference.py`](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/services/inference.py) (Serving inference), [`ml/src/training/trainer.py`](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/ml/src/training/trainer.py) (Training orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React Client"]
  node_spa["React/Vite SPA<br/>client application<br/>[App.jsx]"]
  node_client_api["API client<br/>frontend service<br/>[api.js]"]
  node_history["Prediction history<br/>client state hook"]
end

subgraph group_api["FastAPI Serving"]
  node_fastapi["FastAPI application<br/>public application boundary<br/>[main.py]"]
  node_predict_route["Prediction endpoint<br/>API route<br/>[predict.py]"]
  node_metadata_route["Metadata endpoint<br/>API route<br/>[metadata.py]"]
  node_contracts["Prediction &amp; breed contracts<br/>typed schemas<br/>[predict.py]"]
  node_image_loader["Image loader<br/>inference input service<br/>[image_loader.py]"]
  node_serving_inference["Serving inference<br/>model execution service<br/>[inference.py]"]
  node_breed_info["Breed information<br/>metadata service<br/>[breed_info.py]"]
  node_breed_metadata[("Breed metadata<br/>CSV dataset<br/>[breed_metadata.csv]")]
  node_model_config["Runtime model config<br/>environment configuration<br/>[config.py]"]
end

subgraph group_ml["ML Lifecycle"]
  node_corpus[("Breed image corpus<br/>labeled image dataset")]
  node_dataset_prep["Dataset preparation<br/>manifest pipeline<br/>[prepare_dataset.py]"]
  node_training["Training orchestration<br/>training pipeline<br/>[trainer.py]"]
  node_model_defs["Model architectures<br/>PyTorch model definitions"]
  node_evaluation["Model evaluation<br/>comparison pipeline<br/>[compare_models.py]"]
  node_artifacts[("ML artifacts<br/>reports and manifests")]
end

subgraph group_deploy["Packaging &amp; Hosting"]
  node_docker{{"Unified Docker build<br/>container packaging"}}
  node_hosting["Hugging Face Spaces<br/>production hosting target<br/>[deploy.py]"]
end

node_spa -->|"calls"| node_client_api
node_spa -->|"records results"| node_history
node_client_api -->|"HTTP API"| node_fastapi
node_fastapi -->|"routes"| node_predict_route
node_fastapi -->|"routes"| node_metadata_route
node_predict_route -->|"validates"| node_contracts
node_predict_route -->|"loads upload"| node_image_loader
node_image_loader -->|"prepared image"| node_serving_inference
node_serving_inference -->|"selects model"| node_model_config
node_metadata_route -->|"queries"| node_breed_info
node_breed_info -->|"reads"| node_breed_metadata
node_corpus -->|"source images"| node_dataset_prep
node_dataset_prep -->|"dataset manifests"| node_training
node_model_defs -->|"architectures"| node_training
node_training -->|"trained models"| node_evaluation
node_evaluation -->|"reports and candidates"| node_artifacts
node_artifacts -.->|"selected .pth artifact"| node_serving_inference
node_docker -->|"packages backend and SPA"| node_fastapi
node_docker -->|"deploys"| node_hosting

click node_spa "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/frontend/src/App.jsx"
click node_client_api "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/frontend/src/services/api.js"
click node_history "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/frontend/src/hooks/usePredictionHistory.js"
click node_fastapi "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/main.py"
click node_predict_route "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/api/routes/predict.py"
click node_metadata_route "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/api/routes/metadata.py"
click node_contracts "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/schemas/predict.py"
click node_image_loader "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/services/image_loader.py"
click node_serving_inference "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/services/inference.py"
click node_breed_info "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/services/breed_info.py"
click node_breed_metadata "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/data/breed_metadata.csv"
click node_model_config "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/core/config.py"
click node_dataset_prep "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/ml/src/data/prepare_dataset.py"
click node_training "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/ml/src/training/trainer.py"
click node_evaluation "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/ml/src/evaluation/compare_models.py"
click node_docker "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/Dockerfile"
click node_hosting "https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/deploy.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spa,node_client_api,node_history toneBlue
class node_fastapi,node_predict_route,node_metadata_route,node_contracts,node_image_loader,node_serving_inference,node_breed_info,node_breed_metadata,node_model_config toneAmber
class node_corpus,node_dataset_prep,node_training,node_model_defs,node_evaluation,node_artifacts toneMint
class node_docker,node_hosting toneRose
```

## Components

### React Client

- [React/Vite SPA](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/frontend/src/App.jsx): client application · Prediction, exploration, and informational views. · `frontend/src/App.jsx`
- [API client](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/frontend/src/services/api.js): frontend service · `frontend/src/services/api.js`
- [Prediction history](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/frontend/src/hooks/usePredictionHistory.js): client state hook · Stores local prediction history. · `frontend/src/hooks/usePredictionHistory.js`

### FastAPI Serving

- [FastAPI application](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/main.py): public application boundary · Hosts API endpoints and the compiled SPA in unified deployment. · `backend/app/main.py`
- [Prediction endpoint](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/api/routes/predict.py): API route · `backend/app/api/routes/predict.py`
- [Metadata endpoint](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/api/routes/metadata.py): API route · `backend/app/api/routes/metadata.py`
- [Prediction & breed contracts](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/schemas/predict.py): typed schemas · `backend/app/schemas/predict.py`
- [Image loader](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/services/image_loader.py): inference input service · `backend/app/services/image_loader.py`
- [Serving inference](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/services/inference.py): model execution service · `backend/app/services/inference.py`
- [Breed information](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/services/breed_info.py): metadata service · `backend/app/services/breed_info.py`
- [Breed metadata](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/data/breed_metadata.csv): CSV dataset · `data/breed_metadata.csv`
- [Runtime model config](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/backend/app/core/config.py): environment configuration · Selects MODEL_PATH and MODEL_NAME. · `backend/app/core/config.py`

### ML Lifecycle

- **Breed image corpus**: labeled image dataset · Images are organized by breed.
- [Dataset preparation](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/ml/src/data/prepare_dataset.py): manifest pipeline · Creates train, validation, and test manifests. · `ml/src/data/prepare_dataset.py`
- [Training orchestration](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/ml/src/training/trainer.py): training pipeline · Trains MLP, custom CNN, ResNet, and ViT variants. · `ml/src/training/trainer.py`
- **Model architectures**: PyTorch model definitions
- [Model evaluation](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/ml/src/evaluation/compare_models.py): comparison pipeline · Computes metrics and compares architectures. · `ml/src/evaluation/compare_models.py`
- **ML artifacts**: reports and manifests · Provides durable evaluation outputs and deployable model artifacts.

### Packaging & Hosting

- [Unified Docker build](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/Dockerfile): container packaging · Builds the hosted React and FastAPI application. · `Dockerfile`
- [Hugging Face Spaces](https://github.com/ajitsingh98/cattle-breed-classifier-webapp/blob/master/deploy.py): production hosting target · `deploy.py`

## Connections

- 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
