File size: 4,974 Bytes
62c311b
a7f66d4
ff59733
81a136d
1e42e10
c8d1c47
71c7fc8
7be5b04
71c7fc8
7be5b04
9291ec2
86c53c2
 
 
7be5b04
71c7fc8
86c53c2
 
 
9291ec2
86c53c2
 
 
9291ec2
86c53c2
 
 
9291ec2
86c53c2
 
a7f66d4
71c7fc8
 
ff59733
 
 
 
 
1e42e10
ff59733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71c7fc8
ea6e24c
86c53c2
b6792a4
86c53c2
 
 
 
 
 
b6792a4
 
a7f66d4
7e55fba
 
 
b6792a4
71c7fc8
b6792a4
 
71c7fc8
b6792a4
ff59733
86c53c2
b6792a4
86c53c2
 
 
 
ff59733
b6792a4
 
ff59733
 
a5b7aa4
b6792a4
 
71c7fc8
b6792a4
71c7fc8
ff59733
62a3747
ff59733
b6792a4
ff59733
b6792a4
 
71c7fc8
b6792a4
 
71c7fc8
b6792a4
ff59733
 
 
 
b6792a4
ff59733
b6792a4
 
ff59733
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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.")