Upload 9 files
Browse files- bert_toxicity_final_model/config.json +32 -0
- bert_toxicity_final_model/inference_example.py +46 -0
- bert_toxicity_final_model/model.safetensors +3 -0
- bert_toxicity_final_model/model_config.json +30 -0
- bert_toxicity_final_model/special_tokens_map.json +7 -0
- bert_toxicity_final_model/tokenizer.json +0 -0
- bert_toxicity_final_model/tokenizer_config.json +56 -0
- bert_toxicity_final_model/training_args.bin +3 -0
- bert_toxicity_final_model/vocab.txt +0 -0
bert_toxicity_final_model/config.json
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"activation": "gelu",
|
3 |
+
"architectures": [
|
4 |
+
"DistilBertForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_dropout": 0.1,
|
7 |
+
"dim": 768,
|
8 |
+
"dropout": 0.1,
|
9 |
+
"hidden_dim": 3072,
|
10 |
+
"id2label": {
|
11 |
+
"0": "Safe",
|
12 |
+
"1": "Unsafe"
|
13 |
+
},
|
14 |
+
"initializer_range": 0.02,
|
15 |
+
"label2id": {
|
16 |
+
"Safe": 0,
|
17 |
+
"Unsafe": 1
|
18 |
+
},
|
19 |
+
"max_position_embeddings": 512,
|
20 |
+
"model_type": "distilbert",
|
21 |
+
"n_heads": 12,
|
22 |
+
"n_layers": 6,
|
23 |
+
"pad_token_id": 0,
|
24 |
+
"problem_type": "single_label_classification",
|
25 |
+
"qa_dropout": 0.1,
|
26 |
+
"seq_classif_dropout": 0.2,
|
27 |
+
"sinusoidal_pos_embds": false,
|
28 |
+
"tie_weights_": true,
|
29 |
+
"torch_dtype": "float32",
|
30 |
+
"transformers_version": "4.52.4",
|
31 |
+
"vocab_size": 30522
|
32 |
+
}
|
bert_toxicity_final_model/inference_example.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Example code to load and use the trained model
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
+
import json
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# load the saved model
|
10 |
+
model_path = "./bert_toxicity_final_model"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
12 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
13 |
+
|
14 |
+
# load configuration
|
15 |
+
with open(f"{model_path}/model_config.json", 'r') as f:
|
16 |
+
config = json.load(f)
|
17 |
+
|
18 |
+
MAX_LENGTH = config['max_length']
|
19 |
+
THRESHOLD = config['best_threshold']
|
20 |
+
|
21 |
+
def predict_toxicity(text):
|
22 |
+
"""
|
23 |
+
Predict toxicity for a single text input
|
24 |
+
Returns: (is_toxic: bool, toxicity_score: float)
|
25 |
+
"""
|
26 |
+
# tokenize
|
27 |
+
inputs = tokenizer(
|
28 |
+
text,
|
29 |
+
truncation=True,
|
30 |
+
padding=True,
|
31 |
+
max_length=MAX_LENGTH,
|
32 |
+
return_tensors="pt"
|
33 |
+
)
|
34 |
+
|
35 |
+
# predict
|
36 |
+
with torch.no_grad():
|
37 |
+
outputs = model(**inputs)
|
38 |
+
probabilities = torch.softmax(outputs.logits, dim=1)
|
39 |
+
toxicity_score = probabilities[0][1].item() # probability of toxic class
|
40 |
+
is_toxic = toxicity_score >= THRESHOLD
|
41 |
+
|
42 |
+
return is_toxic, toxicity_score
|
43 |
+
|
44 |
+
# example usage
|
45 |
+
# is_toxic, score = predict_toxicity("Your text here")
|
46 |
+
# print(f"Toxic: {is_toxic}, Score: {score:.3f}")
|
bert_toxicity_final_model/model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c12dc14dd33bce1d8cfc4de5ac4c6e1db5780aa7aa0e977753ea70ccc25ffef8
|
3 |
+
size 267832560
|
bert_toxicity_final_model/model_config.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"model_name": "distilbert-base-uncased",
|
3 |
+
"max_length": 385,
|
4 |
+
"num_epochs": 10,
|
5 |
+
"batch_size": 16,
|
6 |
+
"learning_rate": 2e-05,
|
7 |
+
"best_threshold": 0.1,
|
8 |
+
"best_f1_score": 0.7503015681544029,
|
9 |
+
"training_samples": 4448,
|
10 |
+
"validation_samples": 1245,
|
11 |
+
"test_samples": 717,
|
12 |
+
"final_test_metrics": {
|
13 |
+
"accuracy": 0.7350069735006973,
|
14 |
+
"precision": 0.7175792507204611,
|
15 |
+
"recall": 0.7302052785923754,
|
16 |
+
"f1": 0.7238372093023255,
|
17 |
+
"auc_roc": 0.8193907156673115
|
18 |
+
},
|
19 |
+
"optimized_metrics": {
|
20 |
+
"threshold": 0.1,
|
21 |
+
"f1": 0.7503015681544029,
|
22 |
+
"precision": 0.6372950819672131,
|
23 |
+
"recall": 0.9120234604105572,
|
24 |
+
"accuracy": 0.7112970711297071,
|
25 |
+
"tp": 311,
|
26 |
+
"fp": 177,
|
27 |
+
"tn": 199,
|
28 |
+
"fn": 30
|
29 |
+
}
|
30 |
+
}
|
bert_toxicity_final_model/special_tokens_map.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"mask_token": "[MASK]",
|
4 |
+
"pad_token": "[PAD]",
|
5 |
+
"sep_token": "[SEP]",
|
6 |
+
"unk_token": "[UNK]"
|
7 |
+
}
|
bert_toxicity_final_model/tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
bert_toxicity_final_model/tokenizer_config.json
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "[PAD]",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"100": {
|
12 |
+
"content": "[UNK]",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"101": {
|
20 |
+
"content": "[CLS]",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"102": {
|
28 |
+
"content": "[SEP]",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"103": {
|
36 |
+
"content": "[MASK]",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"clean_up_tokenization_spaces": false,
|
45 |
+
"cls_token": "[CLS]",
|
46 |
+
"do_lower_case": true,
|
47 |
+
"extra_special_tokens": {},
|
48 |
+
"mask_token": "[MASK]",
|
49 |
+
"model_max_length": 512,
|
50 |
+
"pad_token": "[PAD]",
|
51 |
+
"sep_token": "[SEP]",
|
52 |
+
"strip_accents": null,
|
53 |
+
"tokenize_chinese_chars": true,
|
54 |
+
"tokenizer_class": "DistilBertTokenizer",
|
55 |
+
"unk_token": "[UNK]"
|
56 |
+
}
|
bert_toxicity_final_model/training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:11c7db08cc04b34f748962dc11fae11e7ce243734a70a42364be827325b24f7a
|
3 |
+
size 5304
|
bert_toxicity_final_model/vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|