Prashant885084/deep-defender

hackX 2.0 hackthaon project

Python

0

76 commits

updated Jun 25, 2026

See the code

README

Deep Defender

website URL:-- https://deepdefenders.netlify.app/ Deep Defender is a Flask-based prototype for detecting potentially fake or synthetic media. It combines a lightweight web UI, PostgreSQL-backed detection history, URL-based media analysis, and optional model code for image and audio deepfake detection. for api testing only https://deep-defender-an6z.onrender.com/

This repository has been built as a hackathon/demo project, and the current implementation mixes production-like pieces with heuristic fallbacks. The README below reflects the code as it exists today.

What the project does

  • Analyzes image and audio and URLs through /api/url/analyze /api/image/detect /api/voice/detect
  • Extracts linked media from HTML pages when the URL is not a direct media file
  • Stores image, voice, and URL detection history in detections.db
  • Exposes analytics and history endpoints for the UI
  • Includes optional TensorFlow and Hugging Face model wrappers for future or partial integration

Current behavior

There are two different detection paths in the codebase:

  1. Upload Media flow in the UI The frontend uploads the actual selected image or audio file. The backend saves it temporarily, analyzes the media bytes through the optional trained model path when weights are available, and otherwise uses deterministic image/audio heuristics.

  2. Analyze URL flow in the UI The backend fetches a remote image or audio file, tries to use optional local TensorFlow weights if present, and otherwise falls back to heuristic analysis based on media characteristics and URL metadata.

Tech stack

  • Backend: Flask
  • Frontend: HTML, CSS, vanilla JavaScript
  • Database: PostgreSQL
  • Optional ML/runtime dependencies:
    • TensorFlow
    • OpenCV
    • librosa
    • NumPy
    • Pillow
    • PyTorch
    • transformers

Project structure

deep-defender/
├── app.py                          # Main Flask app, routes, DB setup, URL analysis
├── .app.py                         # Small launcher that calls main()
├── index.html                      # Frontend UI
├── config.js                       # Frontend backend API URL config
├── script.js                       # Frontend interactions and API calls
├── style.css                       # Frontend styling
├── render.yaml                     # Render backend deploy config
├── requirements.txt                # Backend/runtime dependencies
├── requirements-ml.txt             # Optional TensorFlow/Hugging Face dependencies
├── .python-version                 # Render Python runtime line
├── detections.db                   # SQLite database used by the app
├── image_preprocess.py             # Image preprocessing for TensorFlow model path
├── image_xception.py               # Xception-based image classifier definition
├── audio_preprocess.py             # Audio preprocessing for TensorFlow model path
├── audio_transformer.py            # Audio transformer model definition
├── auto_transformer.py             # Duplicate audio transformer model file
├── backend/pipelines/
│   ├── image_pipeline.py           # HF image pipeline wrapper
│   └── audio_pipeline.py           # HF audio pipeline wrapper
└── src/
    ├── models/
    │   ├── hf_image_model.py       # Hugging Face image model loader
    │   └── hf_audio_model.py       # Hugging Face audio model loader
    └── utils/
        └── postprocess.py          # Label mapping helper

How detection works

1. File upload endpoints

The frontend chooses one of these endpoints based on file extension:

  • POST /api/image/detect
  • POST /api/voice/detect

These routes:

  • accept multipart form data with a real file
  • reject filename-only requests because they cannot be accurately analyzed
  • run the optional trained model path when compatible weights are present
  • otherwise run deterministic media heuristics
  • store the record in PostgreSQL
  • return a response for the UI

Uploaded filenames are not used as suspicious keyword evidence, which helps avoid false positives for authentic files with confusing names.

2. URL analysis endpoint

POST /api/url/analyze is the most substantial path in the app.

Flow:

  1. Validate the URL and reject private/local hosts
  2. Fetch the remote resource with size limits
  3. If the URL is HTML, parse it and try to find a linked media candidate
  4. Detect whether the resolved file is an image or audio asset
  5. Try to load optional local TensorFlow weights:
    • models/image_xception.weights.h5 or image_xception.weights.h5
    • models/audio_transformer.weights.h5 or audio_transformer.weights.h5
  6. If weights are unavailable or inference fails, fall back to heuristic analysis
  7. Store the result in url_detections

3. Heuristic fallback behavior

When trained weights are not available, the app uses heuristics:

  • Image heuristics:

    • URL metadata keyword scoring
    • edge detail via Laplacian variance
    • saturation level
    • grayscale contrast
  • Audio heuristics:

    • URL metadata keyword scoring
    • spectral flatness
    • RMS variation
    • short-duration penalty

API overview

Detection routes

  • POST /api/image/detect
  • GET /api/image/history
  • POST /api/voice/detect
  • GET /api/voice/history
  • POST /api/url/analyze
  • GET /api/url/history

Analytics route

  • GET /api/analytics

Returns:

  • total image detections
  • total voice detections
  • fake vs real counts
  • average confidence
  • 7-day image and voice activity trends

Database schema

The app creates three tables on startup:

  • image_detections
  • voice_detections
  • url_detections

The database is initialized in init_db() and seeded with demo image/voice history in seed_demo_data() if the history tables are empty.

Setup

1. Create a virtual environment

python -m venv .venv
source .venv/bin/activate

2. Install dependencies

pip install -r requirements.txt

3. Install optional ML dependencies

The Render deploy uses the lighter backend dependency set in requirements.txt. If you later want the TensorFlow and Hugging Face helper code locally, install:

pip install -r requirements-ml.txt

TensorFlow wheels are not available for every Python release, so use Python 3.12 for the optional ML environment.

Run the app

python app.py

Or:

python .app.py

The server starts on:

http://localhost:9000

Deploy backend on Render

This repo includes render.yaml, so the easiest path is Render Blueprints:

  1. Push the latest code to GitHub.
  2. In Render, choose New > Blueprint.
  3. Select this repository and apply the render.yaml.
  4. Render will create a Python web service named deep-defender-an6z.
  5. After deploy, test:
https://deep-defender-an6z.onrender.com/health

If Render gives your service a different URL, update config.js with that URL and push again so Netlify calls the correct backend.

Manual Render settings are:

Language: Python 3
Build Command: pip install -r requirements.txt
Start Command: gunicorn app:app --timeout 120
Health Check Path: /health

Environment variables:

CORS_ORIGINS=https://deepdefenders.netlify.app
FLASK_DEBUG=false
SEED_DEMO_DATA=true

The app uses SQLite by default. For persistent production history, create a Render PostgreSQL database and set DATABASE_URL to its internal database URL.

Connect Netlify frontend

The frontend reads window.DEEP_DEFENDER_API_BASE_URL from config.js. For the default Render service name it already points Netlify traffic to:

https://deep-defender-an6z.onrender.com

For local Flask testing, the value stays blank and calls the same origin.

Frontend usage

Open the app in a browser and use one of the tabs:

  • Upload Media

    • choose an image or audio file
    • the selected file is uploaded and analyzed by the backend
  • Analyze URL

    • paste a public http:// or https:// URL
    • the backend fetches and analyzes image or audio content from that URL

The repository contains model code that is only partially wired into the live app:

Important note:

  • The Flask routes use the TensorFlow weight-loading path for both file uploads and URL analysis when compatible local weights are available
  • The Hugging Face pipeline classes are present but not currently called from app.py

Security and safety measures already present

  • Blocks localhost and private/reserved IP ranges for URL analysis
  • Limits HTML fetch size to 1 MB
  • Limits media download size to 15 MB
  • Restricts served frontend files to index.html, script.js, style.css, and config.js

Known limitations

  • The TensorFlow model path depends on local .weights.h5 files that are not included here
  • Hugging Face model wrappers are present but not integrated into the running Flask app
  • auto_transformer.py appears to duplicate audio_transformer.py
  • detections.db is committed into the repo and may contain mutable local demo data
  • The UI focuses on a single detection result and does not surface history or analytics yet

Good next steps

  • Add a labeled validation set and tune thresholds against real examples
  • Add a proper dependency manifest
  • Wire the Hugging Face pipelines into the Flask routes
  • Add tests for URL validation, media fetching, and analytics responses
  • Split the Flask backend and frontend assets into clearer modules
  • Add environment-based config for port, debug mode, and model locations

Demo-friendly summary

Deep Defender is a strong prototype for a deepfake-detection dashboard. The upload and URL flows now inspect actual media bytes, with deterministic heuristic fallbacks when trained weights are not available. To reach dependable 70-80% accuracy, the next major step is validating against a labeled image/audio dataset and wiring in a trained model that matches that data.

Prashant885084/deep-defender

hackX 2.0 hackthaon project

Python

0

76 commits

updated Jun 25, 2026

See the code

README

Deep Defender

website URL:-- https://deepdefenders.netlify.app/ Deep Defender is a Flask-based prototype for detecting potentially fake or synthetic media. It combines a lightweight web UI, PostgreSQL-backed detection history, URL-based media analysis, and optional model code for image and audio deepfake detection. for api testing only https://deep-defender-an6z.onrender.com/

This repository has been built as a hackathon/demo project, and the current implementation mixes production-like pieces with heuristic fallbacks. The README below reflects the code as it exists today.

What the project does

  • Analyzes image and audio and URLs through /api/url/analyze /api/image/detect /api/voice/detect
  • Extracts linked media from HTML pages when the URL is not a direct media file
  • Stores image, voice, and URL detection history in detections.db
  • Exposes analytics and history endpoints for the UI
  • Includes optional TensorFlow and Hugging Face model wrappers for future or partial integration

Current behavior

There are two different detection paths in the codebase:

  1. Upload Media flow in the UI The frontend uploads the actual selected image or audio file. The backend saves it temporarily, analyzes the media bytes through the optional trained model path when weights are available, and otherwise uses deterministic image/audio heuristics.

  2. Analyze URL flow in the UI The backend fetches a remote image or audio file, tries to use optional local TensorFlow weights if present, and otherwise falls back to heuristic analysis based on media characteristics and URL metadata.

Tech stack

  • Backend: Flask
  • Frontend: HTML, CSS, vanilla JavaScript
  • Database: PostgreSQL
  • Optional ML/runtime dependencies:
    • TensorFlow
    • OpenCV
    • librosa
    • NumPy
    • Pillow
    • PyTorch
    • transformers

Project structure

deep-defender/
├── app.py                          # Main Flask app, routes, DB setup, URL analysis
├── .app.py                         # Small launcher that calls main()
├── index.html                      # Frontend UI
├── config.js                       # Frontend backend API URL config
├── script.js                       # Frontend interactions and API calls
├── style.css                       # Frontend styling
├── render.yaml                     # Render backend deploy config
├── requirements.txt                # Backend/runtime dependencies
├── requirements-ml.txt             # Optional TensorFlow/Hugging Face dependencies
├── .python-version                 # Render Python runtime line
├── detections.db                   # SQLite database used by the app
├── image_preprocess.py             # Image preprocessing for TensorFlow model path
├── image_xception.py               # Xception-based image classifier definition
├── audio_preprocess.py             # Audio preprocessing for TensorFlow model path
├── audio_transformer.py            # Audio transformer model definition
├── auto_transformer.py             # Duplicate audio transformer model file
├── backend/pipelines/
│   ├── image_pipeline.py           # HF image pipeline wrapper
│   └── audio_pipeline.py           # HF audio pipeline wrapper
└── src/
    ├── models/
    │   ├── hf_image_model.py       # Hugging Face image model loader
    │   └── hf_audio_model.py       # Hugging Face audio model loader
    └── utils/
        └── postprocess.py          # Label mapping helper

How detection works

1. File upload endpoints

The frontend chooses one of these endpoints based on file extension:

  • POST /api/image/detect
  • POST /api/voice/detect

These routes:

  • accept multipart form data with a real file
  • reject filename-only requests because they cannot be accurately analyzed
  • run the optional trained model path when compatible weights are present
  • otherwise run deterministic media heuristics
  • store the record in PostgreSQL
  • return a response for the UI

Uploaded filenames are not used as suspicious keyword evidence, which helps avoid false positives for authentic files with confusing names.

2. URL analysis endpoint

POST /api/url/analyze is the most substantial path in the app.

Flow:

  1. Validate the URL and reject private/local hosts
  2. Fetch the remote resource with size limits
  3. If the URL is HTML, parse it and try to find a linked media candidate
  4. Detect whether the resolved file is an image or audio asset
  5. Try to load optional local TensorFlow weights:
    • models/image_xception.weights.h5 or image_xception.weights.h5
    • models/audio_transformer.weights.h5 or audio_transformer.weights.h5
  6. If weights are unavailable or inference fails, fall back to heuristic analysis
  7. Store the result in url_detections

3. Heuristic fallback behavior

When trained weights are not available, the app uses heuristics:

  • Image heuristics:

    • URL metadata keyword scoring
    • edge detail via Laplacian variance
    • saturation level
    • grayscale contrast
  • Audio heuristics:

    • URL metadata keyword scoring
    • spectral flatness
    • RMS variation
    • short-duration penalty

API overview

Detection routes

  • POST /api/image/detect
  • GET /api/image/history
  • POST /api/voice/detect
  • GET /api/voice/history
  • POST /api/url/analyze
  • GET /api/url/history

Analytics route

  • GET /api/analytics

Returns:

  • total image detections
  • total voice detections
  • fake vs real counts
  • average confidence
  • 7-day image and voice activity trends

Database schema

The app creates three tables on startup:

  • image_detections
  • voice_detections
  • url_detections

The database is initialized in init_db() and seeded with demo image/voice history in seed_demo_data() if the history tables are empty.

Setup

1. Create a virtual environment

python -m venv .venv
source .venv/bin/activate

2. Install dependencies

pip install -r requirements.txt

3. Install optional ML dependencies

The Render deploy uses the lighter backend dependency set in requirements.txt. If you later want the TensorFlow and Hugging Face helper code locally, install:

pip install -r requirements-ml.txt

TensorFlow wheels are not available for every Python release, so use Python 3.12 for the optional ML environment.

Run the app

python app.py

Or:

python .app.py

The server starts on:

http://localhost:9000

Deploy backend on Render

This repo includes render.yaml, so the easiest path is Render Blueprints:

  1. Push the latest code to GitHub.
  2. In Render, choose New > Blueprint.
  3. Select this repository and apply the render.yaml.
  4. Render will create a Python web service named deep-defender-an6z.
  5. After deploy, test:
https://deep-defender-an6z.onrender.com/health

If Render gives your service a different URL, update config.js with that URL and push again so Netlify calls the correct backend.

Manual Render settings are:

Language: Python 3
Build Command: pip install -r requirements.txt
Start Command: gunicorn app:app --timeout 120
Health Check Path: /health

Environment variables:

CORS_ORIGINS=https://deepdefenders.netlify.app
FLASK_DEBUG=false
SEED_DEMO_DATA=true

The app uses SQLite by default. For persistent production history, create a Render PostgreSQL database and set DATABASE_URL to its internal database URL.

Connect Netlify frontend

The frontend reads window.DEEP_DEFENDER_API_BASE_URL from config.js. For the default Render service name it already points Netlify traffic to:

https://deep-defender-an6z.onrender.com

For local Flask testing, the value stays blank and calls the same origin.

Frontend usage

Open the app in a browser and use one of the tabs:

  • Upload Media

    • choose an image or audio file
    • the selected file is uploaded and analyzed by the backend
  • Analyze URL

    • paste a public http:// or https:// URL
    • the backend fetches and analyzes image or audio content from that URL

The repository contains model code that is only partially wired into the live app:

Important note:

  • The Flask routes use the TensorFlow weight-loading path for both file uploads and URL analysis when compatible local weights are available
  • The Hugging Face pipeline classes are present but not currently called from app.py

Security and safety measures already present

  • Blocks localhost and private/reserved IP ranges for URL analysis
  • Limits HTML fetch size to 1 MB
  • Limits media download size to 15 MB
  • Restricts served frontend files to index.html, script.js, style.css, and config.js

Known limitations

  • The TensorFlow model path depends on local .weights.h5 files that are not included here
  • Hugging Face model wrappers are present but not integrated into the running Flask app
  • auto_transformer.py appears to duplicate audio_transformer.py
  • detections.db is committed into the repo and may contain mutable local demo data
  • The UI focuses on a single detection result and does not surface history or analytics yet

Good next steps

  • Add a labeled validation set and tune thresholds against real examples
  • Add a proper dependency manifest
  • Wire the Hugging Face pipelines into the Flask routes
  • Add tests for URL validation, media fetching, and analytics responses
  • Split the Flask backend and frontend assets into clearer modules
  • Add environment-based config for port, debug mode, and model locations

Demo-friendly summary

Deep Defender is a strong prototype for a deepfake-detection dashboard. The upload and URL flows now inspect actual media bytes, with deterministic heuristic fallbacks when trained weights are not available. To reach dependable 70-80% accuracy, the next major step is validating against a labeled image/audio dataset and wiring in a trained model that matches that data.