Spaces:
Sleeping
Sleeping
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="β ") | |