đ Spam detection of Tweets
This model classifies Tweets from X (formerly known as Twitter) into 'Spam' (1) or 'Quality' (0), providing an effective solution for spam detection in the Twitter environment.
đ Quick Start
This model classifies Tweets from X (formerly known as Twitter) into 'Spam' (1) or 'Quality' (0).
⨠Features
- Classify Tweets from X (formerly Twitter) into 'Spam' or 'Quality'.
- Finetuned on a specific dataset with a well - known base model.
đĻ Installation
No installation steps are provided in the original document, so this section is skipped.
đģ Usage Examples
Basic Usage
def classify_texts(df, text_col, model_path="cja5553/xlm-roberta-Twitter-spam-classification", batch_size=24):
'''
Classifies texts as either "Quality" or "Spam" using a pre-trained sequence classification model.
Parameters:
-----------
df : pandas.DataFrame
DataFrame containing the texts to classify.
text_col : str
Name of the column in that contains the text data to be classified.
model_path : str, default="cja5553/xlm-roberta-Twitter-spam-classification"
Path to the pre-trained model for sequence classification.
batch_size : int, optional, default=24
Batch size for loading and processing data in batches. Adjust based on available GPU memory.
Returns:
--------
pandas.DataFrame
The original DataFrame with an additional column `spam_prediction`, containing the predicted labels ("Quality" or "Spam") for each text.
'''
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path).to("cuda")
model.eval()
df["text"] = df[text_col].astype(str)
text_dataset = Dataset.from_pandas(df)
def tokenize_function(example):
return tokenizer(
example["text"],
padding="max_length",
truncation=True,
max_length=512
)
text_dataset = text_dataset.map(tokenize_function, batched=True)
text_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask'])
text_loader = DataLoader(text_dataset, batch_size=batch_size)
predictions = []
with torch.no_grad():
for batch in tqdm_notebook(text_loader):
input_ids = batch['input_ids'].to("cuda")
attention_mask = batch['attention_mask'].to("cuda")
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
logits = outputs.logits
preds = torch.argmax(logits, dim=-1).cpu().numpy()
predictions.extend(preds)
id2label = {0: "Quality", 1: "Spam"}
predicted_labels = [id2label[pred] for pred in predictions]
df["spam_prediction"] = predicted_labels
return df
spam_df_classification = classify_texts(df, "text_col")
print(spam_df_classification)
đ Documentation
Training Dataset
This was finetuned on the UtkMl's Twitter Spam Detection dataset with FacebookAI/xlm-roberta-large
as the base model.
Metrics
Based on a 80 - 10 - 10 train - val - test split, the following results were obtained on the test set:
- Accuracy: 0.974555
- Precision: 0.97457
- Recall: 0.97455
- F1 - Score: 0.97455
Code
Code used to train these models are available on GitHub at github.com/cja5553/Twitter_spam_detection
Questions?
Contact me at alba@wustl.edu
đ License
The model is released under the MIT license.