Spaces:
Sleeping
Sleeping
usmanabbasi
commited on
fake app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Module 1: Import necessary packages
|
2 |
+
import streamlit as st
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
6 |
+
from sklearn.svm import LinearSVC
|
7 |
+
from sklearn.naive_bayes import MultinomialNB
|
8 |
+
import warnings
|
9 |
+
import streamlit_lottie
|
10 |
+
|
11 |
+
warnings.filterwarnings("ignore")
|
12 |
+
|
13 |
+
# Set page configuration (must be the first Streamlit command)
|
14 |
+
page_icon = ":metro:" # emojis: https://www.webfx.com/tools/emoji-cheat-sheet/
|
15 |
+
layout = "wide"
|
16 |
+
page_title = "Fake News Detection"
|
17 |
+
st.set_page_config(page_title=page_title, page_icon=page_icon, layout=layout)
|
18 |
+
|
19 |
+
# Module 2: Load the dataset
|
20 |
+
@st.cache_data
|
21 |
+
def load_data():
|
22 |
+
data = pd.read_csv("fake_or_real_news.csv")
|
23 |
+
data['fake'] = data['label'].apply(lambda x: 0 if x == 'REAL' else 1)
|
24 |
+
return data
|
25 |
+
|
26 |
+
# Module 3: Select Vectorizer and Classifier
|
27 |
+
def select_model():
|
28 |
+
vectorizer_type = st.sidebar.selectbox("Select Vectorizer", ["TF-IDF", "Bag of Words"])
|
29 |
+
classifier_type = st.sidebar.selectbox("Select Classifier", ["Linear SVM", "Naive Bayes"])
|
30 |
+
|
31 |
+
vectorizer = None
|
32 |
+
if vectorizer_type == "TF-IDF":
|
33 |
+
vectorizer = TfidfVectorizer(stop_words='english', max_df=0.7)
|
34 |
+
elif vectorizer_type == "Bag of Words":
|
35 |
+
vectorizer = CountVectorizer(stop_words='english', max_df=0.7)
|
36 |
+
|
37 |
+
classifier = None
|
38 |
+
if classifier_type == "Linear SVM":
|
39 |
+
classifier = LinearSVC()
|
40 |
+
elif classifier_type == "Naive Bayes":
|
41 |
+
classifier = MultinomialNB()
|
42 |
+
|
43 |
+
return vectorizer, classifier
|
44 |
+
|
45 |
+
# Module 4: Train the model (no caching here)
|
46 |
+
def train_model(data, vectorizer, classifier):
|
47 |
+
x_vectorized = vectorizer.fit_transform(data['text'])
|
48 |
+
clf = classifier.fit(x_vectorized, data['fake'])
|
49 |
+
return clf
|
50 |
+
|
51 |
+
# Module 5: Streamlit app
|
52 |
+
def main():
|
53 |
+
# Streamlit app
|
54 |
+
st.title(page_title + " " + page_icon)
|
55 |
+
st.lottie("https://lottie.host/bd0c4818-c5a6-4e42-b407-746bc448c2c7/ipVUdgFncO.json", width=200, height=200)
|
56 |
+
|
57 |
+
# --- HIDE STREAMLIT STYLE ---
|
58 |
+
hide_st_style = """
|
59 |
+
<style>
|
60 |
+
#MainMenu {visibility: hidden;}
|
61 |
+
footer {visibility: hidden;}
|
62 |
+
header {visibility: hidden;}
|
63 |
+
</style>
|
64 |
+
"""
|
65 |
+
st.markdown(hide_st_style, unsafe_allow_html=True)
|
66 |
+
|
67 |
+
# Load data
|
68 |
+
data = load_data()
|
69 |
+
|
70 |
+
# Select vectorizer and classifier
|
71 |
+
vectorizer, classifier = select_model()
|
72 |
+
|
73 |
+
# Text input for user to input news article
|
74 |
+
user_input = st.text_area("Enter your news article here:")
|
75 |
+
|
76 |
+
# When user submits the input
|
77 |
+
if st.button("Check"):
|
78 |
+
# Train the model
|
79 |
+
clf = train_model(data, vectorizer, classifier)
|
80 |
+
|
81 |
+
# Vectorize the user input
|
82 |
+
input_vectorized = vectorizer.transform([user_input])
|
83 |
+
|
84 |
+
# Predict the label of the input
|
85 |
+
prediction = clf.predict(input_vectorized)
|
86 |
+
|
87 |
+
# Convert prediction to integer for interpretation
|
88 |
+
result = int(prediction[0])
|
89 |
+
|
90 |
+
# Display the result
|
91 |
+
if result == 1:
|
92 |
+
st.error("This news article is likely fake!")
|
93 |
+
else:
|
94 |
+
st.success("This news article seems to be real.")
|
95 |
+
|
96 |
+
# Run the Streamlit app
|
97 |
+
if __name__ == "__main__":
|
98 |
+
main()
|
99 |
+
|
100 |
+
st.markdown("**Created with enthusiasm by SuperSam**")
|