🚀 ModerationBERT-ML-En
ModerationBERT-ML-En 是一個基於 bert-base-multilingual-cased
的審核模型。該模型旨在執行文本審核任務,具體來說,是將文本分類為 18 個不同的類別。目前,它僅支持處理英文文本。
🚀 快速開始
安裝指南
若要使用 ModerationBERT-ML-En,你需要從 Hugging Face 安裝 transformers
庫以及 torch
。
pip install transformers torch
使用示例
以下是一個使用 ModerationBERT-ML-En 對給定文本進行審核類別預測的示例:
import json
import torch
from transformers import BertTokenizer, BertForSequenceClassification
model_name = "ifmain/ModerationBERT-En-02"
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=18)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
def predict(text, model, tokenizer):
encoding = tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=128,
return_token_type_ids=False,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt'
)
input_ids = encoding['input_ids'].to(device)
attention_mask = encoding['attention_mask'].to(device)
model.eval()
with torch.no_grad():
outputs = model(input_ids, attention_mask=attention_mask)
predictions = torch.sigmoid(outputs.logits)
return predictions
new_text = "Fuck off stuped trash"
predictions = predict(new_text, model, tokenizer)
categories = ['harassment', 'harassment_threatening', 'hate', 'hate_threatening',
'self_harm', 'self_harm_instructions', 'self_harm_intent', 'sexual',
'sexual_minors', 'violence', 'violence_graphic', 'self-harm',
'sexual/minors', 'hate/threatening', 'violence/graphic',
'self-harm/intent', 'self-harm/instructions', 'harassment/threatening']
category_scores = {categories[i]: predictions[0][i].item() for i in range(len(categories))}
output = {
"text": new_text,
"category_scores": category_scores
}
print(json.dumps(output, indent=4, ensure_ascii=False))
輸出結果如下:
{
"text": "Fuck off stuped trash",
"category_scores": {
"harassment": 0.9272650480270386,
"harassment_threatening": 0.0013139015063643456,
"hate": 0.011709265410900116,
"hate_threatening": 1.1083522622357123e-05,
"self_harm": 0.00039102151640690863,
"self_harm_instructions": 0.0002464024000801146,
"self_harm_intent": 0.00031603744719177485,
"sexual": 0.020730027928948402,
"sexual_minors": 0.00018848323088604957,
"violence": 0.008375612087547779,
"violence_graphic": 2.8763401132891886e-05,
"self-harm": 0.00043840022408403456,
"sexual/minors": 0.00018241720681544393,
"hate/threatening": 1.1130881830467843e-05,
"violence/graphic": 2.7211604901822284e-05,
"self-harm/intent": 0.00026327319210395217,
"self-harm/instructions": 0.00023905260604806244,
"harassment/threatening": 0.0012845908058807254
}
}
✨ 主要特性
- 基於
bert-base-multilingual-cased
構建,可將文本分類為 18 個不同類別。
- 目前僅支持英文文本處理,未來更新可能會支持更多語言。
📚 詳細文檔
數據集
該模型使用 text-moderation-410K 數據集進行訓練和微調。此數據集包含各種不同的文本樣本,並標註了不同的審核類別。
模型描述
ModerationBERT-ML-En 使用 BERT 架構將文本分類為以下類別:
- 騷擾
- 威脅性騷擾
- 仇恨言論
- 威脅性仇恨言論
- 自我傷害
- 自我傷害指導
- 自我傷害意圖
- 色情內容
- 涉及未成年人的色情內容
- 暴力內容
- 血腥暴力內容
- 自我傷害
- 涉及未成年人的色情內容
- 威脅性仇恨言論
- 血腥暴力內容
- 自我傷害意圖
- 自我傷害指導
- 威脅性騷擾
訓練與微調
該模型使用數據集的 95% 進行訓練,5% 進行評估。訓練分兩個階段進行:
- 初始訓練:凍結 BERT 層,訓練分類器層。
- 微調:解凍 BERT 模型的前兩層,並對整個模型進行微調。
📄 許可證
本模型採用 Apache-2.0 許可證。
⚠️ 重要提示
- 該模型目前僅配置為處理英文文本。
- 未來更新可能會支持其他語言。