VidGuard / app.py
TruthLens's picture
Update app.py
f31bdd1 verified
raw
history blame
1.98 kB
import streamlit as st
from PIL import Image
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
import torch
import requests
from io import BytesIO
# Load deepfake detection model and extractor
model_name = "systemkc/deepfake-detection-v1" # Example model (replace if needed)
extractor = AutoFeatureExtractor.from_pretrained(model_name)
model = AutoModelForImageClassification.from_pretrained(model_name)
st.title("Deepfake Thumbnail Detector")
st.write("Upload a YouTube video link, and we’ll analyze the thumbnail to check for deepfakes.")
video_url = st.text_input("Enter YouTube Video Link:")
submit = st.button("Detect Deepfake")
if submit and video_url:
try:
video_id = video_url.split("v=")[-1].split("&")[0] if "v=" in video_url else video_url.split("/")[-1]
thumbnail_url = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
response = requests.get(thumbnail_url)
response.raise_for_status()
image = Image.open(BytesIO(response.content))
st.image(image, caption="Video Thumbnail", use_container_width=True)
# Preprocess and predict
inputs = extractor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class = torch.argmax(logits, dim=1).item()
confidence = torch.softmax(logits, dim=1)[0, predicted_class].item()
# Interpret results
if predicted_class == 1: # Assuming class 1 = Deepfake
st.error(f"🚨 **Deepfake Detected** (Confidence: {confidence:.2%})")
st.write("⚠️ Indicators of manipulation detected in the thumbnail.")
else:
st.success(f"✅ **No Deepfake Detected** (Confidence: {confidence:.2%})")
st.write("👍 Thumbnail appears authentic based on the model’s analysis.")
except Exception as e:
st.error(f"Error: {str(e)}")