{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.0"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":121150,"databundleVersionId":15833391},{"sourceType":"kernelVersion","sourceId":303628302}],"dockerImageVersionId":31287,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"cell-0","cell_type":"markdown","source":"# DPC Inference - ByT5-large\n\nGenerate translations for competition submission.","metadata":{}},{"id":"cell-1","cell_type":"code","source":"# our code starts here\nimport os\nos.environ[\"HF_HUB_OFFLINE\"] = \"1\"\n\nimport re\nimport pandas as pd\nimport torch\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n\nMODEL_DIR = \"/kaggle/input/notebooks/iankahn/ian-train-large/byt5-akkadian-large\"\nTEST_PATH = \"/kaggle/input/competitions/deep-past-initiative-machine-translation/test.csv\"\nPREFIX = \"translate Akkadian to English: \"\nBATCH_SIZE = 8\nMAX_LENGTH = 512\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\n\ndef clean_transliteration(text):\n    if pd.isna(text):\n        return \"\"\n    text = str(text)\n    text = text.replace(\"\\u1e2a\", \"H\").replace(\"\\u1e2b\", \"h\")\n    text = re.sub(r\"\\[x\\]\", \"<gap>\", text)\n    text = re.sub(r\"\\[\\u2026\\]\", \"<big_gap>\", text)\n    text = re.sub(r\"\\[\\.\\.\\.\\]\", \"<big_gap>\", text)\n    text = re.sub(r\"\\u2026\", \"<big_gap>\", text)\n    text = re.sub(r\"\\.{3,}\", \"<big_gap>\", text)\n    text = re.sub(r\"[!?/:.]\", \"\", text)\n    text = re.sub(r\"[\\u02f9\\u02fa\\[\\]<>]\", \"\", text)\n    text = re.sub(r\"\\s+\", \" \", text).strip()\n    return text\n\n\ntokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR).to(DEVICE)\nmodel.eval()\n\ntest_df = pd.read_csv(TEST_PATH)\ntest_df[\"transliteration\"] = test_df[\"transliteration\"].apply(clean_transliteration)\nprint(f\"Model loaded. Test set: {len(test_df)} rows\")","metadata":{},"outputs":[],"execution_count":null},{"id":"cell-2","cell_type":"code","source":"all_preds = []\n\nfor i in range(0, len(test_df), BATCH_SIZE):\n    batch = [PREFIX + str(t) for t in test_df[\"transliteration\"].iloc[i:i+BATCH_SIZE]]\n    inputs = tokenizer(batch, return_tensors=\"pt\", padding=True, truncation=True, max_length=MAX_LENGTH).to(DEVICE)\n    with torch.no_grad():\n        out = model.generate(**inputs, max_length=MAX_LENGTH, num_beams=4, early_stopping=True)\n    all_preds.extend([d.strip() for d in tokenizer.batch_decode(out, skip_special_tokens=True)])\n    if (i // BATCH_SIZE) % 10 == 0:\n        print(f\"Batch {i // BATCH_SIZE + 1}/{(len(test_df) + BATCH_SIZE - 1) // BATCH_SIZE}\")\n\nprint(f\"Generated {len(all_preds)} translations\")","metadata":{},"outputs":[],"execution_count":null},{"id":"cell-3","cell_type":"code","source":"submission = pd.DataFrame({\"id\": test_df[\"id\"], \"translation\": all_preds})\nsubmission[\"translation\"] = submission[\"translation\"].apply(lambda x: x if len(x) > 0 else \"broken text\")\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(f\"Submission saved: {len(submission)} rows\")\nsubmission.head(10)\n# our code ends here","metadata":{},"outputs":[],"execution_count":null}]}