TruthLens commited on
Commit
f31bdd1
·
verified ·
1 Parent(s): 67ddf1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -43
app.py CHANGED
@@ -1,49 +1,46 @@
1
  import streamlit as st
2
- import requests
3
- from transformers import pipeline
4
  from PIL import Image
 
 
 
5
  from io import BytesIO
6
- import re
7
-
8
- # Initialize image classification pipeline
9
- model = pipeline("image-classification", model="google/vit-base-patch16-224")
10
-
11
- def get_thumbnail_url(youtube_url):
12
- """Extract YouTube video ID and generate thumbnail URL."""
13
- video_id_match = re.search(r"(?:v=|youtu.be/|shorts/)([a-zA-Z0-9_-]{11})", youtube_url)
14
- if video_id_match:
15
- video_id = video_id_match.group(1)
16
- return f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
17
- return None
18
-
19
- def detect_thumbnail(thumbnail_url):
20
- """Run AI detection on the thumbnail image."""
21
- response = requests.get(thumbnail_url)
22
- if response.status_code == 200:
 
 
23
  image = Image.open(BytesIO(response.content))
24
- results = model(image)
25
- return results, image
26
- return None, None
27
-
28
- def main():
29
- st.title("🔍 Video Thumbnail AI Detector")
30
- st.write("Enter a YouTube video link to detect content from its thumbnail.")
31
-
32
- video_url = st.text_input("YouTube Video URL:")
33
-
34
- if st.button("Analyze Thumbnail") and video_url:
35
- thumbnail_url = get_thumbnail_url(video_url)
36
- if thumbnail_url:
37
- results, image = detect_thumbnail(thumbnail_url)
38
- if results and image:
39
- st.image(image, caption="Video Thumbnail", use_container_width=True)
40
- st.subheader("Detection Results:")
41
- for res in results:
42
- st.write(f"- **{res['label']}**: {res['score']:.4f}")
43
- else:
44
- st.error("Failed to retrieve or analyze the thumbnail.")
45
  else:
46
- st.error("Invalid YouTube URL.")
 
47
 
48
- if __name__ == "__main__":
49
- main()
 
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
+ # Load deepfake detection model and extractor
9
+ model_name = "systemkc/deepfake-detection-v1" # Example model (replace if needed)
10
+ extractor = AutoFeatureExtractor.from_pretrained(model_name)
11
+ model = AutoModelForImageClassification.from_pretrained(model_name)
12
+
13
+ st.title("Deepfake Thumbnail Detector")
14
+ st.write("Upload a YouTube video link, and we’ll analyze the thumbnail to check for deepfakes.")
15
+
16
+ video_url = st.text_input("Enter YouTube Video Link:")
17
+ submit = st.button("Detect Deepfake")
18
+
19
+ if submit and video_url:
20
+ try:
21
+ video_id = video_url.split("v=")[-1].split("&")[0] if "v=" in video_url else video_url.split("/")[-1]
22
+ thumbnail_url = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
23
+ response = requests.get(thumbnail_url)
24
+ response.raise_for_status()
25
+
26
  image = Image.open(BytesIO(response.content))
27
+ st.image(image, caption="Video Thumbnail", use_container_width=True)
28
+
29
+ # Preprocess and predict
30
+ inputs = extractor(images=image, return_tensors="pt")
31
+ with torch.no_grad():
32
+ outputs = model(**inputs)
33
+ logits = outputs.logits
34
+ predicted_class = torch.argmax(logits, dim=1).item()
35
+ confidence = torch.softmax(logits, dim=1)[0, predicted_class].item()
36
+
37
+ # Interpret results
38
+ if predicted_class == 1: # Assuming class 1 = Deepfake
39
+ st.error(f"🚨 **Deepfake Detected** (Confidence: {confidence:.2%})")
40
+ st.write("⚠️ Indicators of manipulation detected in the thumbnail.")
 
 
 
 
 
 
 
41
  else:
42
+ st.success(f" **No Deepfake Detected** (Confidence: {confidence:.2%})")
43
+ st.write("👍 Thumbnail appears authentic based on the model’s analysis.")
44
 
45
+ except Exception as e:
46
+ st.error(f"Error: {str(e)}")