Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,51 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
# ---------- Model Definition ----------
|
| 7 |
-
class SpamClassifier(nn.Module):
|
| 8 |
-
def __init__(self, input_dim):
|
| 9 |
-
super(SpamClassifier, self).__init__()
|
| 10 |
-
self.fc1 = nn.Linear(input_dim, 128)
|
| 11 |
-
self.relu = nn.ReLU()
|
| 12 |
-
self.fc2 = nn.Linear(128, 2)
|
| 13 |
-
self.softmax = nn.Softmax(dim=1)
|
| 14 |
-
|
| 15 |
-
def forward(self, x):
|
| 16 |
-
x = self.fc1(x)
|
| 17 |
-
x = self.relu(x)
|
| 18 |
-
x = self.fc2(x)
|
| 19 |
-
x = self.softmax(x)
|
| 20 |
-
return x
|
| 21 |
-
|
| 22 |
-
# ---------- Load Vectorizer ----------
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
model =
|
| 30 |
-
model.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
labels
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import joblib
|
| 5 |
+
|
| 6 |
+
# ---------- Model Definition ----------
|
| 7 |
+
class SpamClassifier(nn.Module):
|
| 8 |
+
def __init__(self, input_dim):
|
| 9 |
+
super(SpamClassifier, self).__init__()
|
| 10 |
+
self.fc1 = nn.Linear(input_dim, 128)
|
| 11 |
+
self.relu = nn.ReLU()
|
| 12 |
+
self.fc2 = nn.Linear(128, 2)
|
| 13 |
+
self.softmax = nn.Softmax(dim=1)
|
| 14 |
+
|
| 15 |
+
def forward(self, x):
|
| 16 |
+
x = self.fc1(x)
|
| 17 |
+
x = self.relu(x)
|
| 18 |
+
x = self.fc2(x)
|
| 19 |
+
x = self.softmax(x)
|
| 20 |
+
return x
|
| 21 |
+
|
| 22 |
+
# ---------- Load Vectorizer ----------
|
| 23 |
+
vectorizer = joblib.load("model/vectorizer.pkl") # Use joblib for TF-IDF
|
| 24 |
+
|
| 25 |
+
input_dim = len(vectorizer.get_feature_names_out())
|
| 26 |
+
|
| 27 |
+
# ---------- Load Model ----------
|
| 28 |
+
model = SpamClassifier(input_dim)
|
| 29 |
+
model.load_state_dict(torch.load("model/email_spam_classifier.pth", map_location=torch.device("cpu")))
|
| 30 |
+
model.eval()
|
| 31 |
+
|
| 32 |
+
# ---------- Prediction Function ----------
|
| 33 |
+
def predict_email(text):
|
| 34 |
+
X = vectorizer.transform([text]).toarray()
|
| 35 |
+
X_tensor = torch.tensor(X, dtype=torch.float32)
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
probs = model(X_tensor).numpy()[0]
|
| 38 |
+
labels = ["Ham", "Spam"]
|
| 39 |
+
return {labels[i]: float(probs[i]) for i in range(2)}
|
| 40 |
+
|
| 41 |
+
# ---------- Gradio Interface ----------
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=predict_email,
|
| 44 |
+
inputs=gr.Textbox(lines=5, placeholder="Paste your email here..."),
|
| 45 |
+
outputs=gr.Label(num_top_classes=2),
|
| 46 |
+
title="Email Spam Classifier",
|
| 47 |
+
description="Classify emails as Spam or Ham using a PyTorch model."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
iface.launch()
|
|
|