{"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":"none","dataSources":[{"sourceType":"competition","sourceId":121150,"databundleVersionId":15833391}],"dockerImageVersionId":31286,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q \"transformers>=4.38.0,<6.0.0\"\n!pip install -q torch\n!pip install -q sentencepiece\n!pip install -q accelerate>=0.30.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:15:54.661068Z","iopub.execute_input":"2026-02-27T09:15:54.662116Z","iopub.status.idle":"2026-02-27T09:16:10.383197Z","shell.execute_reply.started":"2026-02-27T09:15:54.662083Z","shell.execute_reply":"2026-02-27T09:16:10.382072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport torch\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline, logging\nimport matplotlib.pyplot as plt\nlogging.set_verbosity_error()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:17:48.300679Z","iopub.execute_input":"2026-02-27T09:17:48.301006Z","iopub.status.idle":"2026-02-27T09:17:48.306639Z","shell.execute_reply.started":"2026-02-27T09:17:48.300961Z","shell.execute_reply":"2026-02-27T09:17:48.305608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Config:\n    MODEL_PATH = \"/kaggle/input/dpc-starter-infer-add-sentencealign/model_weights\"\n    TRAIN_CSV = \"/kaggle/input/deep-past-challenge/train.csv\"\n    TEST_CSV = \"/kaggle/input/deep-past-challenge/test.csv\"\n    \n    MAX_LENGTH = 128\n    BATCH_SIZE = 8   \n    BEAM_SIZE = 4    \n    SEED = 42","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:18:01.563975Z","iopub.execute_input":"2026-02-27T09:18:01.564758Z","iopub.status.idle":"2026-02-27T09:18:01.569389Z","shell.execute_reply.started":"2026-02-27T09:18:01.564725Z","shell.execute_reply":"2026-02-27T09:18:01.568236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = 0 if torch.cuda.is_available() else -1\nprint(\"Using GPU\" if device==0 else \"Using CPU\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:18:25.342383Z","iopub.execute_input":"2026-02-27T09:18:25.343149Z","iopub.status.idle":"2026-02-27T09:18:25.348360Z","shell.execute_reply.started":"2026-02-27T09:18:25.343115Z","shell.execute_reply":"2026-02-27T09:18:25.347304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def batch_translate(texts, batch_size=Config.BATCH_SIZE):\n    predictions = []\n\n    for i in range(0, len(texts), batch_size):\n        batch = texts[i:i+batch_size]\n        batch = [str(t) if t else \"\" for t in batch]\n\n        outputs = translator(\n            batch,\n            max_length=Config.MAX_LENGTH,\n            num_beams=Config.BEAM_SIZE\n        )\n\n        predictions.extend([o[\"generated_text\"] for o in outputs])\n\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:23:41.254056Z","iopub.execute_input":"2026-02-27T09:23:41.255132Z","iopub.status.idle":"2026-02-27T09:23:41.260630Z","shell.execute_reply.started":"2026-02-27T09:23:41.255099Z","shell.execute_reply":"2026-02-27T09:23:41.259856Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if os.path.exists(Config.TRAIN_CSV):\n    print(\"Loading train data...\")\n    train_df = pd.read_csv(Config.TRAIN_CSV)\n\n    print(\"Train Shape:\", train_df.shape)\n    display(train_df.head())\n\n    print(\"Missing Values:\")\n    print(train_df.isnull().sum())\n\n    train_df[\"akkadian_len\"] = train_df[\"akkadian_text\"].astype(str).apply(len)\n    train_df[\"english_len\"] = train_df[\"english_text\"].astype(str).apply(len)\n\n    display(train_df[[\"akkadian_len\",\"english_len\"]].describe())\n\n    train_df[\"akkadian_len\"].hist(bins=50)\n    plt.title(\"Train Akkadian Length\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:26:07.869832Z","iopub.execute_input":"2026-02-27T09:26:07.870668Z","iopub.status.idle":"2026-02-27T09:26:07.876441Z","shell.execute_reply.started":"2026-02-27T09:26:07.870635Z","shell.execute_reply":"2026-02-27T09:26:07.875395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if os.path.exists(Config.TRAIN_CSV):\n    print(\"Loading train data...\")\n    train_df = pd.read_csv(Config.TRAIN_CSV)\n\n    print(\"Train Shape:\", train_df.shape)\n    display(train_df.head())\n\n    print(\"Missing Values:\")\n    print(train_df.isnull().sum())\n\n    train_df[\"akkadian_len\"] = train_df[\"akkadian_text\"].astype(str).apply(len)\n    train_df[\"english_len\"] = train_df[\"english_text\"].astype(str).apply(len)\n\n    display(train_df[[\"akkadian_len\",\"english_len\"]].describe())\n\n    train_df[\"akkadian_len\"].hist(bins=50)\n    plt.title(\"Train Akkadian Length\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:27:14.789779Z","iopub.execute_input":"2026-02-27T09:27:14.790280Z","iopub.status.idle":"2026-02-27T09:27:14.796599Z","shell.execute_reply.started":"2026-02-27T09:27:14.790249Z","shell.execute_reply":"2026-02-27T09:27:14.795464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess_akkadian(text):\n    if pd.isna(text):\n        return \"\"\n    return str(text).replace(\"[\", \"\").replace(\"]\", \"\").strip()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:19:08.162946Z","iopub.execute_input":"2026-02-27T09:19:08.163803Z","iopub.status.idle":"2026-02-27T09:19:08.168608Z","shell.execute_reply.started":"2026-02-27T09:19:08.163753Z","shell.execute_reply":"2026-02-27T09:19:08.167637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def batch_translate(texts, batch_size=Config.BATCH_SIZE):\n    results = []\n    \n    for i in tqdm(range(0, len(texts), batch_size)):\n        batch = texts[i:i+batch_size]\n        batch = [t if isinstance(t,str) else \"\" for t in batch]\n        \n        try:\n            translations = translator(batch)\n            results.extend([t['translation_text'] for t in translations])\n        except:\n            results.extend([\"\"] * len(batch))\n            \n    return results","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:19:19.989548Z","iopub.execute_input":"2026-02-27T09:19:19.990334Z","iopub.status.idle":"2026-02-27T09:19:19.996313Z","shell.execute_reply.started":"2026-02-27T09:19:19.990300Z","shell.execute_reply":"2026-02-27T09:19:19.995104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if os.path.exists(Config.TRAIN_CSV):\n    print(\"Running validation...\")\n    train_full = pd.read_csv(Config.TRAIN_CSV)\n\n    sample_size = min(200, len(train_full))\n    _, val_df = train_test_split(train_full, test_size=sample_size, random_state=Config.SEED)\n\n    val_sources = val_df['akkadian_text'].apply(preprocess_akkadian).tolist()\n    val_refs = val_df['english_text'].fillna(\"\").tolist()\n\n    val_preds = batch_translate(val_sources)\n    bleu = sacrebleu.corpus_bleu(val_preds, [val_refs])\n\n    print(f\"BLEU Score: {bleu.score:.2f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:19:42.075497Z","iopub.execute_input":"2026-02-27T09:19:42.075812Z","iopub.status.idle":"2026-02-27T09:19:42.081657Z","shell.execute_reply.started":"2026-02-27T09:19:42.075786Z","shell.execute_reply":"2026-02-27T09:19:42.080708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if os.path.exists(Config.TEST_CSV):\n    print(\"Generating submission...\")\n\n    test_df = pd.read_csv(Config.TEST_CSV)\n    test_df['cleaned'] = test_df['akkadian_text'].apply(preprocess_akkadian)\n\n    final_preds = batch_translate(test_df['cleaned'].tolist())\n\n    submission = pd.DataFrame({\n        \"id\": test_df[\"id\"],\n        \"translated_text\": final_preds\n    })\n\n    submission.to_csv(\"/kaggle/working/submission.csv\", index=False)\n    print(\"submission.csv saved successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:19:55.534427Z","iopub.execute_input":"2026-02-27T09:19:55.535451Z","iopub.status.idle":"2026-02-27T09:19:55.540674Z","shell.execute_reply.started":"2026-02-27T09:19:55.535417Z","shell.execute_reply":"2026-02-27T09:19:55.539626Z"}},"outputs":[],"execution_count":null}]}