Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,65 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import sklearn
|
| 5 |
-
from sklearn.model_selection import train_test_split
|
| 6 |
-
from sklearn.feature_extraction.text import CountVectorizer
|
| 7 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 8 |
-
from sklearn.naive_bayes import MultinomialNB
|
| 9 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
-
from sklearn.linear_model import LogisticRegression
|
| 11 |
-
from sklearn.svm import SVC
|
| 12 |
-
from sklearn.metrics import accuracy_score
|
| 13 |
-
|
| 14 |
-
# Load Dataset
|
| 15 |
-
df = pd.read_csv("spam.csv")
|
| 16 |
-
|
| 17 |
-
# Title
|
| 18 |
-
st.title(":blue[Spam and Ham Detection]")
|
| 19 |
-
|
| 20 |
-
# Preparing Data
|
| 21 |
-
x = df["Message"]
|
| 22 |
-
y = df["Category"]
|
| 23 |
-
|
| 24 |
-
bow = CountVectorizer(stop_words="english")
|
| 25 |
-
final_data = pd.DataFrame(bow.fit_transform(x).toarray(), columns=bow.get_feature_names_out())
|
| 26 |
-
|
| 27 |
-
x_train, x_test, y_train, y_test = train_test_split(final_data, y, test_size=0.2, random_state=20)
|
| 28 |
-
|
| 29 |
-
# Available Models
|
| 30 |
-
models = {
|
| 31 |
-
"Naive Bayes": MultinomialNB(),
|
| 32 |
-
"KNN": KNeighborsClassifier(),
|
| 33 |
-
"Decision Tree": DecisionTreeClassifier(),
|
| 34 |
-
"Logistic Regression": LogisticRegression(),
|
| 35 |
-
"SVM": SVC()
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
# Select Model
|
| 39 |
-
model_choice = st.radio("Choose a Classification Algorithm", list(models.keys()))
|
| 40 |
-
|
| 41 |
-
# Train and Evaluate Model
|
| 42 |
-
obj = models[model_choice]
|
| 43 |
-
obj.fit(x_train, y_train)
|
| 44 |
-
y_pred = obj.predict(x_test)
|
| 45 |
-
accuracy = accuracy_score(y_test, y_pred)*100
|
| 46 |
-
|
| 47 |
-
# Show Accuracy when button is clicked
|
| 48 |
-
if st.button("Show Accuracy"):
|
| 49 |
-
st.write(f"Accuracy of {model_choice}: {accuracy:.4f}")
|
| 50 |
-
|
| 51 |
-
# Input Field for Email
|
| 52 |
-
email_input = st.text_input("enter email")
|
| 53 |
-
|
| 54 |
-
# Prediction Function
|
| 55 |
-
def predict_email(email):
|
| 56 |
-
data = bow.transform([email]).toarray() # Convert sparse to dense
|
| 57 |
-
prediction = obj.predict(data)[0]
|
| 58 |
-
st.write(f"Prediction: {prediction}")
|
| 59 |
-
|
| 60 |
-
# Predict Button
|
| 61 |
-
if st.button("Predict Email"):
|
| 62 |
-
if email_input:
|
| 63 |
-
predict_email(email_input)
|
| 64 |
-
else:
|
| 65 |
st.write(":red[enter mail]")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import sklearn
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 7 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 8 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 9 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
+
from sklearn.linear_model import LogisticRegression
|
| 11 |
+
from sklearn.svm import SVC
|
| 12 |
+
from sklearn.metrics import accuracy_score
|
| 13 |
+
|
| 14 |
+
# Load Dataset
|
| 15 |
+
df = pd.read_csv("spam.csv")
|
| 16 |
+
|
| 17 |
+
# Title
|
| 18 |
+
st.title(":blue[Spam and Ham Detection]")
|
| 19 |
+
|
| 20 |
+
# Preparing Data
|
| 21 |
+
x = df["Message"]
|
| 22 |
+
y = df["Category"]
|
| 23 |
+
|
| 24 |
+
bow = CountVectorizer(stop_words="english")
|
| 25 |
+
final_data = pd.DataFrame(bow.fit_transform(x).toarray(), columns=bow.get_feature_names_out())
|
| 26 |
+
|
| 27 |
+
x_train, x_test, y_train, y_test = train_test_split(final_data, y, test_size=0.2, random_state=20)
|
| 28 |
+
|
| 29 |
+
# Available Models
|
| 30 |
+
models = {
|
| 31 |
+
"Naive Bayes": MultinomialNB(),
|
| 32 |
+
"KNN": KNeighborsClassifier(),
|
| 33 |
+
"Decision Tree": DecisionTreeClassifier(),
|
| 34 |
+
"Logistic Regression": LogisticRegression(),
|
| 35 |
+
"SVM": SVC()
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Select Model
|
| 39 |
+
model_choice = st.radio("Choose a Classification Algorithm", list(models.keys()))
|
| 40 |
+
|
| 41 |
+
# Train and Evaluate Model
|
| 42 |
+
obj = models[model_choice]
|
| 43 |
+
obj.fit(x_train, y_train)
|
| 44 |
+
y_pred = obj.predict(x_test)
|
| 45 |
+
accuracy = accuracy_score(y_test, y_pred)*100
|
| 46 |
+
|
| 47 |
+
# Show Accuracy when button is clicked
|
| 48 |
+
if st.button("Show Accuracy"):
|
| 49 |
+
st.write(f"Accuracy of {model_choice}: {accuracy:.4f}")
|
| 50 |
+
|
| 51 |
+
# Input Field for Email
|
| 52 |
+
email_input = st.text_input("enter email")
|
| 53 |
+
|
| 54 |
+
# Prediction Function
|
| 55 |
+
def predict_email(email):
|
| 56 |
+
data = bow.transform([email]).toarray() # Convert sparse to dense
|
| 57 |
+
prediction = obj.predict(data)[0]
|
| 58 |
+
st.write(f"Prediction: {prediction}")
|
| 59 |
+
|
| 60 |
+
# Predict Button
|
| 61 |
+
if st.button("Predict Email"):
|
| 62 |
+
if email_input:
|
| 63 |
+
predict_email(email_input)
|
| 64 |
+
else:
|
| 65 |
st.write(":red[enter mail]")
|