File size: 10,073 Bytes
33e3dc0 7c69936 33e3dc0 7c69936 33e3dc0 7c69936 33e3dc0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
---
library_name: transformers
tags: []
---
# **Model Card for Gemma2-2B-IT-Finetuned-KO-Bias-Detection**
This model is fine-tuned from the base `google/gemma-2-2b-it` model using the Korean UnSmile Dataset, which focuses on identifying hate speech in Korean. The model detects various categories of hate speech, including but not limited to gender, race, age, and sexual orientation.
## Model Details
### **Model Description**
In this project, I fine-tuned the google/gemma-2-2b-it model using the Korean UnSmile Datasetโa comprehensive dataset for hate speech in Koreanโreleased by Smilegate AI. The fine-tuned model is capable of identifying the following hate speech categories:
- Women/Family: Stereotypes or derogatory remarks targeting women or family structures, including non-traditional families.
- Men: Comments that demean or mock men.
- LGBTQ+: Hate speech aimed at individuals based on sexual orientation or gender identity.
- Race/Nationality: Offensive language or stereotypes related to race or nationality, such as insults towards specific ethnic groups or nationalities.
- Age: Disparaging comments targeting specific age groups or generations.
- Region: Hate speech directed at people from specific regions.
- Religion: Negative or offensive comments aimed at religious groups or beliefs.
- Other Hate Speech: Includes hate speech targeting other groups, such as individuals with disabilities or specific professions (e.g., police, journalists).
This fine-tuned model offers a practical solution for detecting toxic behavior on online platforms, helping ensure safer and more inclusive online environments by classifying harmful content efficiently and accurately.
### **Access Requirements**
To use the Gemma model, please follow these steps:
[**1. Access Gemma**]
- 1-1. Navigate to the gemma-2-2b-it repository that you wish to use.
- 1-2. Click on "Acknowledge license" in the "Access Gemma on Hugging Face" section.
- 1-3. Click on "Authorize," check the required fields, and then click "Accept."
- 1-4. If you see the message "You have been granted access to this model" [Gated model], this means you now have access to the Gemma model.
[**2. Access Tokens**]
- 2-1. Click on your profile in the upper-right corner and select "Settings."
- 2-2. In the menu, click on "Access Tokens," then click on [+ Create new token].
- 2-3. Under [Token type], choose ['Read'] from the options: 'Fine-grained', 'Read', 'Write'.
- 2-4. Enter a name for the token, then click "Create token."
- 2-5. Save the generated Access Token, and use it for Hugging Face authentication, such as in notebook_login.
```python
from huggingface_hub import notebook_login
notebook_login()
```
### **Training Procedure**
**Environment Setup**
We fine-tuned the google/gemma-2-2b-it model using the Korean UnSmile Dataset. Note that versions of transformers before 4.38.1 contain bugs related to Gemma models. Please update to version 4.38.1 or later.
```python
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
model_id = "google/gemma-2-2b-it"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
quantization_config=bnb_config,
device_map={"": 0}
)
tokenizer = AutoTokenizer.from_pretrained(model_id, add_eos_token=True)
```
**Setting Up LoRA Configuration**
```python
from peft import LoraConfig, PeftModel, prepare_model_for_kbit_training, get_peft_model
model.gradient_checkpointing_enable()
model = prepare_model_for_kbit_training(model)
import bitsandbytes as bnb
def find_all_linear_names(model):
cls = bnb.nn.Linear4bit # For 4-bit precision
lora_module_names = set()
for name, module in model.named_modules():
if isinstance(module, cls):
names = name.split('.')
lora_module_names.add(names[0] if len(names) == 1 else names[-1])
if 'lm_head' in lora_module_names: # Needed for 16-bit
lora_module_names.remove('lm_head')
return list(lora_module_names)
modules = find_all_linear_names(model)
lora_config = LoraConfig(
r=64,
lora_alpha=32,
target_modules=modules,
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM"
)
model = get_peft_model(model, lora_config)
trainable, total = model.get_nb_trainable_parameters()
print(f"Trainable: {trainable} | Total: {total} | Percentage: {trainable/total*100:.4f}%")
```
**Loading Training Data**
```python
from datasets import load_dataset
import pandas as pd
df = load_dataset('smilegate-ai/kor_unsmile')
df_hf = pd.concat([df['train'].to_pandas(), df['valid'].to_pandas()]).reset_index()
```
**Preparing the Training Data**
The training data follows the Gemma conversation format:
```shell
<start_of_turn>user
Comment: [User comment]<end_of_turn>
<start_of_turn>model
Hate Speech:
[Categories]<end_of_turn>
```
We create the prompts as follows:
```python
def create_filtered_prompts_v2(row):
labels = {
"Women/Family": row['์ฌ์ฑ/๊ฐ์กฑ'],
"Men": row['๋จ์ฑ'],
"LGBTQ+": row['์ฑ์์์'],
"Race/Nationality": row['์ธ์ข
/๊ตญ์ '],
"Age": row['์ฐ๋ น'],
"Region": row['์ง์ญ'],
"Religion": row['์ข
๊ต'],
"Other Hate Speech": row['๊ธฐํ ํ์ค']
}
non_zero_labels = [key for key, value in labels.items() if value == 1]
if not non_zero_labels:
non_zero_labels.append('None')
return (
"<start_of_turn>user\nComment: " + row['๋ฌธ์ฅ'] + "<end_of_turn>\n"
"<start_of_turn>model\n"
"Hate Speech: " + "\n".join(non_zero_labels) + "\n<end_of_turn>"
)
df_hf['prompt'] = df_hf.apply(create_filtered_prompts_v2, axis=1)
df_hf = df_hf[["prompt", "๋ฌธ์ฅ"]].dropna()
```
**Converting to Hugging Face Dataset Format**
```python
from datasets import Dataset
data = Dataset.from_pandas(df_hf)
```
**Tokenizing the Data**
```python
data = data.map(lambda samples: tokenizer(samples["prompt"]), batched=True)
data = data.train_test_split(test_size=0.2)
```
**Training the Model**
```python
import transformers
from trl import SFTTrainer
tokenizer.pad_token = tokenizer.eos_token
torch.cuda.empty_cache()
trainer = SFTTrainer(
model=model,
train_dataset=data["train"],
eval_dataset=data["test"],
dataset_text_field="prompt",
peft_config=lora_config,
args=transformers.TrainingArguments(
per_device_train_batch_size=1,
gradient_accumulation_steps=2,
max_steps=2000,
push_to_hub=True,
push_to_hub_model_id="gemma2-2b-it-finetuned-ko-bias-detection",
push_to_hub_token=userdata.get('HUGGINGFACEHUB_API_TOKEN'),
learning_rate=2e-4,
logging_steps=500,
output_dir="outputs",
optim="paged_adamw_8bit",
save_strategy="steps",
evaluation_strategy="steps",
eval_steps=500,
),
data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),
)
import os
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
model.config.use_cache = False # Silence the warnings. Please re-enable for inference!
trainer.train()
```
**Testing the Model**
```python
new_model = "Hyeonseo/gemma2-2b-it-finetuned-ko-bias-detection"
base_model = AutoModelForCausalLM.from_pretrained(
model_id,
low_cpu_mem_usage=True,
return_dict=True,
torch_dtype=torch.float16,
device_map={"": 0},
)
merged_model = PeftModel.from_pretrained(base_model, new_model)
merged_model = merged_model.merge_and_unload()
# Save the merged model
merged_model.save_pretrained("merged_model", safe_serialization=True)
tokenizer.save_pretrained("merged_model")
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
# Push the merged model to the Hugging Face Hub
merged_model.push_to_hub("Hyeonseo/gemma2-2b-it-finetuned-ko-bias-detection_merged", safe_serialization=True)
# Push the tokenizer to the Hugging Face Hub
tokenizer.push_to_hub("Hyeonseo/gemma2-2b-it-finetuned-ko-bias-detection_merged")
```
### **Performance Comparison**
**Base Model("google/gemma-2-2b-it")**
```python
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it", device_map="auto")
def generate_response(input_text, max_length=500):
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)
outputs = model.generate(**input_ids, max_length=max_length)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
while True:
input_text = input("์
๋ ฅํ ๋ฌธ์ฅ์ ์ ์ด์ฃผ์ธ์ (์ข
๋ฃํ๋ ค๋ฉด 'exit' ์
๋ ฅ): ")
if input_text.lower() == 'exit':
break
response = generate_response(input_text)
print("\n=== ์์ฑ๋ ๋ต๋ณ ===")
print(response)
print("\n====================\n")
```
```shell
=== ์์ฑ๋ ๋ต๋ณ ===
์ด๋์ ๊ตํ์ ๋ชฉ์ฌ๋ ๋๋ผ์ด๊ณ ๋ง!!!
์ด๋ฅผ ๊ทน๋ณตํ๊ธฐ ์ํด์๋ ๊ตํ์ ๋ชฉ์ฌ๊ฐ ๊ตํ๋๋ ์ ๋ณด์ ๊ตํ์ ๋ฐ์์ผ ํ ๊ฒ์.
์ด๋ฅผ ํตํด ๊ตํ์ ๋ชฉ์ฌ๊ฐ ๊ตํ๋๋ ์ ๋ณด์ ๊ตํ์ ๋ฐ์ ๊ตํ์ ๋ชฉํ์ ๋ชฉ์ฌ์ ๋ชฉํ๋ฅผ ๊ณต์ ํ๊ณ , ๊ตํ์ ๊ตฌ์ฑ์์ด ๊ตํ์ ์๋์ ๋ชฉํ์ ๋์์ด ๋๋ ๋ฐ ๋์์ด ๋ ์ ์์ ๊ฒ์.
====================
```
**Fine-tuned Model(gemma2-2b-it-finetuned-ko-bias-detection_merged)**
![image/png](https://cdn-uploads.huggingface.co/production/uploads/66de9a659679be1ef804045b/645gk4BhOGdWA7JH_s1ZG.png)
### **Limitations and Bias**
While the model is effective at detecting specific categories of hate speech in Korean, it may not generalize well to other forms of toxicity or to content in other languages. Additionally, the model's performance is contingent on the quality and representativeness of the training data. Users should be cautious and consider potential biases inherited from the dataset.
|