from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier from sklearn.naive_bayes import MultinomialNB from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, ConfusionMatrixDisplay from sklearn.preprocessing import LabelEncoder from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer, TfidfTransformer import joblib import gradio as gr # Load the saved model and preprocessing objects model = joblib.load('models/spam_classifier_model.joblib') cv = joblib.load('models/count_vectorizer.joblib') tfidf = joblib.load('models/tfidf_transformer.joblib') le = joblib.load('models/label_encoder.joblib') def predict_spam(message): X_new_counts = cv.transform([message]) X_new_tfidf = tfidf.transform(X_new_counts) pred = model.predict(X_new_tfidf)[0] label = le.inverse_transform([pred])[0] return f"Prediction: {label}" # Create Gradio interface iface = gr.Interface( title=gr.Markdown('# 📱💬 SMS Spam Classifier'), theme=gr.themes.Soft(), fn=predict_spam, inputs=gr.Textbox(lines=10, placeholder="Enter an SMS message..."), outputs=gr.Textbox(lines=10), description="# 📱💬 SMS Spam Classifier\n\nEnter an SMS message to classify it as 'spam' or 'ham' using the best model.", 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.', 'Hi, how are you?'] ) iface.launch()