import streamlit as st import requests from transformers import pipeline from deepface import DeepFace from PIL import Image import cv2 import torch import torchvision.transforms as transforms import numpy as np import os import json def load_text_model(): return pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-fake-news-detection") def classify_text(news_text, text_model): result = text_model(news_text)[0] label = result['label'].lower() score = result['score'] * 100 return ("Fake" if label == "fake" else "Real"), round(score, 2) def verify_news(news_text): 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" ] search_url = f"https://www.google.com/search?q={'+'.join(news_text.split())}" return sources, search_url def analyze_image(image_path): try: result = DeepFace.analyze(image_path, actions=['age', 'gender', 'emotion']) return "Real", 85.0 if result else "Fake", 60.0 except: return "Fake", 50.0 def analyze_video(video_path): cap = cv2.VideoCapture(video_path) frames = [] while cap.isOpened(): ret, frame = cap.read() if not ret: break frames.append(frame) cap.release() return "Real", 80.0 if len(frames) > 10 else "Fake", 40.0 st.set_page_config(page_title="Fake News Detector", layout="wide") st.title("📰 Fake News Detector") text_model = load_text_model() st.sidebar.title("Select Input Type") option = st.sidebar.radio("Choose an option", ["Text", "Image", "Video"]) if option == "Text": news_text = st.text_area("Enter the news content:") if st.button("Analyze News"): if news_text.strip(): result, accuracy = classify_text(news_text, text_model) sources, verification_link = verify_news(news_text) st.write(f"Result: **{result}** (Accuracy: {accuracy}%)") for link in sources: st.markdown(f"[🔗 {link}]({link})") st.markdown(f"[🔎 Verify on Google]({verification_link})") else: st.warning("Please enter some text.") elif option == "Image": uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"]) if uploaded_image: img = Image.open(uploaded_image) img_path = "uploaded_image.jpg" img.save(img_path) if st.button("Analyze Image"): result, accuracy = analyze_image(img_path) st.image(img, caption="Uploaded Image", use_column_width=True) st.write(f"Result: **{result}** (Accuracy: {accuracy}%)") os.remove(img_path) elif option == "Video": uploaded_video = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"]) if uploaded_video: video_path = "uploaded_video.mp4" with open(video_path, "wb") as f: f.write(uploaded_video.read()) if st.button("Analyze Video"): result, accuracy = analyze_video(video_path) st.video(video_path) st.write(f"Result: **{result}** (Accuracy: {accuracy}%)") os.remove(video_path)