Spaces:
Runtime error
Runtime error
|
Download README.md from jitendra25/Image-Classification-Pipeline: direct link, hf CLI and curl.
- Browser
- Download file 9.1 kB
-
https://huggingface.co/spaces/jitendra25/Image-Classification-Pipeline/resolve/main/README.md
- Command line
-
hf download hf://spaces/jitendra25/Image-Classification-Pipeline/README.md
-
curl -L -o README.md https://huggingface.co/spaces/jitendra25/Image-Classification-Pipeline/resolve/main/README.md
9.1 kB
| title: AI Image Classifier | |
| emoji: πΌοΈ | |
| colorFrom: indigo | |
| colorTo: blue | |
| sdk: docker | |
| app_port: 8000 | |
| pinned: false | |
| license: mit | |
| # πΌοΈ Advanced Image Classification Pipeline | |
| [](https://jitendra25-image-classification-pipeline.hf.space) | |
| [](LICENSE) | |
| **π Live Demo:** **https://jitendra25-image-classification-pipeline.hf.space** | |
| A **production-ready PyTorch image classifier** with a modern FastAPI backend, a | |
| beautiful drag-and-drop web UI (Tailwind CSS), a polished CLI, and a fully | |
| optimized multi-stage Docker build ready for free / cheap cloud hosting. | |
| > Uses **MobileNet_V3_Large** (default) or **EfficientNet_B0** β both more modern, | |
| > faster and more accurate than the classic ResNet-18, pretrained on ImageNet (1000 classes). | |
| --- | |
| ## β¨ Features | |
| - β‘ **FastAPI** backend with async upload handling | |
| - π§ Modern CNN with **singleton model caching** (loads weights only once) | |
| - π `/predict` returns **top-k classes, confidence scores & inference time (ms)** | |
| - π¨ **Drag-and-drop web UI** with live results, confidence bars & loading spinner | |
| - π₯οΈ **CLI tool** (`predict.py`) with a clean ASCII results table | |
| - π Structured **request + timing logging** | |
| - π³ **Multi-stage Dockerfile** (CPU-only torch wheels, slim final image, non-root user) | |
| - π©Ί `/health` endpoint for cloud readiness probes | |
| --- | |
| ## π Directory Structure | |
| ``` | |
| Image-Classification-Pipeline/ | |
| βββ app/ | |
| β βββ __init__.py | |
| β βββ main.py # FastAPI backend + Tailwind single-page UI | |
| βββ predict.py # Advanced argparse CLI classifier | |
| βββ requirements.txt # Pinned dependencies | |
| βββ Dockerfile # Optimized multi-stage build (python:3.10-slim) | |
| βββ .dockerignore | |
| βββ .gitignore | |
| βββ README.md # You are here | |
| ``` | |
| --- | |
| ## π Local Setup (Windows) | |
| > Requires **Python 3.10+**. Commands shown for **PowerShell**. | |
| ### 1. Clone / open the project | |
| ```powershell | |
| cd D:\Image-Classification-Pipeline | |
| ``` | |
| ### 2. Create & activate a virtual environment | |
| ```powershell | |
| python -m venv .venv | |
| .\.venv\Scripts\Activate.ps1 | |
| ``` | |
| > If activation is blocked by execution policy, run once: | |
| > `Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned` | |
| ### 3. Install dependencies | |
| ```powershell | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| ``` | |
| > π‘ For a **smaller, CPU-only** PyTorch install (recommended for laptops/servers | |
| > without a GPU): | |
| > ```powershell | |
| > pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu | |
| > ``` | |
| ### 4. Run the web server | |
| ```powershell | |
| uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload | |
| ``` | |
| Now open **http://localhost:8000** in your browser and drag in an image. π | |
| --- | |
| ## π₯οΈ CLI Usage | |
| Classify any image directly from the terminal: | |
| ```powershell | |
| python predict.py path\to\cat.jpg | |
| python predict.py path\to\cat.jpg --top_k 10 | |
| python predict.py path\to\cat.jpg --model efficientnet_b0 | |
| ``` | |
| ### Sample output | |
| ``` | |
| Model : mobilenet_v3_large | |
| Image : path\to\cat.jpg | |
| +------+------------------------------+------------+------------------------+ | |
| | # | Class | Conf. | Confidence | | |
| +------+------------------------------+------------+------------------------+ | |
| | 1 | tabby | 74.18% | ################...... | | |
| | 2 | tiger cat | 18.55% | ####.................. | | |
| | 3 | Egyptian cat | 4.92% | #..................... | | |
| | 4 | lynx | 0.81% | ...................... | | |
| | 5 | tiger | 0.22% | ...................... | | |
| +------+------------------------------+------------+------------------------+ | |
| Top prediction : tabby (74.18%) | |
| Inference time : 41.3 ms | |
| ``` | |
| --- | |
| ## π API Usage | |
| ### `POST /predict` | |
| ```powershell | |
| curl -X POST "http://localhost:8000/predict?top_k=5" -F "file=@cat.jpg" | |
| ``` | |
| ```json | |
| { | |
| "filename": "cat.jpg", | |
| "model": "mobilenet_v3_large", | |
| "device": "cpu", | |
| "inference_time_ms": 41.3, | |
| "total_time_ms": 58.7, | |
| "predictions": [ | |
| { "rank": 1, "label": "tabby", "confidence": 0.7418, "confidence_pct": 74.18 }, | |
| { "rank": 2, "label": "tiger cat", "confidence": 0.1855, "confidence_pct": 18.55 } | |
| ] | |
| } | |
| ``` | |
| ### `GET /health` | |
| ```json | |
| { "status": "ok", "model": "mobilenet_v3_large", "device": "cpu" } | |
| ``` | |
| ### Interactive docs | |
| FastAPI auto-generates Swagger UI at **http://localhost:8000/docs**. | |
| --- | |
| ## π³ Docker | |
| Build and run locally: | |
| ```powershell | |
| docker build -t image-classifier . | |
| docker run -p 8000:8000 image-classifier | |
| ``` | |
| Then visit **http://localhost:8000**. | |
| > The build uses a **multi-stage** approach: dependencies are compiled in a | |
| > builder stage, and only the slim virtualenv + app are copied into the final | |
| > `python:3.10-slim` runtime. CPU-only torch wheels keep the image small and | |
| > free-tier friendly. | |
| --- | |
| ## βοΈ Production Deployment Guides | |
| ### a) Hugging Face Spaces (Docker Space) | |
| 1. Create a new Space β **SDK: Docker** β **Blank** template. | |
| 2. Push this repo to the Space (it ships with a `Dockerfile`). | |
| ```powershell | |
| git remote add space https://huggingface.co/spaces/<your-username>/<space-name> | |
| git push space main | |
| ``` | |
| 3. **Important:** HF Spaces routes traffic to **port 7860** by default. Either: | |
| - Add this line to your `Dockerfile` and update the `CMD` port: | |
| ```dockerfile | |
| ENV PORT=7860 | |
| CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"] | |
| ``` | |
| - **or** add `app_port: 8000` to the Space's `README.md` YAML front-matter: | |
| ```yaml | |
| --- | |
| title: AI Image Classifier | |
| sdk: docker | |
| app_port: 8000 | |
| --- | |
| ``` | |
| 4. The Space builds automatically and serves your UI publicly. β | |
| --- | |
| ### b) Render.com (Web Service via GitHub) | |
| 1. Push your repo to GitHub (see Git section below). | |
| 2. On [render.com](https://render.com): **New β Web Service β Connect your repo**. | |
| 3. Settings: | |
| - **Environment:** `Docker` | |
| - **Region:** closest to you | |
| - **Instance type:** `Free` | |
| - Render auto-detects the `Dockerfile` β no build/start command needed. | |
| 4. Render injects a `$PORT` env var. Make uvicorn respect it by changing the | |
| `CMD` in your `Dockerfile`: | |
| ```dockerfile | |
| CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}"] | |
| ``` | |
| 5. Click **Create Web Service**. Render builds & deploys on every `git push`. β | |
| --- | |
| ### c) Koyeb / Railway (modern alternatives) | |
| #### Koyeb | |
| 1. [app.koyeb.com](https://app.koyeb.com) β **Create App β GitHub** (or Docker image). | |
| 2. Builder: **Dockerfile**. Koyeb provides `$PORT` (default `8000`). | |
| 3. Set the exposed port to **8000** in the *Ports* section, health check path `/health`. | |
| 4. Deploy β Koyeb gives you a public `*.koyeb.app` URL on the free tier. β | |
| #### Railway | |
| 1. [railway.app](https://railway.app) β **New Project β Deploy from GitHub repo**. | |
| 2. Railway detects the `Dockerfile` automatically. | |
| 3. Add a public domain under **Settings β Networking β Generate Domain**. | |
| 4. Railway sets `$PORT` β use the same `${PORT:-8000}` `CMD` shown for Render. β | |
| > π **General cloud tip:** Most PaaS providers inject a `$PORT` environment | |
| > variable. The portable production command is: | |
| > ```dockerfile | |
| > CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}"] | |
| > ``` | |
| --- | |
| ## π Git: Initialize & Push to GitHub | |
| ```powershell | |
| # 1. Initialize the repository | |
| git init | |
| git add . | |
| git commit -m "Initial commit: advanced PyTorch image classification pipeline" | |
| # 2. Rename default branch to main | |
| git branch -M main | |
| # 3. Create a new EMPTY repo on github.com, then link it: | |
| git remote add origin https://github.com/<your-username>/image-classification-pipeline.git | |
| # 4. Push | |
| git push -u origin main | |
| ``` | |
| ### Subsequent updates | |
| ```powershell | |
| git add . | |
| git commit -m "Describe your change" | |
| git push | |
| ``` | |
| > π‘ Prefer the GitHub CLI? After `gh auth login`: | |
| > ```powershell | |
| > gh repo create image-classification-pipeline --public --source=. --remote=origin --push | |
| > ``` | |
| --- | |
| ## βοΈ Configuration (Environment Variables) | |
| | Variable | Default | Description | | |
| |-------------------|-----------------------|-----------------------------------------------| | |
| | `MODEL_NAME` | `mobilenet_v3_large` | `mobilenet_v3_large` or `efficientnet_b0` | | |
| | `MAX_UPLOAD_BYTES`| `10485760` (10 MB) | Max accepted upload size | | |
| | `PORT` | `8000` | Port to bind (when using the portable `CMD`) | | |
| --- | |
| ## π§ͺ Quick Smoke Test | |
| ```powershell | |
| # Terminal 1 β start the server | |
| uvicorn app.main:app --reload | |
| # Terminal 2 β hit the health endpoint | |
| curl http://localhost:8000/health | |
| ``` | |
| --- | |
| ## π License | |
| MIT β free to use, modify and deploy. Happy classifying! π | |