DasariHarshitha commited on
Commit
5213f58
·
verified ·
1 Parent(s): 9b62406

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -116
app.py CHANGED
@@ -1,116 +1,123 @@
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)
 
 
 
 
 
 
 
 
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)