Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,57 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
-
import tempfile
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
video_url = st.text_input("Enter YouTube Video URL:")
|
|
|
|
| 9 |
if st.button("Submit") and video_url:
|
| 10 |
with st.spinner("Downloading and analyzing video..."):
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
video_path = os.path.join(temp_dir, "video.mp4")
|
| 16 |
-
stream.download(output_path=temp_dir, filename="video.mp4")
|
| 17 |
-
|
| 18 |
-
model = pipeline("image-classification", model="facebook/deit-base-distilled-patch16-224")
|
| 19 |
-
results = model(video_path)
|
| 20 |
-
|
| 21 |
-
st.success("Analysis Complete")
|
| 22 |
-
st.write("Prediction:", results[0]['label'])
|
| 23 |
-
st.write("Confidence:", f"{results[0]['score'] * 100:.2f}%")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
st.error(f"Error: {e}")
|
|
|
|
| 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}")
|