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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
app.py CHANGED
@@ -1,50 +1,49 @@
1
  import streamlit as st
2
- from yt_dlp import YoutubeDL
3
  from transformers import pipeline
4
  from PIL import Image
5
- import requests
6
  from io import BytesIO
 
7
 
8
- @st.cache_resource
9
- def load_model():
10
- return pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch16")
11
 
12
- def fetch_thumbnail(video_url):
13
- ydl_opts = {
14
- 'skip_download': True,
15
- 'quiet': True,
16
- 'extract_flat': True,
17
- 'cookiefile': 'cookies.txt'
18
- }
19
- with YoutubeDL(ydl_opts) as ydl:
20
- info = ydl.extract_info(video_url, download=False)
21
- return info.get('title'), info.get('thumbnail')
22
 
23
- def classify_thumbnail(model, thumbnail_url):
 
24
  response = requests.get(thumbnail_url)
25
- image = Image.open(BytesIO(response.content)).convert("RGB")
26
- labels = ["real", "AI-generated", "manipulated", "deepfake"]
27
- return model(image, candidate_labels=labels)
 
 
28
 
29
  def main():
30
- st.title("🎥 Video Integrity Checker")
31
- video_url = st.text_input("Enter YouTube video URL:")
32
- if st.button("Submit") and video_url:
33
- try:
34
- title, thumbnail = fetch_thumbnail(video_url)
35
- st.success(f"Video Title: {title}")
36
- if thumbnail:
37
- st.image(thumbnail, caption="Video Thumbnail", use_container_width=True)
38
- model = load_model()
39
- with st.spinner("Analyzing..."):
40
- results = classify_thumbnail(model, thumbnail)
41
  st.subheader("Detection Results:")
42
- for label, score in zip(results['labels'], results['scores']):
43
- st.write(f"{label}: {score:.2%}")
44
  else:
45
- st.warning("No thumbnail found.")
46
- except Exception as e:
47
- st.error(f"Error: {e}")
48
 
49
  if __name__ == "__main__":
50
  main()
 
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()