CoolFace
Apppublic

Irfee/spam-classifier

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
streamlit_app.py52 linesDownload Raw Back to src
1import altair as alt2import numpy as np3import pandas as pd4import streamlit as st5import joblib6 7# Set the title of the Streamlit app8st.title("Spam Message Classifier ๐Ÿ“ง")9st.markdown("Enter a message below to determine if it is spam or ham.")10 11# --- Load Your Saved Models ---12# The try-except block will handle errors if the files are not found.13try:14    vectorizer = joblib.load('vectorizer.pkl')15    model = joblib.load('model.pkl')16    le = joblib.load('label_encoder.pkl')17except FileNotFoundError:18    st.error("Model files not found. Please ensure 'vectorizer.pkl', 'model.pkl', and 'label_encoder.pkl' are in the same directory.")19    st.stop() # Stop the app if files can't be loaded20 21# --- Create the User Interface ---22# Create a text area for user input23user_input = st.text_area("Message Text:", placeholder="Type your message here...")24 25# Create a button to trigger the prediction26if st.button("Analyze Message"):27    if user_input:28        # The prediction pipeline starts when the button is clicked.29 30        # 1. Vectorize the user input31        # Your trained TfidfVectorizer handles lowercasing, stop words, and punctuation.32        # We pass the raw user input in a list to the .transform() method.33        vectorized_input = vectorizer.transform([user_input])34 35        # 2. Predict using the trained Naive Bayes model36        # .predict() returns an array (e.g., [1]), so we get the first item.37        prediction_encoded = model.predict(vectorized_input)[0]38 39        # 3. Decode the prediction using the LabelEncoder40        # .inverse_transform() expects a list, so we wrap the prediction in [].41        prediction_label = le.inverse_transform([prediction_encoded])[0]42 43        # 4. Display the result44        st.markdown("---")45        st.subheader("Analysis Result")46        if prediction_label == 'spam':47            st.error("๐Ÿšจ This message is likely SPAM.")48        else:49            st.success("โœ… This message seems to be HAM (not spam).")50    else:51        # Show a warning if the user clicks the button without entering text52        st.warning("Please enter a message to analyze.")