Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +42 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.linear_model import LogisticRegression
|
| 2 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 3 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 4 |
+
|
| 5 |
+
from sklearn.model_selection import train_test_split, GridSearchCV
|
| 6 |
+
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, ConfusionMatrixDisplay
|
| 7 |
+
from sklearn.preprocessing import LabelEncoder
|
| 8 |
+
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer, TfidfTransformer
|
| 9 |
+
|
| 10 |
+
import joblib
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Load the saved model and preprocessing objects
|
| 18 |
+
model = joblib.load('models/spam_classifier_model.joblib')
|
| 19 |
+
cv = joblib.load('models/count_vectorizer.joblib')
|
| 20 |
+
tfidf = joblib.load('models/tfidf_transformer.joblib')
|
| 21 |
+
le = joblib.load('models/label_encoder.joblib')
|
| 22 |
+
|
| 23 |
+
def predict_spam(message):
|
| 24 |
+
X_new_counts = cv.transform([message])
|
| 25 |
+
X_new_tfidf = tfidf.transform(X_new_counts)
|
| 26 |
+
pred = model.predict(X_new_tfidf)[0]
|
| 27 |
+
label = le.inverse_transform([pred])[0]
|
| 28 |
+
return f"Prediction: {label}"
|
| 29 |
+
|
| 30 |
+
# Create Gradio interface
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
title=gr.Markdown('# 📱💬 SMS Spam Classifier'),
|
| 33 |
+
theme=gr.themes.Soft(),
|
| 34 |
+
fn=predict_spam,
|
| 35 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter an SMS message..."),
|
| 36 |
+
outputs=gr.Textbox(lines=10),
|
| 37 |
+
description="# 📱💬 SMS Spam Classifier\n\nEnter an SMS message to classify it as 'spam' or 'ham' using the best model.",
|
| 38 |
+
examples=['Congratulations! You have won a $1,000 Walmart gift card. Go to http://bit.ly/123456 to claim your prize now. Reply STOP to opt out.',
|
| 39 |
+
'Hi, how are you?']
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
joblib
|
| 2 |
+
scikit-learn
|
| 3 |
+
gradio
|
| 4 |
+
pandas
|