Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,49 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
-
import requests
|
| 6 |
from io import BytesIO
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
return pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch16")
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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
|
|
|
|
| 24 |
response = requests.get(thumbnail_url)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def main():
|
| 30 |
-
st.title("
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
st.subheader("Detection Results:")
|
| 42 |
-
for
|
| 43 |
-
st.write(f"{label}
|
| 44 |
else:
|
| 45 |
-
st.
|
| 46 |
-
|
| 47 |
-
st.error(
|
| 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()
|