{"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":292791507,"sourceType":"kernelVersion"}],"dockerImageVersionId":31236,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nfrom torch.utils.data import DataLoader, Dataset\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM\nfrom tqdm.auto import tqdm\nimport re","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-31T08:10:04.762334Z","iopub.execute_input":"2025-12-31T08:10:04.762577Z","iopub.status.idle":"2025-12-31T08:10:16.748432Z","shell.execute_reply.started":"2025-12-31T08:10:04.762554Z","shell.execute_reply":"2025-12-31T08:10:16.747819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"MODEL_PATH=\"/kaggle/input/deep-past-challenge-training-gap/byt5-base-akkadian_666/\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T08:10:16.749765Z","iopub.execute_input":"2025-12-31T08:10:16.750298Z","iopub.status.idle":"2025-12-31T08:10:16.753550Z","shell.execute_reply.started":"2025-12-31T08:10:16.750272Z","shell.execute_reply":"2025-12-31T08:10:16.752874Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def replace_transliteration(result_str):\n    result_str = result_str.replace(\"<gap>\", \"∅\")\n    result_str = result_str.replace(\"<big_gap>\", \"∆\")\n    result_str = result_str.replace(\"<gap>\", \"∅\")\n    result_str = result_str.replace(\"<big_gap>\", \"∆\")\n\n    result_str = result_str.replace(\"!\",\"\")\n    result_str = result_str.replace(\"+\",\"\")\n    result_str = result_str.replace(\"ı\",\"\")\n    result_str = result_str.replace(\":\",\"\")\n    result_str = result_str.replace(\";\",\"\")\n    \n    result_str = re.sub(r\"  \",' ',result_str).strip()   \n    result_str = re.sub(r\"  \",' ',result_str).strip()   \n    result_str = re.sub(r\"  \",' ',result_str).strip()   \n    \n    return result_str\n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def replace_translation(result_str):\n    \n    result_str = result_str.replace(\"\\\"\",\"\")\n    result_str = result_str.replace(\"∅\", \"<gap>\")\n    result_str = result_str.replace(\"∆\", \"<big_gap>\")\n    result_str = result_str.replace(\"∅\", \"<gap>\")\n    result_str = result_str.replace(\"∆\", \"<big_gap>\")\n\n    return result_str\n    ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TEST_DATA_PATH = \"/kaggle/input/deep-past-initiative-machine-translation/test.csv\"\nBATCH_SIZE = 16\nMAX_LENGTH = 512\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\ntokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH).to(DEVICE)\nmodel.eval()\n\ntest_df = pd.read_csv(TEST_DATA_PATH)\ntest_df[\"transliteration\"]=test_df[\"transliteration\"].apply(replace_transliteration)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T08:10:16.754589Z","iopub.execute_input":"2025-12-31T08:10:16.754908Z","iopub.status.idle":"2025-12-31T08:10:52.917347Z","shell.execute_reply.started":"2025-12-31T08:10:16.754874Z","shell.execute_reply":"2025-12-31T08:10:52.916710Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PREFIX = \"translate Akkadian to English: \"\n\nclass InferenceDataset(Dataset):\n    def __init__(self, df, tokenizer):\n        self.texts = df['transliteration'].astype(str).tolist()\n        self.texts = [PREFIX + i for i in self.texts]\n        self.tokenizer = tokenizer\n        \n    def __len__(self):\n        return len(self.texts)\n    \n    def __getitem__(self, idx):\n        text = self.texts[idx]\n        inputs = self.tokenizer(\n            text, \n            max_length=MAX_LENGTH, \n            padding=\"max_length\", \n            truncation=True, \n            return_tensors=\"pt\"\n        )\n        return {\n            \"input_ids\": inputs[\"input_ids\"].squeeze(0),\n            \"attention_mask\": inputs[\"attention_mask\"].squeeze(0)\n        }\n\ntest_dataset = InferenceDataset(test_df, tokenizer)\ntest_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)\n\nprint(\"Starting Inference...\")\nall_predictions = []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T08:10:52.918935Z","iopub.execute_input":"2025-12-31T08:10:52.919168Z","iopub.status.idle":"2025-12-31T08:10:52.930106Z","shell.execute_reply.started":"2025-12-31T08:10:52.919145Z","shell.execute_reply":"2025-12-31T08:10:52.929401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nwith torch.no_grad():\n    for batch in tqdm(test_loader):\n        input_ids = batch[\"input_ids\"].to(DEVICE)\n        attention_mask = batch[\"attention_mask\"].to(DEVICE)\n  \n        outputs = model.generate(\n            input_ids=input_ids,\n            attention_mask=attention_mask,\n            max_length=MAX_LENGTH,\n            num_beams=4,\n            early_stopping=True\n        )\n        # outputs = model.generate(\n        #     input_ids=input_ids,\n        #     attention_mask=attention_mask,\n        #     max_length=MAX_LENGTH,\n        #     num_beams=4,\n        #     do_sample=True,      # Permet de sortir de la rigidité du beam search\n        #     top_p=0.95,          # Filtre les queues de distribution improbables\n        #     temperature=0.85,     # Équilibre entre créativité et précision\n        #     early_stopping=True\n        #  )\n        # outputs = model.generate(\n        #     input_ids=input_ids,\n        #     attention_mask=attention_mask,\n        #     max_length=MAX_LENGTH,\n        #     min_length=5,  # 避免输出过短（阿卡德语至少有基础语义）\n        #     num_beams=6,  # 增加束搜索数，提升输出完整性（小众语种数据少，束搜索数稍高更优）\n        #     early_stopping=True,\n        #     #no_repeat_ngram_size=3,  # 阿卡德语转写重复多，提升到3避免重复\n        #     length_penalty=1.2,  # 轻微鼓励稍长输出，避免过度截断\n        #     repetition_penalty=1.1,  # 惩罚重复，减少冗余token占用长度\n        #     force_words_ids=None,  \n        # )\n        \n        decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n        all_predictions.extend([d.strip() for d in decoded])\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T08:10:52.930917Z","iopub.execute_input":"2025-12-31T08:10:52.931195Z","iopub.status.idle":"2025-12-31T08:36:30.826337Z","shell.execute_reply.started":"2025-12-31T08:10:52.931170Z","shell.execute_reply":"2025-12-31T08:36:30.825483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"id\": test_df[\"id\"],\n    \"translation\": all_predictions\n})\nsubmission[\"translation\"] = submission[\"translation\"].apply(lambda x: x if len(x) > 0 else \"broken text\")\nsubmission[\"translation\"]=submission[\"translation\"].apply(replace_translation)\n\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"Submission file saved successfully!\")\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T08:36:30.827353Z","iopub.execute_input":"2025-12-31T08:36:30.827584Z","iopub.status.idle":"2025-12-31T08:36:30.885067Z","shell.execute_reply.started":"2025-12-31T08:36:30.827561Z","shell.execute_reply":"2025-12-31T08:36:30.884147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import re\n# def extract_arabic_numbers(text):\n#     pattern = r\"(?<!\\.)\\d+(?:\\.\\d+)?\"\n#     numbers = re.findall(pattern, text)\n    \n#     return numbers\n\n# test_df=pd.read_csv(\"/kaggle/input/deep-past-initiative-machine-translation/train.csv\")\n\n# end_list=[]\n# for index_,t2 in enumerate(all_predictions,0):\n#     append_text=\"... not Seal silver there and so we can pay back the merchant.\"\n\n#     t1=test_df.iloc[index_][\"transliteration\"]\n\n\n#     num_list1 = extract_arabic_numbers(t1)\n#     num_list2 = extract_arabic_numbers(t2)\n#     num_temp_list=[]                          \n#     for num_k in num_list1:\n#         if num_k not in num_list2:\n#             num_temp_list.append(num_k)\n\n  \n#     append_text=\" \".join(num_temp_list)+\" \"+append_text\n\n#     if len(t1)-len(t2)>180:\n#         #print(len)\n#         t2= t2 + \" \"+append_text.strip()\n        \n#     end_list.append(t2)\n\n# submission = pd.DataFrame({\n#     \"id\": test_df[\"oare_id\"],\n#     \"translation\": end_list\n# })\n\n# submission[\"translation\"] = submission[\"translation\"].apply(lambda x: x if len(x) > 0 else \"broken text\")\n\n# submission.to_csv(\"submission_extend_data_1221.csv\", index=False)\n# print(\"Submission file saved successfully!\")\n# submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T08:36:30.887007Z","iopub.status.idle":"2025-12-31T08:36:30.887291Z","shell.execute_reply.started":"2025-12-31T08:36:30.887176Z","shell.execute_reply":"2025-12-31T08:36:30.887192Z"}},"outputs":[],"execution_count":null}]}