deepfake_gaurd / app.py
Simma7's picture
Update app.py
5faf566 verified
# app.py β€” DeepGuard AI β€” Full updated interface
import gradio as gr
from prog.image import detect_image
from prog.audio import detect_audio
from prog.video import detect_video
from prog.live_stream import detect_live_frame
from prog.document import detect_document
from prog.fusion import detect_fusion
print("πŸš€ DeepGuard Premium UI Starting...")
# ── Helper: route uploaded file ───────────────────────────────
def detect_file(file):
if file is None:
return "⚠️ Please upload a file."
name = file.name.lower()
try:
if name.endswith((".jpg", ".jpeg", ".png")):
return detect_image(file.name)
elif name.endswith((".wav", ".mp3", ".ogg")):
return detect_audio(file.name)
elif name.endswith((".mp4", ".avi", ".mov")):
return detect_video(file.name)
elif name.endswith(".pdf"):
return detect_document(file.name)
else:
return "❌ Unsupported format. Supported: JPG, PNG, WAV, MP3, MP4, AVI, PDF"
except Exception as e:
return f"❌ Error: {str(e)}"
# ── Document-specific handler ─────────────────────────────────
def detect_doc_file(file):
if file is None:
return "⚠️ Please upload a document (PDF, JPG, PNG)."
name = file.name.lower()
if name.endswith((".pdf", ".jpg", ".jpeg", ".png")):
return detect_document(file.name)
return "❌ Please upload a PDF or image of a document / ID card."
# ── Fusion handler ────────────────────────────────────────────
def detect_fusion_file(file):
if file is None:
return "⚠️ Please upload a video file."
name = file.name.lower()
if name.endswith((".mp4", ".avi", ".mov")):
return detect_fusion(file.name)
return "❌ Please upload an MP4, AVI, or MOV video file."
# ── CSS ───────────────────────────────────────────────────────
CSS = """
body { background: #0f172a; }
.gr-button-primary { background: #3b82f6 !important; }
.result-box { font-family: monospace; font-size: 13px; }
"""
# ── UI ────────────────────────────────────────────────────────
with gr.Blocks(theme=gr.themes.Soft(), css=CSS, title="DeepGuard AI") as demo:
gr.Markdown("""
# πŸ›‘οΈ DeepGuard AI
### Multi-Modal Deepfake Detection and Threat Analysis System
""")
with gr.Tabs():
# ── TAB 1: Standard Detection ─────────────────────────
with gr.Tab("πŸ“ File Detection"):
gr.Markdown("Upload any image, audio, video, or PDF for deepfake / forgery analysis.")
with gr.Row():
with gr.Column(scale=1):
file_input = gr.File(
label="Upload File",
file_types=[".jpg", ".jpeg", ".png",
".wav", ".mp3", ".ogg",
".mp4", ".avi", ".mov", ".pdf"]
)
btn_analyze = gr.Button("πŸ” Analyze", variant="primary")
btn_clear = gr.Button("πŸ—‘οΈ Clear")
with gr.Column(scale=2):
output_std = gr.Textbox(
label="Detection Result",
lines=16,
elem_classes=["result-box"]
)
btn_analyze.click(detect_file, inputs=file_input, outputs=output_std)
btn_clear.click(lambda: "", None, output_std)
# ── TAB 2: LIVE STREAM ────────────────────────────────
with gr.Tab("πŸ“· Live Webcam"):
gr.Markdown("""
### Live Deepfake Detection via Webcam
Each frame captured from your webcam is analysed in real-time.
Allow camera access when prompted by your browser.
""")
with gr.Row():
with gr.Column(scale=1):
webcam_input = gr.Image(
sources=["webcam"],
streaming=True,
label="Webcam Feed"
)
with gr.Column(scale=1):
webcam_annotated = gr.Image(
label="Annotated Output",
streaming=True
)
webcam_result = gr.Textbox(
label="Live Result",
lines=10,
elem_classes=["result-box"]
)
webcam_input.stream(
detect_live_frame,
inputs=webcam_input,
outputs=[webcam_annotated, webcam_result]
)
gr.Markdown("""
> **Note:** For best results, ensure good lighting and face the camera directly.
> The overlay colour is **green = REAL**, **red = FAKE**.
""")
# ── TAB 3: DOCUMENT FORGERY ───────────────────────────
with gr.Tab("πŸ“„ Document Forgery"):
gr.Markdown("""
### Document Forgery Detection
Upload a scanned document, ID card, certificate, or PDF.
The system runs three forensic checks:
- **Visual noise analysis** β€” detects copy-paste compression artefacts
- **OCR font consistency** β€” flags inserted text with different font sizes
- **PDF metadata check** β€” detects tool mismatch and date anomalies (PDFs only)
""")
with gr.Row():
with gr.Column(scale=1):
doc_input = gr.File(
label="Upload Document / ID / Certificate",
file_types=[".pdf", ".jpg", ".jpeg", ".png"]
)
btn_doc = gr.Button("πŸ”¬ Analyse Document", variant="primary")
btn_doc_clr = gr.Button("πŸ—‘οΈ Clear")
with gr.Column(scale=2):
doc_output = gr.Textbox(
label="Forensic Report",
lines=20,
elem_classes=["result-box"]
)
btn_doc.click(detect_doc_file, inputs=doc_input, outputs=doc_output)
btn_doc_clr.click(lambda: "", None, doc_output)
# ── TAB 4: VIDEO + AUDIO FUSION ───────────────────────
with gr.Tab("🎬 Video + Audio Fusion"):
gr.Markdown("""
### Multi-Modal Video + Audio Fusion Analysis
Upload a video file. The system combines three independent detectors:
- **Video model** (EfficientNet-B3) β€” analyses face frames
- **Audio model** (Wav2Vec2) β€” analyses the speech track
- **Lip-sync checker** β€” cross-correlates mouth movement with audio energy
A deepfake video often has one or more of these three signals misaligned.
""")
with gr.Row():
with gr.Column(scale=1):
fusion_input = gr.File(
label="Upload Video File",
file_types=[".mp4", ".avi", ".mov"]
)
btn_fusion = gr.Button("πŸ” Run Fusion Analysis", variant="primary")
btn_fusion_clr = gr.Button("πŸ—‘οΈ Clear")
with gr.Column(scale=2):
fusion_output = gr.Textbox(
label="Fusion Report",
lines=25,
elem_classes=["result-box"]
)
btn_fusion.click(detect_fusion_file,
inputs=fusion_input,
outputs=fusion_output)
btn_fusion_clr.click(lambda: "", None, fusion_output)
gr.Markdown("""
---
> DeepGuard AI uses AI-based predictions and may not be 100% accurate.
> Results should be treated as forensic indicators, not definitive proof.
> Always apply human judgement for high-stakes decisions.
""")
if __name__ == "__main__":
demo.launch()