Update app.py
Browse files
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 |
-
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
return
|
| 18 |
|
| 19 |
-
|
| 20 |
def load_model():
|
| 21 |
-
return pipeline("image-classification", model="
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
|
| 43 |
-
st.
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if
|
| 47 |
-
|
| 48 |
-
try:
|
| 49 |
-
video_path = download_video(video_url)
|
| 50 |
model = load_model()
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 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()
|
|
|
|
|
|
|
|
|