Datasets:

Modalities:
Text
Formats:
csv
ArXiv:
DOI:
Libraries:
Datasets
pandas
License:
lheuveline commited on
Commit
b073070
·
1 Parent(s): 813cd99

dataset translation notebook

Browse files
Files changed (1) hide show
  1. notebooks/DatasetTranslation.ipynb +837 -0
notebooks/DatasetTranslation.ipynb ADDED
@@ -0,0 +1,837 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "f5e0b745",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
11
+ "from transformers import AutoModel, AutoTokenizer\n",
12
+ "from transformers import MarianMTModel\n",
13
+ "from transformers import XLMRobertaForSequenceClassification\n",
14
+ "\n",
15
+ "\n",
16
+ "import os\n",
17
+ "import pandas as pd\n",
18
+ "from tqdm import tqdm\n",
19
+ "\n",
20
+ "tqdm.pandas()"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 2,
26
+ "id": "6e228528",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "def detect_language(model, tokenizer, text):\n",
31
+ " \n",
32
+ " token_dict = tokenizer(text, return_tensors=\"pt\").to(\"cuda\")\n",
33
+ " outputs = model(token_dict.input_ids)\n",
34
+ " decoded = outputs.logits.argmax(-1).item()\n",
35
+ " lang = model.config.id2label[decoded]\n",
36
+ " return lang\n",
37
+ "\n",
38
+ "def translate(model, tokenizer, text):\n",
39
+ " input_ids = tokenizer(text, return_tensors=\"pt\").input_ids.to(\"cuda\")\n",
40
+ " outputs = model.generate(input_ids)\n",
41
+ " decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
42
+ " return decoded"
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": 24,
48
+ "id": "454ae3b6",
49
+ "metadata": {},
50
+ "outputs": [
51
+ {
52
+ "data": {
53
+ "text/html": [
54
+ "<div>\n",
55
+ "<style scoped>\n",
56
+ " .dataframe tbody tr th:only-of-type {\n",
57
+ " vertical-align: middle;\n",
58
+ " }\n",
59
+ "\n",
60
+ " .dataframe tbody tr th {\n",
61
+ " vertical-align: top;\n",
62
+ " }\n",
63
+ "\n",
64
+ " .dataframe thead th {\n",
65
+ " text-align: right;\n",
66
+ " }\n",
67
+ "</style>\n",
68
+ "<table border=\"1\" class=\"dataframe\">\n",
69
+ " <thead>\n",
70
+ " <tr style=\"text-align: right;\">\n",
71
+ " <th></th>\n",
72
+ " <th>text</th>\n",
73
+ " <th>label</th>\n",
74
+ " </tr>\n",
75
+ " <tr>\n",
76
+ " <th>INDEX</th>\n",
77
+ " <th></th>\n",
78
+ " <th></th>\n",
79
+ " </tr>\n",
80
+ " </thead>\n",
81
+ " <tbody>\n",
82
+ " <tr>\n",
83
+ " <th>0</th>\n",
84
+ " <td>Maybe the UN could talk to those asian and afr...</td>\n",
85
+ " <td>1</td>\n",
86
+ " </tr>\n",
87
+ " <tr>\n",
88
+ " <th>1</th>\n",
89
+ " <td>‘Juice (Jews) are worse that nukes.’ Caption t...</td>\n",
90
+ " <td>1</td>\n",
91
+ " </tr>\n",
92
+ " <tr>\n",
93
+ " <th>2</th>\n",
94
+ " <td>Foreigners on UK benefits leaps 41% in 5 years...</td>\n",
95
+ " <td>1</td>\n",
96
+ " </tr>\n",
97
+ " <tr>\n",
98
+ " <th>3</th>\n",
99
+ " <td>Jews cannot be patriots, since their allegianc...</td>\n",
100
+ " <td>1</td>\n",
101
+ " </tr>\n",
102
+ " <tr>\n",
103
+ " <th>4</th>\n",
104
+ " <td>Israel, Zionism and Jewish Supremacist control...</td>\n",
105
+ " <td>1</td>\n",
106
+ " </tr>\n",
107
+ " <tr>\n",
108
+ " <th>...</th>\n",
109
+ " <td>...</td>\n",
110
+ " <td>...</td>\n",
111
+ " </tr>\n",
112
+ " <tr>\n",
113
+ " <th>4998</th>\n",
114
+ " <td>Woman are basically childlike.</td>\n",
115
+ " <td>1</td>\n",
116
+ " </tr>\n",
117
+ " <tr>\n",
118
+ " <th>4999</th>\n",
119
+ " <td>women should be submissive to men.</td>\n",
120
+ " <td>1</td>\n",
121
+ " </tr>\n",
122
+ " <tr>\n",
123
+ " <th>5000</th>\n",
124
+ " <td>Women are essentially childlike, unable to und...</td>\n",
125
+ " <td>1</td>\n",
126
+ " </tr>\n",
127
+ " <tr>\n",
128
+ " <th>5001</th>\n",
129
+ " <td>Women should be submissive to men.</td>\n",
130
+ " <td>1</td>\n",
131
+ " </tr>\n",
132
+ " <tr>\n",
133
+ " <th>5002</th>\n",
134
+ " <td>Women should not be allowed to vote, this is a...</td>\n",
135
+ " <td>1</td>\n",
136
+ " </tr>\n",
137
+ " </tbody>\n",
138
+ "</table>\n",
139
+ "<p>5003 rows × 2 columns</p>\n",
140
+ "</div>"
141
+ ],
142
+ "text/plain": [
143
+ " text label\n",
144
+ "INDEX \n",
145
+ "0 Maybe the UN could talk to those asian and afr... 1\n",
146
+ "1 ‘Juice (Jews) are worse that nukes.’ Caption t... 1\n",
147
+ "2 Foreigners on UK benefits leaps 41% in 5 years... 1\n",
148
+ "3 Jews cannot be patriots, since their allegianc... 1\n",
149
+ "4 Israel, Zionism and Jewish Supremacist control... 1\n",
150
+ "... ... ...\n",
151
+ "4998 Woman are basically childlike. 1\n",
152
+ "4999 women should be submissive to men. 1\n",
153
+ "5000 Women are essentially childlike, unable to und... 1\n",
154
+ "5001 Women should be submissive to men. 1\n",
155
+ "5002 Women should not be allowed to vote, this is a... 1\n",
156
+ "\n",
157
+ "[5003 rows x 2 columns]"
158
+ ]
159
+ },
160
+ "execution_count": 24,
161
+ "metadata": {},
162
+ "output_type": "execute_result"
163
+ }
164
+ ],
165
+ "source": [
166
+ "DATASET_PATH = \"../data/processed/\"\n",
167
+ "\n",
168
+ "df = pd.read_csv(DATASET_PATH, index_col=[0])\n",
169
+ "df"
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "execution_count": 9,
175
+ "id": "f2db3a80",
176
+ "metadata": {
177
+ "scrolled": true
178
+ },
179
+ "outputs": [
180
+ {
181
+ "data": {
182
+ "text/plain": [
183
+ "0 2.0\n",
184
+ "1 2.0\n",
185
+ "2 4.0\n",
186
+ "3 2.0\n",
187
+ "4 4.0\n",
188
+ " ... \n",
189
+ "135551 2.0\n",
190
+ "135552 2.0\n",
191
+ "135553 1.0\n",
192
+ "135554 2.0\n",
193
+ "135555 2.0\n",
194
+ "Name: status, Length: 135556, dtype: float64"
195
+ ]
196
+ },
197
+ "execution_count": 9,
198
+ "metadata": {},
199
+ "output_type": "execute_result"
200
+ }
201
+ ],
202
+ "source": [
203
+ "# PREPROCESSING\n",
204
+ "# Need more preprocessing :\n",
205
+ "# - stopwords : Name, URL\n",
206
+ "\n",
207
+ "# Worst 10%\n",
208
+ "# df = df.loc[df.hate_speech_score > df.hate_speech_score.quantile(0.90)]\n",
209
+ "\n",
210
+ "# Get negative examples\n",
211
+ "# n = 12785 # = number of gathered worst examples\n",
212
+ "# df = df.loc[df.hate_speech_score < df.hate_speech_score.quantile(0.1)].iloc[:n]\n"
213
+ ]
214
+ },
215
+ {
216
+ "cell_type": "markdown",
217
+ "id": "5ca78b94",
218
+ "metadata": {},
219
+ "source": [
220
+ "#### Language detection"
221
+ ]
222
+ },
223
+ {
224
+ "cell_type": "code",
225
+ "execution_count": 21,
226
+ "id": "69fc7f94",
227
+ "metadata": {},
228
+ "outputs": [],
229
+ "source": [
230
+ "# Language detection model\n",
231
+ "tokenizer = AutoTokenizer.from_pretrained(\"papluca/xlm-roberta-base-language-detection\")\n",
232
+ "model = XLMRobertaForSequenceClassification.from_pretrained(\"papluca/xlm-roberta-base-language-detection\").to(\"cuda\")"
233
+ ]
234
+ },
235
+ {
236
+ "cell_type": "code",
237
+ "execution_count": 59,
238
+ "id": "f05bbff2",
239
+ "metadata": {
240
+ "scrolled": false
241
+ },
242
+ "outputs": [
243
+ {
244
+ "name": "stderr",
245
+ "output_type": "stream",
246
+ "text": [
247
+ "100%|████████████████████████████████████████████████████████████████████████████████████████████| 135556/135556 [18:54<00:00, 119.45it/s]\n"
248
+ ]
249
+ }
250
+ ],
251
+ "source": [
252
+ "# Add language prediction for later filtering before applying language-dependent translation model\n",
253
+ "df[\"lang\"] = df.text.progress_apply(lambda text: detect_language(model, tokenizer, text))"
254
+ ]
255
+ },
256
+ {
257
+ "cell_type": "markdown",
258
+ "id": "05d07356",
259
+ "metadata": {},
260
+ "source": [
261
+ "#### Text translation\n",
262
+ "\n",
263
+ "- Process requires long processing time\n",
264
+ "- Process should be run multiple times to complete"
265
+ ]
266
+ },
267
+ {
268
+ "cell_type": "code",
269
+ "execution_count": 19,
270
+ "id": "a82a5eb3",
271
+ "metadata": {},
272
+ "outputs": [
273
+ {
274
+ "name": "stdout",
275
+ "output_type": "stream",
276
+ "text": [
277
+ "zsh:1: no matches found: temp/*\r\n"
278
+ ]
279
+ }
280
+ ],
281
+ "source": [
282
+ "! rm -rf temp/*"
283
+ ]
284
+ },
285
+ {
286
+ "cell_type": "code",
287
+ "execution_count": 25,
288
+ "id": "3f3f1bfb",
289
+ "metadata": {},
290
+ "outputs": [],
291
+ "source": [
292
+ "def write_file(path, text):\n",
293
+ " with open(path, \"w\") as f:\n",
294
+ " f.write(text)\n",
295
+ "\n",
296
+ "LANGUAGE = \"en\"\n",
297
+ "OUTPUT_DIR = \"../data/translated\"\n",
298
+ "TEMP_DIR = \"../data/temp\"\n",
299
+ "IN_MEMORY = True\n",
300
+ "\n",
301
+ "output_dir = os.path.join(OUTPUT_DIR, LANGUAGE)\n",
302
+ "if not os.path.exists(OUTPUT_DIR):\n",
303
+ " os.makedirs(output_dir)\n",
304
+ "# os.mkdir(OUTPUT_DIR)\n",
305
+ "if not os.path.exists(TEMP_DIR):\n",
306
+ " os.mkdir(TEMP_DIR)"
307
+ ]
308
+ },
309
+ {
310
+ "cell_type": "code",
311
+ "execution_count": 7,
312
+ "id": "b8122256",
313
+ "metadata": {},
314
+ "outputs": [],
315
+ "source": [
316
+ "# Translation model\n",
317
+ "tokenizer = AutoTokenizer.from_pretrained(\"Abelll/marian-finetuned-kde4-en-to-fr\")\n",
318
+ "model = MarianMTModel.from_pretrained(\"Abelll/marian-finetuned-kde4-en-to-fr\").to(\"cuda\")"
319
+ ]
320
+ },
321
+ {
322
+ "cell_type": "code",
323
+ "execution_count": 26,
324
+ "id": "b9ef106d",
325
+ "metadata": {
326
+ "scrolled": true
327
+ },
328
+ "outputs": [
329
+ {
330
+ "name": "stderr",
331
+ "output_type": "stream",
332
+ "text": [
333
+ " 0%| | 0/5003 [00:00<?, ?it/s]/home/louis/miniconda3/envs/sexism_detection/lib/python3.9/site-packages/transformers/generation/utils.py:1387: UserWarning: Neither `max_length` nor `max_new_tokens` has been set, `max_length` will default to 512 (`self.config.max_length`). Controlling `max_length` via the config is deprecated and `max_length` will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n",
334
+ " warnings.warn(\n",
335
+ "100%|███████████████████████████████████████████████████████████████████████████████████| 5003/5003 [10:27<00:00, 7.97it/s]\n"
336
+ ]
337
+ }
338
+ ],
339
+ "source": [
340
+ "translated_texts = []\n",
341
+ "for i, row in tqdm(df.iterrows(), total=df.shape[0]):\n",
342
+ " output_path = os.path.join(TEMP_DIR, str(i) + \".txt\")\n",
343
+ " if not IN_MEMORY:\n",
344
+ " if os.path.exists(output_path):\n",
345
+ " print(\"Already translated !\")\n",
346
+ " continue\n",
347
+ " translated = translate(model, tokenizer, row.text)\n",
348
+ " if IN_MEMORY:\n",
349
+ " translated_texts.append((i, translated))\n",
350
+ " else:\n",
351
+ " write_file(output_path, translated)"
352
+ ]
353
+ },
354
+ {
355
+ "cell_type": "code",
356
+ "execution_count": 29,
357
+ "id": "0ecc3000",
358
+ "metadata": {},
359
+ "outputs": [],
360
+ "source": [
361
+ "import glob\n",
362
+ "\n",
363
+ "output_filename = DATASET_PATH\n",
364
+ "\n",
365
+ "if not IN_MEMORY:\n",
366
+ " filelist = glob.glob(TEMP_DIR + \"/*\")\n",
367
+ " print(f\"Found {len(filelist)}\")\n",
368
+ " filelist = sorted(filelist, key=lambda x: int(x.split(\"/\")[-1].split(\".\")[0]))\n",
369
+ " translated_texts = []\n",
370
+ " for text_file in tqdm(filelist):\n",
371
+ " with open(text_file) as f:\n",
372
+ " translated_text = f.read().splitlines()\n",
373
+ " translated_texts.append(translated_text)\n",
374
+ " translated_texts = pd.DataFrame(translated_texts)\n",
375
+ " df[\"translated\"] = translated_texts\n",
376
+ "\n",
377
+ "df[\"translated\"] = translated_texts\n",
378
+ "df.translated = df.translated.apply(lambda x: x[1])\n",
379
+ "df.dropna().to_csv(output_filename)"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": 30,
385
+ "id": "88c69fd0",
386
+ "metadata": {},
387
+ "outputs": [
388
+ {
389
+ "data": {
390
+ "text/html": [
391
+ "<div>\n",
392
+ "<style scoped>\n",
393
+ " .dataframe tbody tr th:only-of-type {\n",
394
+ " vertical-align: middle;\n",
395
+ " }\n",
396
+ "\n",
397
+ " .dataframe tbody tr th {\n",
398
+ " vertical-align: top;\n",
399
+ " }\n",
400
+ "\n",
401
+ " .dataframe thead th {\n",
402
+ " text-align: right;\n",
403
+ " }\n",
404
+ "</style>\n",
405
+ "<table border=\"1\" class=\"dataframe\">\n",
406
+ " <thead>\n",
407
+ " <tr style=\"text-align: right;\">\n",
408
+ " <th></th>\n",
409
+ " <th>text</th>\n",
410
+ " <th>label</th>\n",
411
+ " <th>translated</th>\n",
412
+ " </tr>\n",
413
+ " <tr>\n",
414
+ " <th>INDEX</th>\n",
415
+ " <th></th>\n",
416
+ " <th></th>\n",
417
+ " <th></th>\n",
418
+ " </tr>\n",
419
+ " </thead>\n",
420
+ " <tbody>\n",
421
+ " <tr>\n",
422
+ " <th>0</th>\n",
423
+ " <td>Maybe the UN could talk to those asian and afr...</td>\n",
424
+ " <td>1</td>\n",
425
+ " <td>Peut-être que l'Onu pourrait parler aux nation...</td>\n",
426
+ " </tr>\n",
427
+ " <tr>\n",
428
+ " <th>1</th>\n",
429
+ " <td>‘Juice (Jews) are worse that nukes.’ Caption t...</td>\n",
430
+ " <td>1</td>\n",
431
+ " <td>« Juice (Juifs) sont pire que les nuées. « &amp; #...</td>\n",
432
+ " </tr>\n",
433
+ " <tr>\n",
434
+ " <th>2</th>\n",
435
+ " <td>Foreigners on UK benefits leaps 41% in 5 years...</td>\n",
436
+ " <td>1</td>\n",
437
+ " <td>Les étrangers au Royaume-Uni bénéficient d'un ...</td>\n",
438
+ " </tr>\n",
439
+ " <tr>\n",
440
+ " <th>3</th>\n",
441
+ " <td>Jews cannot be patriots, since their allegianc...</td>\n",
442
+ " <td>1</td>\n",
443
+ " <td>Les Juifs ne peuvent pas être patriotes, car l...</td>\n",
444
+ " </tr>\n",
445
+ " <tr>\n",
446
+ " <th>4</th>\n",
447
+ " <td>Israel, Zionism and Jewish Supremacist control...</td>\n",
448
+ " <td>1</td>\n",
449
+ " <td>Israël, le sionisme et le contrôle suprémacist...</td>\n",
450
+ " </tr>\n",
451
+ " <tr>\n",
452
+ " <th>...</th>\n",
453
+ " <td>...</td>\n",
454
+ " <td>...</td>\n",
455
+ " <td>...</td>\n",
456
+ " </tr>\n",
457
+ " <tr>\n",
458
+ " <th>4998</th>\n",
459
+ " <td>Woman are basically childlike.</td>\n",
460
+ " <td>1</td>\n",
461
+ " <td>La femme ressemble à un enfant.</td>\n",
462
+ " </tr>\n",
463
+ " <tr>\n",
464
+ " <th>4999</th>\n",
465
+ " <td>women should be submissive to men.</td>\n",
466
+ " <td>1</td>\n",
467
+ " <td>Les filles doivent être soumises aux hommes.</td>\n",
468
+ " </tr>\n",
469
+ " <tr>\n",
470
+ " <th>5000</th>\n",
471
+ " <td>Women are essentially childlike, unable to und...</td>\n",
472
+ " <td>1</td>\n",
473
+ " <td>Les filles sont pour l'essentiel des filles, i...</td>\n",
474
+ " </tr>\n",
475
+ " <tr>\n",
476
+ " <th>5001</th>\n",
477
+ " <td>Women should be submissive to men.</td>\n",
478
+ " <td>1</td>\n",
479
+ " <td>Les filles doivent être soumises aux hommes.</td>\n",
480
+ " </tr>\n",
481
+ " <tr>\n",
482
+ " <th>5002</th>\n",
483
+ " <td>Women should not be allowed to vote, this is a...</td>\n",
484
+ " <td>1</td>\n",
485
+ " <td>Les femmes ne devraient pas être autorisées à ...</td>\n",
486
+ " </tr>\n",
487
+ " </tbody>\n",
488
+ "</table>\n",
489
+ "<p>5003 rows × 3 columns</p>\n",
490
+ "</div>"
491
+ ],
492
+ "text/plain": [
493
+ " text label \\\n",
494
+ "INDEX \n",
495
+ "0 Maybe the UN could talk to those asian and afr... 1 \n",
496
+ "1 ‘Juice (Jews) are worse that nukes.’ Caption t... 1 \n",
497
+ "2 Foreigners on UK benefits leaps 41% in 5 years... 1 \n",
498
+ "3 Jews cannot be patriots, since their allegianc... 1 \n",
499
+ "4 Israel, Zionism and Jewish Supremacist control... 1 \n",
500
+ "... ... ... \n",
501
+ "4998 Woman are basically childlike. 1 \n",
502
+ "4999 women should be submissive to men. 1 \n",
503
+ "5000 Women are essentially childlike, unable to und... 1 \n",
504
+ "5001 Women should be submissive to men. 1 \n",
505
+ "5002 Women should not be allowed to vote, this is a... 1 \n",
506
+ "\n",
507
+ " translated \n",
508
+ "INDEX \n",
509
+ "0 Peut-être que l'Onu pourrait parler aux nation... \n",
510
+ "1 « Juice (Juifs) sont pire que les nuées. « & #... \n",
511
+ "2 Les étrangers au Royaume-Uni bénéficient d'un ... \n",
512
+ "3 Les Juifs ne peuvent pas être patriotes, car l... \n",
513
+ "4 Israël, le sionisme et le contrôle suprémacist... \n",
514
+ "... ... \n",
515
+ "4998 La femme ressemble à un enfant. \n",
516
+ "4999 Les filles doivent être soumises aux hommes. \n",
517
+ "5000 Les filles sont pour l'essentiel des filles, i... \n",
518
+ "5001 Les filles doivent être soumises aux hommes. \n",
519
+ "5002 Les femmes ne devraient pas être autorisées à ... \n",
520
+ "\n",
521
+ "[5003 rows x 3 columns]"
522
+ ]
523
+ },
524
+ "execution_count": 30,
525
+ "metadata": {},
526
+ "output_type": "execute_result"
527
+ }
528
+ ],
529
+ "source": [
530
+ "df"
531
+ ]
532
+ },
533
+ {
534
+ "cell_type": "code",
535
+ "execution_count": 12,
536
+ "id": "885ebb18",
537
+ "metadata": {},
538
+ "outputs": [],
539
+ "source": [
540
+ "df.to_csv(\"../data/ucberkeley-dlab_measuring-hate-speech_translated_positive.csv\")"
541
+ ]
542
+ },
543
+ {
544
+ "cell_type": "code",
545
+ "execution_count": 65,
546
+ "id": "576a76f1",
547
+ "metadata": {},
548
+ "outputs": [
549
+ {
550
+ "name": "stdout",
551
+ "output_type": "stream",
552
+ "text": [
553
+ "translate English to French: for starters bend over the one in pink and kick that ass and pussy to get a taste until she's begging for a dick inside her.\n"
554
+ ]
555
+ },
556
+ {
557
+ "data": {
558
+ "text/plain": [
559
+ "\"j'ai lu cette vidéo et j'ai\""
560
+ ]
561
+ },
562
+ "execution_count": 65,
563
+ "metadata": {},
564
+ "output_type": "execute_result"
565
+ }
566
+ ],
567
+ "source": [
568
+ "def translate(text):\n",
569
+ " input_text = f\"translate {BASE_LANGUAGE} to French: \" + text.lower()\n",
570
+ " print(input_text)\n",
571
+ " input_ids = tokenizer(input_text, return_tensors=\"pt\").input_ids\n",
572
+ " outputs = model.generate(input_ids)\n",
573
+ " decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
574
+ " return decoded\n",
575
+ "\n",
576
+ "translate(df.text[4])"
577
+ ]
578
+ },
579
+ {
580
+ "cell_type": "code",
581
+ "execution_count": 8,
582
+ "id": "1bbc6dab",
583
+ "metadata": {},
584
+ "outputs": [
585
+ {
586
+ "data": {
587
+ "text/plain": [
588
+ "MarianMTModel(\n",
589
+ " (model): MarianModel(\n",
590
+ " (shared): Embedding(59514, 512, padding_idx=59513)\n",
591
+ " (encoder): MarianEncoder(\n",
592
+ " (embed_tokens): Embedding(59514, 512, padding_idx=59513)\n",
593
+ " (embed_positions): MarianSinusoidalPositionalEmbedding(512, 512)\n",
594
+ " (layers): ModuleList(\n",
595
+ " (0): MarianEncoderLayer(\n",
596
+ " (self_attn): MarianAttention(\n",
597
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
598
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
599
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
600
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
601
+ " )\n",
602
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
603
+ " (activation_fn): SiLUActivation()\n",
604
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
605
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
606
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
607
+ " )\n",
608
+ " (1): MarianEncoderLayer(\n",
609
+ " (self_attn): MarianAttention(\n",
610
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
611
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
612
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
613
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
614
+ " )\n",
615
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
616
+ " (activation_fn): SiLUActivation()\n",
617
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
618
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
619
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
620
+ " )\n",
621
+ " (2): MarianEncoderLayer(\n",
622
+ " (self_attn): MarianAttention(\n",
623
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
624
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
625
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
626
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
627
+ " )\n",
628
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
629
+ " (activation_fn): SiLUActivation()\n",
630
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
631
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
632
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
633
+ " )\n",
634
+ " (3): MarianEncoderLayer(\n",
635
+ " (self_attn): MarianAttention(\n",
636
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
637
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
638
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
639
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
640
+ " )\n",
641
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
642
+ " (activation_fn): SiLUActivation()\n",
643
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
644
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
645
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
646
+ " )\n",
647
+ " (4): MarianEncoderLayer(\n",
648
+ " (self_attn): MarianAttention(\n",
649
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
650
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
651
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
652
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
653
+ " )\n",
654
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
655
+ " (activation_fn): SiLUActivation()\n",
656
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
657
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
658
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
659
+ " )\n",
660
+ " (5): MarianEncoderLayer(\n",
661
+ " (self_attn): MarianAttention(\n",
662
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
663
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
664
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
665
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
666
+ " )\n",
667
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
668
+ " (activation_fn): SiLUActivation()\n",
669
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
670
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
671
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
672
+ " )\n",
673
+ " )\n",
674
+ " )\n",
675
+ " (decoder): MarianDecoder(\n",
676
+ " (embed_tokens): Embedding(59514, 512, padding_idx=59513)\n",
677
+ " (embed_positions): MarianSinusoidalPositionalEmbedding(512, 512)\n",
678
+ " (layers): ModuleList(\n",
679
+ " (0): MarianDecoderLayer(\n",
680
+ " (self_attn): MarianAttention(\n",
681
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
682
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
683
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
684
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
685
+ " )\n",
686
+ " (activation_fn): SiLUActivation()\n",
687
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
688
+ " (encoder_attn): MarianAttention(\n",
689
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
690
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
691
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
692
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
693
+ " )\n",
694
+ " (encoder_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
695
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
696
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
697
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
698
+ " )\n",
699
+ " (1): MarianDecoderLayer(\n",
700
+ " (self_attn): MarianAttention(\n",
701
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
702
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
703
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
704
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
705
+ " )\n",
706
+ " (activation_fn): SiLUActivation()\n",
707
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
708
+ " (encoder_attn): MarianAttention(\n",
709
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
710
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
711
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
712
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
713
+ " )\n",
714
+ " (encoder_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
715
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
716
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
717
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
718
+ " )\n",
719
+ " (2): MarianDecoderLayer(\n",
720
+ " (self_attn): MarianAttention(\n",
721
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
722
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
723
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
724
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
725
+ " )\n",
726
+ " (activation_fn): SiLUActivation()\n",
727
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
728
+ " (encoder_attn): MarianAttention(\n",
729
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
730
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
731
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
732
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
733
+ " )\n",
734
+ " (encoder_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
735
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
736
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
737
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
738
+ " )\n",
739
+ " (3): MarianDecoderLayer(\n",
740
+ " (self_attn): MarianAttention(\n",
741
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
742
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
743
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
744
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
745
+ " )\n",
746
+ " (activation_fn): SiLUActivation()\n",
747
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
748
+ " (encoder_attn): MarianAttention(\n",
749
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
750
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
751
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
752
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
753
+ " )\n",
754
+ " (encoder_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
755
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
756
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
757
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
758
+ " )\n",
759
+ " (4): MarianDecoderLayer(\n",
760
+ " (self_attn): MarianAttention(\n",
761
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
762
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
763
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
764
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
765
+ " )\n",
766
+ " (activation_fn): SiLUActivation()\n",
767
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
768
+ " (encoder_attn): MarianAttention(\n",
769
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
770
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
771
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
772
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
773
+ " )\n",
774
+ " (encoder_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
775
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
776
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
777
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
778
+ " )\n",
779
+ " (5): MarianDecoderLayer(\n",
780
+ " (self_attn): MarianAttention(\n",
781
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
782
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
783
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
784
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
785
+ " )\n",
786
+ " (activation_fn): SiLUActivation()\n",
787
+ " (self_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
788
+ " (encoder_attn): MarianAttention(\n",
789
+ " (k_proj): Linear(in_features=512, out_features=512, bias=True)\n",
790
+ " (v_proj): Linear(in_features=512, out_features=512, bias=True)\n",
791
+ " (q_proj): Linear(in_features=512, out_features=512, bias=True)\n",
792
+ " (out_proj): Linear(in_features=512, out_features=512, bias=True)\n",
793
+ " )\n",
794
+ " (encoder_attn_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
795
+ " (fc1): Linear(in_features=512, out_features=2048, bias=True)\n",
796
+ " (fc2): Linear(in_features=2048, out_features=512, bias=True)\n",
797
+ " (final_layer_norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)\n",
798
+ " )\n",
799
+ " )\n",
800
+ " )\n",
801
+ " )\n",
802
+ " (lm_head): Linear(in_features=512, out_features=59514, bias=False)\n",
803
+ ")"
804
+ ]
805
+ },
806
+ "execution_count": 8,
807
+ "metadata": {},
808
+ "output_type": "execute_result"
809
+ }
810
+ ],
811
+ "source": [
812
+ "model"
813
+ ]
814
+ }
815
+ ],
816
+ "metadata": {
817
+ "kernelspec": {
818
+ "display_name": "sexism_detection",
819
+ "language": "python",
820
+ "name": "sexism_detection"
821
+ },
822
+ "language_info": {
823
+ "codemirror_mode": {
824
+ "name": "ipython",
825
+ "version": 3
826
+ },
827
+ "file_extension": ".py",
828
+ "mimetype": "text/x-python",
829
+ "name": "python",
830
+ "nbconvert_exporter": "python",
831
+ "pygments_lexer": "ipython3",
832
+ "version": "3.9.15"
833
+ }
834
+ },
835
+ "nbformat": 4,
836
+ "nbformat_minor": 5
837
+ }