File size: 2,070 Bytes
ddc394e
 
39e420b
 
 
 
 
 
0375059
70a78ec
ddc394e
39e420b
ddc394e
0375059
 
 
39e420b
ddc394e
 
39e420b
 
 
ddc394e
70a78ec
39e420b
 
ddc394e
39e420b
 
0375059
 
70a78ec
0375059
 
39e420b
 
 
 
ddc394e
39e420b
 
 
 
 
 
 
 
ddc394e
39e420b
 
 
 
 
0375059
39e420b
 
0375059
39e420b
 
 
 
 
 
0375059
39e420b
 
 
 
 
 
 
 
 
0375059
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
67
68
69
70
71
72
73
74
75
#1. Importing Lib

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import gradio as gr

#2. Data Preprocessing

df=pd.read_csv("mail_data.csv")

df.loc[df["Category"]=="spam","Category",]=0
df.loc[df["Category"]=="ham","Category",]=1

# Spliting Data into xand y

x=df["Message"]
y=df["Category"]

#3. Modeling Part

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

# Features extractions using TfidfVectorizer
feature_extraction=TfidfVectorizer(min_df=1,stop_words="english",lowercase=True)

x_train_features = feature_extraction.fit_transform(x_train)
x_test_features  =  feature_extraction.transform(x_test)

y_train = y_train.astype("int")
y_test = y_test.astype("int")



model=LogisticRegression()
# Trains the model only at Train data features
model.fit(x_train_features,y_train)

x_predict=model.predict(x_train_features)
x_accuracy=accuracy_score(x_predict,y_train)

y_predict=model.predict(x_test_features)
y_accuracy=accuracy_score(y_predict,y_test)

#4. UI For Model

# Function to predict whether the email is spam or ham
def classify_email(email_text):
    # Transform the input email text using the same vectorizer used during training
    input_data_features = feature_extraction.transform([email_text])

    # Predict using the trained model
    prediction = model.predict(input_data_features)

    # Return the result based on the prediction
    if prediction[0] == 0:
        return "Your email is Spam"
    else:
        return "Your email is Ham"

# Create a Gradio interface for user input
interface = gr.Interface(
    fn=classify_email,  # Function to be called when user interacts
    inputs=gr.Textbox(label="Enter your email text here", placeholder="Type your email...", lines=5),
    outputs=gr.Textbox(label="Prediction"),
    live=True  # Live prediction update
)

# Launch the interface
interface.launch()