Prasanna-ETH commited on
Commit
27a43fb
Β·
verified Β·
1 Parent(s): ea2739f

Create app.py for the KYC verification

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from transformers import pipeline
5
+
6
+ # Step 2: Load the AI Hunter (The Model)
7
+ # This single line downloads the "Deepfake Hunter" model
8
+ # It is trained to recognize 'Fake' (Class 0) vs 'Real' (Class 1)
9
+ v_detector = pipeline("image-classification", model="prithivMLmods/Deepfake-Detect-Siglip2")
10
+
11
+ # Step 3: The "Video Slicer" (OpenCV)
12
+ def analyze_video(video_path):
13
+ cap = cv2.VideoCapture(video_path)
14
+ frame_scores = []
15
+
16
+ # Slice the video: Grab 1 frame every 30 frames (roughly 1 frame per second)
17
+ count = 0
18
+ while cap.isOpened():
19
+ ret, frame = cap.read()
20
+ if not ret: break
21
+
22
+ if count % 30 == 0:
23
+ # Convert the video frame to a picture the AI can read
24
+ color_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
25
+ pil_img = Image.fromarray(color_frame)
26
+
27
+ # Ask the AI: "Is this real?"
28
+ prediction = v_detector(pil_img)
29
+ # Store the 'Real' confidence score
30
+ real_score = [res['score'] for res in prediction if res['label'] == 'Real']
31
+ if real_score:
32
+ frame_scores.append(real_score[0])
33
+
34
+ count += 1
35
+ cap.release()
36
+
37
+ if not frame_scores:
38
+ return "ERROR: Could not read video frames"
39
+
40
+ # Step 4: Give the final verdict
41
+ avg_score = sum(frame_scores) / len(frame_scores)
42
+ return "REAL" if avg_score > 0.5 else "DEEPFAKE"
43
+
44
+ # Step 4: The IOB Dashboard (Gradio Demo)
45
+ with gr.Blocks(title="IOB Sentinel") as demo:
46
+ gr.Markdown("# IOB Sentinel: Video KYC Deepfake Detector")
47
+ gr.Markdown("Analyzing visual artifacts, skin texture, and lighting inconsistencies.")
48
+
49
+ with gr.Tabs():
50
+ with gr.TabItem("Upload Video"):
51
+ upload_input = gr.Video(label="Upload KYC Video")
52
+ upload_output = gr.Textbox(label="Result", text_align="center", show_copy_button=True)
53
+ upload_btn = gr.Button("Analyze Uploaded Video", variant="primary")
54
+ upload_btn.click(fn=analyze_video, inputs=upload_input, outputs=upload_output)
55
+
56
+ with gr.TabItem("Live Webcam"):
57
+ # Gradio lets you record via webcam using sources=["webcam"]
58
+ webcam_input = gr.Video(sources=["webcam"], label="Record Live Video")
59
+ webcam_output = gr.Textbox(label="Result", text_align="center", show_copy_button=True)
60
+ webcam_btn = gr.Button("Analyze Live Video", variant="primary")
61
+ webcam_btn.click(fn=analyze_video, inputs=webcam_input, outputs=webcam_output)
62
+
63
+ if __name__ == "__main__":
64
+ demo.launch()