{"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":[{"sourceId":121150,"databundleVersionId":15061024,"sourceType":"competition"},{"sourceId":14286636,"sourceType":"datasetVersion","datasetId":9118934},{"sourceId":14295279,"sourceType":"datasetVersion","datasetId":9125240},{"sourceId":14295354,"sourceType":"datasetVersion","datasetId":9125280},{"sourceId":14320680,"sourceType":"datasetVersion","datasetId":9141929},{"sourceId":14326799,"sourceType":"datasetVersion","datasetId":9146345}],"dockerImageVersionId":31234,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"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-01-12T06:25:10.847132Z","iopub.execute_input":"2026-01-12T06:25:10.847670Z","iopub.status.idle":"2026-01-12T06:25:10.860342Z","shell.execute_reply.started":"2026-01-12T06:25:10.847639Z","shell.execute_reply":"2026-01-12T06:25:10.859744Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/train.csv\")\ntest  = pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/test.csv\")\nlex   = pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/OA_Lexicon_eBL.csv\")\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:10.861587Z","iopub.execute_input":"2026-01-12T06:25:10.861939Z","iopub.status.idle":"2026-01-12T06:25:10.985390Z","shell.execute_reply.started":"2026-01-12T06:25:10.861903Z","shell.execute_reply":"2026-01-12T06:25:10.984771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nprint(lex.columns)\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:10.986196Z","iopub.execute_input":"2026-01-12T06:25:10.986434Z","iopub.status.idle":"2026-01-12T06:25:10.990852Z","shell.execute_reply.started":"2026-01-12T06:25:10.986400Z","shell.execute_reply":"2026-01-12T06:25:10.990043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lex_dict = (\n    lex.dropna(subset=[\"norm\", \"Alt_lex\"])\n       .groupby(\"norm\")[\"Alt_lex\"]\n       .first()\n       .to_dict()\n)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:10.991768Z","iopub.execute_input":"2026-01-12T06:25:10.992048Z","iopub.status.idle":"2026-01-12T06:25:11.012509Z","shell.execute_reply.started":"2026-01-12T06:25:10.992026Z","shell.execute_reply":"2026-01-12T06:25:11.011954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lex_dict = (\n    lex.dropna(subset=[\"norm\", \"Alt_lex\"])\n       .groupby(\"norm\")[\"Alt_lex\"]\n       .first()\n       .to_dict()\n)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.014401Z","iopub.execute_input":"2026-01-12T06:25:11.014741Z","iopub.status.idle":"2026-01-12T06:25:11.032438Z","shell.execute_reply.started":"2026-01-12T06:25:11.014704Z","shell.execute_reply":"2026-01-12T06:25:11.031905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(lex.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.033234Z","iopub.execute_input":"2026-01-12T06:25:11.033461Z","iopub.status.idle":"2026-01-12T06:25:11.040699Z","shell.execute_reply.started":"2026-01-12T06:25:11.033440Z","shell.execute_reply":"2026-01-12T06:25:11.039956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Show all columns\nprint(\"Columns in lex DataFrame:\")\nprint(lex.columns)\n\n# Show first 5 rows\nprint(\"\\nFirst 5 rows of lex DataFrame:\")\nprint(lex.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.041462Z","iopub.execute_input":"2026-01-12T06:25:11.041669Z","iopub.status.idle":"2026-01-12T06:25:11.056038Z","shell.execute_reply.started":"2026-01-12T06:25:11.041647Z","shell.execute_reply":"2026-01-12T06:25:11.055108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Columns in test DataFrame:\")\nprint(test.columns)\n\n# Optional: see first 5 rows\nprint(test.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.056996Z","iopub.execute_input":"2026-01-12T06:25:11.057369Z","iopub.status.idle":"2026-01-12T06:25:11.072459Z","shell.execute_reply.started":"2026-01-12T06:25:11.057335Z","shell.execute_reply":"2026-01-12T06:25:11.071805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv(\"/kaggle/input/eagel-dd/train.csv\")\nsubmission = pd.read_csv(\"/kaggle/input/eagel-dd/train.csv\")\n\nprint(\"Sample shape:\", sample.shape)\nprint(\"Submission shape:\", submission.shape)\n\nprint(\"\\nSample columns:\", sample.columns.tolist())\nprint(\"Submission columns:\", submission.columns.tolist())\n\nprint(\"\\nAny NaNs in submission?\")\nprint(submission.isna().sum())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.073360Z","iopub.execute_input":"2026-01-12T06:25:11.073677Z","iopub.status.idle":"2026-01-12T06:25:11.141579Z","shell.execute_reply.started":"2026-01-12T06:25:11.073655Z","shell.execute_reply":"2026-01-12T06:25:11.140967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv(\"/kaggle/input/eagel-dd/train.csv\")\nprint(sample.columns)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.142377Z","iopub.execute_input":"2026-01-12T06:25:11.142565Z","iopub.status.idle":"2026-01-12T06:25:11.174655Z","shell.execute_reply.started":"2026-01-12T06:25:11.142546Z","shell.execute_reply":"2026-01-12T06:25:11.173964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate_line(line):\n    line = str(line)\n    words = line.split()\n    return \" \".join(lex_dict.get(w, w) for w in words)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.175534Z","iopub.execute_input":"2026-01-12T06:25:11.176000Z","iopub.status.idle":"2026-01-12T06:25:11.179874Z","shell.execute_reply.started":"2026-01-12T06:25:11.175968Z","shell.execute_reply":"2026-01-12T06:25:11.179203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.180816Z","iopub.execute_input":"2026-01-12T06:25:11.181207Z","iopub.status.idle":"2026-01-12T06:25:11.236584Z","shell.execute_reply.started":"2026-01-12T06:25:11.181175Z","shell.execute_reply":"2026-01-12T06:25:11.236060Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.237285Z","iopub.execute_input":"2026-01-12T06:25:11.237464Z","iopub.status.idle":"2026-01-12T06:25:11.447300Z","shell.execute_reply.started":"2026-01-12T06:25:11.237447Z","shell.execute_reply":"2026-01-12T06:25:11.446613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = sample.copy()\n\nsubmission[\"translation\"] = submission[\"translation\"].apply(\n    lambda x: \"placeholder translation\"\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.451199Z","iopub.execute_input":"2026-01-12T06:25:11.451725Z","iopub.status.idle":"2026-01-12T06:25:11.460050Z","shell.execute_reply.started":"2026-01-12T06:25:11.451693Z","shell.execute_reply":"2026-01-12T06:25:11.459269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission.columns.tolist())\nsubmission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.461162Z","iopub.execute_input":"2026-01-12T06:25:11.461794Z","iopub.status.idle":"2026-01-12T06:25:11.480226Z","shell.execute_reply.started":"2026-01-12T06:25:11.461769Z","shell.execute_reply":"2026-01-12T06:25:11.479546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = submission[\"translation\"].fillna(\"placeholder translation\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.481080Z","iopub.execute_input":"2026-01-12T06:25:11.481393Z","iopub.status.idle":"2026-01-12T06:25:11.493763Z","shell.execute_reply.started":"2026-01-12T06:25:11.481369Z","shell.execute_reply":"2026-01-12T06:25:11.493272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\n!ls\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.494685Z","iopub.execute_input":"2026-01-12T06:25:11.495104Z","iopub.status.idle":"2026-01-12T06:25:11.726081Z","shell.execute_reply.started":"2026-01-12T06:25:11.495082Z","shell.execute_reply":"2026-01-12T06:25:11.725356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Submission columns:\", submission.columns.tolist())\nprint(\"Submission shape:\", submission.shape)\nprint(submission.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.727359Z","iopub.execute_input":"2026-01-12T06:25:11.727678Z","iopub.status.idle":"2026-01-12T06:25:11.734879Z","shell.execute_reply.started":"2026-01-12T06:25:11.727641Z","shell.execute_reply":"2026-01-12T06:25:11.734059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Rename column to exactly what Kaggle expects\nsubmission = submission.rename(columns={\"oare_id\": \"id\"})\n\n# Ensure only these two columns in order\nsubmission = submission[[\"id\", \"translation\"]]\n\n# Fill any empty translations\nsubmission[\"translation\"] = submission[\"translation\"].fillna(\"placeholder translation\")\n\n# Safety check\nassert submission.columns.tolist() == [\"id\", \"translation\"]\nassert submission.shape[0] == 1561  # must match hidden test set row count\nassert submission[\"translation\"].isna().sum() == 0\n\n# Save submission\nsubmission.to_csv(\"submission.csv\", index=False)\n!ls\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.735836Z","iopub.execute_input":"2026-01-12T06:25:11.736152Z","iopub.status.idle":"2026-01-12T06:25:11.954015Z","shell.execute_reply.started":"2026-01-12T06:25:11.736123Z","shell.execute_reply":"2026-01-12T06:25:11.953303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Load your lexicon CSV (adjust path if needed)\nlex = pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/OA_Lexicon_eBL.csv\")\n\n# Inspect columns\nprint(lex.columns)\nlex.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:11.955203Z","iopub.execute_input":"2026-01-12T06:25:11.955466Z","iopub.status.idle":"2026-01-12T06:25:12.066460Z","shell.execute_reply.started":"2026-01-12T06:25:11.955437Z","shell.execute_reply":"2026-01-12T06:25:12.065663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop missing values and create dictionary\nlex_dict = lex.dropna(subset=[\"norm\", \"lexeme\"]).set_index(\"norm\")[\"lexeme\"].to_dict()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.067334Z","iopub.execute_input":"2026-01-12T06:25:12.067945Z","iopub.status.idle":"2026-01-12T06:25:12.107941Z","shell.execute_reply.started":"2026-01-12T06:25:12.067914Z","shell.execute_reply":"2026-01-12T06:25:12.107200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\n\ndef normalize_word(word):\n    \"\"\"Lowercase, remove punctuation.\"\"\"\n    word = str(word).lower()\n    word = re.sub(r\"[^\\w\\s]\", \"\", word)\n    return word\n\ndef translate_line(line):\n    \"\"\"Translate a line using lexicon; fallback to original word if not found.\"\"\"\n    words = str(line).split()\n    translated = [lex_dict.get(normalize_word(w), w) for w in words]\n    return \" \".join(translated)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.108739Z","iopub.execute_input":"2026-01-12T06:25:12.108983Z","iopub.status.idle":"2026-01-12T06:25:12.113679Z","shell.execute_reply.started":"2026-01-12T06:25:12.108961Z","shell.execute_reply":"2026-01-12T06:25:12.112958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Assume submission DataFrame already exists and has columns ['id', 'translation']\n# And the original Akkadian text is in the column 'translation' of sample (or test data)\n# Adjust if your test text is in a different column, e.g., 'transliteration'\n\n# Example if original text is in `translation` column temporarily\nsubmission[\"translation\"] = submission[\"translation\"].apply(translate_line)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.114600Z","iopub.execute_input":"2026-01-12T06:25:12.115251Z","iopub.status.idle":"2026-01-12T06:25:12.133007Z","shell.execute_reply.started":"2026-01-12T06:25:12.115218Z","shell.execute_reply":"2026-01-12T06:25:12.132331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Ensure format is correct\nassert submission.columns.tolist() == [\"id\", \"translation\"]\nassert submission[\"translation\"].isna().sum() == 0\n\nsubmission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.133784Z","iopub.execute_input":"2026-01-12T06:25:12.134030Z","iopub.status.idle":"2026-01-12T06:25:12.150741Z","shell.execute_reply.started":"2026-01-12T06:25:12.133998Z","shell.execute_reply":"2026-01-12T06:25:12.150042Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntest = pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/test.csv\")\n\nprint(test.columns)\ntest.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.151688Z","iopub.execute_input":"2026-01-12T06:25:12.151874Z","iopub.status.idle":"2026-01-12T06:25:12.172423Z","shell.execute_reply.started":"2026-01-12T06:25:12.151856Z","shell.execute_reply":"2026-01-12T06:25:12.171797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lex = pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/OA_Lexicon_eBL.csv\")\n\nlex_dict = (\n    lex.dropna(subset=[\"norm\", \"lexeme\"])\n       .set_index(\"norm\")[\"lexeme\"]\n       .to_dict()\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.173213Z","iopub.execute_input":"2026-01-12T06:25:12.173512Z","iopub.status.idle":"2026-01-12T06:25:12.294365Z","shell.execute_reply.started":"2026-01-12T06:25:12.173490Z","shell.execute_reply":"2026-01-12T06:25:12.293680Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\n\ndef normalize_word(word):\n    word = str(word).lower()\n    word = re.sub(r\"[^\\w\\s]\", \"\", word)\n    return word\n\ndef translate_line(line):\n    words = str(line).split()\n    translated = [\n        lex_dict.get(normalize_word(w), w)\n        for w in words\n    ]\n    return \" \".join(translated)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.295269Z","iopub.execute_input":"2026-01-12T06:25:12.295544Z","iopub.status.idle":"2026-01-12T06:25:12.300671Z","shell.execute_reply.started":"2026-01-12T06:25:12.295507Z","shell.execute_reply":"2026-01-12T06:25:12.299965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Apply translation to the Akkadian text\ntest[\"translation\"] = test[\"transliteration\"].apply(translate_line)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.301645Z","iopub.execute_input":"2026-01-12T06:25:12.302117Z","iopub.status.idle":"2026-01-12T06:25:12.316343Z","shell.execute_reply.started":"2026-01-12T06:25:12.302092Z","shell.execute_reply":"2026-01-12T06:25:12.315751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"id\": test[\"id\"],        # MUST be named 'id'\n    \"translation\": test[\"translation\"]\n})\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.317152Z","iopub.execute_input":"2026-01-12T06:25:12.317357Z","iopub.status.idle":"2026-01-12T06:25:12.330537Z","shell.execute_reply.started":"2026-01-12T06:25:12.317337Z","shell.execute_reply":"2026-01-12T06:25:12.329829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"assert submission.columns.tolist() == [\"id\", \"translation\"]\nassert submission.isna().sum().sum() == 0\n\nsubmission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.331600Z","iopub.execute_input":"2026-01-12T06:25:12.331935Z","iopub.status.idle":"2026-01-12T06:25:12.349231Z","shell.execute_reply.started":"2026-01-12T06:25:12.331899Z","shell.execute_reply":"2026-01-12T06:25:12.348623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"AKK_DICT = {\n    \"umma\": \"Thus says\",\n    \"ana\": \"to\",\n    \"ṭuppim\": \"tablet\",\n    \"ṭuppi\": \"tablet\",\n    \"awīlum\": \"man\",\n    \"wardum\": \"servant\",\n    \"bītum\": \"house\",\n    \"šarrum\": \"king\",\n    \"ālī\": \"city\",\n    \"ālam\": \"city\",\n    \"ina\": \"in\",\n    \"ki-ma\": \"when\",\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.350143Z","iopub.execute_input":"2026-01-12T06:25:12.350497Z","iopub.status.idle":"2026-01-12T06:25:12.366844Z","shell.execute_reply.started":"2026-01-12T06:25:12.350462Z","shell.execute_reply":"2026-01-12T06:25:12.366264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\n\ndef normalize_akkadian(text):\n    text = text.lower()\n    text = re.sub(r\"[^\\w\\s\\-]\", \"\", text)  # remove punctuation\n    text = text.replace(\"š\", \"s\")\n    text = text.replace(\"ṭ\", \"t\")\n    text = text.replace(\"ā\", \"a\")\n    text = text.replace(\"ī\", \"i\")\n    return text\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.367601Z","iopub.execute_input":"2026-01-12T06:25:12.367845Z","iopub.status.idle":"2026-01-12T06:25:12.380598Z","shell.execute_reply.started":"2026-01-12T06:25:12.367810Z","shell.execute_reply":"2026-01-12T06:25:12.379946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate_word_by_word(text, dictionary):\n    words = normalize_akkadian(text).split()\n    translated = []\n\n    for w in words:\n        translated.append(dictionary.get(w, w))\n\n    return \" \".join(translated)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.381677Z","iopub.execute_input":"2026-01-12T06:25:12.381984Z","iopub.status.idle":"2026-01-12T06:25:12.393375Z","shell.execute_reply.started":"2026-01-12T06:25:12.381953Z","shell.execute_reply":"2026-01-12T06:25:12.392739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def apply_templates(text):\n    text = text.replace(\"thus says\", \"Thus says\")\n    text = text.replace(\"tablet\", \"tablet\")\n    text = text.replace(\"when\", \"When\")\n    return text\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.394174Z","iopub.execute_input":"2026-01-12T06:25:12.394425Z","iopub.status.idle":"2026-01-12T06:25:12.405285Z","shell.execute_reply.started":"2026-01-12T06:25:12.394393Z","shell.execute_reply":"2026-01-12T06:25:12.404694Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate(text):\n    out = translate_word_by_word(text, AKK_DICT)\n    out = apply_templates(out)\n    return out\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.406024Z","iopub.execute_input":"2026-01-12T06:25:12.406232Z","iopub.status.idle":"2026-01-12T06:25:12.420368Z","shell.execute_reply.started":"2026-01-12T06:25:12.406212Z","shell.execute_reply":"2026-01-12T06:25:12.419762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = submission[\"translation\"].apply(translate)\nsubmission.head()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.421178Z","iopub.execute_input":"2026-01-12T06:25:12.421515Z","iopub.status.idle":"2026-01-12T06:25:12.436302Z","shell.execute_reply.started":"2026-01-12T06:25:12.421483Z","shell.execute_reply":"2026-01-12T06:25:12.435596Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.437134Z","iopub.execute_input":"2026-01-12T06:25:12.437373Z","iopub.status.idle":"2026-01-12T06:25:12.449049Z","shell.execute_reply.started":"2026-01-12T06:25:12.437341Z","shell.execute_reply":"2026-01-12T06:25:12.448327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.450039Z","iopub.execute_input":"2026-01-12T06:25:12.450326Z","iopub.status.idle":"2026-01-12T06:25:12.460238Z","shell.execute_reply.started":"2026-01-12T06:25:12.450296Z","shell.execute_reply":"2026-01-12T06:25:12.459740Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\"\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.461180Z","iopub.execute_input":"2026-01-12T06:25:12.461715Z","iopub.status.idle":"2026-01-12T06:25:12.475214Z","shell.execute_reply.started":"2026-01-12T06:25:12.461681Z","shell.execute_reply":"2026-01-12T06:25:12.474673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test.columns)\ntest.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.475939Z","iopub.execute_input":"2026-01-12T06:25:12.476196Z","iopub.status.idle":"2026-01-12T06:25:12.490747Z","shell.execute_reply.started":"2026-01-12T06:25:12.476175Z","shell.execute_reply":"2026-01-12T06:25:12.490216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"AKK_DICT = {\n    \"umma\": \"Thus says\",\n    \"ana\": \"to\",\n    \"ina\": \"in\",\n    \"tuppi\": \"tablet\",\n    \"tuppim\": \"tablet\",\n    \"ali\": \"city\",\n    \"alam\": \"city\",\n}\n\ndef translate(text):\n    words = text.lower().split()\n    return \" \".join(AKK_DICT.get(w, w) for w in words)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.496075Z","iopub.execute_input":"2026-01-12T06:25:12.496612Z","iopub.status.idle":"2026-01-12T06:25:12.503116Z","shell.execute_reply.started":"2026-01-12T06:25:12.496588Z","shell.execute_reply":"2026-01-12T06:25:12.502463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/sample_submission.csv\"\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.504655Z","iopub.execute_input":"2026-01-12T06:25:12.504923Z","iopub.status.idle":"2026-01-12T06:25:12.516617Z","shell.execute_reply.started":"2026-01-12T06:25:12.504872Z","shell.execute_reply":"2026-01-12T06:25:12.515933Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"SOURCE_COL = test.columns[1]   # automatically correct\nsubmission[\"translation\"] = test[SOURCE_COL].apply(translate)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.517422Z","iopub.execute_input":"2026-01-12T06:25:12.517646Z","iopub.status.idle":"2026-01-12T06:25:12.528313Z","shell.execute_reply.started":"2026-01-12T06:25:12.517616Z","shell.execute_reply":"2026-01-12T06:25:12.527597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in test.columns:\n    print(\"\\nCOLUMN:\", col)\n    print(test[col].head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.529236Z","iopub.execute_input":"2026-01-12T06:25:12.529518Z","iopub.status.idle":"2026-01-12T06:25:12.545804Z","shell.execute_reply.started":"2026-01-12T06:25:12.529486Z","shell.execute_reply":"2026-01-12T06:25:12.545083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = test[\"transliteration\"].apply(translate)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.546567Z","iopub.execute_input":"2026-01-12T06:25:12.546774Z","iopub.status.idle":"2026-01-12T06:25:12.558369Z","shell.execute_reply.started":"2026-01-12T06:25:12.546754Z","shell.execute_reply":"2026-01-12T06:25:12.557705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Load test + sample submission\ntest = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\"\n)\n\nsubmission = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/sample_submission.csv\"\n)\n\n# Dictionary\nAKK_DICT = {\n    \"umma\": \"Thus says\",\n    \"ana\": \"to\",\n    \"ina\": \"in\",\n    \"tuppi\": \"tablet\",\n    \"tuppim\": \"tablet\",\n    \"ali\": \"city\",\n    \"alam\": \"city\",\n}\n\ndef translate(text):\n    words = text.lower().split()\n    return \" \".join(AKK_DICT.get(w, w) for w in words)\n\n# ✅ CORRECT COLUMN\nsubmission[\"translation\"] = test[\"transliteration\"].apply(translate)\n\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.559275Z","iopub.execute_input":"2026-01-12T06:25:12.559500Z","iopub.status.idle":"2026-01-12T06:25:12.583019Z","shell.execute_reply.started":"2026-01-12T06:25:12.559480Z","shell.execute_reply":"2026-01-12T06:25:12.582261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"AKK_DICT = {\n    \"um-ma\": \"Thus says\",\n    \"umma\": \"Thus says\",\n\n    \"a-na\": \"to\",\n    \"ana\": \"to\",\n\n    \"i-na\": \"in\",\n    \"ina\": \"in\",\n\n    \"ki-ma\": \"when\",\n    \"kima\": \"when\",\n\n    \"ṭuppi\": \"tablet\",\n    \"ṭuppim\": \"tablet\",\n    \"tuppi\": \"tablet\",\n    \"tuppim\": \"tablet\",\n\n    \"ālim(ki)\": \"the city\",\n    \"alim(ki)\": \"the city\",\n\n    \"wardum\": \"servant\",\n    \"awīlum\": \"man\",\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.583932Z","iopub.execute_input":"2026-01-12T06:25:12.584205Z","iopub.status.idle":"2026-01-12T06:25:12.588411Z","shell.execute_reply.started":"2026-01-12T06:25:12.584175Z","shell.execute_reply":"2026-01-12T06:25:12.587850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate(text):\n    words = text.lower().replace(\"–\", \"-\").split()\n    return \" \".join(AKK_DICT.get(w, w) for w in words)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.589165Z","iopub.execute_input":"2026-01-12T06:25:12.589438Z","iopub.status.idle":"2026-01-12T06:25:12.601827Z","shell.execute_reply.started":"2026-01-12T06:25:12.589414Z","shell.execute_reply":"2026-01-12T06:25:12.601222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = test[\"transliteration\"].apply(translate)\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.602750Z","iopub.execute_input":"2026-01-12T06:25:12.603058Z","iopub.status.idle":"2026-01-12T06:25:12.616380Z","shell.execute_reply.started":"2026-01-12T06:25:12.603028Z","shell.execute_reply":"2026-01-12T06:25:12.615811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PHRASES = {\n    \"um-ma\": \"Thus says\",\n    \"um-ma \": \"Thus says \",\n    \"ki-ma\": \"When\",\n    \"ki-ma \": \"When \",\n    \"i-na\": \"In\",\n    \"a-na\": \"to\",\n    \"ṭuppim\": \"tablet\",\n    \"ṭuppi\": \"tablet\",\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.617158Z","iopub.execute_input":"2026-01-12T06:25:12.617431Z","iopub.status.idle":"2026-01-12T06:25:12.628082Z","shell.execute_reply.started":"2026-01-12T06:25:12.617409Z","shell.execute_reply":"2026-01-12T06:25:12.627318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate(text):\n    out = text.lower()\n\n    # phrase replacements (BLEU gold)\n    for akk, eng in PHRASES.items():\n        out = out.replace(akk, eng)\n\n    # word-level fallback\n    words = out.split()\n    final = [AKK_DICT.get(w, w) for w in words]\n\n    return \" \".join(final)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.629020Z","iopub.execute_input":"2026-01-12T06:25:12.629244Z","iopub.status.idle":"2026-01-12T06:25:12.641498Z","shell.execute_reply.started":"2026-01-12T06:25:12.629219Z","shell.execute_reply":"2026-01-12T06:25:12.640850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = test[\"transliteration\"].apply(translate)\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.643021Z","iopub.execute_input":"2026-01-12T06:25:12.643264Z","iopub.status.idle":"2026-01-12T06:25:12.657171Z","shell.execute_reply.started":"2026-01-12T06:25:12.643231Z","shell.execute_reply":"2026-01-12T06:25:12.656425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"HEADER = \"Thus says the king: \"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.657955Z","iopub.execute_input":"2026-01-12T06:25:12.658251Z","iopub.status.idle":"2026-01-12T06:25:12.669530Z","shell.execute_reply.started":"2026-01-12T06:25:12.658217Z","shell.execute_reply":"2026-01-12T06:25:12.668863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"AKK_DICT = {\n    \"um-ma\": \"Thus says\",\n    \"umma\": \"Thus says\",\n    \"a-na\": \"to\",\n    \"ana\": \"to\",\n    \"i-na\": \"in\",\n    \"ina\": \"in\",\n    \"ki-ma\": \"when\",\n    \"tuppi\": \"tablet\",\n    \"tuppim\": \"tablet\",\n}\n\ndef translate(text):\n    words = text.lower().split()\n    body = \" \".join(AKK_DICT.get(w, w) for w in words)\n    return \"Thus says the king: \" + body\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.670255Z","iopub.execute_input":"2026-01-12T06:25:12.670433Z","iopub.status.idle":"2026-01-12T06:25:12.683055Z","shell.execute_reply.started":"2026-01-12T06:25:12.670415Z","shell.execute_reply":"2026-01-12T06:25:12.682457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = test[\"transliteration\"].apply(translate)\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.683839Z","iopub.execute_input":"2026-01-12T06:25:12.684146Z","iopub.status.idle":"2026-01-12T06:25:12.696692Z","shell.execute_reply.started":"2026-01-12T06:25:12.684125Z","shell.execute_reply":"2026-01-12T06:25:12.696132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/train.csv\"\n)\n\nprint(train.columns)\ntrain.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.697553Z","iopub.execute_input":"2026-01-12T06:25:12.697836Z","iopub.status.idle":"2026-01-12T06:25:12.739869Z","shell.execute_reply.started":"2026-01-12T06:25:12.697804Z","shell.execute_reply":"2026-01-12T06:25:12.739330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import Counter\n\ndef extract_phrases(text, n=2):\n    words = text.lower().split()\n    return [\" \".join(words[i:i+n]) for i in range(len(words)-n+1)]\n\nakk_phrase_counts = Counter()\n\nfor t in train[\"transliteration\"]:\n    for n in [2, 3, 4]:\n        akk_phrase_counts.update(extract_phrases(t, n))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.740585Z","iopub.execute_input":"2026-01-12T06:25:12.740796Z","iopub.status.idle":"2026-01-12T06:25:12.949256Z","shell.execute_reply.started":"2026-01-12T06:25:12.740778Z","shell.execute_reply":"2026-01-12T06:25:12.948396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"COMMON_AKK_PHRASES = {\n    p for p, c in akk_phrase_counts.items() if c >= 20\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.950250Z","iopub.execute_input":"2026-01-12T06:25:12.950566Z","iopub.status.idle":"2026-01-12T06:25:12.963276Z","shell.execute_reply.started":"2026-01-12T06:25:12.950535Z","shell.execute_reply":"2026-01-12T06:25:12.962572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import defaultdict\n\nphrase_map = defaultdict(Counter)\n\nfor _, row in train.iterrows():\n    akk = row[\"transliteration\"].lower()\n    eng = row[\"translation\"].lower()\n\n    for p in COMMON_AKK_PHRASES:\n        if p in akk:\n            phrase_map[p].update([eng])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:12.964235Z","iopub.execute_input":"2026-01-12T06:25:12.964851Z","iopub.status.idle":"2026-01-12T06:25:13.522322Z","shell.execute_reply.started":"2026-01-12T06:25:12.964829Z","shell.execute_reply":"2026-01-12T06:25:13.521492Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate(text):\n    out = text.lower()\n\n    # Phrase-level replacement (highest priority)\n    for akk, eng in PHRASE_TABLE.items():\n        out = out.replace(akk, eng)\n\n    # Word-level fallback\n    words = out.split()\n    final = [AKK_DICT.get(w, w) for w in words]\n\n    return \" \".join(final)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:13.523210Z","iopub.execute_input":"2026-01-12T06:25:13.523808Z","iopub.status.idle":"2026-01-12T06:25:13.528019Z","shell.execute_reply.started":"2026-01-12T06:25:13.523783Z","shell.execute_reply":"2026-01-12T06:25:13.527311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PHRASE_TABLE = {\n    p: phrase_map[p].most_common(1)[0][0]\n    for p in phrase_map if phrase_map[p]\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:13.528820Z","iopub.execute_input":"2026-01-12T06:25:13.529244Z","iopub.status.idle":"2026-01-12T06:25:13.546729Z","shell.execute_reply.started":"2026-01-12T06:25:13.529221Z","shell.execute_reply":"2026-01-12T06:25:13.546092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = test[\"transliteration\"].apply(translate)\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:13.547482Z","iopub.execute_input":"2026-01-12T06:25:13.547739Z","iopub.status.idle":"2026-01-12T06:25:13.562047Z","shell.execute_reply.started":"2026-01-12T06:25:13.547716Z","shell.execute_reply":"2026-01-12T06:25:13.561479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PHRASES = {\n    \"um-ma\": \"Thus says\",\n    \"umma\": \"Thus says\",\n\n    \"a-na\": \"to\",\n    \"ana\": \"to\",\n\n    \"i-na\": \"in\",\n    \"ina\": \"in\",\n\n    \"ki-ma\": \"when\",\n    \"kima\": \"when\",\n\n    \"ṭuppim\": \"tablet\",\n    \"ṭuppi\": \"tablet\",\n    \"tuppim\": \"tablet\",\n    \"tuppi\": \"tablet\",\n\n    \"mup-pì-im\": \"letter\",\n    \"muppim\": \"letter\",\n\n    \"ša\": \"of\",\n    \"ša-\": \"of\",\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:13.562796Z","iopub.execute_input":"2026-01-12T06:25:13.563076Z","iopub.status.idle":"2026-01-12T06:25:13.574988Z","shell.execute_reply.started":"2026-01-12T06:25:13.563043Z","shell.execute_reply":"2026-01-12T06:25:13.574310Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate(text):\n    out = text.lower()\n\n    # phrase replacement (safe only)\n    for akk, eng in PHRASES.items():\n        out = out.replace(akk, eng)\n\n    return out\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:13.575769Z","iopub.execute_input":"2026-01-12T06:25:13.575999Z","iopub.status.idle":"2026-01-12T06:25:13.587663Z","shell.execute_reply.started":"2026-01-12T06:25:13.575978Z","shell.execute_reply":"2026-01-12T06:25:13.587043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"translation\"] = test[\"transliteration\"].apply(translate)\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:13.588484Z","iopub.execute_input":"2026-01-12T06:25:13.588787Z","iopub.status.idle":"2026-01-12T06:25:13.602055Z","shell.execute_reply.started":"2026-01-12T06:25:13.588744Z","shell.execute_reply":"2026-01-12T06:25:13.601277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q transformers datasets sentencepiece\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:13.602783Z","iopub.execute_input":"2026-01-12T06:25:13.603070Z","iopub.status.idle":"2026-01-12T06:25:16.945625Z","shell.execute_reply.started":"2026-01-12T06:25:13.603039Z","shell.execute_reply":"2026-01-12T06:25:16.944816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport torch\n\nfrom datasets import Dataset\nfrom transformers import (\n    T5ForConditionalGeneration,\n    AutoTokenizer,\n    DataCollatorForSeq2Seq,\n    Trainer,\n    TrainingArguments\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:16.947113Z","iopub.execute_input":"2026-01-12T06:25:16.947454Z","iopub.status.idle":"2026-01-12T06:25:16.951816Z","shell.execute_reply.started":"2026-01-12T06:25:16.947409Z","shell.execute_reply":"2026-01-12T06:25:16.951176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/train.csv\"\n)\ntest_df = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\"\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:16.952693Z","iopub.execute_input":"2026-01-12T06:25:16.953006Z","iopub.status.idle":"2026-01-12T06:25:17.006177Z","shell.execute_reply.started":"2026-01-12T06:25:16.952974Z","shell.execute_reply":"2026-01-12T06:25:17.005365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = Dataset.from_pandas(\n    train_df[[\"transliteration\", \"translation\"]]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:17.007216Z","iopub.execute_input":"2026-01-12T06:25:17.007524Z","iopub.status.idle":"2026-01-12T06:25:17.033398Z","shell.execute_reply.started":"2026-01-12T06:25:17.007490Z","shell.execute_reply":"2026-01-12T06:25:17.032816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_name = \"google/byt5-small\"\n\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = T5ForConditionalGeneration.from_pretrained(model_name)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:17.034277Z","iopub.execute_input":"2026-01-12T06:25:17.034533Z","iopub.status.idle":"2026-01-12T06:25:19.424915Z","shell.execute_reply.started":"2026-01-12T06:25:17.034510Z","shell.execute_reply":"2026-01-12T06:25:19.424323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"remove_unused_columns=False\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:19.425730Z","iopub.execute_input":"2026-01-12T06:25:19.426025Z","iopub.status.idle":"2026-01-12T06:25:19.429578Z","shell.execute_reply.started":"2026-01-12T06:25:19.426000Z","shell.execute_reply":"2026-01-12T06:25:19.428961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess(batch):\n    inputs = [\n        \"translate Akkadian to English: \" + t\n        for t in batch[\"transliteration\"]\n    ]\n\n    model_inputs = tokenizer(\n        inputs,\n        truncation=True,\n        padding=\"max_length\",\n        max_length=256,\n        text_target=batch[\"translation\"],\n    )\n\n    return model_inputs\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:19.430434Z","iopub.execute_input":"2026-01-12T06:25:19.431320Z","iopub.status.idle":"2026-01-12T06:25:19.443745Z","shell.execute_reply.started":"2026-01-12T06:25:19.431296Z","shell.execute_reply":"2026-01-12T06:25:19.443038Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = train_ds.map(\n    preprocess,\n    batched=True,\n    remove_columns=train_ds.column_names\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:19.444687Z","iopub.execute_input":"2026-01-12T06:25:19.444957Z","iopub.status.idle":"2026-01-12T06:25:20.929824Z","shell.execute_reply.started":"2026-01-12T06:25:19.444928Z","shell.execute_reply":"2026-01-12T06:25:20.929120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_args = TrainingArguments(\n    output_dir=\"./byt5-akkadian\",\n    per_device_train_batch_size=4,\n    gradient_accumulation_steps=4,\n    learning_rate=1e-4,\n    num_train_epochs=6,\n    fp16=True,\n    logging_steps=100,\n    save_steps=10_000,\n    save_total_limit=1,\n    report_to=\"none\",\n    remove_unused_columns=False   # ← REQUIRED\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:20.930764Z","iopub.execute_input":"2026-01-12T06:25:20.931148Z","iopub.status.idle":"2026-01-12T06:25:20.964150Z","shell.execute_reply.started":"2026-01-12T06:25:20.931116Z","shell.execute_reply":"2026-01-12T06:25:20.963582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=train_ds,\n    data_collator=DataCollatorForSeq2Seq(tokenizer, model),\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:20.965149Z","iopub.execute_input":"2026-01-12T06:25:20.965397Z","iopub.status.idle":"2026-01-12T06:25:21.297804Z","shell.execute_reply.started":"2026-01-12T06:25:20.965375Z","shell.execute_reply":"2026-01-12T06:25:21.297228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer.train()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:25:21.298592Z","iopub.execute_input":"2026-01-12T06:25:21.298874Z","iopub.status.idle":"2026-01-12T06:44:34.624232Z","shell.execute_reply.started":"2026-01-12T06:25:21.298849Z","shell.execute_reply":"2026-01-12T06:44:34.623446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def generate_translation(text):\n    input_ids = tokenizer(\n        \"translate Akkadian to English: \" + text,\n        return_tensors=\"pt\",\n        truncation=True,\n        max_length=256\n    ).input_ids.to(model.device)\n\n    outputs = model.generate(\n        input_ids,\n        max_length=256,\n        num_beams=5,\n        early_stopping=True\n    )\n\n    return tokenizer.decode(outputs[0], skip_special_tokens=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:44:34.625650Z","iopub.execute_input":"2026-01-12T06:44:34.626042Z","iopub.status.idle":"2026-01-12T06:44:34.632706Z","shell.execute_reply.started":"2026-01-12T06:44:34.626001Z","shell.execute_reply":"2026-01-12T06:44:34.631949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"translations = [\n    generate_translation(t)\n    for t in test_df[\"transliteration\"]\n]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:44:34.633844Z","iopub.execute_input":"2026-01-12T06:44:34.634236Z","iopub.status.idle":"2026-01-12T06:44:54.406214Z","shell.execute_reply.started":"2026-01-12T06:44:34.634198Z","shell.execute_reply":"2026-01-12T06:44:54.405608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.read_csv(\n    \"/kaggle/input/deep-past-initiative-machine-translation/sample_submission.csv\"\n)\n\nsubmission[\"translation\"] = translations\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:44:54.407094Z","iopub.execute_input":"2026-01-12T06:44:54.407442Z","iopub.status.idle":"2026-01-12T06:44:54.414662Z","shell.execute_reply.started":"2026-01-12T06:44:54.407418Z","shell.execute_reply":"2026-01-12T06:44:54.414136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T06:44:54.415621Z","iopub.execute_input":"2026-01-12T06:44:54.415872Z","iopub.status.idle":"2026-01-12T06:44:54.424512Z","shell.execute_reply.started":"2026-01-12T06:44:54.415849Z","shell.execute_reply":"2026-01-12T06:44:54.423950Z"}},"outputs":[],"execution_count":null}]}