{"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,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":700594,"sourceType":"modelInstanceVersion","isSourceIdPinned":false,"modelInstanceId":531445,"modelId":545271}],"dockerImageVersionId":31236,"isInternetEnabled":false,"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":"2025-12-28T18:40:54.144533Z","iopub.execute_input":"2025-12-28T18:40:54.145290Z","iopub.status.idle":"2025-12-28T18:40:54.413930Z","shell.execute_reply.started":"2025-12-28T18:40:54.145263Z","shell.execute_reply":"2025-12-28T18:40:54.413379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nfrom torch.utils.data import DataLoader, Dataset\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5Tokenizer\nfrom tqdm.auto import tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-28T18:41:27.594865Z","iopub.execute_input":"2025-12-28T18:41:27.595278Z","iopub.status.idle":"2025-12-28T18:41:39.230721Z","shell.execute_reply.started":"2025-12-28T18:41:27.595250Z","shell.execute_reply":"2025-12-28T18:41:39.229993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"MODEL_PATH = \"/kaggle/input/akkad-byt5-base/transformers/default/2/byt5-base-akkadian\"\nTEST_DATA_PATH = \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\"\nBATCH_SIZE = 4\nMAX_LENGTH = 512\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\nprint(f\"Device: {DEVICE}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-28T18:42:47.486901Z","iopub.execute_input":"2025-12-28T18:42:47.487222Z","iopub.status.idle":"2025-12-28T18:42:47.492108Z","shell.execute_reply.started":"2025-12-28T18:42:47.487194Z","shell.execute_reply":"2025-12-28T18:42:47.491228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load model and data - EXACT same as baseline\ntokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH).to(DEVICE)\nmodel.eval()\n\ntest_df = pd.read_csv(TEST_DATA_PATH)\nprint(f\"Test samples: {len(test_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-28T18:42:48.062734Z","iopub.execute_input":"2025-12-28T18:42:48.063055Z","iopub.status.idle":"2025-12-28T18:43:29.987642Z","shell.execute_reply.started":"2025-12-28T18:42:48.063020Z","shell.execute_reply":"2025-12-28T18:43:29.986863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PREFIX = \"translate Akkadian to English: \"\n\nclass InferenceDataset(Dataset):\n    def __init__(self, df, tokenizer):\n        self.texts = df['transliteration'].astype(str).tolist()\n        self.texts = [PREFIX + i for i in self.texts]\n        self.tokenizer = tokenizer\n        \n    def __len__(self):\n        return len(self.texts)\n    \n    def __getitem__(self, idx):\n        text = self.texts[idx]\n        inputs = self.tokenizer(\n            text, \n            max_length=MAX_LENGTH, \n            padding=\"max_length\", \n            truncation=True, \n            return_tensors=\"pt\"\n        )\n        return {\n            \"input_ids\": inputs[\"input_ids\"].squeeze(0),\n            \"attention_mask\": inputs[\"attention_mask\"].squeeze(0)\n        }\n\ntest_dataset = InferenceDataset(test_df, tokenizer)\ntest_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)\n\nprint(\"Starting Inference...\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-28T18:43:57.655943Z","iopub.execute_input":"2025-12-28T18:43:57.656249Z","iopub.status.idle":"2025-12-28T18:43:57.666662Z","shell.execute_reply.started":"2025-12-28T18:43:57.656222Z","shell.execute_reply":"2025-12-28T18:43:57.665778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_predictions = []\n\nwith torch.no_grad():\n    for batch in tqdm(test_loader):\n        input_ids = batch[\"input_ids\"].to(DEVICE)\n        attention_mask = batch[\"attention_mask\"].to(DEVICE)\n  \n        outputs = model.generate(\n            input_ids=input_ids,\n            attention_mask=attention_mask,\n            max_length=MAX_LENGTH,\n            num_beams=4,\n            early_stopping=True\n        )\n        \n        decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n        all_predictions.extend([d.strip() for d in decoded])\n\nprint(f\"Generated {len(all_predictions)} predictions\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-28T18:44:10.206450Z","iopub.execute_input":"2025-12-28T18:44:10.207185Z","iopub.status.idle":"2025-12-28T18:44:17.212458Z","shell.execute_reply.started":"2025-12-28T18:44:10.207156Z","shell.execute_reply":"2025-12-28T18:44:17.211617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"id\": test_df[\"id\"],\n    \"translation\": all_predictions\n})\n\nsubmission[\"translation\"] = submission[\"translation\"].apply(lambda x: x if len(x) > 0 else \"broken text\")\n\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"Submission file saved successfully!\")\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-28T18:44:27.899956Z","iopub.execute_input":"2025-12-28T18:44:27.900509Z","iopub.status.idle":"2025-12-28T18:44:27.927111Z","shell.execute_reply.started":"2025-12-28T18:44:27.900479Z","shell.execute_reply":"2025-12-28T18:44:27.926567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}