import gradio as gr import torch from transformers import BertTokenizer, BertForSequenceClassification MODEL_PATH = "AntiSpamInstitute/spam-detector-bert-MoE-v2.2" tokenizer = BertTokenizer.from_pretrained(MODEL_PATH) model = BertForSequenceClassification.from_pretrained(MODEL_PATH) # Function to predict if a given text is Spam or Ham def predict_spam(text): # Tokenize the input text inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512) # Perform inference with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits prediction = torch.argmax(logits, axis=-1).item() # Map prediction to label if prediction == 1: return "Spam" else: return "Ham" # Gradio UI - Input and Output components inputs = gr.Textbox(label="Enter Text", placeholder="Type a message to check if it's Spam or Ham...") outputs = gr.Label(label="Prediction") # List of example inputs examples = [ ["Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's"], ["Yeah he got in at 2 and was v apologetic. n had fallen out and she was actin like spoilt child and he got caught up in that. Till 2! But we won't go there! Not doing too badly cheers. You?"] ] gr_interface = gr.Interface( fn=predict_spam, inputs=inputs, outputs=outputs, examples=examples, title="Spam Detection with BERT", description="Type a message in the text box to check if it's Spam or Ham using a pre-trained BERT model." ) gr_interface.launch()