{"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":15061024},{"sourceType":"kernelVersion","sourceId":287824283},{"sourceType":"kernelVersion","sourceId":287970238},{"sourceType":"kernelVersion","sourceId":288894049},{"sourceType":"kernelVersion","sourceId":289019861}],"dockerImageVersionId":31236,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# [Deep Past Challenge](https://www.kaggle.com/competitions/deep-past-initiative-machine-translation/overview) \n## 📜Converting Akkadian -> English","metadata":{}},{"cell_type":"markdown","source":"This notebook uses some top LB notebooks and creates a blend using them for making robust predictions.\n\n#### Notebooks used:\n\n| Notebook | Author | Score/Version |\n|----------|--------|---------------|\n|[DPC Infer with post-processing by LLM](https://www.kaggle.com/code/takamichitoda/dpc-infer-with-post-processing-by-llm) | [Takamichi Toda](https://www.kaggle.com/takamichitoda) | LB-28.9_v6|\n|[Deep Past Challenge_byt5-base_Inference](https://www.kaggle.com/code/alperenaslann/deep-past-challenge-byt5-base-inference) | [Alp Eren Aslan](https://www.kaggle.com/alperenaslann) | LB-28.9_v1|\n|[Deep Past Challenge Inference](https://www.kaggle.com/code/imaadmahmood/deep-past-challenge-inference) | [Imaad Mahmood](https://www.kaggle.com/imaadmahmood) | LB-28.9_v1|\n\n#### 🙌Thank you to the authors for providing such wonderful notebooks!","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-29T09:57:21.429097Z","iopub.execute_input":"2025-12-29T09:57:21.429391Z","iopub.status.idle":"2025-12-29T09:57:21.469745Z","shell.execute_reply.started":"2025-12-29T09:57:21.429364Z","shell.execute_reply":"2025-12-29T09:57:21.468869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom collections import Counter\n\n# ============================================================\n# STEP 1: Load Test Data\n# ============================================================\nprint(\"=\"*80)\nprint(\"STEP 1: Loading Test Data\")\nprint(\"=\"*80)\n\ntest_df = pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/test.csv\")\nprint(f\"✓ Test data loaded: {len(test_df)} rows\")\n\n# ============================================================\n# STEP 2: Load Model Predictions\n# ============================================================\nprint(\"\\n\" + \"=\"*80)\nprint(\"STEP 2: Loading Model Predictions\")\nprint(\"=\"*80)\n\ndpc_sub = pd.read_csv(\"/kaggle/input/dpc-infer-with-post-processing-by-llm/submission.csv\")\nprint(f\"✓ DPC loaded: {len(dpc_sub)} rows\")\n\nbyt5_sub = pd.read_csv(\"/kaggle/input/deep-past-challenge-byt5-base-inference/submission.csv\")\nprint(f\"✓ ByT5 loaded: {len(byt5_sub)} rows\")\n\ninference_sub = pd.read_csv(\"/kaggle/input/deep-past-challenge-inference/submission.csv\")\nprint(f\"✓ Inference loaded: {len(inference_sub)} rows\")\n\n\ngap_sub = pd.read_csv(\"/kaggle/input/submission-gap/submission.csv\")\nprint(f\"✓ Inference loaded: {len(gap_sub)} rows\")\n# ============================================================\n# STEP 3: Merge All Predictions\n# ============================================================\nprint(\"\\n\" + \"=\"*80)\nprint(\"STEP 3: Merging All Predictions\")\nprint(\"=\"*80)\n\n# Start with test IDs to ensure we have all rows\nmerged = test_df[['id']].copy()\n\n# Merge each submission using LEFT join to keep all test IDs\nmerged = merged.merge(\n    dpc_sub[['id', 'translation']].rename(columns={'translation': 'dpc'}),\n    on='id',\n    how='left'\n)\n\nmerged = merged.merge(\n    byt5_sub[['id', 'translation']].rename(columns={'translation': 'byt5'}),\n    on='id',\n    how='left'\n)\n\nmerged = merged.merge(\n    inference_sub[['id', 'translation']].rename(columns={'translation': 'inference'}),\n    on='id',\n    how='left'\n)\nprint(f\"✓ Merged data: {len(merged)} rows\")\n\nmerged = merged.merge(\n    gap_sub[['id', 'translation']].rename(columns={'translation': 'sub_gap'}),\n    on='id',\n    how='left'\n)\nprint(f\"✓ Merged data: {len(merged)} rows\")\n\nmerged","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T09:59:03.787560Z","iopub.execute_input":"2025-12-29T09:59:03.787937Z","iopub.status.idle":"2025-12-29T09:59:04.265749Z","shell.execute_reply.started":"2025-12-29T09:59:03.787910Z","shell.execute_reply":"2025-12-29T09:59:04.264545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check for missing predictions\nmissing_allcaps = (merged['inference'] == '').sum()\nmissing_dpc = (merged['dpc'] == '').sum()\nmissing_byt5 = (merged['byt5'] == '').sum()\nmissing_byt5 = (merged['sub_gap'] == '').sum()\n\nif missing_allcaps > 0:\n    print(f\"⚠️ WARNING: {missing_allcaps} missing AllCaps predictions\")\nif missing_dpc > 0:\n    print(f\"⚠️ WARNING: {missing_dpc} missing DPC predictions\")\nif missing_byt5 > 0:\n    print(f\"⚠️ WARNING: {missing_byt5} missing ByT5 predictions\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T09:59:40.365748Z","iopub.execute_input":"2025-12-29T09:59:40.367057Z","iopub.status.idle":"2025-12-29T09:59:40.374910Z","shell.execute_reply.started":"2025-12-29T09:59:40.367020Z","shell.execute_reply":"2025-12-29T09:59:40.373692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ============================================================\n# STEP 4: Hybrid Ensemble Function\n# ============================================================\n\ndef hybrid_ensemble(row):\n    \"\"\"\n    Hybrid Ensemble Strategy:\n    1. If all 3 models agree -> use that translation\n    2. If 2 models agree -> use the agreed translation\n    3. If no agreement -> use the longest translation (often more complete)\n    \"\"\"\n    translations = [str(row['dpc']), str(row['byt5']), str(row['inference']), str(row['sub_gap'])]\n    \n    # Remove empty strings\n    translations = [t for t in translations if t.strip()]\n    \n    if not translations:\n        return \"\"\n    \n    # Check for full agreement (all 3 same)\n    if len(set(translations)) == 1:\n        return translations[0]\n    \n    # Check for majority agreement (2 out of 3)\n    counter = Counter(translations)\n    for translation, count in counter.items():\n        if count >= 2:\n            return translation\n    \n    # No agreement - use longest translation\n    return max(translations, key=len)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T10:00:23.545869Z","iopub.execute_input":"2025-12-29T10:00:23.546206Z","iopub.status.idle":"2025-12-29T10:00:23.553944Z","shell.execute_reply.started":"2025-12-29T10:00:23.546180Z","shell.execute_reply":"2025-12-29T10:00:23.552880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ============================================================\n# STEP 5: Apply Hybrid Ensemble to All Test Data\n# ============================================================\nprint(\"\\n\" + \"=\"*80)\nprint(\"STEP 4: Creating Hybrid Ensemble Predictions\")\nprint(\"=\"*80)\n\nsubmission = pd.DataFrame()\nsubmission['id'] = merged['id']\nsubmission['translation'] = merged.apply(hybrid_ensemble, axis=1)\n\nprint(f\"✓ Hybrid ensemble created: {len(submission)} predictions\")\n\nsubmission.to_csv('submission.csv', index=False)\nprint(f\"✅ Saved: submission.csv ({len(submission)} rows)\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T10:00:25.632985Z","iopub.execute_input":"2025-12-29T10:00:25.633321Z","iopub.status.idle":"2025-12-29T10:00:25.652819Z","shell.execute_reply.started":"2025-12-29T10:00:25.633293Z","shell.execute_reply":"2025-12-29T10:00:25.651636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T10:00:29.089724Z","iopub.execute_input":"2025-12-29T10:00:29.090464Z","iopub.status.idle":"2025-12-29T10:00:29.100772Z","shell.execute_reply.started":"2025-12-29T10:00:29.090423Z","shell.execute_reply":"2025-12-29T10:00:29.099330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}