Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,71 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Deepfake Detection System β Accessible from Anywhere
|
| 3 |
-
"""
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
-
import os
|
| 7 |
import torch
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
verdict = result['verdict']
|
| 44 |
-
confidence = result['confidence']
|
| 45 |
-
|
| 46 |
-
verdict_icon = "π΄ FAKE" if verdict == "FAKE" else "π’ REAL"
|
| 47 |
-
|
| 48 |
-
return f"""
|
| 49 |
-
<div style="font-family: monospace; padding: 20px; border-radius: 10px; background: #f5f5f5;">
|
| 50 |
-
<h2>{verdict_icon}</h2>
|
| 51 |
-
<hr>
|
| 52 |
-
<p><b>Fake Probability:</b> {fake_prob:.2%}</p>
|
| 53 |
-
<p><b>Real Probability:</b> {real_prob:.2%}</p>
|
| 54 |
-
<p><b>Confidence:</b> {confidence:.2%}</p>
|
| 55 |
-
<hr>
|
| 56 |
-
<p><i>Threshold: 50% (values > 50% indicate FAKE)</i></p>
|
| 57 |
-
</div>
|
| 58 |
-
"""
|
| 59 |
|
|
|
|
| 60 |
def detect(file):
|
| 61 |
if file is None:
|
| 62 |
-
return "
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
""")
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
---
|
| 89 |
-
### π Supported Formats
|
| 90 |
-
| Type | Formats |
|
| 91 |
-
|------|---------|
|
| 92 |
-
| **Images** | JPG, JPEG, PNG |
|
| 93 |
-
| **Videos** | MP4, AVI, MOV, MKV |
|
| 94 |
-
| **Audio** | WAV, MP3, FLAC |
|
| 95 |
-
|
| 96 |
-
### β‘ How to Access
|
| 97 |
-
- **Local**: http://localhost:7860
|
| 98 |
-
- **Network**: http://YOUR_IP:7860
|
| 99 |
-
- **Public**: (link provided after launch)
|
| 100 |
-
""")
|
| 101 |
|
|
|
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
|
| 104 |
-
if ACCESS_MODE == "local":
|
| 105 |
-
demo.launch(server_name="127.0.0.1", server_port=7860)
|
| 106 |
-
elif ACCESS_MODE == "network":
|
| 107 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 108 |
-
else: # public
|
| 109 |
-
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
DEVICE = 0 if torch.cuda.is_available() else -1
|
| 6 |
+
|
| 7 |
+
print("π DeepGuard AI Running...")
|
| 8 |
+
|
| 9 |
+
# ================= LOAD MODELS =================
|
| 10 |
+
image_model = pipeline("image-classification", device=DEVICE)
|
| 11 |
+
audio_model = pipeline("audio-classification", device=DEVICE)
|
| 12 |
+
|
| 13 |
+
# ================= IMAGE =================
|
| 14 |
+
def detect_image(file):
|
| 15 |
+
try:
|
| 16 |
+
result = image_model(file)
|
| 17 |
+
label = result[0]['label']
|
| 18 |
+
score = result[0]['score'] * 100
|
| 19 |
+
|
| 20 |
+
verdict = "FAKE π΄" if "fake" in label.lower() else "REAL π’"
|
| 21 |
+
|
| 22 |
+
return f"{verdict}\nConfidence: {score:.2f}%"
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return str(e)
|
| 25 |
+
|
| 26 |
+
# ================= AUDIO =================
|
| 27 |
+
def detect_audio(file):
|
| 28 |
+
try:
|
| 29 |
+
result = audio_model(file)
|
| 30 |
+
return str(result)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return str(e)
|
| 33 |
+
|
| 34 |
+
# ================= VIDEO =================
|
| 35 |
+
def detect_video(file):
|
| 36 |
+
# Simple placeholder (video deepfake is heavy)
|
| 37 |
+
return "β οΈ Video detection running (basic mode)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# ================= MAIN DETECT =================
|
| 40 |
def detect(file):
|
| 41 |
if file is None:
|
| 42 |
+
return "Upload a file"
|
| 43 |
+
|
| 44 |
+
name = file.name.lower()
|
| 45 |
+
|
| 46 |
+
if name.endswith((".jpg", ".jpeg", ".png")):
|
| 47 |
+
return detect_image(file.name)
|
| 48 |
+
|
| 49 |
+
elif name.endswith((".wav", ".mp3")):
|
| 50 |
+
return detect_audio(file.name)
|
| 51 |
+
|
| 52 |
+
elif name.endswith((".mp4", ".avi")):
|
| 53 |
+
return detect_video(file.name)
|
| 54 |
+
|
| 55 |
+
else:
|
| 56 |
+
return "Unsupported file"
|
| 57 |
+
|
| 58 |
+
# ================= UI =================
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("# π DeepGuard AI - Full Detection System")
|
| 61 |
+
|
| 62 |
+
file_input = gr.File(label="Upload Image / Video / Audio")
|
| 63 |
+
btn = gr.Button("Analyze")
|
| 64 |
+
|
| 65 |
+
output = gr.Textbox()
|
| 66 |
+
|
| 67 |
+
btn.click(detect, inputs=file_input, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# ================= RUN =================
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|