File size: 2,223 Bytes
dc4f473
d67c44a
b1f6a8b
f31bdd1
d67c44a
b1f6a8b
f31bdd1
d67c44a
 
 
 
f31bdd1
d67c44a
f31bdd1
d67c44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f31bdd1
d67c44a
 
 
 
 
 
 
 
 
 
 
 
f31bdd1
d67c44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67ddf1e
d67c44a
 
 
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
58
59
60
61
62
63
64
65
import streamlit as st
from transformers import pipeline
from PIL import Image
import requests
from urllib.parse import urlparse, parse_qs
from io import BytesIO

# Initialize the deepfake detection model
@st.cache_resource
def load_model():
    return pipeline("image-classification", model="Wvolf/ViT_Deepfake_Detection")

model = load_model()

def get_thumbnail_url(video_url):
    """
    Extracts the YouTube video ID and returns the thumbnail URL.
    """
    parsed_url = urlparse(video_url)
    video_id = None
    if 'youtube' in parsed_url.netloc:
        query_params = parse_qs(parsed_url.query)
        video_id = query_params.get('v', [None])[0]
    elif 'youtu.be' in parsed_url.netloc:
        video_id = parsed_url.path.lstrip('/')
    
    if video_id:
        return f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
    return None

def analyze_thumbnail(thumbnail_url):
    """
    Downloads the thumbnail image and analyzes it using the deepfake detection model.
    """
    response = requests.get(thumbnail_url)
    if response.status_code == 200:
        image = Image.open(BytesIO(response.content)).convert("RGB")
        results = model(image)
        return results, image
    else:
        st.error("Failed to retrieve the thumbnail image.")
        return None, None

# Streamlit UI
st.title("Deepfake Detection from YouTube Thumbnails")
video_url = st.text_input("Enter YouTube Video URL:")
if st.button("Analyze"):
    if video_url:
        thumbnail_url = get_thumbnail_url(video_url)
        if thumbnail_url:
            results, image = analyze_thumbnail(thumbnail_url)
            if results and image:
                st.image(image, caption="YouTube Video Thumbnail", use_column_width=True)
                st.subheader("Detection Results:")
                for result in results:
                    label = result['label']
                    confidence = result['score'] * 100
                    st.write(f"**{label}**: {confidence:.2f}%")
            else:
                st.error("Could not analyze the thumbnail.")
        else:
            st.error("Invalid YouTube URL. Please enter a valid URL.")
    else:
        st.warning("Please enter a YouTube video URL.")