File size: 1,845 Bytes
92eb32f
 
 
 
 
6a450c0
92eb32f
 
 
a552c23
52e9136
a552c23
52e9136
 
6a450c0
 
 
 
a552c23
92eb32f
 
a552c23
92eb32f
 
6a450c0
a552c23
92eb32f
 
 
 
 
 
 
 
 
 
 
6a450c0
92eb32f
6a450c0
 
 
 
21d7262
92eb32f
 
 
 
 
 
 
11d3eb0
92eb32f
 
 
52e9136
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
import gradio as gr
import torch
import torch.nn as nn
import joblib

# ---------- Model Definition (Fixed Architecture) ----------
class SpamClassifier(nn.Module):
    def __init__(self, input_dim):
        super(SpamClassifier, self).__init__()
        self.model = nn.Sequential(
            nn.Linear(input_dim, 64),
            nn.ReLU(),
            nn.Linear(64, 32),
            nn.ReLU(),
            # CHANGED: Output layer is 1, not 2
            nn.Linear(32, 1),
            # CHANGED: Use Sigmoid for binary output (0 to 1) instead of Softmax
            nn.Sigmoid()
        )

    def forward(self, x):
        return self.model(x)

# ---------- Load Vectorizer ----------
# Ensure you have scikit-learn==1.2.2 in requirements.txt if you get warnings
vectorizer = joblib.load("model/vectorizer.pkl")
input_dim = len(vectorizer.get_feature_names_out())

# ---------- Load Model ----------
model = SpamClassifier(input_dim)
model.load_state_dict(torch.load("model/email_spam_classifier.pth", map_location=torch.device("cpu")))
model.eval()

# ---------- Prediction Function ----------
def predict_email(text):
    X = vectorizer.transform([text]).toarray()
    X_tensor = torch.tensor(X, dtype=torch.float32)
    
    with torch.no_grad():
        # Get the single probability value (0 = Ham, 1 = Spam)
        prob_spam = model(X_tensor).item()
        
    # Manually calculate Ham probability as the complement of Spam
    return {"Safe": 1 - prob_spam, "Spam": prob_spam}

# ---------- Gradio Interface ----------
iface = gr.Interface(
    fn=predict_email,
    inputs=gr.Textbox(lines=5, placeholder="Paste your email here..."),
    outputs=gr.Label(num_top_classes=2),
    title="Email Spam Classifier",
    description="Classify emails as Spam or Safe using a PyTorch model."
)

if __name__ == "__main__":
    iface.launch()