{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":121150,"databundleVersionId":14976537,"sourceType":"competition"}],"dockerImageVersionId":31236,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport re\n\n# Correct path for the dataset\nBASE_PATH = '/kaggle/input/deep-past-initiative-machine-translation/'\n\n# Loading CSV files\ntrain_df = pd.read_csv(f\"{BASE_PATH}train.csv\")\nlexicon_df = pd.read_csv(f\"{BASE_PATH}OA_Lexicon_eBL.csv\")\ntest_df = pd.read_csv(f\"{BASE_PATH}test.csv\")\n\n# Text cleaning function\ndef clean_text(text):\n    if pd.isna(text): return \"\"\n    text = re.sub(r'[!/?/˹˺\\[\\]]', '', text) \n    text = text.replace('...', '<big_gap>').replace('x', '<gap>')\n    return text.strip()\n\ntrain_df['clean_transliteration'] = train_df['transliteration'].apply(clean_text)\nprint(f\"Data loaded! Total samples: {len(train_df)}\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-23T09:55:53.850441Z","iopub.execute_input":"2025-12-23T09:55:53.850766Z","iopub.status.idle":"2025-12-23T09:55:56.049009Z","shell.execute_reply.started":"2025-12-23T09:55:53.850743Z","shell.execute_reply":"2025-12-23T09:55:56.048292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q transformers datasets sacrebleu torch\nimport torch\nfrom transformers import AutoModelForSeq2SeqLM, AutoTokenizer, Trainer, TrainingArguments, DataCollatorForSeq2Seq\nfrom datasets import Dataset\n\n# Using mT5-base for high accuracy\nmodel_name = \"google/mt5-base\"\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n\ndef preprocess_function(examples):\n    inputs = [\"translate Akkadian to English: \" + doc for doc in examples[\"clean_transliteration\"]]\n    model_inputs = tokenizer(inputs, max_length=128, truncation=True, padding=\"max_length\")\n    with tokenizer.as_target_tokenizer():\n        labels = tokenizer(examples[\"translation\"], max_length=128, truncation=True, padding=\"max_length\")\n    model_inputs[\"labels\"] = labels[\"input_ids\"]\n    return model_inputs\n\ndataset = Dataset.from_pandas(train_df[['clean_transliteration', 'translation']])\ntokenized_dataset = dataset.map(preprocess_function, batched=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-23T09:55:56.050113Z","iopub.execute_input":"2025-12-23T09:55:56.050403Z","iopub.status.idle":"2025-12-23T09:57:03.049615Z","shell.execute_reply.started":"2025-12-23T09:55:56.050370Z","shell.execute_reply":"2025-12-23T09:57:03.048739Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from transformers import DataCollatorForSeq2Seq, TrainingArguments, Trainer\nimport torch\nimport gc\n\n# Clear memory\ngc.collect()\ntorch.cuda.empty_cache()\n\ndata_collator = DataCollatorForSeq2Seq(tokenizer, model=model)\n\ntraining_args = TrainingArguments(\n    output_dir=\"./results\",\n    eval_strategy=\"no\",\n    save_strategy=\"no\",\n    learning_rate=2e-4,             # Slightly higher for faster convergence\n    per_device_train_batch_size=4,  # Increased from 1 to 4 for speed\n    gradient_accumulation_steps=2,  # 4 * 2 = 8 (Virtual batch size remains 8)\n    num_train_epochs=15, \n    weight_decay=0.01,\n    fp16=True,                      # Essential for GPU speed\n    optim=\"adafactor\",\n    gradient_checkpointing=False,   # Turned off to increase speed (Batch 4 should fit)\n    report_to=\"none\"\n)\n\ntrainer = Trainer(\n    model=model, \n    args=training_args, \n    train_dataset=tokenized_dataset, \n    data_collator=data_collator\n)\n\nprint(\"Starting Faster Training...\")\ntrainer.train()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-23T09:57:03.051629Z","iopub.execute_input":"2025-12-23T09:57:03.052105Z","iopub.status.idle":"2025-12-23T10:50:38.100319Z","shell.execute_reply.started":"2025-12-23T09:57:03.052074Z","shell.execute_reply":"2025-12-23T10:50:38.099728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom tqdm.auto import tqdm\n\ndef translate_sentence(text):\n    if not text.strip(): return \"\"\n    input_text = \"translate Akkadian to English: \" + clean_text(text)\n    \n    # Move inputs to GPU\n    inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\")\n    \n    with torch.no_grad():\n        outputs = model.generate(\n            **inputs, \n            max_length=128, \n            num_beams=4,              # Slightly reduced for speed\n            no_repeat_ngram_size=2,\n            early_stopping=True\n        )\n    return tokenizer.decode(outputs[0], skip_special_tokens=True)\n\n# Generate translations\nprint(\"Generating final submission file...\")\ntqdm.pandas()\ntest_df['translation'] = test_df['transliteration'].progress_apply(translate_sentence)\n\n# Save to CSV\nsubmission = test_df[['id', 'translation']]\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Success! Download 'submission.csv' from the Output folder.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-23T10:50:38.101288Z","iopub.execute_input":"2025-12-23T10:50:38.101548Z","iopub.status.idle":"2025-12-23T10:50:50.794497Z","shell.execute_reply.started":"2025-12-23T10:50:38.101524Z","shell.execute_reply":"2025-12-23T10:50:50.793907Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"def translate_sentence(text):\n    if not text.strip(): return \"\"\n    input_text = \"translate Akkadian to English: \" + clean_text(text)\n    inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\")\n    \n    # Using Beam Search for better translation quality\n    outputs = model.generate(\n        **inputs, \n        max_length=128, \n        num_beams=5, \n        no_repeat_ngram_size=2,\n        early_stopping=True\n    )\n    return tokenizer.decode(outputs[0], skip_special_tokens=True)\n\n# Predicting on Test Data\nprint(\"Generating translations for submission...\")\ntest_df['translation'] = test_df['transliteration'].apply(translate_sentence)\n\n# Saving to CSV\nsubmission = test_df[['id', 'translation']]\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Submission file 'submission.csv' is ready!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-23T10:50:50.795169Z","iopub.execute_input":"2025-12-23T10:50:50.795374Z","iopub.status.idle":"2025-12-23T10:51:00.795983Z","shell.execute_reply.started":"2025-12-23T10:50:50.795353Z","shell.execute_reply":"2025-12-23T10:51:00.795308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"--- Akkadian AI Translator Active ---\")\nprint(\"Type 'quit' to exit.\")\n\nwhile True:\n    user_input = input(\"\\nEnter Akkadian Transliteration: \")\n    if user_input.lower() in ['quit', 'exit']:\n        break\n    if user_input.strip() == \"\":\n        continue\n    try:\n        print(f\"AI Translation: {translate_sentence(user_input)}\")\n    except Exception as e:\n        print(f\"Error: {e}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-23T10:51:00.796849Z","iopub.execute_input":"2025-12-23T10:51:00.797071Z"}},"outputs":[],"execution_count":null}]}