{"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":15061024,"sourceType":"competition"},{"sourceId":14403567,"sourceType":"datasetVersion","datasetId":9199182},{"sourceId":710051,"sourceType":"modelInstanceVersion","modelInstanceId":539382,"modelId":552598}],"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 numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-05T15:42:53.416475Z","iopub.execute_input":"2026-01-05T15:42:53.416674Z","iopub.status.idle":"2026-01-05T15:42:55.415415Z","shell.execute_reply.started":"2026-01-05T15:42:53.416655Z","shell.execute_reply":"2026-01-05T15:42:55.414608Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"INSTALLING DEPENDENCIES.......","metadata":{}},{"cell_type":"code","source":"!pip install \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\"\n!pip install --no-deps \"trl<0.9.0\" peft accelerate bitsandbytes\n!pip install chromadb sentence-transformers pandas pybase64","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T15:54:39.756697Z","iopub.execute_input":"2026-02-18T15:54:39.757055Z","iopub.status.idle":"2026-02-18T15:57:39.702807Z","shell.execute_reply.started":"2026-02-18T15:54:39.757011Z","shell.execute_reply":"2026-02-18T15:57:39.701892Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"MODEL LOADING....","metadata":{}},{"cell_type":"code","source":"from unsloth import FastLanguageModel\nimport torch\nimport pandas as pd\nimport chromadb\nfrom chromadb.utils import embedding_functions\nfrom sentence_transformers import SentenceTransformer\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# Model config (matching training)\nmax_seq_length = 2048\ndtype = None\nload_in_4bit = True\n\n# Load your fine-tuned LoRA model\nmodel_path=r'/kaggle/input/lora-model/transformers/default/1'\nmodel, tokenizer = FastLanguageModel.from_pretrained(\n    model_name=model_path,\n    max_seq_length=max_seq_length,\n    dtype=dtype,\n    load_in_4bit=load_in_4bit,\n)\nFastLanguageModel.for_inference(model)\n\n# Chat template\nfrom unsloth.chat_templates import get_chat_template\ntokenizer = get_chat_template(tokenizer, chat_template=\"llama-3.1\")\n\nprint(\"✅ LoRA model loaded!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T16:18:58.927630Z","iopub.execute_input":"2026-02-18T16:18:58.927941Z","iopub.status.idle":"2026-02-18T16:20:05.748837Z","shell.execute_reply.started":"2026-02-18T16:18:58.927916Z","shell.execute_reply":"2026-02-18T16:20:05.747788Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"BUILDING RAG KNOWLEDGE BASE.....","metadata":{}},{"cell_type":"code","source":"def build_rag_db(dict_csv_path=\"/kaggle/input/dictionary/final_processed_dict_df.csv\"):\n    \"\"\"Build ChromaDB from your dictionary CSV (upload to Kaggle input).\"\"\"\n    dict_df = pd.read_csv(dict_csv_path)\n    print(f\"📖 Loaded {len(dict_df)} dictionary entries\")\n    \n    # Prepare embedding text (matching training)\n    dict_df['embedding_text'] = (\n        dict_df['word'].astype(str) + \" \" + \n        dict_df['derived_from'].astype(str)\n    )\n    \n    # Metadatas\n    metadatas = [{\n        'word': str(row['word']),\n        'derived_from': str(row['derived_from']),\n        'meaning': str(row['Meanings']),\n        'quality': str(row.get('Quality', 'unknown'))\n    } for _, row in dict_df.iterrows()]\n    \n    # ChromaDB setup\n    client = chromadb.PersistentClient(path=\"./akkadian_rag_db\")\n    ef = embedding_functions.SentenceTransformerEmbeddingFunction(\n        model_name=\"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2\",\n        device=\"cuda\"\n    )\n    \n    # Create/recreate collection\n    try:\n        client.delete_collection(\"akkadian_dict\")\n    except:\n        pass\n    \n    collection = client.create_collection(name=\"akkadian_dict\", embedding_function=ef)\n    \n    # Batch add (Kaggle-safe)\n    BATCH_SIZE = 2000\n    documents = dict_df['embedding_text'].tolist()\n    ids = [f\"id_{i}\" for i in range(len(documents))]\n    \n    for i in range(0, len(documents), BATCH_SIZE):\n        batch_docs = documents[i:i+BATCH_SIZE]\n        batch_meta = metadatas[i:i+BATCH_SIZE]\n        batch_ids = ids[i:i+BATCH_SIZE]\n        collection.add(documents=batch_docs, metadatas=batch_meta, ids=batch_ids)\n        print(f\"Added batch {i//BATCH_SIZE + 1}\")\n    \n    print(f\"✅ RAG DB ready: {collection.count()} entries\")\n    return client.get_collection(\"akkadian_dict\")\n\n# Build RAG (upload final_processed_dict_df.csv to /kaggle/input/)\ncollection = build_rag_db()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T16:11:44.088867Z","iopub.execute_input":"2026-02-18T16:11:44.089807Z","iopub.status.idle":"2026-02-18T16:11:59.282282Z","shell.execute_reply.started":"2026-02-18T16:11:44.089769Z","shell.execute_reply":"2026-02-18T16:11:59.281508Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(r\"/kaggle/input/deep-past-initiative-machine-translation/test.csv\")\ntest_transliterations = data['transliteration']\nbatch_size = 2000","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T16:24:46.662062Z","iopub.execute_input":"2026-02-18T16:24:46.662378Z","iopub.status.idle":"2026-02-18T16:24:46.670714Z","shell.execute_reply.started":"2026-02-18T16:24:46.662346Z","shell.execute_reply":"2026-02-18T16:24:46.669920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T16:20:52.067202Z","iopub.execute_input":"2026-02-18T16:20:52.067788Z","iopub.status.idle":"2026-02-18T16:20:52.076476Z","shell.execute_reply.started":"2026-02-18T16:20:52.067759Z","shell.execute_reply":"2026-02-18T16:20:52.075690Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"RAG ENHANCED INFERENCE.....","metadata":{}},{"cell_type":"code","source":"def translate_akkadian_rag_batched(transliterations, collectionbatch_size=4, top_k=3):\n    predictions = []\n    #collection = client.get_collection(name=\"akkadian_dict\")  # Load existing\n\n\n    for i in range(0, len(transliterations), batch_size):\n        batch = transliterations[i:i+batch_size]\n        batch_preds = []\n\n\n        for t in batch:\n            # Retrieve relevant dictionary entries\n            results = collection.query(\n                query_texts=[f\"Translate Akkadian: {t}\"],\n                n_results=top_k,\n                include=[\"documents\", \"metadatas\"]\n            )\n\n\n            # Format context (prioritize high-quality)\n            high_qual = [res for j, res in enumerate(results['metadatas'][0])\n                        if results['metadatas'][0][j]['quality'] == 'high']\n            context = \"\\n\".join([results['documents'][0][j] for j in range(len(high_qual))]) if high_qual else \"\\n\".join(results['documents'][0])\n\n\n            rag_context = (\n                f\"Use this Akkadian dictionary for accurate translation (high quality prioritized):\\n\"\n                f\"{context}\\n\\n\"\n            )\n\n\n            # Enhanced prompt with RAG context\n            messages = [\n                {\"role\": \"system\", \"content\": \"You are an expert translator of Akkadian language to English. Use the provided dictionary context for precise word meanings and derivations. Translate accurately.\"},\n                {\"role\": \"user\", \"content\": f\"{rag_context}Translate this Akkadian sentence to English:\\n\\n{t}\"}\n            ]\n\n\n            inputs = tokenizer.apply_chat_template(\n                messages,\n                tokenize=True,\n                add_generation_prompt=True,\n                return_tensors=\"pt\",\n            ).to(\"cuda\")\n\n\n            outputs = model.generate(\n                inputs,\n                max_new_tokens=150,\n                temperature=0.1,\n                do_sample=False,\n                pad_token_id=tokenizer.eos_token_id\n            )\n\n\n            pred = tokenizer.decode(outputs[0], skip_special_tokens=True)\n            pred = pred.split(\"assistant\")[-1].split(\"<|eot_id|>\")[0].strip()\n            batch_preds.append(pred)\n\n\n        predictions.extend(batch_preds)\n        print(f\"Batch {i//batch_size+1}/{len(transliterations)//batch_size+1}\")\n\n\n    return predictions\n\n\ntest_predictions = translate_akkadian_rag_batched(test_transliterations, collection)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T16:24:49.399098Z","iopub.execute_input":"2026-02-18T16:24:49.399574Z","iopub.status.idle":"2026-02-18T16:25:09.353797Z","shell.execute_reply.started":"2026-02-18T16:24:49.399540Z","shell.execute_reply":"2026-02-18T16:25:09.352990Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"TESTING MODEL INFERENCE....","metadata":{}},{"cell_type":"code","source":"data\nlen(test_predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T16:25:12.893962Z","iopub.execute_input":"2026-02-18T16:25:12.894256Z","iopub.status.idle":"2026-02-18T16:25:12.899496Z","shell.execute_reply.started":"2026-02-18T16:25:12.894230Z","shell.execute_reply":"2026-02-18T16:25:12.898697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submissions = pd.DataFrame({\n    'id': data['id'],\n    'translation':test_predictions\n})\nsubmissions.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-18T16:26:57.636786Z","iopub.execute_input":"2026-02-18T16:26:57.637588Z","iopub.status.idle":"2026-02-18T16:26:57.651673Z","shell.execute_reply.started":"2026-02-18T16:26:57.637549Z","shell.execute_reply":"2026-02-18T16:26:57.650892Z"}},"outputs":[],"execution_count":null}]}