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