File size: 2,008 Bytes
b48b3fd
 
 
 
 
 
 
c3db3b9
b48b3fd
c3db3b9
b48b3fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3db3b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
import pandas as pd
import gradio as gr
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

# Load and clean the dataset
data = pd.read_csv("spam.csv")
data.drop_duplicates(inplace=True)
data['Category'] = data['Category'].replace(['ham', 'spam'], ['Not spam', 'Spam'])

# Prepare data
X = data['Message']
y = data['Category']

# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Convert text data to numerical features
vectorizer = CountVectorizer(stop_words='english')
X_train_features = vectorizer.fit_transform(X_train)
X_test_features = vectorizer.transform(X_test)

# Train the model
model = MultinomialNB()
model.fit(X_train_features, y_train)

# Define prediction function
def predict_spam(message):
    message_features = vectorizer.transform([message])
    prediction = model.predict(message_features)[0]
    return prediction

# Build better UI
with gr.Blocks(theme=gr.themes.Default()) as demo:
    gr.Markdown("## πŸ“© Spam Detector\nEnter any message below to check if it's spam or not.")
    
    with gr.Row():
        with gr.Column(scale=3):
            message_input = gr.Textbox(
                label="Your Message",
                placeholder="e.g. Congratulations! You've won a prize...",
                lines=4
            )
            submit_btn = gr.Button("Detect Spam")
        with gr.Column(scale=2):
            result_output = gr.Label(label="Prediction")
    
    examples = [
        ["Congratulations! You have been selected for a free cruise!"],
        ["Hey, what time is class tomorrow?"],
        ["Win cash now!!! Click here"],
        ["Lunch at 1 PM?"],
    ]
    
    gr.Examples(
        examples=examples,
        inputs=message_input
    )
    
    submit_btn.click(fn=predict_spam, inputs=message_input, outputs=result_output)

# Launch app
if __name__ == "__main__":
    demo.launch()