Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,64 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
| 4 |
-
import torch
|
| 5 |
import requests
|
|
|
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
model
|
| 12 |
|
| 13 |
-
|
| 14 |
-
st.write("Upload a YouTube video link, and we’ll analyze the thumbnail to check for deepfakes.")
|
| 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 |
else:
|
| 42 |
-
st.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
except Exception as e:
|
| 46 |
-
st.error(f"Error: {str(e)}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
+
from urllib.parse import urlparse, parse_qs
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
# Initialize the deepfake detection model
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
return pipeline("image-classification", model="Wvolf/ViT_Deepfake_Detection")
|
| 12 |
|
| 13 |
+
model = load_model()
|
|
|
|
| 14 |
|
| 15 |
+
def get_thumbnail_url(video_url):
|
| 16 |
+
"""
|
| 17 |
+
Extracts the YouTube video ID and returns the thumbnail URL.
|
| 18 |
+
"""
|
| 19 |
+
parsed_url = urlparse(video_url)
|
| 20 |
+
video_id = None
|
| 21 |
+
if 'youtube' in parsed_url.netloc:
|
| 22 |
+
query_params = parse_qs(parsed_url.query)
|
| 23 |
+
video_id = query_params.get('v', [None])[0]
|
| 24 |
+
elif 'youtu.be' in parsed_url.netloc:
|
| 25 |
+
video_id = parsed_url.path.lstrip('/')
|
| 26 |
+
|
| 27 |
+
if video_id:
|
| 28 |
+
return f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 29 |
+
return None
|
| 30 |
|
| 31 |
+
def analyze_thumbnail(thumbnail_url):
|
| 32 |
+
"""
|
| 33 |
+
Downloads the thumbnail image and analyzes it using the deepfake detection model.
|
| 34 |
+
"""
|
| 35 |
+
response = requests.get(thumbnail_url)
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 38 |
+
results = model(image)
|
| 39 |
+
return results, image
|
| 40 |
+
else:
|
| 41 |
+
st.error("Failed to retrieve the thumbnail image.")
|
| 42 |
+
return None, None
|
| 43 |
|
| 44 |
+
# Streamlit UI
|
| 45 |
+
st.title("Deepfake Detection from YouTube Thumbnails")
|
| 46 |
+
video_url = st.text_input("Enter YouTube Video URL:")
|
| 47 |
+
if st.button("Analyze"):
|
| 48 |
+
if video_url:
|
| 49 |
+
thumbnail_url = get_thumbnail_url(video_url)
|
| 50 |
+
if thumbnail_url:
|
| 51 |
+
results, image = analyze_thumbnail(thumbnail_url)
|
| 52 |
+
if results and image:
|
| 53 |
+
st.image(image, caption="YouTube Video Thumbnail", use_column_width=True)
|
| 54 |
+
st.subheader("Detection Results:")
|
| 55 |
+
for result in results:
|
| 56 |
+
label = result['label']
|
| 57 |
+
confidence = result['score'] * 100
|
| 58 |
+
st.write(f"**{label}**: {confidence:.2f}%")
|
| 59 |
+
else:
|
| 60 |
+
st.error("Could not analyze the thumbnail.")
|
| 61 |
else:
|
| 62 |
+
st.error("Invalid YouTube URL. Please enter a valid URL.")
|
| 63 |
+
else:
|
| 64 |
+
st.warning("Please enter a YouTube video URL.")
|
|
|
|
|
|