Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,21 +10,28 @@ 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 |
df = pd.read_csv(r"spam.csv")
|
15 |
|
16 |
-
|
17 |
st.title("Identifying Spam and Ham Emails")
|
18 |
|
|
|
19 |
x = df["Message"]
|
20 |
y = df["Category"]
|
21 |
|
|
|
22 |
bow = CountVectorizer(stop_words="english")
|
23 |
final_data = pd.DataFrame(bow.fit_transform(x).toarray(), columns=bow.get_feature_names_out())
|
24 |
|
|
|
25 |
x_train, x_test, y_train, y_test = train_test_split(final_data, y, test_size=0.2, random_state=20)
|
26 |
|
27 |
-
|
28 |
models = {
|
29 |
"Naive Bayes": MultinomialNB(),
|
30 |
"KNN": KNeighborsClassifier(),
|
@@ -33,26 +40,36 @@ models = {
|
|
33 |
"SVM": SVC()
|
34 |
}
|
35 |
|
|
|
36 |
model_choice = st.selectbox("Choose a Classification Algorithm", list(models.keys()))
|
37 |
|
38 |
-
|
39 |
obj = models[model_choice]
|
40 |
obj.fit(x_train, y_train)
|
41 |
y_pred = obj.predict(x_test)
|
42 |
accuracy = accuracy_score(y_test, y_pred)
|
43 |
|
44 |
-
|
45 |
if st.button("Show Accuracy"):
|
46 |
st.write(f"Accuracy of {model_choice}: {accuracy:.4f}")
|
47 |
|
|
|
48 |
email_input = st.text_input("Enter an Email for Prediction")
|
49 |
|
50 |
-
|
51 |
def predict_email(email):
|
52 |
data = bow.transform([email]).toarray()
|
53 |
prediction = obj.predict(data)[0]
|
54 |
st.write(f"Prediction: {prediction}")
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
@app.get("/predict/")
|
57 |
def predict_spam(email: str):
|
58 |
"""
|
@@ -61,11 +78,16 @@ def predict_spam(email: str):
|
|
61 |
"""
|
62 |
data = bow.transform([email]).toarray()
|
63 |
prediction = obj.predict(data)[0]
|
64 |
-
return JSONResponse(content={"prediction": prediction})
|
65 |
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
10 |
from sklearn.linear_model import LogisticRegression
|
11 |
from sklearn.svm import SVC
|
12 |
from sklearn.metrics import accuracy_score
|
13 |
+
from fastapi import FastAPI
|
14 |
+
from fastapi.responses import JSONResponse
|
15 |
+
import threading
|
16 |
|
17 |
+
# Read dataset
|
18 |
df = pd.read_csv(r"spam.csv")
|
19 |
|
20 |
+
# Initialize Streamlit app
|
21 |
st.title("Identifying Spam and Ham Emails")
|
22 |
|
23 |
+
# Define feature and target variables
|
24 |
x = df["Message"]
|
25 |
y = df["Category"]
|
26 |
|
27 |
+
# Create a Bag of Words (BoW) model
|
28 |
bow = CountVectorizer(stop_words="english")
|
29 |
final_data = pd.DataFrame(bow.fit_transform(x).toarray(), columns=bow.get_feature_names_out())
|
30 |
|
31 |
+
# Train-test split
|
32 |
x_train, x_test, y_train, y_test = train_test_split(final_data, y, test_size=0.2, random_state=20)
|
33 |
|
34 |
+
# Initialize models
|
35 |
models = {
|
36 |
"Naive Bayes": MultinomialNB(),
|
37 |
"KNN": KNeighborsClassifier(),
|
|
|
40 |
"SVM": SVC()
|
41 |
}
|
42 |
|
43 |
+
# Model selection
|
44 |
model_choice = st.selectbox("Choose a Classification Algorithm", list(models.keys()))
|
45 |
|
46 |
+
# Train the selected model
|
47 |
obj = models[model_choice]
|
48 |
obj.fit(x_train, y_train)
|
49 |
y_pred = obj.predict(x_test)
|
50 |
accuracy = accuracy_score(y_test, y_pred)
|
51 |
|
52 |
+
# Display accuracy
|
53 |
if st.button("Show Accuracy"):
|
54 |
st.write(f"Accuracy of {model_choice}: {accuracy:.4f}")
|
55 |
|
56 |
+
# Email input and prediction function
|
57 |
email_input = st.text_input("Enter an Email for Prediction")
|
58 |
|
|
|
59 |
def predict_email(email):
|
60 |
data = bow.transform([email]).toarray()
|
61 |
prediction = obj.predict(data)[0]
|
62 |
st.write(f"Prediction: {prediction}")
|
63 |
|
64 |
+
if st.button("Predict Email"):
|
65 |
+
if email_input:
|
66 |
+
predict_email(email_input)
|
67 |
+
else:
|
68 |
+
st.write(":red[Please enter an email to classify]")
|
69 |
+
|
70 |
+
# FastAPI app to handle GET requests
|
71 |
+
app = FastAPI()
|
72 |
+
|
73 |
@app.get("/predict/")
|
74 |
def predict_spam(email: str):
|
75 |
"""
|
|
|
78 |
"""
|
79 |
data = bow.transform([email]).toarray()
|
80 |
prediction = obj.predict(data)[0]
|
81 |
+
return JSONResponse(content={"prediction": prediction})
|
82 |
|
83 |
+
# Running FastAPI in a separate thread to work alongside Streamlit
|
84 |
+
def run_api():
|
85 |
+
import uvicorn
|
86 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
87 |
|
88 |
+
# Start FastAPI in a separate thread
|
89 |
+
api_thread = threading.Thread(target=run_api, daemon=True)
|
90 |
+
api_thread.start()
|
91 |
+
|
92 |
+
# You can also check API response using the link below:
|
93 |
+
# http://localhost:8000/predict/?email=Your_email_text_here
|