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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- ja
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
base_model: b4c0n/KAi-Toxicity-Filter
|
| 6 |
+
library_name: onnx
|
| 7 |
+
pipeline_tag: text-classification
|
| 8 |
+
tags:
|
| 9 |
+
- text-classification
|
| 10 |
+
- toxicity-detection
|
| 11 |
+
- japanese
|
| 12 |
+
- bert
|
| 13 |
+
- onnx
|
| 14 |
+
- optimized
|
| 15 |
+
datasets:
|
| 16 |
+
- inspection-ai/japanese-toxic-dataset
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# KAi Toxicity Filter (ONNX)
|
| 20 |
+
|
| 21 |
+
日本語の有害表現検出モデルのONNX最適化版
|
| 22 |
+
ONNX-optimized version of Japanese toxicity detection model
|
| 23 |
+
|
| 24 |
+
**元のPyTorchモデル**: [b4c0n/KAi-Toxicity-Filter](https://huggingface.co/b4c0n/KAi-Toxicity-Filter)
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## 日本語版
|
| 29 |
+
|
| 30 |
+
### モデル概要
|
| 31 |
+
|
| 32 |
+
このモデルは[b4c0n/KAi-Toxicity-Filter](https://huggingface.co/b4c0n/KAi-Toxicity-Filter)をONNX形式に変換した最適化版です。推論速度とデプロイの柔軟性が向上しています。
|
| 33 |
+
|
| 34 |
+
### ONNX版の利点
|
| 35 |
+
|
| 36 |
+
- ✅ **高速な推論**: PyTorch版と比較して推論が高速
|
| 37 |
+
- ✅ **軽量デプロイ**: PyTorch不要、ONNXランタイムのみで動作
|
| 38 |
+
- ✅ **クロスプラットフォーム**: C++, C#, Java, JavaScriptなど多言語対応
|
| 39 |
+
- ✅ **エッジデバイス対応**: IoTデバイスやモバイルでの実行が容易
|
| 40 |
+
|
| 41 |
+
### モデル詳細
|
| 42 |
+
|
| 43 |
+
- **元のモデル**: b4c0n/KAi-Toxicity-Filter
|
| 44 |
+
- **ベースアーキテクチャ**: BERT (tohoku-nlp/bert-base-japanese-v3)
|
| 45 |
+
- **タスク**: 二値分類(有害/非有害)
|
| 46 |
+
- **ONNX Opset**: 18
|
| 47 |
+
- **モデルサイズ**: 約426 MB (単一ファイル)
|
| 48 |
+
|
| 49 |
+
### 性能
|
| 50 |
+
|
| 51 |
+
元のPyTorchモデルと同等の精度を維持:
|
| 52 |
+
|
| 53 |
+
- **Accuracy**: 86.32%
|
| 54 |
+
- **F1 Score**: 70.68%
|
| 55 |
+
- **Precision**: 72.31%
|
| 56 |
+
- **Recall**: 69.12%
|
| 57 |
+
|
| 58 |
+
推論結果の誤差: < 0.000001(実質的に同一)
|
| 59 |
+
|
| 60 |
+
### 使用例
|
| 61 |
+
|
| 62 |
+
#### Python (ONNX Runtime)
|
| 63 |
+
|
| 64 |
+
```python
|
| 65 |
+
import onnxruntime as ort
|
| 66 |
+
import numpy as np
|
| 67 |
+
from transformers import AutoTokenizer
|
| 68 |
+
|
| 69 |
+
# トークナイザーをロード(元のモデルから)
|
| 70 |
+
tokenizer = AutoTokenizer.from_pretrained("b4c0n/KAi-Toxicity-Filter")
|
| 71 |
+
|
| 72 |
+
# ONNXセッションを作成
|
| 73 |
+
session = ort.InferenceSession("toxicity_model.onnx")
|
| 74 |
+
|
| 75 |
+
# テキストをトークナイズ
|
| 76 |
+
text = "終わってる暴言"
|
| 77 |
+
inputs = tokenizer(text, return_tensors="np", padding=True, truncation=True, max_length=512)
|
| 78 |
+
|
| 79 |
+
# 推論実行
|
| 80 |
+
onnx_inputs = {
|
| 81 |
+
"input_ids": inputs["input_ids"].astype(np.int64),
|
| 82 |
+
"attention_mask": inputs["attention_mask"].astype(np.int64),
|
| 83 |
+
"token_type_ids": inputs["token_type_ids"].astype(np.int64)
|
| 84 |
+
}
|
| 85 |
+
outputs = session.run(None, onnx_inputs)
|
| 86 |
+
|
| 87 |
+
# Softmaxで確率に変換
|
| 88 |
+
logits = outputs[0][0]
|
| 89 |
+
exp_logits = np.exp(logits - np.max(logits))
|
| 90 |
+
probs = exp_logits / exp_logits.sum()
|
| 91 |
+
|
| 92 |
+
print(f"有害確率: {probs[1]:.2%}")
|
| 93 |
+
print(f"健全確率: {probs[0]:.2%}")
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
#### インストール
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
pip install onnxruntime transformers
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
GPU版を使用する場合:
|
| 103 |
+
```bash
|
| 104 |
+
pip install onnxruntime-gpu transformers
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### ファイル構成
|
| 108 |
+
|
| 109 |
+
```
|
| 110 |
+
KAi-Toxicity-Filter-ONNX/
|
| 111 |
+
├── toxicity_model.onnx # ONNXモデル(単一ファイル、外部データ埋め込み済み)
|
| 112 |
+
└── README.md
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
**注意**: トークナイザーは含まれていません。元のモデル[b4c0n/KAi-Toxicity-Filter](https://huggingface.co/b4c0n/KAi-Toxicity-Filter)からロードしてください。
|
| 116 |
+
|
| 117 |
+
### 使用目的
|
| 118 |
+
|
| 119 |
+
KAi (かい鯖グループAI) における日本語テキストの有害コンテンツ検出・フィルタリングのために開発されました。
|
| 120 |
+
|
| 121 |
+
**主な用途:**
|
| 122 |
+
- 高速な推論が必要なリアルタイムモデレーション
|
| 123 |
+
- サーバーレス環境でのデプロイ
|
| 124 |
+
- エッジデバイスでの有害性検出
|
| 125 |
+
- マルチプラットフォーム対応アプリケーション
|
| 126 |
+
|
| 127 |
+
### 制限事項
|
| 128 |
+
|
| 129 |
+
- トークナイザーは別途ロードが必要(元のPyTorchモデルから)
|
| 130 |
+
- 短い口語表現に特化しており、長文や文脈依存の有害性検出には限界があります
|
| 131 |
+
- 誤検出(偽陽性/偽陰性)の可能性があります
|
| 132 |
+
- 訓練データに含まれない新しいタイプの有害表現は検出できない場合があります
|
| 133 |
+
|
| 134 |
+
### 技術詳細
|
| 135 |
+
|
| 136 |
+
- **変換方法**: torch.onnx.export with dynamo=True
|
| 137 |
+
- **外部データ**: モデルに埋め込み済み(単一ファイル)
|
| 138 |
+
- **検証**: PyTorchモデルとの出力一致確認済み
|
| 139 |
+
- **最適化**: ONNX標準最適化適用済み
|
| 140 |
+
|
| 141 |
+
### ライセンス
|
| 142 |
+
|
| 143 |
+
Apache 2.0
|
| 144 |
+
|
| 145 |
+
### 関連リンク
|
| 146 |
+
|
| 147 |
+
- **元のPyTorchモデル**: [b4c0n/KAi-Toxicity-Filter](https://huggingface.co/b4c0n/KAi-Toxicity-Filter)
|
| 148 |
+
- **ベースモデル**: [tohoku-nlp/bert-base-japanese-v3](https://huggingface.co/tohoku-nlp/bert-base-japanese-v3)
|
| 149 |
+
- **データセット**: [inspection-ai/japanese-toxic-dataset](https://github.com/inspection-ai/japanese-toxic-dataset)
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
## English
|
| 154 |
+
|
| 155 |
+
### Model Description
|
| 156 |
+
|
| 157 |
+
This is the ONNX-optimized version of [b4c0n/KAi-Toxicity-Filter](https://huggingface.co/b4c0n/KAi-Toxicity-Filter), offering improved inference speed and deployment flexibility.
|
| 158 |
+
|
| 159 |
+
### ONNX Benefits
|
| 160 |
+
|
| 161 |
+
- ✅ **Faster Inference**: Optimized for speed compared to PyTorch
|
| 162 |
+
- ✅ **Lightweight Deployment**: No PyTorch dependency, ONNX Runtime only
|
| 163 |
+
- ✅ **Cross-Platform**: Compatible with C++, C#, Java, JavaScript, etc.
|
| 164 |
+
- ✅ **Edge Device Ready**: Easy deployment on IoT and mobile devices
|
| 165 |
+
|
| 166 |
+
### Model Details
|
| 167 |
+
|
| 168 |
+
- **Original Model**: b4c0n/KAi-Toxicity-Filter
|
| 169 |
+
- **Base Architecture**: BERT (tohoku-nlp/bert-base-japanese-v3)
|
| 170 |
+
- **Task**: Binary Text Classification (toxic/not-toxic)
|
| 171 |
+
- **ONNX Opset**: 18
|
| 172 |
+
- **Model Size**: ~426 MB (single file)
|
| 173 |
+
|
| 174 |
+
### Performance
|
| 175 |
+
|
| 176 |
+
Maintains equivalent accuracy to the original PyTorch model:
|
| 177 |
+
|
| 178 |
+
- **Accuracy**: 86.32%
|
| 179 |
+
- **F1 Score**: 70.68%
|
| 180 |
+
- **Precision**: 72.31%
|
| 181 |
+
- **Recall**: 69.12%
|
| 182 |
+
|
| 183 |
+
Inference output difference: < 0.000001 (virtually identical)
|
| 184 |
+
|
| 185 |
+
### Usage
|
| 186 |
+
|
| 187 |
+
#### Python (ONNX Runtime)
|
| 188 |
+
|
| 189 |
+
```python
|
| 190 |
+
import onnxruntime as ort
|
| 191 |
+
import numpy as np
|
| 192 |
+
from transformers import AutoTokenizer
|
| 193 |
+
|
| 194 |
+
# Load tokenizer from original model
|
| 195 |
+
tokenizer = AutoTokenizer.from_pretrained("b4c0n/KAi-Toxicity-Filter")
|
| 196 |
+
|
| 197 |
+
# Create ONNX session
|
| 198 |
+
session = ort.InferenceSession("toxicity_model.onnx")
|
| 199 |
+
|
| 200 |
+
# Tokenize text
|
| 201 |
+
text = "toxic expression"
|
| 202 |
+
inputs = tokenizer(text, return_tensors="np", padding=True, truncation=True, max_length=512)
|
| 203 |
+
|
| 204 |
+
# Run inference
|
| 205 |
+
onnx_inputs = {
|
| 206 |
+
"input_ids": inputs["input_ids"].astype(np.int64),
|
| 207 |
+
"attention_mask": inputs["attention_mask"].astype(np.int64),
|
| 208 |
+
"token_type_ids": inputs["token_type_ids"].astype(np.int64)
|
| 209 |
+
}
|
| 210 |
+
outputs = session.run(None, onnx_inputs)
|
| 211 |
+
|
| 212 |
+
# Convert to probabilities with softmax
|
| 213 |
+
logits = outputs[0][0]
|
| 214 |
+
exp_logits = np.exp(logits - np.max(logits))
|
| 215 |
+
probs = exp_logits / exp_logits.sum()
|
| 216 |
+
|
| 217 |
+
print(f"Toxic probability: {probs[1]:.2%}")
|
| 218 |
+
print(f"Not-toxic probability: {probs[0]:.2%}")
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
#### Installation
|
| 222 |
+
|
| 223 |
+
```bash
|
| 224 |
+
pip install onnxruntime transformers
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
For GPU support:
|
| 228 |
+
```bash
|
| 229 |
+
pip install onnxruntime-gpu transformers
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
### File Structure
|
| 233 |
+
|
| 234 |
+
```
|
| 235 |
+
KAi-Toxicity-Filter-ONNX/
|
| 236 |
+
├── toxicity_model.onnx # ONNX model (single file with embedded external data)
|
| 237 |
+
└── README.md
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
**Note**: Tokenizer not included. Load from original model [b4c0n/KAi-Toxicity-Filter](https://huggingface.co/b4c0n/KAi-Toxicity-Filter).
|
| 241 |
+
|
| 242 |
+
### Intended Use
|
| 243 |
+
|
| 244 |
+
Developed for KAi (KaisabaGroupAI) to detect and filter harmful content in Japanese text.
|
| 245 |
+
|
| 246 |
+
**Primary Use Cases:**
|
| 247 |
+
- Real-time moderation requiring fast inference
|
| 248 |
+
- Serverless environment deployment
|
| 249 |
+
- Edge device toxicity detection
|
| 250 |
+
- Multi-platform applications
|
| 251 |
+
|
| 252 |
+
### Limitations
|
| 253 |
+
|
| 254 |
+
- Tokenizer must be loaded separately from the original PyTorch model
|
| 255 |
+
- Optimized for short colloquial expressions; limited for long texts or context-dependent toxicity
|
| 256 |
+
- May have false positives/negatives
|
| 257 |
+
- Cannot detect new types of toxic expressions not present in training data
|
| 258 |
+
|
| 259 |
+
### Technical Details
|
| 260 |
+
|
| 261 |
+
- **Conversion Method**: torch.onnx.export with dynamo=True
|
| 262 |
+
- **External Data**: Embedded in model (single file)
|
| 263 |
+
- **Validation**: Output verified against PyTorch model
|
| 264 |
+
- **Optimization**: Standard ONNX optimizations applied
|
| 265 |
+
|
| 266 |
+
### License
|
| 267 |
+
|
| 268 |
+
Apache 2.0
|
| 269 |
+
|
| 270 |
+
### Related Links
|
| 271 |
+
|
| 272 |
+
- **Original PyTorch Model**: [b4c0n/KAi-Toxicity-Filter](https://huggingface.co/b4c0n/KAi-Toxicity-Filter)
|
| 273 |
+
- **Base Model**: [tohoku-nlp/bert-base-japanese-v3](https://huggingface.co/tohoku-nlp/bert-base-japanese-v3)
|
| 274 |
+
- **Dataset**: [inspection-ai/japanese-toxic-dataset](https://github.com/inspection-ai/japanese-toxic-dataset)
|