File size: 11,230 Bytes
c710b24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "execution": {
     "iopub.execute_input": "2025-04-09T09:04:50.582374Z",
     "iopub.status.busy": "2025-04-09T09:04:50.581446Z",
     "iopub.status.idle": "2025-04-09T09:04:54.831276Z",
     "shell.execute_reply": "2025-04-09T09:04:54.829937Z",
     "shell.execute_reply.started": "2025-04-09T09:04:50.582330Z"
    },
    "id": "POBbLwluCMeK",
    "outputId": "9589beb5-86c8-4b44-d9bd-cc3316c838c9"
   },
   "outputs": [],
   "source": [
    "%pip install kagglehub\n",
    "%pip install sacremoses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-04-09T09:04:54.834196Z",
     "iopub.status.busy": "2025-04-09T09:04:54.833289Z",
     "iopub.status.idle": "2025-04-09T09:04:58.835896Z",
     "shell.execute_reply": "2025-04-09T09:04:58.834641Z",
     "shell.execute_reply.started": "2025-04-09T09:04:54.834135Z"
    },
    "id": "BwJ36n6vZUB2",
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import os\n",
    "from pathlib import Path\n",
    "from transformers import pipeline\n",
    "from tqdm import tqdm\n",
    "import pandas as pd\n",
    "import torch\n",
    "import kagglehub\n",
    "import signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-04-09T09:04:58.838507Z",
     "iopub.status.busy": "2025-04-09T09:04:58.837160Z",
     "iopub.status.idle": "2025-04-09T09:04:58.856737Z",
     "shell.execute_reply": "2025-04-09T09:04:58.855801Z",
     "shell.execute_reply.started": "2025-04-09T09:04:58.838466Z"
    },
    "id": "cOIT5Hu5FdT2"
   },
   "outputs": [],
   "source": [
    "class GracefulExiter:\n",
    "    # to catch keyboard interrupts\n",
    "    def __init__(self):\n",
    "        self.should_exit = False\n",
    "        signal.signal(signal.SIGINT, self.exit_gracefully)\n",
    "        signal.signal(signal.SIGTERM, self.exit_gracefully)\n",
    "\n",
    "    def exit_gracefully(self, signum, frame):\n",
    "        print(\n",
    "            \"\\nReceived interrupt signal. Finishing current work and saving progress...\"\n",
    "        )\n",
    "        self.should_exit = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-04-09T09:04:58.859897Z",
     "iopub.status.busy": "2025-04-09T09:04:58.858860Z",
     "iopub.status.idle": "2025-04-09T09:04:58.886712Z",
     "shell.execute_reply": "2025-04-09T09:04:58.885792Z",
     "shell.execute_reply.started": "2025-04-09T09:04:58.859858Z"
    },
    "id": "Fg9c5cFZZyoG"
   },
   "outputs": [],
   "source": [
    "def get_dataset():\n",
    "    # Download latest version\n",
    "    path = kagglehub.dataset_download(\"Cornell-University/arxiv\")\n",
    "\n",
    "    print(\"Path to dataset files:\", path)\n",
    "\n",
    "    file_name = os.listdir(path)[0]\n",
    "    path_to_dataset = Path(path) / file_name\n",
    "    data = pd.read_json(path_to_dataset, lines=True)\n",
    "\n",
    "    # leave only the first common category\n",
    "    data[\"categories\"] = [category.split()[0] for category in data[\"categories\"]]\n",
    "    data[\"categories\"] = [category.split(\".\")[0] for category in data[\"categories\"]]\n",
    "\n",
    "    # sort data in a proper way\n",
    "    counts = data.groupby(by=\"categories\")[\"title\"].count().sort_index()\n",
    "    unique_categories = counts.index.to_list()\n",
    "\n",
    "    groups_same_category = {\n",
    "        category: data[data[\"categories\"] == category] for category in unique_categories\n",
    "    }\n",
    "\n",
    "    max_group_size = counts.max()\n",
    "\n",
    "    new_df = []\n",
    "\n",
    "    for i in range(max_group_size):\n",
    "        for category in unique_categories:\n",
    "            if i < len(groups_same_category[category]):\n",
    "                new_df.append(groups_same_category[category].iloc[i])\n",
    "\n",
    "    result_df = pd.DataFrame(new_df).reset_index()\n",
    "    return result_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-04-09T09:04:58.889441Z",
     "iopub.status.busy": "2025-04-09T09:04:58.887873Z",
     "iopub.status.idle": "2025-04-09T09:04:58.910755Z",
     "shell.execute_reply": "2025-04-09T09:04:58.909796Z",
     "shell.execute_reply.started": "2025-04-09T09:04:58.889390Z"
    },
    "id": "RqdjPXAk1dyg",
    "tags": []
   },
   "outputs": [],
   "source": [
    "def translate_dataset(\n",
    "    starting_from=0,\n",
    "    count=1000,\n",
    "    batch_size=16,\n",
    "    save_interval=64,\n",
    "    dataset=None,\n",
    "    use_google_drive=False,\n",
    "):\n",
    "    # if dataset is given the function will use it\n",
    "    # else it will download dataset\n",
    "\n",
    "    # for colab to save files in your google drive\n",
    "    # just in case colab ending the session before you could save all the files\n",
    "\n",
    "    # if use_google_drive:\n",
    "    #   from google.colab import drive\n",
    "    #   drive.mount('/content/drive')\n",
    "    #   target_folder = Path(\"/content/drive/MyDrive/arxiv_translations\")\n",
    "    # else:\n",
    "    #   target_folder = Path(\"russian_dataset\")\n",
    "    # target_folder.mkdir(exist_ok=True)\n",
    "\n",
    "    target_folder = Path(\"dataset_parts\")\n",
    "    target_folder.mkdir(exist_ok=True)\n",
    "\n",
    "    # to catch keyboard interrupts\n",
    "    exiter = GracefulExiter()\n",
    "\n",
    "    result_df = dataset.copy()\n",
    "\n",
    "    # download the model\n",
    "    translator = pipeline(\n",
    "        \"translation_en_to_ru\",\n",
    "        model=\"Helsinki-NLP/opus-mt-en-ru\",\n",
    "        device=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n",
    "        torch_dtype=\"auto\",\n",
    "    )\n",
    "\n",
    "    def clean_text(text, max_length=512):\n",
    "        if pd.isna(text) or text.strip() == \"\":\n",
    "            return \"[EMPTY]\"\n",
    "        if len(text) > max_length:\n",
    "            text = text[:max_length]\n",
    "        return str(text).strip()\n",
    "\n",
    "    def translate_batch(texts, batch_size=batch_size, max_length=512):\n",
    "        results = []\n",
    "        texts = [clean_text(text, max_length) for text in texts]\n",
    "        try:\n",
    "            for out in tqdm(\n",
    "                translator(texts, max_length=max_length, batch_size=batch_size),\n",
    "                total=len(texts),\n",
    "                desc=\"Translating...\",\n",
    "            ):\n",
    "                results.append(out)\n",
    "        except Exception as e:\n",
    "            print(f\"Error: {e}\")\n",
    "        return results\n",
    "\n",
    "    # take the necessary interval\n",
    "    part_df = result_df.iloc[starting_from : starting_from + count]\n",
    "\n",
    "    russian_data = pd.DataFrame(columns=[\"authors\", \"title\", \"abstract\", \"categories\"])\n",
    "\n",
    "    previous_temp_file = None\n",
    "\n",
    "    for chunk_start in range(0, count, save_interval):\n",
    "        if exiter.should_exit:\n",
    "            break\n",
    "\n",
    "        chunk_end = min(chunk_start + save_interval, count)\n",
    "        print(f\"Processing records {chunk_start} to {chunk_end}...\")\n",
    "\n",
    "        chunk_df = part_df.iloc[chunk_start:chunk_end]\n",
    "\n",
    "        translated_chunk = {\n",
    "            \"authors\": translate_batch(chunk_df[\"authors\"].tolist()),\n",
    "            \"title\": translate_batch(chunk_df[\"title\"].tolist()),\n",
    "            \"abstract\": translate_batch(chunk_df[\"abstract\"].tolist()),\n",
    "            \"categories\": chunk_df[\"categories\"].tolist(),\n",
    "        }\n",
    "        if exiter.should_exit:\n",
    "            print(\"Interrupt detected. Saving partial results...\")\n",
    "            break\n",
    "        chunk_df_translated = pd.DataFrame(translated_chunk)\n",
    "        russian_data = pd.concat([russian_data, chunk_df_translated], ignore_index=True)\n",
    "\n",
    "        # save temperory results\n",
    "        temp_filename = (\n",
    "            target_folder / f\"{starting_from}_{starting_from + chunk_end}_temp.csv\"\n",
    "        )\n",
    "        russian_data.to_csv(temp_filename, index=False)\n",
    "        print(f\"Saved temporary results to {temp_filename}\")\n",
    "\n",
    "        # removing previous temporary file\n",
    "        if previous_temp_file is not None and previous_temp_file.exists():\n",
    "            previous_temp_file.unlink()\n",
    "            print(f\"Removed previous temporary file: {previous_temp_file}\")\n",
    "\n",
    "        previous_temp_file = temp_filename\n",
    "\n",
    "    if exiter.should_exit:\n",
    "        # keyboard interrupt\n",
    "        final_filename = (\n",
    "            target_folder\n",
    "            / f\"{starting_from}_{starting_from + len(russian_data)}_partial.csv\"\n",
    "        )\n",
    "        print(f\"\\nProcess interrupted. Saving partial results to {final_filename}\")\n",
    "    else:\n",
    "        final_filename = target_folder / f\"{starting_from}_{count}_final.csv\"\n",
    "        print(f\"\\nProcessing completed. Saving final results to {final_filename}\")\n",
    "\n",
    "    russian_data.to_csv(final_filename, index=False)\n",
    "\n",
    "    # remove temperorary files\n",
    "    if not exiter.should_exit:\n",
    "        for temp_file in target_folder.glob(\"*_temp.csv\"):\n",
    "            temp_file.unlink()\n",
    "        print(\"Temporary files removed.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-04-09T09:04:58.913113Z",
     "iopub.status.busy": "2025-04-09T09:04:58.911808Z"
    }
   },
   "outputs": [],
   "source": [
    "df = get_dataset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "mlO-3KoY8uT6",
    "outputId": "bb555bc7-6ad4-43ef-d096-06ef01b07525",
    "tags": []
   },
   "outputs": [],
   "source": [
    "translate_dataset(\n",
    "    starting_from=0, count=50_000, dataset=df, batch_size=128, save_interval=512\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "DataSphere Kernel",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}