prthgo's picture
Update app.py
da76625
raw
history blame
1.05 kB
import gradio as gr
import pickle
import nltk
from nltk.corpus import stopwords
import string
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
nltk.download('punkt')
nltk.download('stopwords')
tfidf=pickle.load(open('tfidf.pkl','rb'))
model=pickle.load(open('model.pkl','rb'))
def classify_msg(Message):
X=preprocess(Message)
X_vector=tfidf.transform([X])
prediction=model.predict(X_vector)[0]
return 'Spam' if prediction==1 else 'Not Spam'
def preprocess(text):
text = text.lower()
tokens = nltk.word_tokenize(text)
text = []
for token in tokens:
if token not in stopwords.words('english') and token not in string.punctuation:
text.append(token)
return ' '.join(text)
iface = gr.Interface(
fn=classify_msg,
inputs=gr.inputs.Textbox(placeholder='If your message has more than 50 words, the probability of a correct prediction is high.'),
outputs="text",
)
if __name__ == "__main__":
iface.launch()