Nimzi's picture
Update app.py
a906516 verified
raw
history blame
3.68 kB
import streamlit as st
import requests
from transformers import pipeline
from PIL import Image
import torchvision.transforms as transforms
import torch
import cv2
import numpy as np
# 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 images for fake news
def analyze_image(image):
transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])
image_tensor = transform(image).unsqueeze(0)
# Convert to OpenCV format
image_cv = np.array(image)
image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
# Apply Fake Image Detection (Basic Example)
edges = cv2.Canny(image_cv, 100, 200)
edge_percentage = np.sum(edges) / (image_cv.shape[0] * image_cv.shape[1])
if edge_percentage > 0.1:
return "❌ This image might be **manipulated** or **deepfake**!"
else:
return "βœ… This image appears to be **authentic**."
# 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"]
return f"πŸŽ₯ Video Title: {title}\nπŸ”— Source: {source}"
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)