Nimzi's picture
Update app.py
71c7fc8 verified
raw
history blame
5.44 kB
import streamlit as st
import requests
from transformers import pipeline
from PIL import Image
import torch
import torchvision.transforms as transforms
import cv2
import numpy as np
from deepface import DeepFace
from bs4 import BeautifulSoup
# Load Fake News Detection Model (Text)
fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection")
# Function to classify text as Fake or Real
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 analyze image authenticity
def analyze_image(image):
try:
image_array = np.array(image)
result = DeepFace.analyze(image_array, actions=["age", "gender", "race"], enforce_detection=False)
return "Real" if result else "Fake", 90 # Placeholder accuracy
except Exception as e:
return "Error", str(e)
# Function to verify news from open sources
def verify_news(news_text):
search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}"
response = requests.get(search_url)
soup = BeautifulSoup(response.text, "html.parser")
results = []
for link in soup.find_all("a", href=True):
if "http" in link["href"] and "google" not in link["href"]:
results.append((link.text.strip(), link["href"]))
if len(results) >= 3: # Limit to 3 sources
break
return results
# Streamlit UI Configuration
st.set_page_config(page_title="Fake News Detector", layout="wide")
st.title("πŸ“° Fake News Detector")
# Sidebar Input Selection
st.sidebar.title("Select Input Type")
option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"])
# Session Variables
if "result_text" not in st.session_state:
st.session_state["result_text"] = None
if "accuracy_text" not in st.session_state:
st.session_state["accuracy_text"] = None
if "result_image" not in st.session_state:
st.session_state["result_image"] = None
if "accuracy_image" not in st.session_state:
st.session_state["accuracy_image"] = None
if "video_result" not in st.session_state:
st.session_state["video_result"] = None
# Input Section
if option == "Text":
news_text = st.text_area("Enter the news content to check:", height=200)
analyze_text_clicked = st.button("Analyze News")
if analyze_text_clicked:
if not news_text.strip():
st.warning("Please enter some text.")
else:
result, accuracy = classify_text(news_text)
st.session_state["result_text"] = result
st.session_state["accuracy_text"] = accuracy
verification_links = verify_news(news_text)
st.session_state["verification_text"] = verification_links
elif option == "Image":
uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"])
analyze_image_clicked = st.button("Analyze Image")
if uploaded_image and analyze_image_clicked:
image = Image.open(uploaded_image)
result, accuracy = analyze_image(image)
st.session_state["result_image"] = result
st.session_state["accuracy_image"] = accuracy
elif option == "Video Link":
video_url = st.text_input("Enter the video link:")
analyze_video_clicked = st.button("Analyze Video")
if analyze_video_clicked:
if not video_url.strip():
st.warning("Please enter a valid video link.")
else:
st.session_state["video_result"] = "Real" # Placeholder (Video verification requires advanced models)
# Results Section
st.subheader("πŸ“Š Analysis Results")
# Text Results
if st.session_state.get("result_text"):
result = st.session_state["result_text"]
accuracy = st.session_state["accuracy_text"]
st.subheader("πŸ“ Text Analysis")
if result == "Fake":
st.error(f"❌ This news is likely **Fake**! (Accuracy: {accuracy}%)", icon="⚠️")
else:
st.success(f"βœ… This news is likely **Real**! (Accuracy: {accuracy}%)", icon="βœ…")
st.subheader("πŸ” Verification & Trusted Sources")
sources = [
"https://www.bbc.com/news",
"https://www.cnn.com",
"https://www.reuters.com",
"https://factcheck.org",
"https://www.snopes.com",
"https://www.politifact.com"
]
for link in sources:
st.markdown(f"[πŸ”— {link}]({link})")
if "verification_text" in st.session_state:
for name, link in st.session_state["verification_text"]:
st.markdown(f"[πŸ” {name}]({link})")
# Image Results
if st.session_state.get("result_image"):
result = st.session_state["result_image"]
accuracy = st.session_state["accuracy_image"]
st.subheader("πŸ–ΌοΈ Image Analysis")
if result == "Fake":
st.error(f"❌ This image is likely **Fake**! (Accuracy: {accuracy}%)", icon="⚠️")
else:
st.success(f"βœ… This image is likely **Real**! (Accuracy: {accuracy}%)", icon="βœ…")
# Video Results
if st.session_state.get("video_result"):
result = st.session_state["video_result"]
st.subheader("πŸ“Ή Video Analysis")
if result == "Fake":
st.error("❌ This video is likely **Fake**!", icon="⚠️")
else:
st.success("βœ… This video is likely **Real**!", icon="βœ…")