File size: 1,074 Bytes
8fabf6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle
from huggingface_hub import hf_hub_download

# Download files from model repo
model_path = hf_hub_download("lokas/spam-emails-classifier", "model.h5")
tokenizer_path = hf_hub_download("lokas/spam-emails-classifier", "tokenizer.pkl")

# Load model and tokenizer
model = load_model(model_path)
with open(tokenizer_path, "rb") as f:
    tokenizer = pickle.load(f)

SEQUENCE_LENGTH = 50  # Must match training

def predict_spam(text):
    seq = tokenizer.texts_to_sequences([text])
    padded = pad_sequences(seq, maxlen=SEQUENCE_LENGTH)
    pred = model.predict(padded)[0][0]
    return "🚫 Spam" if pred > 0.5 else "βœ… Not Spam"

interface = gr.Interface(
    fn=predict_spam,
    inputs=gr.Textbox(lines=3, placeholder="Paste an email message..."),
    outputs="text",
    title="Spam Email Detector",
    description="A BiLSTM-based spam classifier trained on the Enron dataset with GloVe embeddings."
)

interface.launch()