Views
No views yet
| Metric | Mean ± Std | Range |
|---|---|---|
| Accuracy | 0.9433 ± 0.0052 | 0.9385 - 0.9497 |
| F1-Score (Weighted) | 0.9434 ± 0.0051 | 0.9387 - 0.9497 |
| Precision (Weighted) | 0.9444 ± 0.0045 | 0.9397 - 0.9498 |
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3import re
4
5# Load model and tokenizer
6model_name = "junaid1993/distilroberta-bot-detection"
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
10def preprocess_text(text):
11 """Clean text for bot detection"""
12 if not isinstance(text, str):
13 return ""
14
15 # Remove URLs
16 text = re.sub(r'http\S+|www\.\S+', '', text)
17 # Remove @ and # symbols
18 text = re.sub(r'[@#]', '', text)
19 # Remove punctuation and special characters
20 text = re.sub(r'[^\w\s]', '', text)
21 # Remove numbers
22 text = re.sub(r'\d+', '', text)
23 # Clean whitespace
24 text = re.sub(r'\s+', ' ', text).strip()
25
26 return text.lower()
27
28def predict_bot(text, threshold=0.5):
29 """Predict if text is bot-generated"""
30 clean_text = preprocess_text(text)
31
32 if not clean_text:
33 return {"prediction": "unknown", "confidence": 0.5}
34
35 inputs = tokenizer(
36 clean_text,
37 return_tensors="pt",
38 truncation=True,
39 padding=True,
40 max_length=512
41 )
42
43 with torch.no_grad():
44 outputs = model(**inputs)
45 probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
46
47 bot_prob = probabilities[0][1].item()
48 prediction = "bot" if bot_prob > threshold else "human"
49
50 return {
51 "prediction": prediction,
52 "bot_probability": round(bot_prob, 4),
53 "human_probability": round(probabilities[0][0].item(), 4)
54 }
55
56# Example usage
57text = "🔥 AMAZING DEAL! Click here now!"
58result = predict_bot(text)
59print(f"Prediction: {result['prediction']} (Bot: {result['bot_probability']})")1@model{distilroberta-bot-detection-2024,
2 title={Bot Detection Model using DistilRoBERTa},
3 author={Junaid Ahmed and Dariusz Jemielniak and Leon Ciechanowski},
4 year={2025},
5 publisher={Hugging Face},
6 url={https://huggingface.co/junaid1993/distilroberta-bot-detection}
7}