Spaces:
Running
Running
import streamlit as st | |
import requests | |
import torch | |
from transformers import pipeline | |
from deepface import DeepFace | |
from PIL import Image | |
import cv2 | |
import numpy as np | |
from bs4 import BeautifulSoup | |
# Load Fake News Detection Models | |
fake_news_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection") | |
sentiment_pipeline = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") | |
ai_detector_pipeline = pipeline("text-classification", model="roberta-base-openai-detector") | |
def classify_text(news_text): | |
fake_news_result = fake_news_pipeline(news_text)[0] | |
sentiment_result = sentiment_pipeline(news_text)[0] | |
ai_result = ai_detector_pipeline(news_text)[0] | |
fake_label = fake_news_result['label'].lower() | |
sentiment_label = sentiment_result['label'].lower() | |
ai_label = ai_result['label'].lower() | |
fake_score = fake_news_result['score'] * 100 | |
sentiment_score = sentiment_result['score'] * 100 | |
ai_score = ai_result['score'] * 100 | |
final_label = "Fake" if fake_label == "fake" or ai_label == "ai-generated" else "Real" | |
return final_label, round(fake_score, 2), round(sentiment_score, 2), round(ai_score, 2) | |
def analyze_image(image): | |
try: | |
analysis = DeepFace.analyze(image, actions=["emotion"]) | |
dominant_emotion = analysis[0]["dominant_emotion"] | |
return "Fake" if dominant_emotion in ["fear", "surprise"] else "Real" | |
except Exception as e: | |
return "Error: " + str(e) | |
def analyze_video(video_path): | |
try: | |
cap = cv2.VideoCapture(video_path) | |
frame_count = 0 | |
results = [] | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret or frame_count >= 10: | |
break | |
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) | |
result = analyze_image(image) | |
results.append(result) | |
frame_count += 1 | |
cap.release() | |
return "Fake" if results.count("Fake") > results.count("Real") else "Real" | |
except Exception as e: | |
return "Error: " + str(e) | |
def verify_news(news_text): | |
search_url = f"https://www.google.com/search?q={'%20'.join(news_text.split())}" | |
sources = [ | |
f"https://www.bbc.co.uk/search?q={'%20'.join(news_text.split())}", | |
f"https://www.cnn.com/search?q={'%20'.join(news_text.split())}", | |
f"https://www.reuters.com/search/news?blob={'%20'.join(news_text.split())}", | |
f"https://www.factcheck.org/?s={'%20'.join(news_text.split())}", | |
f"https://www.snopes.com/?s={'%20'.join(news_text.split())}", | |
f"https://www.politifact.com/search/?q={'%20'.join(news_text.split())}" | |
] | |
return search_url, sources | |
st.set_page_config(page_title="Fake News Detector", layout="wide") | |
st.title("π° Fake News Detector") | |
col1, col2, col3 = st.columns(3) | |
with col1: | |
st.header("Text News Analysis") | |
news_text = st.text_area("Enter the news content to check:", height=200) | |
if st.button("Analyze News", key="text_analyze"): | |
if news_text.strip(): | |
result, fake_acc, sentiment_acc, ai_acc = classify_text(news_text) | |
verification_link, sources = verify_news(news_text) | |
st.write(f"**Result:** {result}") | |
st.write(f"**Fake News Score:** {fake_acc}%") | |
st.write(f"**Sentiment Score:** {sentiment_acc}%") | |
st.write(f"**AI Detection Score:** {ai_acc}%") | |
st.markdown(f"[Verify on Google]({verification_link})") | |
for source in sources: | |
st.markdown(f"[Check Source]({source})") | |
else: | |
st.warning("Please enter some text.") | |
with col2: | |
st.header("Image News Analysis") | |
uploaded_image = st.file_uploader("Upload a news image", type=["jpg", "png", "jpeg"]) | |
if uploaded_image and st.button("Analyze Image", key="image_analyze"): | |
image = Image.open(uploaded_image) | |
result = analyze_image(image) | |
st.image(image, caption="Uploaded Image", use_container_width=True) | |
st.write(f"**Result:** {result}") | |
verification_link, sources = verify_news("Fake news image verification") | |
st.markdown(f"[Verify on Google]({verification_link})") | |
for source in sources: | |
st.markdown(f"[Check Source]({source})") | |
with col3: | |
st.header("Video News Analysis") | |
video_url = st.text_input("Enter the video link:") | |
if st.button("Analyze Video", key="video_analyze"): | |
if video_url.strip(): | |
result = analyze_video(video_url) | |
st.video(video_url) | |
st.write(f"**Result:** {result}") | |
verification_link, sources = verify_news("Fake news video verification") | |
st.markdown(f"[Verify on Google]({verification_link})") | |
for source in sources: | |
st.markdown(f"[Check Source]({source})") | |
else: | |
st.warning("Please enter a valid video link.") | |