Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,57 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
-
import requests
|
| 5 |
-
from PIL import Image
|
| 6 |
from pytube import YouTube
|
| 7 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def detect_image(image):
|
| 10 |
-
model =
|
| 11 |
return model(image)
|
| 12 |
|
| 13 |
def detect_video(video_url):
|
| 14 |
yt = YouTube(video_url)
|
| 15 |
-
stream = yt.streams.filter(file_extension='mp4').first()
|
| 16 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=
|
| 17 |
-
stream.download(filename=
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
from pytube import YouTube
|
| 4 |
import tempfile
|
| 5 |
+
from moviepy.editor import VideoFileClip
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import requests
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="AI Media Verifier", layout="wide")
|
| 11 |
+
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_image_model():
|
| 14 |
+
return pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 15 |
+
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_video_model():
|
| 18 |
+
return pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 19 |
|
| 20 |
def detect_image(image):
|
| 21 |
+
model = load_image_model()
|
| 22 |
return model(image)
|
| 23 |
|
| 24 |
def detect_video(video_url):
|
| 25 |
yt = YouTube(video_url)
|
| 26 |
+
stream = yt.streams.filter(file_extension='mp4', progressive=True).order_by('resolution').desc().first()
|
| 27 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
| 28 |
+
stream.download(filename=tmp.name)
|
| 29 |
+
clip = VideoFileClip(tmp.name).subclip(0, min(10, yt.length))
|
| 30 |
+
frame = clip.get_frame(clip.duration / 2)
|
| 31 |
+
image = Image.fromarray(frame)
|
| 32 |
+
return detect_image(image)
|
| 33 |
+
|
| 34 |
+
st.title("AI Media Verifier: Image & Video Authenticity")
|
| 35 |
+
|
| 36 |
+
option = st.radio("Choose input type:", ("Image", "Video"))
|
| 37 |
+
|
| 38 |
+
if option == "Image":
|
| 39 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 40 |
+
if uploaded_image:
|
| 41 |
+
image = Image.open(uploaded_image)
|
| 42 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 43 |
+
with st.spinner("Analyzing image..."):
|
| 44 |
+
results = detect_image(uploaded_image)
|
| 45 |
+
st.success("Analysis Complete")
|
| 46 |
+
for res in results:
|
| 47 |
+
st.write(f"**{res['label']}**: {res['score']*100:.2f}%")
|
| 48 |
+
|
| 49 |
+
elif option == "Video":
|
| 50 |
+
video_url = st.text_input("Enter YouTube Video Link")
|
| 51 |
+
if video_url:
|
| 52 |
+
st.video(video_url)
|
| 53 |
+
with st.spinner("Analyzing video..."):
|
| 54 |
+
results = detect_video(video_url)
|
| 55 |
+
st.success("Video Analysis Complete")
|
| 56 |
+
for res in results:
|
| 57 |
+
st.write(f"**{res['label']}**: {res['score']*100:.2f}%")
|