Spaces:
Running
Running
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() | |