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 io | |
# Load Fake News Detection Model from Hugging Face | |
fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection") | |
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) | |
def analyze_image(image): | |
transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()]) | |
image_tensor = transform(image).unsqueeze(0) | |
return "Image analysis feature coming soon!" | |
def verify_news(news_text): | |
search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}" | |
return search_url | |
# Streamlit UI | |
st.set_page_config(page_title="Fake News Detector", layout="wide") | |
st.title("π° Fake News Detector") | |
# Sidebar for Input Selection | |
st.sidebar.title("Select Input Type") | |
option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video Link"]) | |
# Ensure session state variables are initialized before modifying them | |
if "analyze_text" not in st.session_state: | |
st.session_state["analyze_text"] = False | |
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 "analyze_image" not in st.session_state: | |
st.session_state["analyze_image"] = False | |
if "analyze_video" not in st.session_state: | |
st.session_state["analyze_video"] = False | |
# 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["news_text"] = news_text | |
st.session_state["analyze_text"] = True | |
st.session_state["result_text"] = result | |
st.session_state["accuracy_text"] = accuracy | |
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) | |
st.session_state["news_image"] = image | |
st.session_state["analyze_image"] = True | |
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_url"] = video_url | |
st.session_state["analyze_video"] = True | |
# Results Section | |
st.subheader("π Analysis Results") | |
if st.session_state.get("analyze_text", False): | |
result = st.session_state.get("result_text") | |
accuracy = st.session_state.get("accuracy_text") | |
verification_link = verify_news(st.session_state.get("news_text")) | |
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", | |
"https://deepfake-o-meter.ai", | |
"https://huggingface.co/models?pipeline_tag=text-classification" | |
] | |
for link in sources: | |
st.markdown(f"[π {link}]({link})") | |
st.markdown(f"[π Verify on Google]({verification_link})") | |
if st.session_state.get("analyze_image", False): | |
image = st.session_state.get("news_image") | |
st.image(image, caption="Uploaded Image", use_column_width=True) | |
st.info(analyze_image(image)) | |
if st.session_state.get("analyze_video", False): | |
video_url = st.session_state.get("video_url", "") | |
st.video(video_url) | |
st.info("Video analysis feature coming soon!") | |