🚀 推文垃圾邮件检测模型
本模型用于将来自X(原Twitter)的推文分类为“垃圾邮件”(1)或“优质内容”(0),有效助力用户识别推文质量,提升信息获取效率。
🚀 快速开始
本模型可将来自X(原Twitter)的推文分类为“垃圾邮件”(1)或“优质内容”(0)。
✨ 主要特性
- 基于
FacebookAI/xlm - roberta - large
基础模型,具有强大的特征提取能力。
- 针对推文垃圾邮件检测进行了微调,能有效区分垃圾推文和优质推文。
📦 安装指南
文档未提及具体安装步骤,故跳过此章节。
💻 使用示例
基础用法
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)
高级用法
文档未提及高级用法相关代码,故不展示此部分。
📚 详细文档
训练数据集
该模型在UtkMl的Twitter垃圾邮件检测数据集上进行了微调,使用FacebookAI/xlm - roberta - large
作为基础模型。
指标
基于80 - 10 - 10的训练 - 验证 - 测试集划分,在测试集上取得了以下结果:
属性 |
详情 |
准确率 |
0.974555 |
精确率 |
0.97457 |
召回率 |
0.97455 |
F1分数 |
0.97455 |
代码
用于训练这些模型的代码可在GitHub上获取:github.com/cja5553/Twitter_spam_detection
问题咨询
如有问题,请联系:alba@wustl.edu
📄 许可证
本项目采用MIT许可证。