File size: 1,346 Bytes
18fb410
887180a
7300197
9cedfb2
 
 
af7387d
18fb410
552f284
db085d0
552f284
 
5caffca
5f9e365
18fb410
5f9e365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
887180a
 
5f9e365
 
 
 
 
 
 
 
 
 
 
 
 
 
af7387d
5f9e365
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
import pickle
import string
from nltk.corpus import stopwords
import nltk
from nltk.stem.porter import PorterStemmer
import sklearn

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


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'))


def predict_spam(input_sms):
    # 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 result
    return "Spam" if result == 1 else "Not Spam"
    
title = "Email/SMS Spam Classifier"

inputs = gr.Text("Enter the message")
outputs = gr.Textbox(label='Results',lines = 20)
interface = gr.Interface(fn=predict_spam, inputs=inputs, outputs=outputs,title=title)
interface.launch(share=True)