CoolFace
Apppublic

priyeraj/spam_classifier

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py77 linesDownload Raw Back to root
1import gradio as gr2import pickle3import string4import nltk5from nltk.corpus import stopwords6from nltk.stem.porter import PorterStemmer7 8# -----------------------------9# Ensure NLTK resources10# -----------------------------11for pkg in ("punkt", "punkt_tab", "stopwords"):12    try:13        if pkg.startswith("punkt"):14            nltk.data.find("tokenizers/" + pkg)15        else:16            nltk.data.find("corpora/" + pkg)17    except LookupError:18        nltk.download(pkg)19 20# -----------------------------21# Preprocessing (same as notebook)22# -----------------------------23ps = PorterStemmer()24 25def transform_text(text):26    text = text.lower()27    text = nltk.word_tokenize(text)28 29    y = []30    for i in text:31        if i.isalnum():32            y.append(i)33    text = y[:]34    y.clear()35 36    for i in text:37        if i not in stopwords.words("english") and i not in string.punctuation:38            y.append(i)39    text = y[:]40    y.clear()41 42    for i in text:43        y.append(ps.stem(i))44 45    return " ".join(y)46 47# -----------------------------48# Load trained artifacts49# -----------------------------50tfidf = pickle.load(open("vectorizer.pkl", "rb"))51model = pickle.load(open("model.pkl", "rb"))52 53# -----------------------------54# Prediction function55# -----------------------------56def predict_spam(message):57    transformed = transform_text(message)58    vector_input = tfidf.transform([transformed])59    result = model.predict(vector_input)[0]60    if result == 1:61        return "๐Ÿšจ Spam"62    else:63        return "โœ… Not Spam"64 65# -----------------------------66# Gradio Interface67# -----------------------------68iface = gr.Interface(69    fn=predict_spam,70    inputs=gr.Textbox(lines=3, placeholder="Enter SMS or Email text here..."),71    outputs="text",72    title="๐Ÿ“ฉ Email / SMS Spam Classifier",73)74 75if __name__ == "__main__":76    iface.launch()77