{"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":14976537,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":287195187,"sourceType":"kernelVersion"}],"dockerImageVersionId":31236,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **TAE: Inference & Submission Baseline**\n**Translate Akkadian to English**","metadata":{}},{"cell_type":"markdown","source":"Imports","metadata":{}},{"cell_type":"code","source":"import os\nimport torch\nimport pandas as pd\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:11:53.369578Z","iopub.execute_input":"2025-12-19T07:11:53.370358Z","iopub.status.idle":"2025-12-19T07:11:53.374113Z","shell.execute_reply.started":"2025-12-19T07:11:53.370327Z","shell.execute_reply":"2025-12-19T07:11:53.373306Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Path Config","metadata":{}},{"cell_type":"code","source":"ARTIFACT_DIR = \"/kaggle/input/tae-training-model-creation-baseline/artifacts\"\n\nTOKENIZER_DIR = f\"{ARTIFACT_DIR}/tokenizer\"\nMODEL_DIR = f\"{ARTIFACT_DIR}/translator\"\n\nTEST_PATH = \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\"\nSUB_PATH = \"/kaggle/working/submission.csv\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:11:53.375563Z","iopub.execute_input":"2025-12-19T07:11:53.375853Z","iopub.status.idle":"2025-12-19T07:11:53.391340Z","shell.execute_reply.started":"2025-12-19T07:11:53.375829Z","shell.execute_reply":"2025-12-19T07:11:53.390794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/input/tae-training-model-creation-baseline/artifacts/tokenizer","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:11:53.392143Z","iopub.execute_input":"2025-12-19T07:11:53.392416Z","iopub.status.idle":"2025-12-19T07:11:53.546727Z","shell.execute_reply.started":"2025-12-19T07:11:53.392395Z","shell.execute_reply":"2025-12-19T07:11:53.546010Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Load Test Data","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv(TEST_PATH)\n\nassert \"transliteration\" in df_test.columns\nprint(df_test.head())\nprint(\"Test size:\", len(df_test))\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:11:53.548783Z","iopub.execute_input":"2025-12-19T07:11:53.549029Z","iopub.status.idle":"2025-12-19T07:11:53.563972Z","shell.execute_reply.started":"2025-12-19T07:11:53.549000Z","shell.execute_reply":"2025-12-19T07:11:53.563291Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Load Tokenizer & Model","metadata":{}},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\ntokenizer = AutoTokenizer.from_pretrained(TOKENIZER_DIR, local_files_only=True)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR, local_files_only=True)\n\nmodel.to(device)\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:11:53.564699Z","iopub.execute_input":"2025-12-19T07:11:53.564939Z","iopub.status.idle":"2025-12-19T07:12:21.154394Z","shell.execute_reply.started":"2025-12-19T07:11:53.564908Z","shell.execute_reply":"2025-12-19T07:12:21.153648Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Preprocess","metadata":{}},{"cell_type":"code","source":"def preprocess_text(x):\n    return x.strip()\n\ndf_test[\"src\"] = df_test[\"transliteration\"].apply(preprocess_text)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:12:21.155962Z","iopub.execute_input":"2025-12-19T07:12:21.156591Z","iopub.status.idle":"2025-12-19T07:12:21.161391Z","shell.execute_reply.started":"2025-12-19T07:12:21.156565Z","shell.execute_reply":"2025-12-19T07:12:21.160663Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Generation","metadata":{}},{"cell_type":"code","source":"MAX_SRC_LEN = 128\nMAX_GEN_LEN = 128\n\ndef translate_batch(texts, batch_size=16):\n    outputs = []\n\n    for i in range(0, len(texts), batch_size):\n        batch = texts[i:i+batch_size]\n\n        enc = tokenizer(\n            batch,\n            return_tensors=\"pt\",\n            padding=True,\n            truncation=True,\n            max_length=MAX_SRC_LEN\n        ).to(device)\n\n        with torch.no_grad():\n            gen = model.generate(\n                **enc,\n                max_length=MAX_GEN_LEN,\n                num_beams=4,\n                early_stopping=True\n            )\n\n        decoded = tokenizer.batch_decode(gen, skip_special_tokens=True)\n        outputs.extend(decoded)\n\n    return outputs\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:12:21.162461Z","iopub.execute_input":"2025-12-19T07:12:21.162784Z","iopub.status.idle":"2025-12-19T07:12:21.181664Z","shell.execute_reply.started":"2025-12-19T07:12:21.162754Z","shell.execute_reply":"2025-12-19T07:12:21.181013Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Run Inference","metadata":{}},{"cell_type":"code","source":"preds = translate_batch(df_test[\"src\"].tolist(), batch_size=16)\ndf_test[\"translation\"] = preds\n\nprint(df_test[[\"transliteration\", \"translation\"]].head())\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:12:21.183921Z","iopub.execute_input":"2025-12-19T07:12:21.184401Z","iopub.status.idle":"2025-12-19T07:12:24.278895Z","shell.execute_reply.started":"2025-12-19T07:12:21.184370Z","shell.execute_reply":"2025-12-19T07:12:24.278060Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Save Submission","metadata":{}},{"cell_type":"code","source":"submission = df_test[[\"id\",\"translation\"]]\nsubmission.to_csv(SUB_PATH, index=False)\n\nprint(\"Saved:\", SUB_PATH)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-19T07:12:24.280066Z","iopub.execute_input":"2025-12-19T07:12:24.280431Z","iopub.status.idle":"2025-12-19T07:12:24.292982Z","shell.execute_reply.started":"2025-12-19T07:12:24.280396Z","shell.execute_reply":"2025-12-19T07:12:24.292330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}