Update README.md
Browse files
README.md
CHANGED
|
@@ -7,7 +7,43 @@ My second project in Natural Language Processing (NLP), where I fine-tuned a ber
|
|
| 7 |
How to use this model?
|
| 8 |
|
| 9 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
model = BertForSequenceClassification.from_pretrained('fzn0x/bert-spam-classification-model')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
```
|
| 12 |
|
| 13 |
## ✅ Install requirements
|
|
|
|
| 7 |
How to use this model?
|
| 8 |
|
| 9 |
```
|
| 10 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
tokenizer = BertTokenizer.from_pretrained('fzn0x/bert-spam-classification-model')
|
| 14 |
model = BertForSequenceClassification.from_pretrained('fzn0x/bert-spam-classification-model')
|
| 15 |
+
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
model.to(device)
|
| 18 |
+
model.eval()
|
| 19 |
+
|
| 20 |
+
def model_predict(text: str):
|
| 21 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
logits = outputs.logits
|
| 25 |
+
prediction = torch.argmax(logits, dim=1).item()
|
| 26 |
+
return 'SPAM' if prediction == 1 else 'HAM'
|
| 27 |
+
|
| 28 |
+
def predict():
|
| 29 |
+
text = "Hello, do you know with this crypto you can be rich? contact us in 88888"
|
| 30 |
+
predicted_label = model_predict(text)
|
| 31 |
+
print(f"1. Predicted class: {predicted_label}") # EXPECT: SPAM
|
| 32 |
+
|
| 33 |
+
text = "Help me richard!"
|
| 34 |
+
predicted_label = model_predict(text)
|
| 35 |
+
print(f"2. Predicted class: {predicted_label}") # EXPECT: HAM
|
| 36 |
+
|
| 37 |
+
text = "You can buy loopstation for 100$, try buyloopstation.com"
|
| 38 |
+
predicted_label = model_predict(text)
|
| 39 |
+
print(f"3. Predicted class: {predicted_label}") # EXPECT: SPAM
|
| 40 |
+
|
| 41 |
+
text = "Mate, I try to contact your phone, where are you?"
|
| 42 |
+
predicted_label = model_predict(text)
|
| 43 |
+
print(f"4. Predicted class: {predicted_label}") # EXPECT: HAM
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
predict()
|
| 47 |
```
|
| 48 |
|
| 49 |
## ✅ Install requirements
|