{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":121150,"databundleVersionId":15833391},{"sourceType":"datasetVersion","sourceId":15299924,"datasetId":9786517,"databundleVersionId":16203592},{"sourceType":"datasetVersion","sourceId":15299752,"datasetId":9786388,"databundleVersionId":16203406}],"dockerImageVersionId":31329,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:12:35.987318Z","iopub.execute_input":"2026-03-22T08:12:35.988185Z","iopub.status.idle":"2026-03-22T08:12:35.995514Z","shell.execute_reply.started":"2026-03-22T08:12:35.988153Z","shell.execute_reply":"2026-03-22T08:12:35.994560Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport random\nimport numpy as np\n\nSEED = 42\nrandom.seed(SEED)\nnp.random.seed(SEED)\ntorch.manual_seed(SEED)\ntorch.cuda.manual_seed_all(SEED)\n\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(\"Using device:\", device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:12:44.850075Z","iopub.execute_input":"2026-03-22T08:12:44.850868Z","iopub.status.idle":"2026-03-22T08:12:44.861459Z","shell.execute_reply.started":"2026-03-22T08:12:44.850836Z","shell.execute_reply":"2026-03-22T08:12:44.860757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q transformers datasets sentencepiece accelerate","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:12:52.748322Z","iopub.execute_input":"2026-03-22T08:12:52.749092Z","iopub.status.idle":"2026-03-22T08:12:56.636553Z","shell.execute_reply.started":"2026-03-22T08:12:52.749059Z","shell.execute_reply":"2026-03-22T08:12:56.635477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/datasets/anitejsingh6633/train-csv/train (1).csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:13:00.713631Z","iopub.execute_input":"2026-03-22T08:13:00.714134Z","iopub.status.idle":"2026-03-22T08:13:00.776021Z","shell.execute_reply.started":"2026-03-22T08:13:00.714095Z","shell.execute_reply":"2026-03-22T08:13:00.775215Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train[[\"transliteration\", \"translation\"]]\n\n# Remove missing values\ntrain = train.dropna().reset_index(drop=True)\n\nprint(\"Dataset size:\", len(train))\ntrain.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:13:05.283144Z","iopub.execute_input":"2026-03-22T08:13:05.283907Z","iopub.status.idle":"2026-03-22T08:13:05.318592Z","shell.execute_reply.started":"2026-03-22T08:13:05.283865Z","shell.execute_reply":"2026-03-22T08:13:05.317774Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from datasets import Dataset\n\ndataset = Dataset.from_pandas(train)\n\n# 90% train / 10% validation\ndataset = dataset.train_test_split(test_size=0.1, seed=42)\n\nprint(dataset)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:13:16.112277Z","iopub.execute_input":"2026-03-22T08:13:16.112995Z","iopub.status.idle":"2026-03-22T08:13:17.941376Z","shell.execute_reply.started":"2026-03-22T08:13:16.112961Z","shell.execute_reply":"2026-03-22T08:13:17.940653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import MBartForConditionalGeneration, MBart50TokenizerFast\n\nMODEL_NAME = \"facebook/mbart-large-50-many-to-many-mmt\"\n\ntokenizer = MBart50TokenizerFast.from_pretrained(\n    MODEL_NAME,\n    src_lang=\"en_XX\",   # temporary (we fix later if needed)\n    tgt_lang=\"en_XX\"\n)\n\nmodel = MBartForConditionalGeneration.from_pretrained(MODEL_NAME)\nmodel.to(device)\n\nmodel.save_pretrained(\"mbart_model\")\ntokenizer.save_pretrained(\"mbart_model\")\n\nprint(\"Model loaded successfully\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:16:16.818157Z","iopub.execute_input":"2026-03-22T08:16:16.818691Z","iopub.status.idle":"2026-03-22T08:16:25.190640Z","shell.execute_reply.started":"2026-03-22T08:16:16.818660Z","shell.execute_reply":"2026-03-22T08:16:25.189788Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"MAX_SOURCE_LEN = 200\nMAX_TARGET_LEN = 256\n\ndef preprocess(batch):\n    inputs = tokenizer(\n        batch[\"transliteration\"],\n        truncation=True,\n        padding=\"max_length\",\n        max_length=MAX_SOURCE_LEN\n    )\n\n    labels = tokenizer(\n        text_target=batch[\"translation\"],\n        truncation=True,\n        padding=\"max_length\",\n        max_length=MAX_TARGET_LEN\n    )\n\n    inputs[\"labels\"] = labels[\"input_ids\"]\n    return inputs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:17:20.272231Z","iopub.execute_input":"2026-03-22T08:17:20.272695Z","iopub.status.idle":"2026-03-22T08:17:20.277407Z","shell.execute_reply.started":"2026-03-22T08:17:20.272665Z","shell.execute_reply":"2026-03-22T08:17:20.276568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tokenized = dataset.map(\n    preprocess,\n    batched=True,\n    remove_columns=dataset[\"train\"].column_names\n)\n\nprint(tokenized)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:17:28.091034Z","iopub.execute_input":"2026-03-22T08:17:28.091577Z","iopub.status.idle":"2026-03-22T08:17:31.174152Z","shell.execute_reply.started":"2026-03-22T08:17:28.091545Z","shell.execute_reply":"2026-03-22T08:17:31.171992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import DataCollatorForSeq2Seq\n\ndata_collator = DataCollatorForSeq2Seq(\n    tokenizer=tokenizer,\n    model=model\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:17:40.982370Z","iopub.execute_input":"2026-03-22T08:17:40.983109Z","iopub.status.idle":"2026-03-22T08:17:41.032965Z","shell.execute_reply.started":"2026-03-22T08:17:40.983079Z","shell.execute_reply":"2026-03-22T08:17:41.032402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import TrainingArguments\n\ntraining_args = TrainingArguments(\n    output_dir=\"./mbart_akk_en\",\n\n    eval_strategy=\"steps\",\n    eval_steps=500,\n\n    per_device_train_batch_size=2,\n    per_device_eval_batch_size=2,\n\n    gradient_accumulation_steps=8,\n\n    learning_rate=2e-5,\n    num_train_epochs=5,\n\n    fp16=True,\n\n    logging_steps=100,\n    save_steps=500,\n    save_total_limit=2,\n\n    load_best_model_at_end=True,\n    metric_for_best_model=\"eval_loss\",\n\n    report_to=\"none\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:17:49.631525Z","iopub.execute_input":"2026-03-22T08:17:49.632136Z","iopub.status.idle":"2026-03-22T08:17:49.715545Z","shell.execute_reply.started":"2026-03-22T08:17:49.632104Z","shell.execute_reply":"2026-03-22T08:17:49.714727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import Trainer\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=tokenized[\"train\"],\n    eval_dataset=tokenized[\"test\"],\n    data_collator=data_collator\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:18:01.250611Z","iopub.execute_input":"2026-03-22T08:18:01.251376Z","iopub.status.idle":"2026-03-22T08:18:01.831907Z","shell.execute_reply.started":"2026-03-22T08:18:01.251345Z","shell.execute_reply":"2026-03-22T08:18:01.830901Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer.train()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-22T08:18:12.621525Z","iopub.execute_input":"2026-03-22T08:18:12.622170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/datasets/anitejsingh6633/test-csv/test (1).csv\")\n\nprint(\"Test size:\", len(test))\ntest.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T10:28:22.192370Z","iopub.execute_input":"2026-03-21T10:28:22.192706Z","iopub.status.idle":"2026-03-21T10:28:22.222446Z","shell.execute_reply.started":"2026-03-21T10:28:22.192679Z","shell.execute_reply":"2026-03-21T10:28:22.221815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = test[[\"id\", \"transliteration\"]]\ntest = test.dropna().reset_index(drop=True)\n\nprint(\"Test size:\", len(test))\ntest.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T10:28:48.952060Z","iopub.execute_input":"2026-03-21T10:28:48.952754Z","iopub.status.idle":"2026-03-21T10:28:48.963527Z","shell.execute_reply.started":"2026-03-21T10:28:48.952724Z","shell.execute_reply":"2026-03-21T10:28:48.962730Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def clean_text(text):\n    text = text.replace(\"…\", \" \")\n    text = text.replace(\"...\", \" \")\n    return text\n\ntest[\"transliteration\"] = test[\"transliteration\"].apply(clean_text)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T10:29:14.626795Z","iopub.execute_input":"2026-03-21T10:29:14.627572Z","iopub.status.idle":"2026-03-21T10:29:14.632104Z","shell.execute_reply.started":"2026-03-21T10:29:14.627538Z","shell.execute_reply":"2026-03-21T10:29:14.631211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def generate_batch(texts):\n    inputs = tokenizer(\n        texts,\n        return_tensors=\"pt\",\n        padding=True,\n        truncation=True,\n        max_length=200\n    ).to(device)\n\n    outputs = model.generate(\n        **inputs,\n        max_length=256,\n        num_beams=4\n    )\n\n    return tokenizer.batch_decode(outputs, skip_special_tokens=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T10:29:48.708967Z","iopub.execute_input":"2026-03-21T10:29:48.709727Z","iopub.status.idle":"2026-03-21T10:29:48.714347Z","shell.execute_reply.started":"2026-03-21T10:29:48.709692Z","shell.execute_reply":"2026-03-21T10:29:48.713455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = []\n\nbatch_size = 8\n\nfor i in range(0, len(test), batch_size):\n    batch_texts = test[\"transliteration\"][i:i+batch_size].tolist()\n    batch_preds = generate_batch(batch_texts)\n    predictions.extend(batch_preds)\n\nprint(\"Total predictions:\", len(predictions))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T10:30:14.670812Z","iopub.execute_input":"2026-03-21T10:30:14.671616Z","iopub.status.idle":"2026-03-21T10:30:18.388140Z","shell.execute_reply.started":"2026-03-21T10:30:14.671585Z","shell.execute_reply":"2026-03-21T10:30:18.387319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\nsubmission = pd.DataFrame({\n    \"id\": test[\"id\"],\n    \"translation\": predictions\n})\n\nsubmission.to_csv(\"submission.csv\", index=False)\n\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T10:30:32.988581Z","iopub.execute_input":"2026-03-21T10:30:32.989377Z","iopub.status.idle":"2026-03-21T10:30:33.004089Z","shell.execute_reply.started":"2026-03-21T10:30:32.989344Z","shell.execute_reply":"2026-03-21T10:30:33.003142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}