File size: 4,440 Bytes
62c311b
a7f66d4
81a136d
097294d
a7f66d4
 
a7c9258
62c311b
a7f66d4
0e6d020
107dbe8
a7f66d4
5bc427c
 
 
 
a7f66d4
 
 
 
 
 
 
 
 
 
62c311b
7e55fba
 
 
5bc427c
 
 
d006eb0
5ff1805
 
 
 
 
 
 
 
 
 
 
 
5bc427c
 
 
ac67c60
5ff1805
 
5bc427c
 
 
 
 
 
 
 
a7c9258
5bc427c
 
ac67c60
5ff1805
 
5bc427c
 
 
a7c9258
5bc427c
 
ac67c60
5ff1805
 
5bc427c
 
 
 
 
a7c9258
5bc427c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e55fba
5bc427c
 
 
 
a7f66d4
5bc427c
 
 
ac67c60
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
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!")