import streamlit as st import requests import torch import torchvision.transforms as transforms 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 Model 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 return ("Fake" if label == "fake" else "Real"), round(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={'+'.join(news_text.split())}" sources = [ f"https://www.bbc.co.uk/search?q={'+'.join(news_text.split())}", f"https://www.cnn.com/search?q={'+'.join(news_text.split())}", f"https://www.reuters.com/search/news?blob={'+'.join(news_text.split())}", f"https://www.factcheck.org/?s={'+'.join(news_text.split())}", f"https://www.snopes.com/?s={'+'.join(news_text.split())}", f"https://www.politifact.com/search/?q={'+'.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, accuracy = classify_text(news_text) verification_link, sources = verify_news(news_text) st.write(f"**Result:** {result} (Accuracy: {accuracy}%)") 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_column_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.")