{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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":15745837,"isSourceIdPinned":false},{"sourceType":"datasetVersion","sourceId":14843017,"datasetId":9493444,"databundleVersionId":15702338},{"sourceType":"datasetVersion","sourceId":14822067,"datasetId":9479100,"databundleVersionId":15679192}],"dockerImageVersionId":31259,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 🏺 Trinity Akkadian SOTA - Offline Inference (V1.5 - Fix & Load)\n\n**Objective**: Offline translation with specialized compatibility fixes for Kaggle/Transformers.\n**Status**: Patching T5 Tokenizer configuration.\n\n---","metadata":{}},{"cell_type":"code","source":"import os\nimport zipfile\nimport json\nimport pandas as pd\nimport numpy as np\nimport torch\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM\nfrom tqdm import tqdm\n\nprint(\"🔍 RECONNAISSANCE: Scanning /kaggle/input...\")\n\nMODEL_PATH = None\n\n# 1. Scour the entire input tree (optimized for depth)\nfor root, dirs, files in os.walk('/kaggle/input'):\n    if 'config.json' in files and 'model.safetensors' in files:\n        if 'checkpoint-11040' in root:\n            MODEL_PATH = root\n            print(f\"✅ FOUND TARGET MODEL: {MODEL_PATH}\")\n            break\n        elif MODEL_PATH is None:\n            MODEL_PATH = root # Fallback to first available checkpoint\n\n# 2. Fallback to User Path if still None\nif MODEL_PATH is None:\n    FALLBACK = \"/kaggle/input/datasets/kiza123123/byt5-offline-tokenizer/added_tokens.json\"\n    if os.path.exists(FALLBACK):\n        MODEL_PATH = FALLBACK\n        print(f\"✅ Using user-provided path: {MODEL_PATH}\")\n\nif MODEL_PATH is None:\n    print(\"❌ FAIL: Could not find model directory. Listing contents for audit:\")\n    !ls -R /kaggle/input/ | head -n 50\n    raise FileNotFoundError(\"Model not found.\")\n\nTEST_PATH = \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\"\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(f\"🚀 Device: {device}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-20T18:56:03.653100Z","iopub.execute_input":"2026-02-20T18:56:03.653999Z","iopub.status.idle":"2026-02-20T18:56:03.673407Z","shell.execute_reply.started":"2026-02-20T18:56:03.653958Z","shell.execute_reply":"2026-02-20T18:56:03.672442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 🛠️ CRITICAL FIX: Patching T5 Tokenizer 'AttributeError: list object has no attribute keys'\nprint(\"🛠️ Checking for tokenizer compatibility issues...\")\n\ntry:\n    # Overriding the problematic extra_special_tokens via kwarg\n    tokenizer = AutoTokenizer.from_pretrained(\n        MODEL_PATH, \n        local_files_only=True, \n        extra_special_tokens={}\n    )\n    model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH, local_files_only=True).to(device)\n    print(\"✅ Model loaded successfully using kwarg override.\")\nexcept Exception as e:\n    print(f\"⚠️ kwarg override failed: {e}. Attempting manual file patch...\")\n    \n    # Manual Patch Plan: Copy config to /kaggle/working and remove the offending key\n    os.makedirs(\"/kaggle/working/patched_model\", exist_ok=True)\n    \n    # List of files needed for tokenizer\n    tok_files = ['tokenizer_config.json', 'tokenizer.json', 'special_tokens_map.json', 'spiece.model']\n    for f in tok_files:\n        src = os.path.join(MODEL_PATH, f)\n        if os.path.exists(src):\n            dest = os.path.join(\"/kaggle/working/patched_model\", f)\n            if f == 'tokenizer_config.json':\n                with open(src, 'r') as j:\n                    cfg = json.load(j)\n                # Remove the key that causes AttributeError in transformers internal init\n                if \"extra_special_tokens\" in cfg and isinstance(cfg[\"extra_special_tokens\"], list):\n                    print(\"💉 Fixing 'extra_special_tokens' list in config...\")\n                    del cfg[\"extra_special_tokens\"]\n                with open(dest, 'w') as j:\n                    json.dump(cfg, j)\n            else:\n                # Copy other files as is\n                os.system(f\"cp '{src}' '{dest}'\")\n    \n    # Try loading from patched tokenizer config but original model binary\n    tokenizer = AutoTokenizer.from_pretrained(\"/kaggle/working/patched_model\", local_files_only=True)\n    model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH, local_files_only=True).to(device)\n    print(\"✅ Model loaded successfully after manual file patch.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-20T18:56:17.895161Z","iopub.execute_input":"2026-02-20T18:56:17.895549Z","iopub.status.idle":"2026-02-20T18:56:18.191519Z","shell.execute_reply.started":"2026-02-20T18:56:17.895517Z","shell.execute_reply":"2026-02-20T18:56:18.190543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df = pd.read_csv(TEST_PATH)\nprint(f\"Loaded {len(test_df)} sentences. Starting inference...\")\n\nBATCH_SIZE = 16\nMAX_LENGTH = 128\nresults = []\n\nfor i in tqdm(range(0, len(test_df), BATCH_SIZE)):\n    batch = test_df['transliteration'].iloc[i:i+BATCH_SIZE].tolist()\n    inputs = tokenizer(batch, return_tensors=\"pt\", padding=\"max_length\", truncation=True, max_length=MAX_LENGTH).to(device)\n    with torch.no_grad():\n        outputs = model.generate(**inputs, max_length=MAX_LENGTH)\n    batch_out = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n    results.extend(batch_out)\n\nsubmission = pd.DataFrame({'id': test_df['id'], 'translation': results})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"✅ submission.csv created.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-20T18:56:24.367409Z","iopub.execute_input":"2026-02-20T18:56:24.367792Z","iopub.status.idle":"2026-02-20T18:56:25.727053Z","shell.execute_reply.started":"2026-02-20T18:56:24.367761Z","shell.execute_reply":"2026-02-20T18:56:25.726110Z"}},"outputs":[],"execution_count":null}]}