vu0018 commited on
Commit
dbe3f97
·
verified ·
1 Parent(s): 5a0cf99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -38
app.py CHANGED
@@ -1,42 +1,93 @@
1
- # app.py
2
  import gradio as gr
3
- from transformers import pipeline
 
 
 
 
4
 
5
- # Load the deepfake detection model from Hugging Face
6
- detector = pipeline(
7
- "video-classification",
8
- model="Hemgg/deepfake-vs-real-video-detection"
 
9
  )
10
 
11
- def analyze_video(video_path):
12
- """
13
- Run deepfake detection on uploaded video.
14
- Returns:
15
- label: predicted class ("FAKE" or "REAL")
16
- score: confidence of the prediction
17
- all_scores: full output from the model
18
- """
19
- results = detector(video_path)
20
- top = results[0] # assume the first element is top prediction
21
- return top["label"], float(top["score"]), results
22
-
23
- # Gradio UI
24
- with gr.Blocks() as demo:
25
- gr.Markdown("# DeepFake Video Detector")
26
- gr.Markdown("Upload a video and the model will classify it as REAL or FAKE.")
27
-
28
- video_input = gr.Video(label="Upload video")
29
- analyze_btn = gr.Button("Analyze Video")
30
-
31
- output_label = gr.Textbox(label="Predicted Label")
32
- output_score = gr.Number(label="Confidence")
33
- output_all = gr.JSON(label="All Predictions")
34
-
35
- analyze_btn.click(
36
- fn=analyze_video,
37
- inputs=[video_input],
38
- outputs=[output_label, output_score, output_all]
39
- )
40
-
41
- if __name__ == "__main__":
42
- demo.launch(server_name="0.0.0.0", share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ import cv2
4
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
5
+ from PIL import Image
6
+ import numpy as np
7
 
8
+ # ----------------------------------------------------------
9
+ # Load Hugging Face GenConViT Model
10
+ # ----------------------------------------------------------
11
+ model = AutoModelForImageClassification.from_pretrained(
12
+ "Thanuja2109/GenConViT"
13
  )
14
 
15
+ processor = AutoImageProcessor.from_pretrained(
16
+ "Thanuja2109/GenConViT"
17
+ )
18
+
19
+ model.eval()
20
+
21
+ # ----------------------------------------------------------
22
+ # Deepfake detection function
23
+ # ----------------------------------------------------------
24
+ def detect_deepfake(video):
25
+
26
+ # Load video
27
+ cap = cv2.VideoCapture(video)
28
+
29
+ if not cap.isOpened():
30
+ return "Error: cannot open video", None
31
+
32
+ scores = []
33
+ frames_collected = 0
34
+
35
+ # Sample 1 frame every 10
36
+ frame_interval = 10
37
+
38
+ frame_img = None
39
+
40
+ i = 0
41
+ while True:
42
+ ret, frame = cap.read()
43
+ if not ret:
44
+ break
45
+
46
+ if i % frame_interval == 0:
47
+ # Convert to RGB
48
+ rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
49
+ pil_img = Image.fromarray(rgb)
50
+
51
+ inputs = processor(images=pil_img, return_tensors="pt")
52
+
53
+ with torch.no_grad():
54
+ logits = model(**inputs).logits
55
+ prob_fake = torch.softmax(logits, dim=1)[0][1].item()
56
+
57
+ scores.append(prob_fake)
58
+ frame_img = pil_img # save last sampled frame
59
+
60
+ i += 1
61
+
62
+ cap.release()
63
+
64
+ if len(scores) == 0:
65
+ return "No frames processed", None
66
+
67
+ avg_score = np.mean(scores)
68
+
69
+ label = "🔴 Deepfake" if avg_score > 0.5 else "🟢 Real"
70
+
71
+ result_text = f"""
72
+ ### Prediction: **{label}**
73
+ **Confidence (fake probability): {avg_score:.4f}**
74
+ """
75
+
76
+ return result_text, frame_img
77
+
78
+
79
+ # ----------------------------------------------------------
80
+ # Gradio Interface
81
+ # ----------------------------------------------------------
82
+ app = gr.Interface(
83
+ fn=detect_deepfake,
84
+ inputs=gr.Video(label="Upload a video"),
85
+ outputs=[
86
+ gr.Markdown(label="Prediction"),
87
+ gr.Image(label="Analyzed Frame")
88
+ ],
89
+ title="GenConViT Deepfake Video Detector",
90
+ description="Upload a video. The app samples frames and uses GenConViT to detect deepfakes."
91
+ )
92
+
93
+ app.launch()