🚀 🙊 Detoxify
このプロジェクトは、3つのJigsawチャレンジ(有害コメント分類、有害コメントの意図しないバイアス、多言語有害コメント分類)における有害コメントを予測するための学習済みモデルとコードを提供します。⚡Pytorch Lightningと🤗Transformersを利用して構築されています。
🚀 クイックスタート
⚠️ 重要提示
huggingfaceのモデルは現在、detoxifyライブラリと異なる結果を返す場合があります(詳細はこちらのissueを参照)。最新のモデルを使用する場合は、https://github.com/unitaryai/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 ejemplo','örnek metin','пример текста'])
import pandas as pd
print(pd.DataFrame(results, index=input_text).round(5))
✨ 主な機能
- 3つのJigsawチャレンジに対応した有害コメント分類モデルを提供。
- 多言語に対応した有害コメント分類が可能。
- ユーザーフレンドリーで使いやすいAPIを備えています。
📦 インストール
依存関係のインストール
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
💻 使用例
基本的な使用法
from detoxify import Detoxify
results = Detoxify('original').predict('example text')
高度な使用法
from detoxify import Detoxify
import pandas as pd
results = Detoxify('multilingual').predict(['example text','exemple de texte','texto de ejemplo','testo di esempio','texto de ejemplo','örnek metin','пример текста'])
print(pd.DataFrame(results, index=input_text).round(5))
📚 ドキュメント
チャレンジの概要
*スコアは、テストセットではなく提供された検証セットで取得されているため、直接比較できません。テストラベルが公開されたら更新します。
ラベルの説明
すべてのチャレンジには有害性ラベルがあります。有害性ラベルは、最大10人のアノテーターの集計評価を表しており、以下のスキーマに従っています。
- 非常に有害(非常に憎悪的、攻撃的、または失礼なコメントで、議論をやめたり、自分の見解を共有するのをやめたりする可能性が非常に高い)
- 有害(失礼な、失礼な、または不合理なコメントで、議論をやめたり、自分の見解を共有するのをやめたりする可能性がやや高い)
- 判断しにくい
- 有害でない
ラベリングスキーマの詳細については、こちらを参照してください。
予測の実行
学習済みモデルの概要:
モデル名 |
Transformerタイプ |
データソース |
original |
bert-base-uncased |
Toxic Comment Classification Challenge |
unbiased |
roberta-base |
Unintended Bias in Toxicity Classification |
multilingual |
xlm-roberta-base |
Multilingual Toxic Comment Classification |
python run_prediction.py --input 'example' --model_name original
python run_prediction.py --input 'example' --from_ckpt_path model_path
python run_prediction.py --input test_set.txt --model_name original --save_to results.csv
python run_prediction.py --help
チェックポイントは、最新のリリースからダウンロードするか、PytorchハブAPIを使用して以下の名前で取得できます。
toxic_bert
unbiased_toxic_roberta
multilingual_toxic_xlm_r
model = torch.hub.load('unitaryai/detoxify','toxic_bert')
学習の実行
データのダウンロード
Kaggleアカウントがない場合は、まずアカウントを作成し、APIトークンをダウンロードして~/.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からダウンロードできます。
python train.py --config configs/Multilingual_toxic_comment_classification_XLMR.json
python train.py --config configs/Multilingual_toxic_comment_classification_XLMR_stage2.json
学習の進捗を監視する
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
🔧 技術詳細
- モデルのトレーニングには、⚡Pytorch Lightningと🤗Transformersを使用しています。
- 多言語モデルは、7つの異なる言語で学習されており、
english
、french
、spanish
、italian
、portuguese
、turkish
、russian
でのテストにのみ適しています。
📄 ライセンス
このプロジェクトは、Apache-2.0ライセンスの下で公開されています。
引用
@misc{Detoxify,
title={Detoxify},
author={Hanu, Laura and {Unitary team}},
howpublished={Github. https://github.com/unitaryai/detoxify},
year={2020}
}