Simma7 commited on
Commit
9f5d208
Β·
verified Β·
1 Parent(s): 14baa69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -102
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 warnings
9
- warnings.filterwarnings('ignore')
10
-
11
- # ==================== CONFIGURATION ====================
12
- WEIGHTS_DIR = os.path.join(os.path.dirname(__file__), "weights")
13
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
14
-
15
- # Access mode – change as needed
16
- ACCESS_MODE = "public" # Options: "local", "network", "public"
17
- # ========================================================
18
-
19
- # Import your detectors
20
- from models.image_detector import ImageDetector
21
- from models.video_detector import VideoDetector
22
- from models.audio_detector import AudioDetector
23
- from models.ensemble import EnsembleDetector
24
-
25
- print("="*60)
26
- print("πŸš€ Deepfake Detection System")
27
- print("="*60)
28
- print(f"Device: {DEVICE}")
29
- print(f"Access Mode: {ACCESS_MODE}")
30
-
31
- # Initialize detectors
32
- image_detector = ImageDetector(WEIGHTS_DIR, DEVICE)
33
- video_detector = VideoDetector(WEIGHTS_DIR, DEVICE)
34
- audio_detector = AudioDetector(DEVICE)
35
- ensemble = EnsembleDetector(image_detector, video_detector, audio_detector)
36
-
37
- def format_result(result):
38
- if 'error' in result:
39
- return f"❌ Error: {result['error']}"
40
-
41
- fake_prob = result['fake_probability']
42
- real_prob = result['real_probability']
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 "Please upload a file"
63
- result = ensemble.detect_file(file.name)
64
- return format_result(result)
65
-
66
- # ==================== Gradio Interface ====================
67
- with gr.Blocks(title="Deepfake Detection System", theme=gr.themes.Soft()) as demo:
68
- gr.Markdown("""
69
- # πŸ” Deepfake Detection System
70
- ### Upload an image, video, or audio file to check if it's real or fake
71
- ---
72
- """)
73
-
74
- with gr.Row():
75
- with gr.Column():
76
- file_input = gr.File(
77
- label="Upload File",
78
- file_types=[".jpg", ".jpeg", ".png", ".mp4", ".avi", ".mov", ".mkv", ".wav", ".mp3", ".flac"]
79
- )
80
- detect_btn = gr.Button("πŸ” Detect Deepfake", variant="primary")
81
-
82
- with gr.Column():
83
- output = gr.HTML(label="Detection Result")
84
-
85
- detect_btn.click(fn=detect, inputs=file_input, outputs=output)
86
-
87
- gr.Markdown("""
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
- # Configure based on access mode
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()