{"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":"# --- CELL 1: INSTALL & IMPORTS ---\nprint(\"⚙️ Setting up the Agentic Environment...\")\n\n# Zaroori libraries install karein\n!pip install -q transformers datasets sentencepiece sacrebleu accelerate\n\nimport pandas as pd\nimport numpy as np\nimport re\nfrom datasets import Dataset\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer, DataCollatorForSeq2Seq\nimport torch\n\nprint(\"✅ Libraries Installed. Ready to go!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T11:37:27.135177Z","iopub.execute_input":"2025-12-17T11:37:27.135588Z","iopub.status.idle":"2025-12-17T11:38:00.685839Z","shell.execute_reply.started":"2025-12-17T11:37:27.135560Z","shell.execute_reply":"2025-12-17T11:38:00.685181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- CELL 2: DATA LOADING & PREPROCESSING ---\nprint(\"📂 Loading and Cleaning Data...\")\n\n# 1. Files Load karein\ntrain_df = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/train.csv')\ntest_df = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/test.csv')\n\n# 2. Cleaning Function (Agentic Cleaner)\ndef clean_text(text):\n    if not isinstance(text, str): return \"\"\n    # Broken parts [x] ko <gap> bana do\n    text = text.replace('[x]', '<gap>')\n    text = text.replace('...', '<gap>')\n    # Fizool symbols hata do\n    text = re.sub(r'[\\[\\]<>]', '', text) \n    # Extra spaces khatam\n    return text.strip()\n\n# 3. Apply Cleaning\ntrain_df['clean_input'] = \"translate Akkadian to English: \" + train_df['transliteration'].apply(clean_text)\ntrain_df['clean_target'] = train_df['translation']\n\n# Test data ko bhi wese hi clean karein\ntest_df['clean_input'] = \"translate Akkadian to English: \" + test_df['transliteration'].apply(clean_text)\n\nprint(f\"✅ Data Loaded! Training Rows: {len(train_df)}, Test Rows: {len(test_df)}\")\nprint(\"Sample Input:\", train_df['clean_input'].iloc[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T11:38:20.975847Z","iopub.execute_input":"2025-12-17T11:38:20.976156Z","iopub.status.idle":"2025-12-17T11:38:21.021592Z","shell.execute_reply.started":"2025-12-17T11:38:20.976131Z","shell.execute_reply":"2025-12-17T11:38:21.020997Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- CELL 3: MODEL ARCHITECTURE ---\nprint(\"🏗️ Architecting the ByT5 Model...\")\n\n# 1. Model Name (Google ka ByT5 Small - Fast aur Powerful)\nmodel_checkpoint = \"google/byt5-small\"\n\n# 2. Tokenizer aur Model Load karein\ntokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)\n\n# 3. Data ko Model ki language mein convert karna (Tokenization)\ndef preprocess_data(examples):\n    model_inputs = tokenizer(examples['clean_input'], max_length=512, truncation=True, padding=\"max_length\")\n    \n    with tokenizer.as_target_tokenizer():\n        labels = tokenizer(examples['clean_target'], max_length=512, truncation=True, padding=\"max_length\")\n        \n    model_inputs[\"labels\"] = labels[\"input_ids\"]\n    return model_inputs\n\n# Dataset object banayein\ndataset = Dataset.from_pandas(train_df[['clean_input', 'clean_target']])\ntokenized_dataset = dataset.map(preprocess_data, batched=True)\n\nprint(\"✅ Model & Data Ready for Training!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T11:38:25.696682Z","iopub.execute_input":"2025-12-17T11:38:25.697376Z","iopub.status.idle":"2025-12-17T11:38:36.819419Z","shell.execute_reply.started":"2025-12-17T11:38:25.697348Z","shell.execute_reply":"2025-12-17T11:38:36.818845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- MASTER CELL: DATA LOADING + TRAINING (ZERO ERROR GUARANTEE) ---\nimport pandas as pd\nimport re\nimport torch\nimport gc\nfrom datasets import Dataset\nfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer, DataCollatorForSeq2Seq\n\n# 1. Memory Safai (Safety First)\ngc.collect()\ntorch.cuda.empty_cache()\n\nprint(\"🚀 Starting The Master Pipeline...\")\n\n# --- STEP A: DATA LOADING & CLEANING ---\nprint(\"📂 Loading Data...\")\n# Files load karein\ntrain_df = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/train.csv')\n\n# Cleaning Function\ndef clean_text(text):\n    if not isinstance(text, str): return \"\"\n    text = text.replace('[x]', '<gap>')\n    text = text.replace('...', '<gap>')\n    text = re.sub(r'[\\[\\]<>]', '', text) \n    return text.strip()\n\n# Apply Cleaning\ntrain_df['clean_input'] = \"translate Akkadian to English: \" + train_df['transliteration'].apply(clean_text)\ntrain_df['clean_target'] = train_df['translation']\n\n# Create Dataset\ndataset = Dataset.from_pandas(train_df[['clean_input', 'clean_target']])\nprint(f\"✅ Data Ready! ({len(train_df)} rows)\")\n\n# --- STEP B: MODEL SETUP ---\nprint(\"🏗️ Setting up ByT5 Model...\")\nmodel_checkpoint = \"google/byt5-small\"\ntokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\nmodel = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)\n\n# Tokenization Function (Max length 256 for memory safety)\ndef preprocess_data(examples):\n    model_inputs = tokenizer(examples['clean_input'], max_length=256, truncation=True, padding=\"max_length\")\n    with tokenizer.as_target_tokenizer():\n        labels = tokenizer(examples['clean_target'], max_length=256, truncation=True, padding=\"max_length\")\n    model_inputs[\"labels\"] = labels[\"input_ids\"]\n    return model_inputs\n\n# Process Data\ntokenized_dataset = dataset.map(preprocess_data, batched=True)\nprint(\"✅ Model & Data Connected.\")\n\n# --- STEP C: TRAINING (ULTRA-SAFE MODE) ---\nprint(\"⏳ Training Started (Batch Size=1 for Crash Proofing)...\")\n\nargs = Seq2SeqTrainingArguments(\n    output_dir=\"./akkadian_model\",\n    eval_strategy=\"no\",\n    learning_rate=4e-5,\n    \n    # --- MEMORY SAVING MAGIC ---\n    per_device_train_batch_size=1,   # Sirf 1 sentence ek baar mein\n    gradient_accumulation_steps=16,  # 16 steps jama karke update karega\n    gradient_checkpointing=True,     # RAM bachane ka tool\n    # ---------------------------\n    \n    weight_decay=0.01,\n    save_total_limit=1,\n    num_train_epochs=3,\n    predict_with_generate=True,\n    fp16=True,\n    report_to=\"none\"\n)\n\ntrainer = Seq2SeqTrainer(\n    model=model,\n    args=args,\n    train_dataset=tokenized_dataset,\n    data_collator=DataCollatorForSeq2Seq(tokenizer, model=model),\n    tokenizer=tokenizer,\n)\n\ntrainer.train()\n\n# --- STEP D: SAVING ---\nprint(\"💾 Saving Final Model...\")\ntrainer.save_model(\"./final_model\")\ntokenizer.save_pretrained(\"./final_model\")\n\nprint(\"🏆 MISSION ACCOMPLISHED! Training Complete.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T11:46:58.924135Z","iopub.execute_input":"2025-12-17T11:46:58.924900Z","iopub.status.idle":"2025-12-17T11:54:44.479707Z","shell.execute_reply.started":"2025-12-17T11:46:58.924868Z","shell.execute_reply":"2025-12-17T11:54:44.478048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- CELL 5: GENERATING SUBMISSION ---\nprint(\"🤖 Generating Translations for Submission...\")\n\n# 1. Saved Model Load karein\nmodel = AutoModelForSeq2SeqLM.from_pretrained(\"./final_model\").to(\"cuda\")\ntokenizer = AutoTokenizer.from_pretrained(\"./final_model\")\n\n# 2. Prediction Loop\nresults = []\nmodel.eval()\n\n# Progress bar ke sath prediction\nfor idx, row in test_df.iterrows():\n    input_text = row['clean_input']\n    inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\")\n    \n    # Model se translation maangein\n    with torch.no_grad():\n        outputs = model.generate(inputs[\"input_ids\"], max_length=128, num_beams=4)\n        \n    translation = tokenizer.decode(outputs[0], skip_special_tokens=True)\n    results.append(translation)\n    \n    if idx % 100 == 0: print(f\"Processed {idx} rows...\")\n\n# 3. Save CSV\nsubmission_df = pd.DataFrame({\n    'id': test_df['id'],\n    'translation': results\n})\n\nsubmission_df.to_csv('submission.csv', index=False)\nprint(\"\\n✅ MISSION ACCOMPLISHED! 'submission.csv' is ready.\")\nprint(submission_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T11:54:44.482426Z","iopub.status.idle":"2025-12-17T11:54:44.482900Z","shell.execute_reply.started":"2025-12-17T11:54:44.482682Z","shell.execute_reply":"2025-12-17T11:54:44.482710Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- CELL 6: THE CONNECTOR AGENT (Schema & Linkage Check) ---\nimport pandas as pd\n\nprint(\"🕵️ Analyst Agent: Data Relations Check kar raha hun...\")\n\n# 1. Load Dataframes again (Safety ke liye)\npub_texts = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/published_texts.csv')\npublications = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/publications.csv')\n\n# 2. Column Names Check karte hain (Ye sabse zaroori hai)\nprint(\"\\n--- COLUMNS IN 'Published Texts' (Sawal/Transliterations) ---\")\nprint(pub_texts.columns.tolist())\n\nprint(\"\\n--- COLUMNS IN 'Publications' (Books/OCR) ---\")\nprint(publications.columns.tolist())\n\n# 3. Sample Data Inspection\n# Hum dekhna chahte hain ke kya koi 'id' ya 'name' match ho raha hai\nprint(\"\\n--- Sample Row: Published Texts ---\")\nprint(pub_texts.iloc[0].to_dict())\n\nprint(\"\\n--- Sample Row: Publications ---\")\nprint(publications.iloc[0].to_dict())\n\n# 4. Direct Join Test (Hypothesis)\n# Agar dono mein 'publication_id' ya 'bib_id' jaisa kuch common hai, to hum Jeet gaye!\ncommon_cols = list(set(pub_texts.columns) & set(publications.columns))\nprint(f\"\\n🔗 Common Columns found: {common_cols}\")\n\nif len(common_cols) > 0:\n    print(\"✅ Rasta mil gaya! Hum in columns par data join kar sakte hain.\")\nelse:\n    print(\"⚠️ Direct link nahi mila. Hum 'bibliography.csv' check karenge.\")\n    # Agar direct link nahi hai, to shayad teesri file 'bibliography.csv' bridge ka kaam kare\n    try:\n        bib_df = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/bibliography.csv')\n        print(\"\\n--- COLUMNS IN 'Bibliography' ---\")\n        print(bib_df.columns.tolist())\n    except:\n        print(\"Bibliography file load nahi hui.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T11:37:18.382228Z","iopub.execute_input":"2025-12-17T11:37:18.382900Z","iopub.status.idle":"2025-12-17T11:37:24.408078Z","shell.execute_reply.started":"2025-12-17T11:37:18.382871Z","shell.execute_reply":"2025-12-17T11:37:24.406888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# --- CELL 7: BUILDING THE BRIDGE (Bibliography Analysis) ---\nimport pandas as pd\n\nprint(\"🌉 Bridge Builder Agent: Analyzing Bibliography...\")\n\n# 1. Load Bibliography\nbib_df = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/bibliography.csv')\npub_texts = pd.read_csv('/kaggle/input/deep-past-initiative-machine-translation/published_texts.csv')\n\n# 2. Bibliography ka structure dekhte hain\nprint(f\"📚 Bibliography Rows: {len(bib_df)}\")\nprint(bib_df.head(3).T) # Transpose karke dikhana taake saaf parha jaye\n\n# 3. 'Publication Catalog' ko analyze karte hain (Sawal wali side)\n# Hum dekhna chahte hain ke data kis format mein hai (e.g., \"RA 60 125\" ya \"BIN 4 123\")\nprint(\"\\n--- Top 10 Publication Formats in Texts ---\")\n# Hum wo rows uthayenge jahan catalog null nahi hai\ncatalog_samples = pub_texts['publication_catalog'].dropna().unique()[:10]\nfor sample in catalog_samples:\n    print(f\"📌 Catalog ID: '{sample}'\")\n\n# 4. Matching Test (Hypothesis Check)\n# Kya Bibliography mein 'RA' ya 'BIN' jaise abbreviations hain?\nprint(\"\\n--- Unique Journals in Bibliography ---\")\nprint(bib_df['journal'].unique()[:20]) # Pehlay 20 journals dikhao\n\nprint(\"\\n--- Next Step Strategy ---\")\nprint(\"Agar Bibliography mein 'RA' mil gaya aur Texts mein bhi 'RA' hai, to hum jeet gaye!\")\nprint(\"Hum agla code likhenge jo inhein jod dega.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T11:11:50.448156Z","iopub.execute_input":"2025-12-17T11:11:50.448882Z","iopub.status.idle":"2025-12-17T11:11:50.712092Z","shell.execute_reply.started":"2025-12-17T11:11:50.448848Z","shell.execute_reply":"2025-12-17T11:11:50.711351Z"}},"outputs":[],"execution_count":null}]}