{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f5e0b745", "metadata": {}, "outputs": [], "source": [ "from transformers import T5Tokenizer, T5ForConditionalGeneration\n", "from transformers import AutoModel, AutoTokenizer\n", "from transformers import MarianMTModel\n", "from transformers import XLMRobertaForSequenceClassification\n", "\n", "\n", "import os\n", "import pandas as pd\n", "from tqdm import tqdm\n", "\n", "tqdm.pandas()" ] }, { "cell_type": "code", "execution_count": 2, "id": "6e228528", "metadata": {}, "outputs": [], "source": [ "def detect_language(model, tokenizer, text):\n", " \n", " token_dict = tokenizer(text, return_tensors=\"pt\").to(\"cuda\")\n", " outputs = model(token_dict.input_ids)\n", " decoded = outputs.logits.argmax(-1).item()\n", " lang = model.config.id2label[decoded]\n", " return lang\n", "\n", "def translate(model, tokenizer, text):\n", " input_ids = tokenizer(text, return_tensors=\"pt\").input_ids.to(\"cuda\")\n", " outputs = model.generate(input_ids)\n", " decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", " return decoded" ] }, { "cell_type": "code", "execution_count": 24, "id": "454ae3b6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | text | \n", "label | \n", "
---|---|---|
INDEX | \n", "\n", " | \n", " |
0 | \n", "Maybe the UN could talk to those asian and afr... | \n", "1 | \n", "
1 | \n", "‘Juice (Jews) are worse that nukes.’ Caption t... | \n", "1 | \n", "
2 | \n", "Foreigners on UK benefits leaps 41% in 5 years... | \n", "1 | \n", "
3 | \n", "Jews cannot be patriots, since their allegianc... | \n", "1 | \n", "
4 | \n", "Israel, Zionism and Jewish Supremacist control... | \n", "1 | \n", "
... | \n", "... | \n", "... | \n", "
4998 | \n", "Woman are basically childlike. | \n", "1 | \n", "
4999 | \n", "women should be submissive to men. | \n", "1 | \n", "
5000 | \n", "Women are essentially childlike, unable to und... | \n", "1 | \n", "
5001 | \n", "Women should be submissive to men. | \n", "1 | \n", "
5002 | \n", "Women should not be allowed to vote, this is a... | \n", "1 | \n", "
5003 rows × 2 columns
\n", "\n", " | text | \n", "label | \n", "translated | \n", "
---|---|---|---|
INDEX | \n", "\n", " | \n", " | \n", " |
0 | \n", "Maybe the UN could talk to those asian and afr... | \n", "1 | \n", "Peut-être que l'Onu pourrait parler aux nation... | \n", "
1 | \n", "‘Juice (Jews) are worse that nukes.’ Caption t... | \n", "1 | \n", "« Juice (Juifs) sont pire que les nuées. « & #... | \n", "
2 | \n", "Foreigners on UK benefits leaps 41% in 5 years... | \n", "1 | \n", "Les étrangers au Royaume-Uni bénéficient d'un ... | \n", "
3 | \n", "Jews cannot be patriots, since their allegianc... | \n", "1 | \n", "Les Juifs ne peuvent pas être patriotes, car l... | \n", "
4 | \n", "Israel, Zionism and Jewish Supremacist control... | \n", "1 | \n", "Israël, le sionisme et le contrôle suprémacist... | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "
4998 | \n", "Woman are basically childlike. | \n", "1 | \n", "La femme ressemble à un enfant. | \n", "
4999 | \n", "women should be submissive to men. | \n", "1 | \n", "Les filles doivent être soumises aux hommes. | \n", "
5000 | \n", "Women are essentially childlike, unable to und... | \n", "1 | \n", "Les filles sont pour l'essentiel des filles, i... | \n", "
5001 | \n", "Women should be submissive to men. | \n", "1 | \n", "Les filles doivent être soumises aux hommes. | \n", "
5002 | \n", "Women should not be allowed to vote, this is a... | \n", "1 | \n", "Les femmes ne devraient pas être autorisées à ... | \n", "
5003 rows × 3 columns
\n", "