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