Spaces:
Running
Running
namandhakad712 commited on
Commit ·
e988eb4
1
Parent(s): 8387408
feat: Add Dockerfile for multi-stage build and Nginx serving for Hugging Face Spaces, and update INSTRUCTIONS.md to guide Spaces deployment.
Browse files- Dockerfile +97 -0
- INSTRUCTIONS.md +46 -91
Dockerfile
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Multi-stage Dockerfile for Hugging Face Spaces
|
| 2 |
+
# This Dockerfile builds the React frontend and serves it alongside the Flask backend
|
| 3 |
+
# It automatically fixes the localhost:5000 URLs to work in the Space environment
|
| 4 |
+
|
| 5 |
+
# ---------------------------------------
|
| 6 |
+
# Stage 1: Build Frontend
|
| 7 |
+
# ---------------------------------------
|
| 8 |
+
FROM node:18-alpine as frontend-build
|
| 9 |
+
WORKDIR /app/frontend
|
| 10 |
+
|
| 11 |
+
# Install dependencies
|
| 12 |
+
COPY frontend/package*.json ./
|
| 13 |
+
RUN npm ci
|
| 14 |
+
|
| 15 |
+
# Copy source code
|
| 16 |
+
COPY frontend/ .
|
| 17 |
+
|
| 18 |
+
# MAGIC FIX: Replace hardcoded localhost:5000 with empty string (relative paths)
|
| 19 |
+
# This allows the frontend to talk to the backend on the same domain/port via Nginx
|
| 20 |
+
RUN sed -i 's|http://localhost:5000||g' src/App.tsx
|
| 21 |
+
RUN sed -i 's|http://localhost:5000||g' src/components/StatusPage.tsx
|
| 22 |
+
|
| 23 |
+
# Build React app
|
| 24 |
+
RUN npm run build
|
| 25 |
+
|
| 26 |
+
# ---------------------------------------
|
| 27 |
+
# Stage 2: Production Image (Python + Nginx)
|
| 28 |
+
# ---------------------------------------
|
| 29 |
+
FROM python:3.10-slim
|
| 30 |
+
WORKDIR /app
|
| 31 |
+
|
| 32 |
+
# Install Nginx and system dependencies for OpenCV
|
| 33 |
+
RUN apt-get update && apt-get install -y \
|
| 34 |
+
nginx \
|
| 35 |
+
libgl1-mesa-glx \
|
| 36 |
+
libglib2.0-0 \
|
| 37 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 38 |
+
|
| 39 |
+
# Install Python dependencies
|
| 40 |
+
COPY backend/requirements.txt .
|
| 41 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 42 |
+
|
| 43 |
+
# Copy Backend Code
|
| 44 |
+
COPY backend/ .
|
| 45 |
+
|
| 46 |
+
# Copy Frontend Build from Stage 1
|
| 47 |
+
COPY --from=frontend-build /app/frontend/build /app/frontend_build
|
| 48 |
+
|
| 49 |
+
# Configure Nginx
|
| 50 |
+
# We configure it to listen on port 7860 (Hugging Face default)
|
| 51 |
+
# It serves the React app static files
|
| 52 |
+
# And proxies API requests to the Flask backend on port 5000
|
| 53 |
+
RUN echo 'server { \
|
| 54 |
+
listen 7860; \
|
| 55 |
+
server_name localhost; \
|
| 56 |
+
\
|
| 57 |
+
# Increase body size for video uploads \
|
| 58 |
+
client_max_body_size 100M; \
|
| 59 |
+
\
|
| 60 |
+
location / { \
|
| 61 |
+
root /app/frontend_build; \
|
| 62 |
+
index index.html; \
|
| 63 |
+
try_files $uri $uri/ /index.html; \
|
| 64 |
+
} \
|
| 65 |
+
\
|
| 66 |
+
# Proxy API endpoints to Flask \
|
| 67 |
+
location /analyze { \
|
| 68 |
+
proxy_pass http://127.0.0.1:5000; \
|
| 69 |
+
proxy_read_timeout 300; \
|
| 70 |
+
} \
|
| 71 |
+
location /upload { \
|
| 72 |
+
proxy_pass http://127.0.0.1:5000; \
|
| 73 |
+
} \
|
| 74 |
+
location /health { \
|
| 75 |
+
proxy_pass http://127.0.0.1:5000; \
|
| 76 |
+
} \
|
| 77 |
+
location /debug-model { \
|
| 78 |
+
proxy_pass http://127.0.0.1:5000; \
|
| 79 |
+
proxy_read_timeout 300; \
|
| 80 |
+
} \
|
| 81 |
+
}' > /etc/nginx/sites-available/default
|
| 82 |
+
|
| 83 |
+
# Create model cache directory with permissions
|
| 84 |
+
RUN mkdir -p model_cache && chmod 777 model_cache
|
| 85 |
+
|
| 86 |
+
# Create startup script
|
| 87 |
+
RUN echo '#!/bin/bash\n\
|
| 88 |
+
# Start Flask in background\n\
|
| 89 |
+
python app.py &\n\
|
| 90 |
+
# Start Nginx in foreground\n\
|
| 91 |
+
nginx -g "daemon off;"' > /app/start.sh && chmod +x /app/start.sh
|
| 92 |
+
|
| 93 |
+
# Expose Hugging Face Space port
|
| 94 |
+
EXPOSE 7860
|
| 95 |
+
|
| 96 |
+
# Run the startup script
|
| 97 |
+
CMD ["/app/start.sh"]
|
INSTRUCTIONS.md
CHANGED
|
@@ -1,112 +1,67 @@
|
|
| 1 |
-
# Deepfake Detector -
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
###
|
| 10 |
-
|
| 11 |
-
- [Git](https://git-scm.com/downloads) installed.
|
| 12 |
-
- A **Hugging Face Token** (Read access). Get one [here](https://huggingface.co/settings/tokens).
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
git clone https://github.com/namandhakad712/Deepfake-detector.git
|
| 19 |
-
cd Deepfake-detector
|
| 20 |
-
```
|
| 21 |
-
|
| 22 |
-
2. **Set up Environment Variables:**
|
| 23 |
-
Create a `.env` file in the root directory (or just set the variable in your terminal) to store your Hugging Face token.
|
| 24 |
-
|
| 25 |
-
**Option A: Create a .env file**
|
| 26 |
-
Create a file named `.env` in the root folder and add:
|
| 27 |
-
```env
|
| 28 |
-
HUGGINGFACE_TOKEN=your_hf_token_here
|
| 29 |
-
```
|
| 30 |
-
|
| 31 |
-
**Option B: Pass it directly (Linux/Mac/PowerShell)**
|
| 32 |
-
```bash
|
| 33 |
-
export HUGGINGFACE_TOKEN=your_hf_token_here
|
| 34 |
-
```
|
| 35 |
-
|
| 36 |
-
3. **Run with Docker Compose:**
|
| 37 |
-
```bash
|
| 38 |
-
docker-compose up --build
|
| 39 |
-
```
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
1. Navigate to the backend folder:
|
| 54 |
-
```bash
|
| 55 |
-
cd backend
|
| 56 |
-
```
|
| 57 |
-
|
| 58 |
-
2. Create and activate a virtual environment (optional but recommended):
|
| 59 |
-
```bash
|
| 60 |
-
python -m venv venv
|
| 61 |
-
# Windows:
|
| 62 |
-
.\venv\Scripts\activate
|
| 63 |
-
# Mac/Linux:
|
| 64 |
-
source venv/bin/activate
|
| 65 |
-
```
|
| 66 |
|
| 67 |
-
|
| 68 |
-
```bash
|
| 69 |
-
pip install -r requirements.txt
|
| 70 |
-
```
|
| 71 |
|
| 72 |
-
|
| 73 |
-
```env
|
| 74 |
-
HUGGINGFACE_TOKEN=your_hf_token_here
|
| 75 |
-
```
|
| 76 |
|
| 77 |
-
|
| 78 |
-
```bash
|
| 79 |
-
python app.py
|
| 80 |
-
```
|
| 81 |
-
|
| 82 |
-
### Frontend Setup
|
| 83 |
|
| 84 |
-
1.
|
| 85 |
```bash
|
| 86 |
-
|
|
|
|
| 87 |
```
|
| 88 |
|
| 89 |
-
2.
|
| 90 |
```bash
|
| 91 |
-
|
| 92 |
-
```
|
| 93 |
-
|
| 94 |
-
3. Start the development server:
|
| 95 |
-
```bash
|
| 96 |
-
npm start
|
| 97 |
```
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
---
|
| 102 |
-
|
| 103 |
-
## 🏗️ Project Structure
|
| 104 |
-
|
| 105 |
-
- **frontend/**: React application (TypeScript, Styled Components).
|
| 106 |
-
- **backend/**: Flask API (PyTorch, Transformers, OpenCV).
|
| 107 |
-
- **docker-compose.yml**: Orchestrates the multi-container setup.
|
| 108 |
-
|
| 109 |
-
## ⚠️ Troubleshooting
|
| 110 |
-
|
| 111 |
-
- **Model Loading Error:** Ensure your `HUGGINGFACE_TOKEN` is valid and has read permissions. The first run will take some time to download the model (~500MB).
|
| 112 |
-
- **CORS Errors:** If the frontend cannot talk to the backend, ensure both are running and the backend allows requests from `http://localhost:3000`.
|
|
|
|
| 1 |
+
# Deepfake Detector - Deployment Guide
|
| 2 |
|
| 3 |
+
## 🌍 Deploying to Hugging Face Spaces
|
| 4 |
|
| 5 |
+
This project is configured to be deployed easily to Hugging Face Spaces using Docker.
|
| 6 |
|
| 7 |
+
### 1. Create a New Space
|
| 8 |
+
1. Go to [huggingface.co/spaces](https://huggingface.co/spaces) and click **Create new Space**.
|
| 9 |
+
2. **Name**: `deepfake-detector` (or anything you like).
|
| 10 |
+
3. **License**: MIT (or your choice).
|
| 11 |
+
4. **SDK**: Select **Docker**.
|
| 12 |
+
5. **Template**: Leave as "Blank".
|
| 13 |
+
6. **Visibility**: Public.
|
| 14 |
|
| 15 |
+
### 2. Upload Code
|
| 16 |
+
You can upload the code directly via the web interface or using Git.
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
**Using Git (Recommended):**
|
| 19 |
+
```bash
|
| 20 |
+
# Clone your Space
|
| 21 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 22 |
|
| 23 |
+
# Copy all files from this project into the Space folder
|
| 24 |
+
cp -r Deepfake-detector/* YOUR_SPACE_NAME/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Push to Hugging Face
|
| 27 |
+
cd YOUR_SPACE_NAME
|
| 28 |
+
git add .
|
| 29 |
+
git commit -m "Initial deploy"
|
| 30 |
+
git push
|
| 31 |
+
```
|
| 32 |
|
| 33 |
+
### 3. Configure Secrets (CRITICAL)
|
| 34 |
+
For the model to load, you need to provide your Hugging Face token.
|
| 35 |
|
| 36 |
+
1. Go to your Space's **Settings** tab.
|
| 37 |
+
2. Scroll to **Variables and secrets**.
|
| 38 |
+
3. Click **New secret**.
|
| 39 |
+
* **Name**: `HUGGINGFACE_TOKEN`
|
| 40 |
+
* **Value**: Your Hugging Face Access Token (get it [here](https://huggingface.co/settings/tokens)).
|
| 41 |
|
| 42 |
+
### 4. Wait for Build
|
| 43 |
+
The Space will start building. It may take a few minutes to:
|
| 44 |
+
1. Build the React frontend.
|
| 45 |
+
2. Install Python dependencies.
|
| 46 |
+
3. Download the model (on first run).
|
| 47 |
|
| 48 |
+
Once "Running", your app is live! 🚀
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
---
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
## 🐳 Running Locally with Docker
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
If you just want to run it on your machine:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
1. **Set your token:**
|
| 57 |
```bash
|
| 58 |
+
# PowerShell
|
| 59 |
+
$env:HUGGINGFACE_TOKEN="your_token_here"
|
| 60 |
```
|
| 61 |
|
| 62 |
+
2. **Run:**
|
| 63 |
```bash
|
| 64 |
+
docker-compose up --build
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
```
|
| 66 |
|
| 67 |
+
3. **Open:** [http://localhost:3000](http://localhost:3000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|