Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,123 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
from
|
| 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 |
-
st.
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
#
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
# Install required libraries if missing
|
| 5 |
+
required_libs = ["streamlit", "pandas", "numpy", "scikit-learn", "nltk"]
|
| 6 |
+
for lib in required_libs:
|
| 7 |
+
subprocess.run(["pip", "install", lib])
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import numpy as np
|
| 11 |
+
import nltk
|
| 12 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 13 |
+
from sklearn.model_selection import train_test_split
|
| 14 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 15 |
+
from sklearn.linear_model import LogisticRegression
|
| 16 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 17 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 18 |
+
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
|
| 19 |
+
from nltk.corpus import stopwords
|
| 20 |
+
|
| 21 |
+
st.image("innomatics-footer-logo.webp")
|
| 22 |
+
st.image("fake_logo.jpg")
|
| 23 |
+
|
| 24 |
+
# Download NLTK stopwords
|
| 25 |
+
nltk.download("stopwords")
|
| 26 |
+
stop_words = set(stopwords.words("english"))
|
| 27 |
+
|
| 28 |
+
# Load Datasets
|
| 29 |
+
@st.cache_data
|
| 30 |
+
def load_data():
|
| 31 |
+
df_fake = pd.read_csv("Fake.csv")
|
| 32 |
+
df_real = pd.read_csv("True.csv")
|
| 33 |
+
|
| 34 |
+
# Assign labels
|
| 35 |
+
df_fake["label"] = 0 # Fake News
|
| 36 |
+
df_real["label"] = 1 # Real News
|
| 37 |
+
|
| 38 |
+
# Merge datasets
|
| 39 |
+
df = pd.concat([df_fake, df_real], ignore_index=True)
|
| 40 |
+
df = df.sample(n=10000, random_state=27).reset_index(drop=True) # Shuffle
|
| 41 |
+
|
| 42 |
+
return df
|
| 43 |
+
|
| 44 |
+
df = load_data()
|
| 45 |
+
|
| 46 |
+
# Text Preprocessing Function
|
| 47 |
+
def preprocess_text(text):
|
| 48 |
+
text = text.lower()
|
| 49 |
+
text = " ".join(word for word in text.split() if word not in stop_words)
|
| 50 |
+
return text
|
| 51 |
+
|
| 52 |
+
df["clean_text"] = df["text"].astype(str).apply(preprocess_text)
|
| 53 |
+
|
| 54 |
+
# TF-IDF Vectorization
|
| 55 |
+
vectorizer = TfidfVectorizer(max_features=2000)
|
| 56 |
+
X = vectorizer.fit_transform(df["clean_text"])
|
| 57 |
+
|
| 58 |
+
# Target variable
|
| 59 |
+
y = df["label"].values
|
| 60 |
+
|
| 61 |
+
# Split Data
|
| 62 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 63 |
+
|
| 64 |
+
# Train Models
|
| 65 |
+
models = {
|
| 66 |
+
"Logistic Regression": LogisticRegression(),
|
| 67 |
+
"Naive Bayes": MultinomialNB(),
|
| 68 |
+
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
|
| 69 |
+
"Decision Tree": DecisionTreeClassifier(random_state=42),
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
# Streamlit App UI
|
| 73 |
+
st.markdown("<h1 style='color: #FF5733;'>📰 Fake News Detection App</h1>", unsafe_allow_html=True)
|
| 74 |
+
st.markdown("<p style='color: #555;'>Select a machine learning model and enter a news article to predict if it's <i>Real or Fake</i>.</p>", unsafe_allow_html=True)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# Model Selection Dropdown
|
| 78 |
+
st.markdown("<h3 style='color: #8A2BE2; font-size: 20px;'>🔍 Choose a Machine Learning Model:</h3>", unsafe_allow_html=True)
|
| 79 |
+
selected_model = st.selectbox("", list(models.keys()))
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Train Selected Model
|
| 83 |
+
model = models[selected_model]
|
| 84 |
+
model.fit(X_train, y_train)
|
| 85 |
+
|
| 86 |
+
# Predictions
|
| 87 |
+
y_pred = model.predict(X_test)
|
| 88 |
+
|
| 89 |
+
# Classification Report
|
| 90 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 91 |
+
precision = precision_score(y_test, y_pred)
|
| 92 |
+
recall = recall_score(y_test, y_pred)
|
| 93 |
+
f1 = f1_score(y_test, y_pred)
|
| 94 |
+
|
| 95 |
+
# Display Model Performance
|
| 96 |
+
st.markdown("<h2 style='color: #3399FF;'>📊 Model Performance</h2>", unsafe_allow_html=True)
|
| 97 |
+
st.write(f"<b style='color: #4CAF50;'>Accuracy:</b> {accuracy:.4f}", unsafe_allow_html=True)
|
| 98 |
+
st.write(f"<b style='color: #FF9800;'>Precision:</b> {precision:.4f}", unsafe_allow_html=True)
|
| 99 |
+
st.write(f"<b style='color: #F44336;'>Recall:</b> {recall:.4f}", unsafe_allow_html=True)
|
| 100 |
+
st.write(f"<b style='color: #9C27B0;'>F1 Score:</b> {f1:.4f}", unsafe_allow_html=True)
|
| 101 |
+
|
| 102 |
+
# User Input
|
| 103 |
+
# Styled Text Area Label
|
| 104 |
+
st.markdown("<h3 style='color: #E91E63; font-size: 18px;'>📝 Enter News Article:</h3>", unsafe_allow_html=True)
|
| 105 |
+
|
| 106 |
+
# Text Area for User Input
|
| 107 |
+
news_input = st.text_area("", height=200)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# Function to Predict News Authenticity
|
| 111 |
+
def predict_news(article, model):
|
| 112 |
+
clean_text = preprocess_text(article)
|
| 113 |
+
text_features = vectorizer.transform([clean_text]).toarray()
|
| 114 |
+
prediction = model.predict(text_features)[0]
|
| 115 |
+
return "🟢 Real News" if prediction == 1 else "🔴 Fake News"
|
| 116 |
+
|
| 117 |
+
if st.button("Check News Authenticity"):
|
| 118 |
+
if news_input.strip() == "":
|
| 119 |
+
st.warning("⚠ Please enter a news article before clicking the button.")
|
| 120 |
+
else:
|
| 121 |
+
result = predict_news(news_input, model)
|
| 122 |
+
st.markdown("<h2 style='color: #FFD700;'>Prediction Result:</h2>", unsafe_allow_html=True)
|
| 123 |
+
st.markdown(f"<h3 style='color: {'#4CAF50' if 'Real' in result else '#F44336'};'>{result}</h3>", unsafe_allow_html=True)
|