Getting error when the message is entered

Summary

Getting nltk library error when clicked on predict button

Steps to reproduce

Code snippet:

import streamlit as st
import pickle
import string
from nltk.corpus import stopwords
import nltk
from nltk.stem.porter import PorterStemmer

ps = PorterStemmer()


def transform_text(text):
    text = text.lower()
    text = nltk.word_tokenize(text)

    y = []
    for i in text:
        if i.isalnum():
            y.append(i)

    text = y[:]
    y.clear()

    for i in text:
        if i not in stopwords.words('english') and i not in string.punctuation:
            y.append(i)

    text = y[:]
    y.clear()

    for i in text:
        y.append(ps.stem(i))

    return " ".join(y)

tfidf = pickle.load(open('vectorizer.pkl','rb'))
model = pickle.load(open('model.pkl','rb'))

st.title("Email/SMS Spam Classifier")

input_sms = st.text_area("Enter the message")

if st.button('Predict'):

    # 1. preprocess
    transformed_sms = transform_text(input_sms)
    # 2. vectorize
    vector_input = tfidf.transform([transformed_sms])
    # 3. predict
    result = model.predict(vector_input)[0]
    # 4. Display
    if result == 1:
        st.header("Spam")
    else:
        st.header("Not Spam")

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

when the code is executed a new web site is opened to enter the mail and it has to predict whether the mail is spam or not when we click on predict button

Actual behavior:

its showing undesired errors saying nltk,and something else

Debug info

  • Streamlit version: (get it with $ streamlit version)
  • Python version: (get it with $ python --3.9)
  • Using Pycharm communtiy
  • OS version:Windows 11
  • Browser version:Microsoft edge

Requirements file

1 Like

Thanks for posting, @VishnuKompelly! And welcome to our community! :hugs:

I haven’t had a chance to install and try, but it seems like you may need to download the required NLTK data files in your Python file:

nltk.download('punkt')
nltk.download('stopwords')

Would you mind trying and seeing whether it solves the issue?

Best,
Charly

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.