File size: 2,187 Bytes
dc4f473 f31bdd1 7e91fc8 d67c44a f31bdd1 7e91fc8 d67c44a 7e91fc8 f31bdd1 d67c44a f31bdd1 7e91fc8 d67c44a 7e91fc8 f31bdd1 7e91fc8 d67c44a 7e91fc8 d67c44a 7e91fc8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import streamlit as st
import requests
from PIL import Image
from transformers import pipeline
from urllib.parse import urlparse, parse_qs
st.set_page_config(page_title="Deepfake Video Detector", layout="centered")
st.title("π₯ Deepfake Video Detector")
st.write("Enter a YouTube video URL (supports Shorts & standard videos) to check for deepfakes.")
@st.cache_data
def extract_video_id(url):
parsed_url = urlparse(url)
if "youtube" in parsed_url.netloc:
if parsed_url.path.startswith("/watch"):
return parse_qs(parsed_url.query).get("v", [None])[0]
elif parsed_url.path.startswith("/shorts/"):
return parsed_url.path.split("/")[-1]
return None
@st.cache_data
def get_thumbnail(video_id):
return f"https://img.youtube.com/vi/{video_id}/hqdefault.jpg"
@st.cache_resource
def load_model():
return pipeline("image-classification", model="facebook/deit-base-distilled-patch16-224")
model = load_model()
video_url = st.text_input("π Paste YouTube video URL:")
if st.button("Detect Deepfake") and video_url:
video_id = extract_video_id(video_url)
if video_id:
thumbnail_url = get_thumbnail(video_id)
try:
response = requests.get(thumbnail_url)
response.raise_for_status()
thumbnail = Image.open(requests.get(thumbnail_url, stream=True).raw)
st.image(thumbnail, caption="Video Thumbnail", use_container_width=True)
st.write("π Analyzing thumbnail...")
results = model(thumbnail)
deepfake_score = sum(result['score'] for result in results if 'fake' in result['label'].lower())
if deepfake_score > 0.5:
st.error(f"β οΈ High probability of deepfake detected! (Confidence: {deepfake_score:.2%})")
else:
st.success(f"β
No strong evidence of deepfake detected. (Confidence: {1 - deepfake_score:.2%})")
except requests.exceptions.RequestException:
st.error("β Failed to fetch thumbnail. Please check the video URL.")
else:
st.error("β Invalid YouTube URL. Please enter a valid video or Shorts link.")
|