Views
No views yet
sms_spampip install transformers torch1from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
2import torch
3
4model_name = "AventIQ-AI/distilbert-spam-detection"
5tokenizer = DistilBertTokenizer.from_pretrained(model_name)
6model = DistilBertForSequenceClassification.from_pretrained(model_name)
7
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
10def predict_spam(text, model, tokenizer, device):
11 model.eval() # Set to evaluation mode
12 inputs = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=128).to(device)
13
14 with torch.no_grad():
15 outputs = model(**inputs)
16 probs = torch.softmax(outputs.logits, dim=-1)
17 pred_class = torch.argmax(probs).item()
18 return "Spam" if pred_class == 1 else "Not Spam"
19
20# Sample test messages
21test_messages = [
22 "Congratulations! You have won a lottery of $1,000,000. Claim now!", # Spam
23 "Hey, are we still meeting for dinner tonight?", # Not Spam
24 "URGENT: Your bank account is at risk! Click this link to secure it now.", # Spam
25 "Let's catch up this weekend. It’s been a while!", # Not Spam
26 "Exclusive offer! Get 50% off on your next purchase. Limited time only!", # Spam
27]
28
29# Run inference on test messages
30for i, msg in enumerate(test_messages):
31 prediction = predict_spam(msg, model, tokenizer, device)
32 print(f"Sample {i+1}: {msg} -> Prediction: {prediction}")| 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% |
sms_spam dataset was used, containing both spam and ham (non-spam) examples..
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── pytorch_model.bin/ # Fine Tuned Model
├── README.md # Model documentation