prithivMLmods
commited on
Commit
•
e6ad0c0
1
Parent(s):
4af0eb2
Update README.md
Browse files
README.md
CHANGED
@@ -52,6 +52,68 @@ The model uses `bert-base-uncased` as the pre-trained backbone and is fine-tuned
|
|
52 |
- **Loss:** Cross-Entropy
|
53 |
|
54 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
## **🚀 How to Train the Model**
|
57 |
|
|
|
52 |
- **Loss:** Cross-Entropy
|
53 |
|
54 |
---
|
55 |
+
## Gradio Build
|
56 |
+
|
57 |
+
```python
|
58 |
+
import gradio as gr
|
59 |
+
import torch
|
60 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
61 |
+
|
62 |
+
# Load the pre-trained BERT model and tokenizer
|
63 |
+
MODEL_PATH = "prithivMLmods/Spam-Bert-Uncased"
|
64 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
|
65 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_PATH)
|
66 |
+
|
67 |
+
# Function to predict if a given text is Spam or Ham
|
68 |
+
def predict_spam(text):
|
69 |
+
# Tokenize the input text
|
70 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
71 |
+
|
72 |
+
# Perform inference
|
73 |
+
with torch.no_grad():
|
74 |
+
outputs = model(**inputs)
|
75 |
+
logits = outputs.logits
|
76 |
+
prediction = torch.argmax(logits, axis=-1).item()
|
77 |
+
|
78 |
+
# Map prediction to label
|
79 |
+
if prediction == 1:
|
80 |
+
return "Spam"
|
81 |
+
else:
|
82 |
+
return "Ham"
|
83 |
+
|
84 |
+
|
85 |
+
# Gradio UI - Input and Output components
|
86 |
+
inputs = gr.Textbox(label="Enter Text", placeholder="Type a message to check if it's Spam or Ham...")
|
87 |
+
outputs = gr.Label(label="Prediction")
|
88 |
+
|
89 |
+
# List of example inputs
|
90 |
+
examples = [
|
91 |
+
["Win $1000 gift cards now by clicking here!"],
|
92 |
+
["You have been selected for a lottery."],
|
93 |
+
["Hello, how was your day?"],
|
94 |
+
["Earn money without any effort. Click here."],
|
95 |
+
["Meeting tomorrow at 10 AM. Don't be late."],
|
96 |
+
["Claim your free prize now!"],
|
97 |
+
["Are we still on for dinner tonight?"],
|
98 |
+
["Exclusive offer just for you, act now!"],
|
99 |
+
["Let's catch up over coffee soon."],
|
100 |
+
["Congratulations, you've won a new car!"]
|
101 |
+
]
|
102 |
+
|
103 |
+
# Create the Gradio interface
|
104 |
+
gr_interface = gr.Interface(
|
105 |
+
fn=predict_spam,
|
106 |
+
inputs=inputs,
|
107 |
+
outputs=outputs,
|
108 |
+
examples=examples,
|
109 |
+
title="Spam Detection with BERT",
|
110 |
+
description="Type a message in the text box to check if it's Spam or Ham using a pre-trained BERT model."
|
111 |
+
)
|
112 |
+
|
113 |
+
# Launch the application
|
114 |
+
gr_interface.launch()
|
115 |
+
|
116 |
+
```
|
117 |
|
118 |
## **🚀 How to Train the Model**
|
119 |
|