Views
No views yet
mail_spam_ham_dataset and 'spam-mail'pip install transformers torch1from transformers import BertTokenizer, BertForSequenceClassification
2import torch
3
4model_name = "AventIQ-AI/bert-spam-detection"
5tokenizer = BertTokenizer.from_pretrained(model_name)
6model = BertForSequenceClassification.from_pretrained(model_name)
7
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
10def predict_spam_quantized(text):
11 """Predicts whether a given text is spam (1) or ham (0) using the quantized BERT model."""
12
13 # Tokenize input text
14 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
15
16 # Move inputs to GPU (if available)
17 inputs = {key: value.to(device) for key, value in inputs.items()}
18
19 # Perform inference
20 with torch.no_grad():
21 outputs = model(**inputs)
22
23 # Get predicted label (0 = ham, 1 = spam)
24 prediction = torch.argmax(outputs.logits, dim=1).item()
25
26 return "Spam" if prediction == 1 else "Ham"
27
28
29# Sample test messages
30print(predict_spam_quantized("WINNER!! As a valued network customer you have been selected to receivea £900 prize reward! To claim call 09061701461. Claim code KL341. Valid 12 hours only."))
31# Expected output: Spam
32
33print(predict_spam_quantized("WINNER!! As a valued network customer you have been selected to receivea £900 prize reward! To claim call 09061701461. Claim code KL341. Valid 12 hours only."))
34# Expected output: Ham| Metric | Class 0 (Non-Spam) | Class 1 (Spam) | Macro Avg | Weighted Avg |
|---|---|---|---|---|
| Precision | 1.00 | 0.98 | 0.99 | 0.99 |
| Recall | 0.99 | 0.99 | 0.99 | 0.99 |
| F1-Score | 0.99 | 0.99 | 0.99 | 0.99 |
| Accuracy | 99% | 99% | 99% | 99% |
.
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── model.safetensors/ # Fine Tuned Model
├── README.md # Model documentation