Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
4 |
+
|
5 |
+
MODEL_PATH = "AntiSpamInstitute/spam-detector-bert-MoE-v2.2"
|
6 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
|
7 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_PATH)
|
8 |
+
|
9 |
+
# Function to predict if a given text is Spam or Ham
|
10 |
+
def predict_spam(text):
|
11 |
+
# Tokenize the input text
|
12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
13 |
+
|
14 |
+
# Perform inference
|
15 |
+
with torch.no_grad():
|
16 |
+
outputs = model(**inputs)
|
17 |
+
logits = outputs.logits
|
18 |
+
prediction = torch.argmax(logits, axis=-1).item()
|
19 |
+
|
20 |
+
# Map prediction to label
|
21 |
+
if prediction == 1:
|
22 |
+
return "Spam"
|
23 |
+
else:
|
24 |
+
return "Ham"
|
25 |
+
|
26 |
+
|
27 |
+
# Gradio UI - Input and Output components
|
28 |
+
inputs = gr.Textbox(label="Enter Text", placeholder="Type a message to check if it's Spam or Ham...")
|
29 |
+
outputs = gr.Label(label="Prediction")
|
30 |
+
|
31 |
+
# List of example inputs
|
32 |
+
examples = [
|
33 |
+
["Win $1000 gift cards now by clicking here!"],
|
34 |
+
["Let's catch up over coffee soon."]
|
35 |
+
|
36 |
+
]
|
37 |
+
|
38 |
+
gr_interface = gr.Interface(
|
39 |
+
fn=predict_spam,
|
40 |
+
inputs=inputs,
|
41 |
+
outputs=outputs,
|
42 |
+
examples=examples,
|
43 |
+
title="Spam Detection with BERT",
|
44 |
+
description="Type a message in the text box to check if it's Spam or Ham using a pre-trained BERT model."
|
45 |
+
)
|
46 |
+
|
47 |
+
gr_interface.launch()
|