Andresmfs commited on
Commit
16130a4
1 Parent(s): 416f00a

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +196 -0
README.md ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-nc-sa-4.0
3
+ datasets:
4
+ - somosnlp/es-inclusive-language
5
+ language:
6
+ - es
7
+ ---
8
+ # es-inclusivo-translator
9
+
10
+ This model is a fine-tuned version of [projecte-aina/aguila-7b](https://huggingface.co/projecte-aina/aguila-7b) on the dataset [somosnlp/es-inclusive-language](https://huggingface.co/datasets/somosnlp/es-inclusive-language).
11
+
12
+ Languages are powerful tools to communicate ideas, but their use is not impartial. The selection of words carries inherent biases and reflects subjective perspectives. In some cases, language is wielded to enforce ideologies, marginalize certain groups, or promote specific political agendas.
13
+ Spanish is not the exception to that. For instance, when we say “los alumnos” or “los ingenieros”, we are excluding women from those groups. Similarly, expressions such as “los gitanos” o “los musulmanes” perpetuate discrimination against these communities.
14
+
15
+ In response to these linguistic challenges, this model offers a way to construct inclusive alternatives in accordance with official guidelines on inclusive language from various Spanish speaking countries. Its purpose is to provide grammatically correct and inclusive solutions to situations where our language choices might otherwise be exclusive.
16
+ This is a tool that contributes to the fifth of the Sustainable Development Goals: Achieve gender equality and empower all women and girls.
17
+
18
+ The model works in such a way that, given an input text, it returns the original text rewritten using inclusive language.
19
+
20
+ It achieves the following results on the evaluation set:
21
+ - Loss: 0.6030
22
+
23
+ ## Model description
24
+ - **Developed by**: Andrés Martínez Fernández-Salguero ([andresmfs](https://huggingface.co/Andresmfs)), Imanuel Rozenberg (manu_20392), Gaia Quintana Fleitas (gaiaq), Josué Sauca (josue_sauca), Miguel López (wizmik12)
25
+ - **Language(s)**: Spanish
26
+ - **Fine-tuned from the model**: [projecte-aina/aguila-7b](https://huggingface.co/projecte-aina/aguila-7b)
27
+ - **License**: cc-by-nc-sa-4.0
28
+
29
+ ## Social Impact
30
+ An inclusive translator holds significant social impact by promoting equity and representation within texts. By rectifying biases ingrained in language and fostering inclusivity, it combats discrimination, amplifies the visibility of marginalized groups, and contributes to the cultivation of a more inclusive and respectful society.
31
+ This is a tool that contributes to the fifth of the Sustainable Development Goals: Achieve gender equality and empower all women and girls.
32
+
33
+ ## Intended uses & limitations
34
+
35
+ More information needed
36
+
37
+ ### How to use
38
+ Here is how to use this model:
39
+ ```python
40
+ from transformers import AutoTokenizer
41
+ from transformers import AutoModelForCausalLM
42
+ import torch
43
+
44
+ # Load tokenizer and model
45
+ tokenizer = AutoTokenizer.from_pretrained('somosnlp/', trust_remote_code=True)
46
+ model = AutoModelForCausalLM.from_pretrained('somosnlp/', trust_remote_code=True,
47
+ quantization_config=bnb_config,
48
+ device_map="auto")
49
+
50
+ # generation_config
51
+ generation_config = model.generation_config
52
+ generation_config.max_new_tokens = 100
53
+ generation_config.temperature = 0.7
54
+ generation_config.top_p = 0.7
55
+ generation_config.num_return_sequences = 1
56
+ generation_config.pad_token_id = tokenizer.eos_token_id
57
+ generation_config.eos_token_id = tokenizer.eos_token_id
58
+
59
+ # Define inference function
60
+ def translate_es_inclusivo(exclusive_text):
61
+
62
+ # generate input prompt
63
+ eval_prompt = f"""Reescribe el siguiente texto utilizando lenguaje inclusivo.\n
64
+ Texto: {exclusive_text}\n
65
+ Texto en lenguaje inclusivo:"""
66
+
67
+ # tokenize input
68
+ model_input = tokenizer(eval_prompt, return_tensors="pt").to(model.device)
69
+
70
+ # set max_new_tokens if necessary
71
+ if len(model_input['input_ids'][0]) > 80:
72
+ model.generation_config.max_new_tokens = len(model_input['input_ids'][0]) + 0.2 * len(model_input['input_ids'][0])
73
+
74
+ # get length of encoded prompt
75
+ prompt_token_len = len(model_input['input_ids'][0])
76
+
77
+ # generate and decode
78
+ with torch.no_grad():
79
+ inclusive_text = tokenizer.decode(model.generate(**model_input, generation_config=generation_config)[0][prompt_token_len:],
80
+ skip_special_tokens=True)
81
+
82
+ return inclusive_text
83
+
84
+ ##########
85
+
86
+ input_text = 'Los alumnos atienden a sus profesores'
87
+
88
+ print(translate_es_inclusivo(input_text))
89
+ ```python
90
+
91
+
92
+ As it is a heavy model, you may want to use it in 4-bits:
93
+ ```python
94
+ from transformers import AutoTokenizer
95
+ from transformers import AutoModelForCausalLM
96
+ from transformers import BitsAndBytesConfig
97
+ import torch
98
+
99
+
100
+ ## Load model in 4bits
101
+ # bnb_configuration
102
+ bnb_config = BitsAndBytesConfig(
103
+ load_in_4bit=True,
104
+ bnb_4bit_quant_type='nf4',
105
+ bnb_4bit_compute_dtype=torch.bfloat16,
106
+ bnb_4bit_use_double_quant=False)
107
+
108
+
109
+ # model
110
+ model = AutoModelForCausalLM.from_pretrained('somosnlp/', trust_remote_code=True,
111
+ quantization_config=bnb_config,
112
+ device_map="auto")
113
+
114
+ # Load tokenizer
115
+ tokenizer = AutoTokenizer.from_pretrained('somosnlp/', trust_remote_code=True)
116
+
117
+ # generation_config
118
+ generation_config = model.generation_config
119
+ generation_config.max_new_tokens = 100
120
+ generation_config.temperature = 0.7
121
+ generation_config.top_p = 0.7
122
+ generation_config.num_return_sequences = 1
123
+ generation_config.pad_token_id = tokenizer.eos_token_id
124
+ generation_config.eos_token_id = tokenizer.eos_token_id
125
+
126
+ # Define inference function
127
+ def translate_es_inclusivo(exclusive_text):
128
+
129
+ # generate input prompt
130
+ eval_prompt = f"""Reescribe el siguiente texto utilizando lenguaje inclusivo.\n
131
+ Texto: {exclusive_text}\n
132
+ Texto en lenguaje inclusivo:"""
133
+
134
+ # tokenize input
135
+ model_input = tokenizer(eval_prompt, return_tensors="pt").to(model.device)
136
+
137
+ # set max_new_tokens if necessary
138
+ if len(model_input['input_ids'][0]) > 80:
139
+ model.generation_config.max_new_tokens = len(model_input['input_ids'][0]) + 0.2 * len(model_input['input_ids'][0])
140
+
141
+ # get length of encoded prompt
142
+ prompt_token_len = len(model_input['input_ids'][0])
143
+
144
+ # generate and decode
145
+ with torch.no_grad():
146
+ inclusive_text = tokenizer.decode(model.generate(**model_input, generation_config=generation_config)[0][prompt_token_len:],
147
+ skip_special_tokens=True)
148
+
149
+ return inclusive_text
150
+
151
+ ##########
152
+
153
+ input_text = 'Los alumnos atienden a sus profesores'
154
+
155
+ print(translate_es_inclusivo(input_text))
156
+ ```python
157
+
158
+ ## Training and evaluation data
159
+
160
+ Training and evaluation data can be found in [somosnlp/es-inclusive-language](https://huggingface.co/datasets/somosnlp/es-inclusive-language)
161
+
162
+ ## Training procedure
163
+
164
+ ### Training hyperparameters
165
+
166
+ The following hyperparameters were used during training:
167
+ - learning_rate: 0.0001
168
+ - train_batch_size: 8
169
+ - eval_batch_size: 8
170
+ - seed: 42
171
+ - optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
172
+ - lr_scheduler_type: linear
173
+ - num_epochs: 10
174
+
175
+ ### Training results
176
+
177
+ | Training Loss | Epoch | Step | Validation Loss |
178
+ |:-------------:|:-----:|:----:|:---------------:|
179
+ | No log | 1.0 | 402 | 0.8020 |
180
+ | 1.0274 | 2.0 | 804 | 0.7019 |
181
+ | 0.6745 | 3.0 | 1206 | 0.6515 |
182
+ | 0.5826 | 4.0 | 1608 | 0.6236 |
183
+ | 0.5104 | 5.0 | 2010 | 0.6161 |
184
+ | 0.5104 | 6.0 | 2412 | 0.6149 |
185
+ | 0.4579 | 7.0 | 2814 | 0.6030 |
186
+ | 0.4255 | 8.0 | 3216 | 0.6151 |
187
+ | 0.3898 | 9.0 | 3618 | 0.6209 |
188
+ | 0.3771 | 10.0 | 4020 | 0.6292 |
189
+
190
+
191
+ ### Framework versions
192
+
193
+ - Transformers 4.30.0
194
+ - Pytorch 2.2.2+cu121
195
+ - Datasets 2.18.0
196
+ - Tokenizers 0.13.3