TruthLens commited on
Commit
b1f6a8b
·
verified ·
1 Parent(s): 1dbeab5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -45
app.py CHANGED
@@ -1,57 +1,45 @@
 
1
  import streamlit as st
2
- import yt_dlp
3
- import torch
4
  from transformers import pipeline
 
 
 
5
 
6
  st.set_page_config(page_title="Video Deepfake Detector", layout="centered")
 
7
 
8
- # Download video using yt-dlp
9
- def download_video(video_url, output_path="video.mp4"):
10
- ydl_opts = {
11
- 'format': 'best',
12
- 'outtmpl': output_path,
13
- 'quiet': True,
14
- }
15
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
16
- ydl.download([video_url])
17
- return output_path
18
 
19
- # Load the deepfake detection model
20
  def load_model():
21
- return pipeline("image-classification", model="microsoft/resnet-50")
22
 
23
- # Analyze video frames for deepfake detection
24
- def analyze_video(video_path, model):
25
- import cv2
26
- cap = cv2.VideoCapture(video_path)
27
- frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
28
- step = max(frame_count // 5, 1)
29
- results = []
30
-
31
- for i in range(0, frame_count, step):
32
- cap.set(cv2.CAP_PROP_POS_FRAMES, i)
33
- ret, frame = cap.read()
34
- if not ret:
35
- continue
36
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
37
- prediction = model(rgb_frame)
38
- results.append(prediction[0])
39
- cap.release()
40
  return results
41
 
42
- # Streamlit interface
43
- st.title("🎥 Video Deepfake Detector")
44
- video_url = st.text_input("Enter YouTube Video URL:")
45
-
46
- if st.button("Submit") and video_url:
47
- with st.spinner("Downloading and analyzing video..."):
48
- try:
49
- video_path = download_video(video_url)
50
  model = load_model()
51
- predictions = analyze_video(video_path, model)
 
 
 
 
 
52
 
53
- st.success("Analysis Complete!")
54
- for idx, pred in enumerate(predictions):
55
- st.write(f"Frame {idx + 1}: {pred['label']} with confidence {pred['score']:.2f}")
56
- except Exception as e:
57
- st.error(f"Error: {e}")
 
1
+ from pytube import YouTube
2
  import streamlit as st
 
 
3
  from transformers import pipeline
4
+ from PIL import Image
5
+ import requests
6
+ from io import BytesIO
7
 
8
  st.set_page_config(page_title="Video Deepfake Detector", layout="centered")
9
+ st.title("🎥 Video Deepfake Detector")
10
 
11
+ @st.cache_data
12
+ def get_thumbnail(url):
13
+ try:
14
+ yt = YouTube(url)
15
+ response = requests.get(yt.thumbnail_url)
16
+ if response.status_code == 200:
17
+ return Image.open(BytesIO(response.content))
18
+ except Exception as e:
19
+ st.error(f"Error fetching thumbnail: {e}")
20
+ return None
21
 
22
+ @st.cache_resource
23
  def load_model():
24
+ return pipeline("image-classification", model="facebook/deit-base-distilled-patch16-224")
25
 
26
+ def detect_deepfake(image, model):
27
+ results = model(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  return results
29
 
30
+ def main():
31
+ video_url = st.text_input("Enter YouTube Video URL:")
32
+ if st.button("Analyze") and video_url:
33
+ thumbnail = get_thumbnail(video_url)
34
+ if thumbnail:
35
+ st.image(thumbnail, caption="Video Thumbnail", use_container_width=True)
 
 
36
  model = load_model()
37
+ results = detect_deepfake(thumbnail, model)
38
+ st.subheader("Detection Results:")
39
+ for res in results:
40
+ st.write(f"{res['label']}: {res['score']:.4f}")
41
+ else:
42
+ st.warning("Unable to fetch thumbnail. Please check the video URL.")
43
 
44
+ if __name__ == "__main__":
45
+ main()