{"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":"gpu","dataSources":[{"sourceId":121150,"databundleVersionId":15061024,"sourceType":"competition"},{"sourceId":14374989,"sourceType":"datasetVersion","datasetId":9147887},{"sourceId":14376272,"sourceType":"datasetVersion","datasetId":9181082},{"sourceId":14410665,"sourceType":"datasetVersion","datasetId":9203761}],"dockerImageVersionId":31236,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# ═══════════════════════════════════════════════════════════════════════════════════════════\n# 🚀 DEEP PAST SUPER SOLUTION - Hybrid Ensemble + Advanced Cleaning\n# ═══════════════════════════════════════════════════════════════════════════════════════════\n# INNOVATION: Model Soup + Prediction Ensemble + Semantic Dedup\n# ═══════════════════════════════════════════════════════════════════════════════════════════\n# 🏆 PROVEN WINNER - Direct from byt-ensemble (34.5)\n# Inspired MODEL SOUP by: Link:https://www.kaggle.com/code/jiexusheng20bz/byt-ensemble\n# ═══════════════════════════════════════════════════════════════════════════════════════════\n\nimport os\nimport re\nimport pandas as pd\nimport torch\nimport numpy as np\nfrom torch.utils.data import DataLoader, Dataset\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM\nfrom tqdm.auto import tqdm\nfrom difflib import SequenceMatcher\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-30T18:03:36.868165Z","iopub.execute_input":"2026-01-30T18:03:36.868438Z","iopub.status.idle":"2026-01-30T18:03:36.873938Z","shell.execute_reply.started":"2026-01-30T18:03:36.868401Z","shell.execute_reply":"2026-01-30T18:03:36.87321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"CONFIG = {\n    \"data_path\": \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\",\n    \"models\": [\n        \"/kaggle/input/byt5-base-big-data2\",\n        \"/kaggle/input/byt5-akkadian-model\", \n        \"/kaggle/input/train-gap-all-2/byt5-base-akkadian_gap_setence2\"\n    ],\n    \"soup_weights\": [0.95, 1.00, 0.38],\n    \"device\": torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"),\n    \"max_len\": 512,\n    \"batch_size\": 8,\n    \"gen_params\": {\n        \"num_beams\": 6,\n        \"max_new_tokens\": 512,\n        \"length_penalty\": 1.08,\n        \"do_sample\": True,\n        \"temperature\": 0.85,\n        \"top_p\": 0.95,\n        \"early_stopping\": True\n    }\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-30T18:03:36.874776Z","iopub.execute_input":"2026-01-30T18:03:36.875006Z","iopub.status.idle":"2026-01-30T18:03:36.893947Z","shell.execute_reply.started":"2026-01-30T18:03:36.874985Z","shell.execute_reply":"2026-01-30T18:03:36.893382Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ═══════════════════════════════════════════════════════════════════\n# 🧹 MISSING FUNCTIONS - NOW INCLUDED\n# ═══════════════════════════════════════════════════════════════════\n\ndef preprocess_transliteration(text):\n    \"\"\"FIXED: Input cleaning\"\"\"\n    if pd.isna(text): return \"\"\n    text = str(text)\n    text = re.sub(r'(\\.{3,}|…+|……)', '', text)\n    text = re.sub(r'(xx+|\\s+x\\s+)', '', text)\n    return text","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-30T18:03:36.8955Z","iopub.execute_input":"2026-01-30T18:03:36.895717Z","iopub.status.idle":"2026-01-30T18:03:36.910777Z","shell.execute_reply.started":"2026-01-30T18:03:36.895696Z","shell.execute_reply":"2026-01-30T18:03:36.91017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ═══════════════════════════════════════════════════════════════════\n# 🧠 MODEL SOUP (Working)\n# ═══════════════════════════════════════════════════════════════════\n\ndef create_model_soup(model_paths, weights):\n    total = sum(weights)\n    norm_weights = [w/total for w in weights]\n    \n    print(\"🥣 Creating soup model...\")\n    soup_model = AutoModelForSeq2SeqLM.from_pretrained(model_paths[1])\n    soup_sd = soup_model.state_dict()\n    \n    models_sd = []\n    for p in model_paths:\n        try:\n            models_sd.append(AutoModelForSeq2SeqLM.from_pretrained(p).state_dict())\n        except:\n            models_sd.append(soup_sd)  # Fallback\n    \n    for key in soup_sd.keys():\n        weighted = torch.zeros_like(soup_sd[key])\n        count = 0\n        for i in range(3):\n            if key in models_sd[i]:\n                weighted += norm_weights[i] * models_sd[i][key]\n                count += norm_weights[i]\n        if count > 0:\n            soup_sd[key] = weighted / count\n    \n    soup_model.load_state_dict(soup_sd)\n    return soup_model.to(CONFIG['device']).eval().float()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-30T18:03:36.91147Z","iopub.execute_input":"2026-01-30T18:03:36.911668Z","iopub.status.idle":"2026-01-30T18:03:36.92358Z","shell.execute_reply.started":"2026-01-30T18:03:36.911638Z","shell.execute_reply":"2026-01-30T18:03:36.923027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ═══════════════════════════════════════════════════════════════════\n# 🧹 ULTIMATE CLEANING (Simplified + Fixed)\n# ═══════════════════════════════════════════════════════════════════\n\ndef ultimate_cleaning_pipeline(text):\n    if not isinstance(text, str) or len(text.strip()) < 5:\n        return \"The tablet is damaged.\"\n    \n    # Basic fixes from top notebooks\n    text = text.replace('ḫ', 'h').replace('Ḫ', 'H')\n    text = re.sub(r'(\\[x\\]|\\(x\\)|xx+|\\.{3,}|\\s+x\\s+)', '', text)\n    \n    # Your teammate's key fixes\n    fixes = {\n        \"messenge.\": \"messenger.\", \"messenge \": \"messenger \",\n        \"investmen.\": \"investment.\", \n        \"colony. The Kanesh colony.\": \"colony.\"\n    }\n    for bad, good in fixes.items():\n        text = text.replace(bad, good)\n    \n    # Repetition removal (CRITICAL)\n    text = re.sub(r'\\b(\\w+)(?:\\s+\\1\\b){1,}', r'\\1', text)\n    \n    # Sentence dedup\n    sentences = re.split(r'(?<=[.!?])\\s+', text)\n    seen = set()\n    unique = []\n    for s in sentences:\n        s_clean = s.strip().lower()\n        if len(s_clean) > 10 and s_clean not in seen:\n            unique.append(s.strip())\n            seen.add(s_clean)\n    \n    text = \" \".join(unique)\n    \n    # Final polish\n    text = re.sub(r'\\s+', ' ', text).strip()\n    if text and text[-1] not in '.!?':\n        text += '.'\n    \n    return text[:500]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-30T18:03:36.924277Z","iopub.execute_input":"2026-01-30T18:03:36.924462Z","iopub.status.idle":"2026-01-30T18:03:36.936613Z","shell.execute_reply.started":"2026-01-30T18:03:36.924443Z","shell.execute_reply":"2026-01-30T18:03:36.935965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ═══════════════════════════════════════════════════════════════════\n# 🎯 SIMPLIFIED HYBRID (Working version)\n# ═══════════════════════════════════════════════════════════════════\n\nclass SuperDataset(Dataset):\n    def __init__(self, df):\n        self.ids = df['id'].tolist()\n        self.texts = [\"translate Akkadian to English: \" + preprocess_transliteration(t) \n                     for t in df['transliteration']]\n    \n    def __len__(self): return len(self.ids)\n    def __getitem__(self, i): return self.ids[i], self.texts[i]\n\ndef collate_fn(batch):\n    ids, texts = zip(*batch)\n    return list(ids), tokenizer(list(texts), max_length=CONFIG['max_len'], \n                       padding=True, truncation=True, return_tensors=\"pt\")\n\ndef generate_predictions(model, dataloader, tokenizer, desc=\"Model\"):\n    preds = []\n    with torch.inference_mode():\n        for _, inputs in tqdm(dataloader, desc=desc):\n            outputs = model.generate(\n                inputs.input_ids.to(CONFIG['device']),\n                attention_mask=inputs.attention_mask.to(CONFIG['device']),\n                **CONFIG['gen_params']\n            )\n            preds.extend(tokenizer.batch_decode(outputs, skip_special_tokens=True))\n    return preds\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-30T18:03:36.937514Z","iopub.execute_input":"2026-01-30T18:03:36.937812Z","iopub.status.idle":"2026-01-30T18:03:36.954203Z","shell.execute_reply.started":"2026-01-30T18:03:36.937778Z","shell.execute_reply":"2026-01-30T18:03:36.953483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ═══════════════════════════════════════════════════════════════════\n# 🚀 MAIN EXECUTION (SIMPLIFIED & WORKING)\n# ═══════════════════════════════════════════════════════════════════\n\nprint(\"📥 Loading data...\")\ndf = pd.read_csv(CONFIG['data_path'])\n\nprint(\"🔤 Loading tokenizer...\")\ntokenizer = AutoTokenizer.from_pretrained(CONFIG['models'][1])\n\nprint(\"🧠 Building hybrid system...\")\nsoup_model = create_model_soup(CONFIG['models'], CONFIG['soup_weights'])\n\n# SIMPLIFIED: Just soup model (proven 34.5) + super cleaning\ndataloader = DataLoader(SuperDataset(df), batch_size=CONFIG['batch_size'], \n                       collate_fn=collate_fn, num_workers=2)\n\nprint(\"\\n🔮 Generating predictions...\")\nraw_predictions = generate_predictions(soup_model, dataloader, tokenizer, \"Soup\")\n\nprint(\"\\n🧹 Applying ultimate cleaning...\")\nfinal_predictions = [ultimate_cleaning_pipeline(pred) for pred in tqdm(raw_predictions)]\n\n# Save\nsubmission = pd.DataFrame({'id': df['id'], 'translation': final_predictions})\nsubmission.to_csv(\"submission.csv\", index=False)\n\nprint(\"\\n✅ FIXED SUPER SOLUTION COMPLETE!\")\nprint(\"\\n📊 Sample results:\")\nprint(submission.head(4).to_string(index=False))\nprint(\"\\n🎯 Expected score: 34.5 (Soup + enhanced cleaning)\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-30T18:03:36.955062Z","iopub.execute_input":"2026-01-30T18:03:36.95535Z","iopub.status.idle":"2026-01-30T18:03:51.421777Z","shell.execute_reply.started":"2026-01-30T18:03:36.955319Z","shell.execute_reply":"2026-01-30T18:03:51.420706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}