File size: 16,493 Bytes
16130a4
 
 
 
 
 
1582803
 
 
 
 
 
16130a4
1582803
 
16130a4
 
 
 
 
 
 
1582803
16130a4
 
 
 
1582803
16130a4
 
1582803
16130a4
1582803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16130a4
 
1582803
 
 
 
 
 
 
 
 
07f30b3
51693c7
16130a4
1582803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a6a63a
aca78e2
16130a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca78e2
16130a4
 
 
6a6a63a
aca78e2
16130a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca78e2
16130a4
1582803
 
 
 
34d86bd
1582803
34d86bd
1582803
16130a4
1582803
16130a4
1582803
16130a4
1582803
16130a4
1582803
 
 
 
 
 
 
 
 
16130a4
1582803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16130a4
1582803
 
 
 
 
 
 
 
 
 
 
 
16130a4
 
 
 
 
 
 
 
 
 
 
 
 
 
1582803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16130a4
1582803
16130a4
1582803
 
 
 
 
 
16130a4
 
 
9617849
1582803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9617849
1582803
55d2a9a
1582803
 
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
---
license: cc-by-nc-sa-4.0
datasets:
- somosnlp/es-inclusive-language
language:
- es
metrics:
- bleurt
- sacrebleu
pipeline_tag: text2text-generation
tags:
- social
---

# Model Card for Traductor Inclusivo

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).

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.
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.

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. 
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.
This is a tool that contributes to the fifth of the Sustainable Development Goals: Achieve gender equality and empower all women and girls.

The model works in such a way that, given an input text, it returns the original text rewritten using inclusive language.

It achieves the following results on the validation set:
- Loss: 0.6030

## Model Details

### Model Description

- **Developed by:** Andrés Martínez Fernández-Salguero, Imanuel Rozenberg, Gaia Quintana Fleitas, Miguel López Pérez y Josué Sauca
- **Funded by:** SomosNLP, HuggingFace
- **Model type:** Language model, instruction tuned
- **Language(s):** Spanish (`es-ES`, `es-AR`, `es-MX`, `es-CR`, `es-CL`)
- **License:** cc-by-nc-sa-4.0
- **Fine-tuned from model:** [projecte-aina/aguila-7b](https://huggingface.co./projecte-aina/aguila-7b)
- **Dataset used:** [somosnlp/es-inclusive-language](https://huggingface.co./datasets/somosnlp/es-inclusive-language)

### Model Sources

- **Repository:** https://github.com/Andresmfs/Traductor_inclusivo
- **Demo:** https://huggingface.co./spaces/somosnlp/es-inclusive-language-demo
- **Video presentation:** https://www.youtube.com/watch?v=7rrNGJIXEHU


## Uses

<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->

### Direct Use

<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->

The general uses of this model are adaptations of texts in Spanish to inclusive language.

It can be used to adapt news, blogposts, emails and official documents among others.

### Downstream Use [optional]

<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->

[More Information Needed]

### Out-of-Scope Use

<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->

[More Information Needed]

## Bias, Risks, and Limitations

<!-- This section is meant to convey both technical and sociotechnical limitations. -->

- Model has not been trained on long-complex texts.
- Model has been trained mostly with sentences where the terms to be modified are at the beginning of the sentence.
- Model returns only one translation option when several might also be adequate.
- Possible small information omission on translation.
- Possible forced use of the term "personas".
- Model does not detect or modify hate speech.
- Model has been trained on data mainly based on Spanish Inclusive Language Guidelines and may inherit any bias comming from the guidelines and institutions behind them. They are official and updated guidelines that should not contain strong biases.
- Model may not work propperly on translation difficulties aside the list of difficulties present on [es-inclusive-language dataset](https://huggingface.co./datasets/somosnlp/es-inclusive-language)
- Other biases coming from the train dataset [es-inclusive-language dataset](https://huggingface.co./datasets/somosnlp/es-inclusive-language) should be taken into account.


### Recommendations

<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->

<!-- Example: Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations. -->
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model.

## How to Get Started with the Model

Use the code below to get started with the model in 16-bits.

```python
from transformers import AutoTokenizer
from transformers import AutoModelForCausalLM
import torch

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained('somosnlp/', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('somosnlp/', trust_remote_code=True,
    quantization_config=bnb_config,
    device_map="auto")

# generation_config
generation_config = model.generation_config
generation_config.max_new_tokens = 100
generation_config.temperature = 0.7
generation_config.top_p = 0.7
generation_config.num_return_sequences = 1
generation_config.pad_token_id = tokenizer.eos_token_id
generation_config.eos_token_id = tokenizer.eos_token_id

# Define inference function
def translate_es_inclusivo(exclusive_text): 
    
    # generate input prompt
    eval_prompt = f"""Reescribe el siguiente texto utilizando lenguaje inclusivo.\n
      Texto: {exclusive_text}\n
      Texto en lenguaje inclusivo:"""
    
    # tokenize input
    model_input = tokenizer(eval_prompt, return_tensors="pt").to(model.device)
    
    # set max_new_tokens if necessary
    if len(model_input['input_ids'][0]) > 80:
        model.generation_config.max_new_tokens = len(model_input['input_ids'][0]) + 0.2 * len(model_input['input_ids'][0])
    
    # get length of encoded prompt
    prompt_token_len = len(model_input['input_ids'][0])
        
    # generate and decode
    with torch.no_grad():
        inclusive_text = tokenizer.decode(model.generate(**model_input, generation_config=generation_config)[0][prompt_token_len:], 
                                          skip_special_tokens=True)                                                                        
    
    return inclusive_text

##########

input_text = 'Los alumnos atienden a sus profesores'

print(translate_es_inclusivo(input_text))
```


As it is a heavy model, you may want to use it in 4-bits:

``` python
from transformers import AutoTokenizer
from transformers import AutoModelForCausalLM
from transformers import BitsAndBytesConfig
import torch


## Load model in 4bits
# bnb_configuration
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type='nf4',
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=False)


# model
model = AutoModelForCausalLM.from_pretrained('somosnlp/', trust_remote_code=True,
    quantization_config=bnb_config,
    device_map="auto")

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained('somosnlp/', trust_remote_code=True)

# generation_config
generation_config = model.generation_config
generation_config.max_new_tokens = 100
generation_config.temperature = 0.7
generation_config.top_p = 0.7
generation_config.num_return_sequences = 1
generation_config.pad_token_id = tokenizer.eos_token_id
generation_config.eos_token_id = tokenizer.eos_token_id

# Define inference function
def translate_es_inclusivo(exclusive_text): 
    
    # generate input prompt
    eval_prompt = f"""Reescribe el siguiente texto utilizando lenguaje inclusivo.\n
      Texto: {exclusive_text}\n
      Texto en lenguaje inclusivo:"""
    
    # tokenize input
    model_input = tokenizer(eval_prompt, return_tensors="pt").to(model.device)
    
    # set max_new_tokens if necessary
    if len(model_input['input_ids'][0]) > 80:
        model.generation_config.max_new_tokens = len(model_input['input_ids'][0]) + 0.2 * len(model_input['input_ids'][0])
    
    # get length of encoded prompt
    prompt_token_len = len(model_input['input_ids'][0])
        
    # generate and decode
    with torch.no_grad():
        inclusive_text = tokenizer.decode(model.generate(**model_input, generation_config=generation_config)[0][prompt_token_len:], 
                                          skip_special_tokens=True)                                                                        
    
    return inclusive_text

##########

input_text = 'Los alumnos atienden a sus profesores'

print(translate_es_inclusivo(input_text))
```

## Training Details
### Training Data

<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->

Train, validation and test data splits can be found in [somosnlp/es-inclusive-language](https://huggingface.co./datasets/somosnlp/es-inclusive-language)

### Training Procedure

<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->

<!-- Detallar la técnica de entrenamiento utilizada y enlazar los scripts/notebooks. -->

For training we used QLoRA technique in 4-bits and rank 8

Find the training script [here](https://github.com/Andresmfs/Traductor_inclusivo/tree/master)

#### Preprocessing [optional]

[More Information Needed]

#### Training Hyperparameters

<!-- Enumerar los valores de los hiperparámetros de entrenamiento. -->
The following hyperparameters were used during training:
- **learning_rate:** 0.0001
- **train_batch_size:** 8
- **eval_batch_size:** 8
- **seed:** 42
- **optimizer:** Adam with betas=(0.9,0.999) and epsilon=1e-08
- **lr_scheduler_type:** linear
- **num_epochs:** 10
- **Training regime:** fp16 mixed precision

#### Speeds, Sizes, Times [optional]

<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->

[More Information Needed]


## Evaluation

<!-- This section describes the evaluation protocols and provides the results. -->

### Testing Data, Factors & Metrics

#### Testing Data
Here you can find the [validation set](https://huggingface.co./datasets/somosnlp/es-inclusive-language/viewer/default/validation) used during training.
Here you can find the [test set](https://huggingface.co./datasets/somosnlp/es-inclusive-language/viewer/default/test) used for evaluating model errors.

#### Factors

<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->

[More Information Needed]

#### Metrics

For test evaluation it has been used a weighted harmonic mean of metrics [bleurt](https://huggingface.co./spaces/evaluate-metric/bleurt) (60%) and [Sacrebleu](https://huggingface.co./spaces/evaluate-metric/sacrebleu) (40%).

In _Sacrebleu_ metric grammatical correctness carries high weight compared to the actual words used, whereas in _Bleurt_ metric the actual words used have higher weight over grammatical correctness.
Combining both metrics, we account for a grammatically correct prediction together with the use of the required specific words.

### Results

<!-- Enlazar aquí los scripts/notebooks de evaluación y especificar los resultados. -->

| Training Loss | Epoch | Step | Validation Loss |
|:-------------:|:-----:|:----:|:---------------:|
| No log        | 1.0   | 402  | 0.8020          |
| 1.0274        | 2.0   | 804  | 0.7019          |
| 0.6745        | 3.0   | 1206 | 0.6515          |
| 0.5826        | 4.0   | 1608 | 0.6236          |
| 0.5104        | 5.0   | 2010 | 0.6161          |
| 0.5104        | 6.0   | 2412 | 0.6149          |
| 0.4579        | 7.0   | 2814 | 0.6030          |
| 0.4255        | 8.0   | 3216 | 0.6151          |
| 0.3898        | 9.0   | 3618 | 0.6209          |
| 0.3771        | 10.0  | 4020 | 0.6292          |

On [this notebook](https://github.com/Andresmfs/Traductor_inclusivo/blob/master/Error%20analysis.ipynb) you can find the results of the test evaluation.

We get an average score of 68.4 (measured with the above described metric). 
Due to the existence of equivalent language formulas (these are inclusive language formulas that can be used indistinctly and the choice of a formula over the other is rather a stylistic decision than a language correctness decision) it is possible to argue that the real score of the model is higher.

## Model Examination [optional]

<!-- Relevant interpretability work for the model goes here. -->

[More Information Needed]

## Environmental Impact

<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly. -->

<!-- Rellenar la información de la lista y calcular las emisiones con la página mencionada. -->

Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).

- **Hardware Type:** Nvidia T4 medium (8 vCPU, 30 Gb RAM, 16 Gb VRAM)
- **Hours used:** 3 hours
- **Cloud Provider:** Google Cloud Platform
- **Compute Region:** [More Information Needed]
- **Carbon Emitted:** [More Information Needed]

## Technical Specifications [optional]

<!-- Esta sección es opcional porque seguramente ya habéis mencionado estos detalles más arriba, igualmente está bien incluirlos aquí de nuevo como bullet points a modo de resumen. -->

### Model Architecture and Objective

[More Information Needed]

### Compute Infrastructure

[More Information Needed]

#### Hardware

<!-- Indicar el hardware utilizado, podéis agradecer aquí a quien lo patrocinó. -->
Hardware used was Nvidia T4 medium (8 vCPU, 30 Gb RAM, 16 Gb VRAM) patrocinated by Hugging Face

#### Software
- Transformers 4.30.0
- Pytorch 2.2.2+cu121
- Datasets 2.18.0
- Tokenizers 0.13.3
- Peft

## License
<!-- Indicar bajo qué licencia se libera el modelo explicando, si no es apache 2.0, a qué se debe la licencia más restrictiva (i.e. herencia de las licencias del modelo pre-entrenado o de los datos utilizados). -->
Creative Commons (cc-by-nc-sa-4.0)

This kind of license is inherited from dataset used for training.

## Citation

**BibTeX:**

```
@software{AIGMJ2024TraductorInclusivo,
  author = {Andrés Martínez Fernández-Salguero, Imanuel Rozenberg, Gaia Quintana Fleitas, Miguel López Pérez, Josué Sauca},
  title = {TraductorInclusivo},
  month = April,
  year = 2024,
  url = {https://huggingface.co./somosnlp/es-inclusivo-translator}
}
```

- AIGMJ2024TraductorInclusivo
- author: Andrés Martínez Fernández-Salguero, Imanuel Rozenberg, Gaia Quintana Fleitas, Miguel López Pérez, Josué Sauca
- title: Traductor Inclusivo
- year: 2024
- url: https://huggingface.co./somosnlp/es-inclusivo-translator



## Glossary [optional]

<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->

## More Information

<!-- Indicar aquí que el marco en el que se desarrolló el proyecto, en esta sección podéis incluir agradecimientos y más información sobre los miembros del equipo. Podéis adaptar el ejemplo a vuestro gusto. -->

This project was developed during the [Hackathon #Somos600M](https://somosnlp.org/hackathon) organized by SomosNLP. The model was trained using GPUs sponsored by HuggingFace.

**Team:** 
- [**Andrés Martínez Fernández-Salguero**](https://huggingface.co./Andresmfs)
- **Imanuel Rozenberg**
- **Gaia Quintana Fleitas**
- **Miguel López Pérez**
- **Josué Sauca**

## Contact

- [**Andrés Martínez Fernández-Salguero**](www.linkedin.com/in/andrés-martínez-fernández-salguero-725674214) ([email protected])
- [**Gaia Quintana Fleitas**](https://www.linkedin.com/in/gaiaquintana/) ([email protected])