Nimzi's picture
Update app.py
28d0426 verified
raw
history blame
3.46 kB
import streamlit as st
import requests
from transformers import pipeline
from PIL import Image
import numpy as np
import cv2
import torch
import torchvision.transforms as transforms
from deepface import DeepFace
# Load Fake News Detection Model
fake_news_pipeline = pipeline("text-classification", model="roberta-base-openai-detector")
# Function to classify text news
def classify_text(news_text):
result = fake_news_pipeline(news_text)[0]
label = result['label'].lower()
score = result['score'] * 100 # Convert to percentage
return ("Fake" if label == "fake" else "Real"), round(score, 2)
# Function to verify news with Google Fact Check API
def verify_news(news_text):
search_url = f"https://toolbox.google.com/factcheck/explorer/search/{'+'.join(news_text.split())}"
return search_url
# Function to analyze deepfake images
def analyze_image(image):
try:
result = DeepFace.analyze(np.array(image), actions=['age', 'gender', 'race', 'emotion'])
return f"βœ… This image appears **real**! AI Analysis: {result}"
except Exception:
return "❌ This image might be **deepfake or manipulated**!"
# Function to analyze video metadata
def analyze_video(video_url):
api_url = f"https://noembed.com/embed?url={video_url}"
response = requests.get(api_url).json()
if "title" in response:
title = response["title"]
source = response["provider_name"]
verification_link = verify_news(title)
return f"πŸŽ₯ Video Title: {title}\nπŸ”— Source: {source}\n[πŸ”Ž Verify this Video]({verification_link})"
else:
return "❌ Unable to fetch video details! Check if the link is correct."
# Streamlit UI
st.set_page_config(page_title="Fake News Detector", layout="wide")
st.title("πŸ“° Fake News Detector")
# Sidebar Navigation
st.sidebar.title("Select Input Type")
option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
# Input Sections
col1, col2, col3 = st.columns(3)
# Text Analysis Section
with col1:
st.subheader("πŸ“„ Text News Check")
news_text = st.text_area("Enter the news content:", height=200)
if st.button("Analyze News"):
if not news_text.strip():
st.warning("Please enter some text.")
else:
result, accuracy = classify_text(news_text)
verification_link = verify_news(news_text)
if result == "Fake":
st.error(f"❌ Likely **Fake News**! (Confidence: {accuracy}%)")
else:
st.success(f"βœ… Likely **Real News**! (Confidence: {accuracy}%)")
st.markdown(f"[πŸ”Ž Verify on Google Fact Check]({verification_link})")
# Image Upload Section
with col2:
st.subheader("πŸ–ΌοΈ Image News Check")
uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
if uploaded_image and st.button("Analyze Image"):
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", use_column_width=True)
result = analyze_image(image)
st.info(result)
# Video Link Section
with col3:
st.subheader("πŸŽ₯ Video News Check")
video_url = st.text_input("Enter the video link:")
if st.button("Analyze Video"):
if not video_url.strip():
st.warning("Please enter a valid video link.")
else:
st.video(video_url)
result = analyze_video(video_url)
st.info(result)