模型概述
模型特點
模型能力
使用案例
🚀 🙊 Detoxify
Detoxify是一個基於⚡ Pytorch Lightning和🤗 Transformers構建的工具,可用於預測3個Jigsaw挑戰中的有毒評論,包括有毒評論分類、有毒評論中的意外偏差以及多語言有毒評論分類。
免責聲明
⚠️ 重要提示
Hugging Face模型目前給出的結果與Detoxify庫不同(請參閱此處的問題)。為了使用最新的模型,我們建議使用來自https://github.com/unitaryai/detoxify的模型。
🚀 快速開始
快速預測
multilingual
模型已經在7種不同的語言上進行了訓練,因此只能在以下語言上進行測試:英語
、法語
、西班牙語
、意大利語
、葡萄牙語
、土耳其語
或俄語
。
# 安裝detoxify
pip install detoxify
from detoxify import Detoxify
# 每個模型可以接受一個字符串或一個字符串列表
results = Detoxify('original').predict('example text')
results = Detoxify('unbiased').predict(['example text 1', 'example text 2'])
results = Detoxify('multilingual').predict(['example text', 'exemple de texte', 'texto de ejemplo', 'testo di esempio', 'texto de esempio', 'örnek metin', 'пример текста'])
# 可選:美觀地顯示結果(需要安裝pandas)
import pandas as pd
print(pd.DataFrame(results, index=input_text).round(5))
運行步驟
首先,安裝依賴項:
# 克隆項目
git clone https://github.com/unitaryai/detoxify
# 創建虛擬環境
python3 -m venv toxic-env
source toxic-env/bin/activate
# 安裝項目
pip install -e detoxify
cd detoxify
# 用於訓練
pip install -r requirements.txt
✨ 主要特性
- 提供訓練好的模型和代碼,用於預測3個Jigsaw挑戰中的有毒評論。
- 支持多語言有毒評論分類。
- 可以通過命令行或Python腳本進行快速預測。
📦 安裝指南
安裝依賴
# 克隆項目
git clone https://github.com/unitaryai/detoxify
# 創建虛擬環境
python3 -m venv toxic-env
source toxic-env/bin/activate
# 安裝項目
pip install -e detoxify
cd detoxify
# 用於訓練
pip install -r requirements.txt
安裝Detoxify
pip install detoxify
💻 使用示例
基礎用法
from detoxify import Detoxify
# 每個模型可以接受一個字符串或一個字符串列表
results = Detoxify('original').predict('example text')
results = Detoxify('unbiased').predict(['example text 1', 'example text 2'])
results = Detoxify('multilingual').predict(['example text', 'exemple de texte', 'texto de ejemplo', 'testo di esempio', 'texto de esempio', 'örnek metin', 'пример текста'])
# 可選:美觀地顯示結果(需要安裝pandas)
import pandas as pd
print(pd.DataFrame(results, index=input_text).round(5))
高級用法
# 直接在評論上運行示例腳本進行快速預測,或從包含評論列表的txt文件中進行預測
# 通過torch.hub加載模型
python run_prediction.py --input 'example' --model_name original
# 從檢查點路徑加載模型
python run_prediction.py --input 'example' --from_ckpt_path model_path
# 將結果保存到.csv文件
python run_prediction.py --input test_set.txt --model_name original --save_to results.csv
# 查看用法
python run_prediction.py --help
📚 詳細文檔
模型信息
屬性 | 詳情 |
---|---|
模型類型 | original :bert-base-uncased ;unbiased :roberta-base ;multilingual :xlm-roberta-base |
訓練數據 | original :Toxic Comment Classification Challenge;unbiased :Unintended Bias in Toxicity Classification;multilingual :Multilingual Toxic Comment Classification |
標籤信息
所有挑戰都有一個毒性標籤。毒性標籤根據以下模式代表最多10名標註者的綜合評分:
- 非常有毒(非常仇恨、攻擊性或不尊重的評論,很可能會讓你離開討論或放棄分享你的觀點)
- 有毒(粗魯、不尊重或不合理的評論,有點可能會讓你離開討論或放棄分享你的觀點)
- 難說
- 無毒
更多關於標籤模式的信息可以在這裡找到。
Toxic Comment Classification Challenge
此挑戰包括以下標籤:
toxic
severe_toxic
obscene
threat
insult
identity_hate
Jigsaw Unintended Bias in Toxicity Classification
此挑戰有2種類型的標籤:主要毒性標籤和一些代表評論中提到的身份的額外身份標籤。
只有在測試集(公共和私有組合)中示例數超過500的身份才會在訓練期間作為額外標籤包含在內,並在評估計算中使用。
toxicity
severe_toxicity
obscene
threat
insult
identity_attack
sexual_explicit
使用的身份標籤:
male
female
homosexual_gay_or_lesbian
christian
jewish
muslim
black
white
psychiatric_or_mental_illness
所有可用身份標籤的完整列表可以在這裡找到。
Jigsaw Multilingual Toxic Comment Classification
由於此挑戰結合了前2個挑戰的數據,它包括上述所有標籤,但最終評估僅針對:
toxicity
預測
訓練好的模型總結:
模型名稱 | 變壓器類型 | 數據來源 |
---|---|---|
original |
bert-base-uncased |
Toxic Comment Classification Challenge |
unbiased |
roberta-base |
Unintended Bias in Toxicity Classification |
multilingual |
xlm-roberta-base |
Multilingual Toxic Comment Classification |
檢查點可以從最新版本下載,或通過Pytorch hub API使用以下名稱下載:
toxic_bert
unbiased_toxic_roberta
multilingual_toxic_xlm_r
model = torch.hub.load('unitaryai/detoxify', 'toxic_bert')
訓練
如果你還沒有Kaggle賬戶:
- 你需要創建一個才能下載數據。
- 轉到“我的賬戶”並點擊“創建新API令牌” - 這將下載一個kaggle.json文件。
- 確保此文件位於
~/.kaggle
中。
# 創建數據目錄
mkdir jigsaw_data
cd jigsaw_data
# 下載數據
kaggle competitions download -c jigsaw-toxic-comment-classification-challenge
kaggle competitions download -c jigsaw-unintended-bias-in-toxicity-classification
kaggle competitions download -c jigsaw-multilingual-toxic-comment-classification
開始訓練
Toxic Comment Classification Challenge
python create_val_set.py
python train.py --config configs/Toxic_comment_classification_BERT.json
Unintended Bias in Toxicicity Challenge
python train.py --config configs/Unintended_bias_toxic_comment_classification_RoBERTa.json
Multilingual Toxic Comment Classification
此模型分2個階段訓練。首先,在所有可用數據上訓練,其次,僅在第一個挑戰的翻譯版本上訓練。
翻譯數據可以從Kaggle下載法語、西班牙語、意大利語、葡萄牙語、土耳其語和俄語(測試集中可用的語言)。
# 階段1
python train.py --config configs/Multilingual_toxic_comment_classification_XLMR.json
# 階段2
python train.py --config configs/Multilingual_toxic_comment_classification_XLMR_stage2.json
使用TensorBoard監控進度
tensorboard --logdir=./saved
模型評估
Toxic Comment Classification Challenge
此挑戰根據所有標籤的平均AUC分數進行評估。
python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
Unintended Bias in Toxicicity Challenge
此挑戰根據一種新的偏差指標進行評估,該指標結合了不同的AUC分數以平衡整體性能。有關此指標的更多信息這裡。
python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
# 獲取最終偏差指標
python model_eval/compute_bias_metric.py
Multilingual Toxic Comment Classification
此挑戰根據主要毒性標籤的AUC分數進行評估。
python evaluate.py --checkpoint saved/lightning_logs/checkpoints/example_checkpoint.pth --test_csv test.csv
🔧 技術細節
侷限性和倫理考慮
如果評論中存在與咒罵、侮辱或褻瀆相關的詞彙,無論作者的語氣或意圖(例如幽默/自嘲)如何,該評論很可能會被分類為有毒。這可能會對已經脆弱的少數群體產生一些偏見。
本庫的預期用途是用於研究目的,在精心構建的反映現實世界人口統計數據的數據集上進行微調,和/或幫助內容審核人員更快地標記有害內容。
一些關於毒性或仇恨言論檢測中不同偏差風險的有用資源:
📄 許可證
本項目採用Apache-2.0許可證。
引用
@misc{Detoxify,
title={Detoxify},
author={Hanu, Laura and {Unitary team}},
howpublished={Github. https://github.com/unitaryai/detoxify},
year={2020}
}








