Spaces:
Sleeping
Sleeping
| title: Deep Detect Api | |
| emoji: π¦ | |
| colorFrom: red | |
| colorTo: blue | |
| sdk: docker | |
| app_port: 7860 | |
| # Image_Detector β FastAPI Backend π | |
| This directory contains the complete Python backend for Deep-Detect. It exposes a REST API that accepts image uploads and returns an AI vs. Real classification result using a custom-trained PyTorch CNN. | |
| --- | |
| ## π Directory Structure | |
| ``` | |
| Image_Detector/ | |
| βββ app.py # FastAPI application β server entry point, routes, CORS | |
| βββ inference.py # Model loading, image preprocessing, prediction pipeline | |
| βββ model.py # Custom CNN architecture (PyTorch nn.Module definition) | |
| βββ predict.py # Standalone Tkinter desktop GUI for local inference | |
| βββ requirements.txt # All Python package dependencies | |
| βββ models/ # Trained model weights (gitignored β download separately) | |
| β βββ custom_cnn_standalone.pt # TorchScript model (~103 MB) | |
| βββ notebooks/ # Jupyter notebooks for research and training | |
| βββ Preprocessing.ipynb # Dataset loading, augmentation, visualization | |
| βββ Model_training.ipynb # Full training loop with loss/accuracy tracking | |
| βββ Model_evaluation.ipynb # Confusion matrix, ROC, per-class metrics | |
| βββ Pretrained_Models.ipynb # Experiments with ResNet50, EfficientNet, ViT | |
| ``` | |
| --- | |
| ## βοΈ Setup | |
| ### 1. Create Virtual Environment | |
| ```bash | |
| cd Image_Detector | |
| python -m venv venv | |
| # Windows | |
| .\venv\Scripts\activate | |
| # macOS / Linux | |
| source venv/bin/activate | |
| ``` | |
| ### 2. Install Dependencies | |
| ```bash | |
| pip install -r requirements.txt | |
| ``` | |
| **Dependencies:** | |
| | Package | Version | Purpose | | |
| |---|---|---| | |
| | `fastapi` | β₯0.100.0 | REST API framework | | |
| | `uvicorn` | β₯0.20.0 | ASGI server | | |
| | `python-multipart` | β₯0.0.6 | File upload parsing | | |
| | `torch` | β₯2.0.0 | PyTorch deep learning | | |
| | `torchvision` | β₯0.15.0 | Image transforms | | |
| | `pillow` | β₯9.5.0 | Image I/O | | |
| ### 3. Download Model Weights | |
| The model file `custom_cnn_standalone.pt` is too large for GitHub (103 MB > 100 MB limit). Download it from the project's [Releases](../../../releases) page and place it at: | |
| ``` | |
| Image_Detector/models/custom_cnn_standalone.pt | |
| ``` | |
| --- | |
| ## π Running the Server | |
| ```bash | |
| python app.py | |
| ``` | |
| The server starts on **`http://0.0.0.0:8000`**. Verify it's healthy: | |
| ```bash | |
| curl http://localhost:8000/ | |
| ``` | |
| Expected response: | |
| ```json | |
| { | |
| "status": "healthy", | |
| "api_name": "Deep-Detect Image Classification Service", | |
| "model_architecture": "Custom CNN Standalone (PyTorch)", | |
| "device_running": "cpu" | |
| } | |
| ``` | |
| --- | |
| ## π API Endpoints | |
| ### `POST /predict` | |
| Classifies an uploaded image as Deep-Fake or Real. | |
| ```bash | |
| curl -X POST http://localhost:8000/predict \ | |
| -F "file=@/path/to/image.jpg" | |
| ``` | |
| **Success Response:** | |
| ```json | |
| { | |
| "prediction": "ai", | |
| "confidence": 94.85, | |
| "status": "success" | |
| } | |
| ``` | |
| - `prediction`: `"ai"` or `"real"` | |
| - `confidence`: percentage confidence (0β100) | |
| --- | |
| ## π§ Model Details | |
| | Property | Value | | |
| |---|---| | |
| | Architecture | Custom CNN (defined in `model.py`) | | |
| | Input Size | 224 Γ 224 (RGB) | | |
| | Normalization | ImageNet mean/std | | |
| | Output | Single logit β Sigmoid β binary probability | | |
| | Format | TorchScript (`.pt`) β runs without class definition at load time | | |
| | Inference Device | CPU (configurable) | | |
| **Inference Pipeline** (see [`inference.py`](inference.py)): | |
| 1. Load image bytes β convert to RGB PIL Image | |
| 2. Resize to 224Γ224 | |
| 3. Normalize with ImageNet statistics | |
| 4. Run forward pass through TorchScript model | |
| 5. Apply Sigmoid to raw logit β confidence score | |
| 6. Threshold at 0.5: `< 0.5` β `"real"`, `β₯ 0.5` β `"ai"` | |
| --- | |
| ## π₯οΈ Desktop Utility | |
| Run local inference via a GUI without starting the API server: | |
| ```bash | |
| python predict.py | |
| ``` | |
| Opens a Tkinter window where you can browse and classify local image files directly. | |
| --- | |
| ## π Notebooks | |
| The [`notebooks/`](notebooks/) directory contains the full ML research workflow: | |
| | Notebook | Description | | |
| |---|---| | |
| | [`Preprocessing.ipynb`](notebooks/Preprocessing.ipynb) | Dataset exploration, augmentation strategy, class balance analysis | | |
| | [`Model_training.ipynb`](notebooks/Model_training.ipynb) | Custom CNN training from scratch with loss curves and checkpointing | | |
| | [`Model_evaluation.ipynb`](notebooks/Model_evaluation.ipynb) | Confusion matrix, ROC-AUC, precision/recall metrics | | |
| | [`Pretrained_Models.ipynb`](notebooks/Pretrained_Models.ipynb) | Transfer learning experiments: ResNet50, EfficientNet, ViT | | |