{"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,"isSourceIdPinned":false}],"dockerImageVersionId":31287,"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-16T17:43:25.335613Z","iopub.execute_input":"2026-03-16T17:43:25.336178Z","iopub.status.idle":"2026-03-16T17:43:25.603722Z","shell.execute_reply.started":"2026-03-16T17:43:25.336143Z","shell.execute_reply":"2026-03-16T17:43:25.603141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.environ[\"PYTORCH_CUDA_ALLOC_CONF\"] = \"expandable_segments:True\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:43:29.718266Z","iopub.execute_input":"2026-03-16T17:43:29.718889Z","iopub.status.idle":"2026-03-16T17:43:29.722854Z","shell.execute_reply.started":"2026-03-16T17:43:29.718862Z","shell.execute_reply":"2026-03-16T17:43:29.722155Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\nimport torch\nimport pandas as pd\n\nfrom datasets import Dataset\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM\nfrom transformers import Trainer, TrainingArguments, DataCollatorForSeq2Seq\n\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:43:40.662412Z","iopub.execute_input":"2026-03-16T17:43:40.662720Z","iopub.status.idle":"2026-03-16T17:44:13.446830Z","shell.execute_reply.started":"2026-03-16T17:43:40.662683Z","shell.execute_reply":"2026-03-16T17:44:13.446261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(\"Device:\", device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:44:17.554679Z","iopub.execute_input":"2026-03-16T17:44:17.555657Z","iopub.status.idle":"2026-03-16T17:44:17.559783Z","shell.execute_reply.started":"2026-03-16T17:44:17.555625Z","shell.execute_reply":"2026-03-16T17:44:17.559114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/competitions/deep-past-initiative-machine-translation\"\n\ntrain_df = pd.read_csv(f\"{DATA_PATH}/train.csv\")\ntest_df = pd.read_csv(f\"{DATA_PATH}/test.csv\")\n\nprint(train_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:44:37.982988Z","iopub.execute_input":"2026-03-16T17:44:37.983637Z","iopub.status.idle":"2026-03-16T17:44:38.081507Z","shell.execute_reply.started":"2026-03-16T17:44:37.983608Z","shell.execute_reply":"2026-03-16T17:44:38.080917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lex1 = pd.read_csv(f\"{DATA_PATH}/OA_Lexicon_eBL.csv\")\nlex2 = pd.read_csv(f\"{DATA_PATH}/eBL_Dictionary.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:44:51.005765Z","iopub.execute_input":"2026-03-16T17:44:51.006477Z","iopub.status.idle":"2026-03-16T17:44:51.192826Z","shell.execute_reply.started":"2026-03-16T17:44:51.006448Z","shell.execute_reply":"2026-03-16T17:44:51.192035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"akk_dict = {}\n\nfor _, r in lex1.iterrows():\n    akk_dict[str(r.iloc[0]).lower()] = str(r.iloc[1])\n\nfor _, r in lex2.iterrows():\n    akk_dict[str(r.iloc[0]).lower()] = str(r.iloc[1])\n\nprint(\"Lexicon size:\", len(akk_dict))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:45:04.281354Z","iopub.execute_input":"2026-03-16T17:45:04.281657Z","iopub.status.idle":"2026-03-16T17:45:06.661648Z","shell.execute_reply.started":"2026-03-16T17:45:04.281630Z","shell.execute_reply":"2026-03-16T17:45:06.661019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess(text):\n\n    text = str(text)\n\n    text = re.sub(r\"[!?/:]\", \"\", text)\n    text = text.replace(\"…\",\"<gap>\")\n    text = re.sub(r\"\\[x\\]\",\"<gap>\", text)\n\n    text = re.sub(r\"[\\[\\]˹˺]\",\"\",text)\n    text = text.replace(\"ḫ\",\"h\")\n\n    text = re.sub(r\"[₀₁₂₃₄₅₆₇₈₉]\",\"\",text)\n    text = re.sub(r\"\\s+\",\" \",text)\n\n    return text.strip()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:45:18.739849Z","iopub.execute_input":"2026-03-16T17:45:18.740176Z","iopub.status.idle":"2026-03-16T17:45:18.745141Z","shell.execute_reply.started":"2026-03-16T17:45:18.740147Z","shell.execute_reply":"2026-03-16T17:45:18.744362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"transliteration\"] = train_df[\"transliteration\"].apply(preprocess)\ntest_df[\"transliteration\"] = test_df[\"transliteration\"].apply(preprocess)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:45:31.012855Z","iopub.execute_input":"2026-03-16T17:45:31.013397Z","iopub.status.idle":"2026-03-16T17:45:31.094149Z","shell.execute_reply.started":"2026-03-16T17:45:31.013368Z","shell.execute_reply":"2026-03-16T17:45:31.093556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def lexicon_hint(sent):\n\n    words = sent.split()\n    out = []\n\n    for w in words:\n        key = w.lower()\n        out.append(akk_dict.get(key, w))\n\n    return \" \".join(out)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:45:42.605427Z","iopub.execute_input":"2026-03-16T17:45:42.606120Z","iopub.status.idle":"2026-03-16T17:45:42.610077Z","shell.execute_reply.started":"2026-03-16T17:45:42.606077Z","shell.execute_reply":"2026-03-16T17:45:42.609387Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"lexicon\"] = train_df[\"transliteration\"].apply(lexicon_hint)\ntest_df[\"lexicon\"] = test_df[\"transliteration\"].apply(lexicon_hint)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:45:53.594434Z","iopub.execute_input":"2026-03-16T17:45:53.595044Z","iopub.status.idle":"2026-03-16T17:45:53.637442Z","shell.execute_reply.started":"2026-03-16T17:45:53.595017Z","shell.execute_reply":"2026-03-16T17:45:53.636921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_split, val_split = train_test_split(\n    train_df,\n    test_size=0.1,\n    random_state=42\n)\n\ntrain_ds = Dataset.from_pandas(train_split)\nval_ds = Dataset.from_pandas(val_split)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:46:05.658851Z","iopub.execute_input":"2026-03-16T17:46:05.659434Z","iopub.status.idle":"2026-03-16T17:46:05.710042Z","shell.execute_reply.started":"2026-03-16T17:46:05.659405Z","shell.execute_reply":"2026-03-16T17:46:05.709490Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"MODEL = \"google/byt5-small\"\n\ntokenizer = AutoTokenizer.from_pretrained(MODEL)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(MODEL).to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:46:18.368350Z","iopub.execute_input":"2026-03-16T17:46:18.368648Z","iopub.status.idle":"2026-03-16T17:46:25.336556Z","shell.execute_reply.started":"2026-03-16T17:46:18.368622Z","shell.execute_reply":"2026-03-16T17:46:25.335467Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def tokenize(example):\n\n    inp = (\n        \"translate Akkadian to English: \"\n        + example[\"transliteration\"]\n        + \" | hint: \"\n        + example[\"lexicon\"]\n    )\n\n    x = tokenizer(inp, max_length=64, truncation=True, padding=\"max_length\")\n\n    y = tokenizer(\n        example[\"translation\"],\n        max_length=64,\n        truncation=True,\n        padding=\"max_length\"\n    )\n\n    x[\"labels\"] = y[\"input_ids\"]\n    return x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:46:41.491637Z","iopub.execute_input":"2026-03-16T17:46:41.491947Z","iopub.status.idle":"2026-03-16T17:46:41.496786Z","shell.execute_reply.started":"2026-03-16T17:46:41.491919Z","shell.execute_reply":"2026-03-16T17:46:41.495898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = train_ds.map(tokenize)\nval_ds = val_ds.map(tokenize)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:46:54.560754Z","iopub.execute_input":"2026-03-16T17:46:54.561263Z","iopub.status.idle":"2026-03-16T17:48:03.804382Z","shell.execute_reply.started":"2026-03-16T17:46:54.561234Z","shell.execute_reply":"2026-03-16T17:48:03.803747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cols = [\"oare_id\",\"transliteration\",\"translation\",\"lexicon\",\"__index_level_0__\"]\n\ntrain_ds = train_ds.remove_columns(cols)\nval_ds = val_ds.remove_columns(cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:48:17.192110Z","iopub.execute_input":"2026-03-16T17:48:17.192682Z","iopub.status.idle":"2026-03-16T17:48:17.200917Z","shell.execute_reply.started":"2026-03-16T17:48:17.192653Z","shell.execute_reply":"2026-03-16T17:48:17.200347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_collator = DataCollatorForSeq2Seq(tokenizer, model)\n\nargs = TrainingArguments(\n    output_dir=\"/kaggle/working/model\",\n    per_device_train_batch_size=4,\n    num_train_epochs=3,\n    learning_rate=3e-5,\n    logging_steps=100,\n    save_strategy=\"no\",\n    fp16=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:48:28.945268Z","iopub.execute_input":"2026-03-16T17:48:28.945562Z","iopub.status.idle":"2026-03-16T17:48:28.990655Z","shell.execute_reply.started":"2026-03-16T17:48:28.945539Z","shell.execute_reply":"2026-03-16T17:48:28.989810Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer = Trainer(\n    model=model,\n    args=args,\n    train_dataset=train_ds,\n    eval_dataset=val_ds,\n    data_collator=data_collator\n)\n\ntrainer.train()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:48:41.495508Z","iopub.execute_input":"2026-03-16T17:48:41.496276Z","iopub.status.idle":"2026-03-16T17:53:22.997069Z","shell.execute_reply.started":"2026-03-16T17:48:41.496244Z","shell.execute_reply":"2026-03-16T17:53:22.996160Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate(text):\n\n    hint = lexicon_hint(text)\n\n    inp = \"translate Akkadian to English: \" + text + \" | hint: \" + hint\n\n    enc = tokenizer(inp, return_tensors=\"pt\", truncation=True).to(device)\n\n    out = model.generate(\n        **enc,\n        max_length=64,\n        num_beams=8,\n        length_penalty=0.8\n    )\n\n    return tokenizer.decode(out[0], skip_special_tokens=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:55:35.640976Z","iopub.execute_input":"2026-03-16T17:55:35.641810Z","iopub.status.idle":"2026-03-16T17:55:35.646415Z","shell.execute_reply.started":"2026-03-16T17:55:35.641779Z","shell.execute_reply":"2026-03-16T17:55:35.645830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = []\n\nfor t in test_df[\"transliteration\"]:\n    preds.append(translate(t))\n\nprint(preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:56:31.069898Z","iopub.execute_input":"2026-03-16T17:56:31.070252Z","iopub.status.idle":"2026-03-16T17:56:35.641668Z","shell.execute_reply.started":"2026-03-16T17:56:31.070220Z","shell.execute_reply":"2026-03-16T17:56:35.640944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save_pretrained(\"/kaggle/working/akk_model\")\ntokenizer.save_pretrained(\"/kaggle/working/akk_model\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T17:57:46.642942Z","iopub.execute_input":"2026-03-16T17:57:46.643271Z","iopub.status.idle":"2026-03-16T17:57:48.653369Z","shell.execute_reply.started":"2026-03-16T17:57:46.643244Z","shell.execute_reply":"2026-03-16T17:57:48.652686Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}