-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeNewsDetector.py
More file actions
52 lines (52 loc) · 1.92 KB
/
FakeNewsDetector.py
File metadata and controls
52 lines (52 loc) · 1.92 KB
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
import streamlit as st
import pickle
import numpy as np
import re
from sklearn.feature_extraction.text import TfidfVectorizer
texts = [
"Breaking news: government announces new policy",
"Scientists discovered a new species in Amazon",
"Click here to win money now!!!",
"You won lottery claim now",
"Fake news spreading about celebrities death",
"Official report released by government"
]
labels = [1, 1, 0, 0, 0, 1]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(texts)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X, labels)
suspicious_words = [
"win", "lottery", "click", "urgent", "free", "money",
"claim", "now", "shocking", "breaking", "!!!"
]
def highlight_text(text):
words = text.split()
highlighted = ""
for word in words:
if word.lower() in suspicious_words:
highlighted += f"**:red[{word}]** "
else:
highlighted += word + " "
return highlighted
st.set_page_config(page_title="Fake News Detector", page_icon="📰")
st.title("📰 Fake News Detector")
st.write("Enter a news headline or content to check if it's Fake or Real.")
user_input = st.text_area("✍️ Enter News Text")
if st.button("Check News"):
if user_input.strip() == "":
st.warning("Please enter some text!")
else:
input_vector = vectorizer.transform([user_input])
prediction = model.predict(input_vector)[0]
probability = model.predict_proba(input_vector)[0]
if prediction == 1:
st.success("✅ This looks like REAL news")
st.write(f"Confidence: {round(max(probability)*100, 2)}%")
else:
st.error("🚨 This looks like FAKE news")
st.write(f"Confidence: {round(max(probability)*100, 2)}%")
st.subheader("🔍 Suspicious Words Highlighted")
highlighted_text = highlight_text(user_input)
st.markdown(highlighted_text)