Fake News Detector
Mon 30 June 2025
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import streamlit as st
data = {
"text": [
"NASA confirms water on Mars",
"You won't believe what this actor did!",
"Government passes new education bill",
"Scientists find cure for cancer in lemons",
"President addresses the nation",
"Shocking! Aliens spotted in Delhi",
"Stock market hits all-time high",
"Celebrity marries alien in secret ceremony"
],
"label": ["real", "fake", "real", "fake", "real", "fake", "real", "fake"]
}
df = pd.DataFrame(data)
df
| text | label | |
|---|---|---|
| 0 | NASA confirms water on Mars | real |
| 1 | You won't believe what this actor did! | fake |
| 2 | Government passes new education bill | real |
| 3 | Scientists find cure for cancer in lemons | fake |
| 4 | President addresses the nation | real |
| 5 | Shocking! Aliens spotted in Delhi | fake |
| 6 | Stock market hits all-time high | real |
| 7 | Celebrity marries alien in secret ceremony | fake |
X = df["text"]
y = df["label"]
vectorizer = TfidfVectorizer()
X_vec = vectorizer.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_vec, y, test_size=0.25, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
LogisticRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LogisticRegression()
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
Accuracy: 0.0
sample = ["Aliens built the pyramids"]
sample_vec = vectorizer.transform(sample)
print("Prediction:", model.predict(sample_vec)[0])
Prediction: real
st.title("Fake News Detector")
user_input = st.text_area("Enter news headline:")
2025-06-30 02:05:46.589 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.112
[33m[1mWarning:[0m to view this Streamlit app on a browser, run it with the following
command:
streamlit run C:\Users\HP\miniconda3\envs\py312\Lib\site-packages\ipykernel_launcher.py [ARGUMENTS]
2025-06-30 02:05:47.114 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.114 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.115 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.116 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.117 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.118 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.120 Session state does not function when running a script without `streamlit run`
2025-06-30 02:05:47.121 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.122 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.126 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
if st.button("Check"):
vec_input = vectorizer.transform([user_input])
prediction = model.predict(vec_input)[0]
st.write(f"### Prediction: `{prediction.upper()}`")
2025-06-30 02:05:47.138 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.141 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.181 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.182 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.184 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
2025-06-30 02:05:47.185 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
Score: 15
Category: pandas-work