{"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":[{"sourceType":"competition","sourceId":116062,"databundleVersionId":14875579,"isSourceIdPinned":false},{"sourceType":"competition","sourceId":134561,"databundleVersionId":16421787,"isSourceIdPinned":false}],"dockerImageVersionId":31329,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# The CAFA 6 competition combines with the strength of the Gemma 4 generation.\n\nGemma 4 Good Hackathon - Trinh Quang Minh","metadata":{}},{"cell_type":"markdown","source":"**Full Code Cập Nhật Dự Đoán Protein & Tích Hợp Gemma 4**\n\nFull Code: Protein Function Prediction with Gemma 4 E2B-it\n\nFull Code: Protein Function Prediction (Gemma 4 Optimized)\n\nEnglish: Optimized for Gemma 4 architecture with hybrid attention and thinking mode. \nTiếng Việt: Tối ưu hóa cho kiến trúc Gemma 4 với cơ chế chú ý hỗn hợp và chế độ suy nghĩ.\n\nThis section will not only export the submission.tsv file but also select 10 random proteins from the test set to display details: sequences, predicted GO codes, and in-depth explanations from Gemma 4 E2B-it in both Vietnamese and English.\n\nConsolidate the entire process into a single code block. This code block includes:\n\n1. Reading data (Using a custom function to avoid Bio library errors).\n\n2. Training KNN (Using all GO labels).\n\n3. Loading Gemma 4 E2B-it (Updated dtype=\"auto\" to avoid warnings).\n\n4. Displaying 10 detailed examples (with bilingual English-Vietnamese explanations).","metadata":{}},{"cell_type":"markdown","source":"**The display structure must be complete and detailed, like professional report templates, including:**\n\n1. Clear Protein ID.\n\n2. Sequence: Display of the amino acid sequence (the beginning).\n\n3. Predicted GO: List of GO codes with function names and confidence levels (%) in parentheses.\n\n4. Gemma 4 Insight: In-depth explanation based on those numbers.","metadata":{}},{"cell_type":"code","source":"# Cài đặt/Cập nhật thư viện transformers từ nguồn mới nhất để hỗ trợ gemma4\n# Install/Update transformers from source to support gemma4 architecture\n!pip install -U git+https://github.com/huggingface/transformers.git\n!pip install -U accelerate \n\n# SAU KHI CHẠY XONG, HÃY RESTART SESSION / AFTER RUNNING, PLEASE RESTART SESSION","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T02:18:22.435564Z","iopub.execute_input":"2026-04-04T02:18:22.436379Z","iopub.status.idle":"2026-04-04T02:18:46.455168Z","shell.execute_reply.started":"2026-04-04T02:18:22.436343Z","shell.execute_reply":"2026-04-04T02:18:46.454318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport torch\nimport random\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.neighbors import NearestNeighbors\nfrom scipy.sparse import csr_matrix\nfrom transformers import AutoProcessor, AutoModelForCausalLM\n\n# 1. Cấu hình hệ thống / System Configuration\nMODEL_ID = \"google/gemma-4-E2B-it\"\nDATA_PATH = '/kaggle/input/competitions/cafa-6-protein-function-prediction'\n\n# Từ điển mô tả mã GO / GO Description Dictionary\nGO_DESCRIPTIONS = {\n    \"GO:0005515\": \"protein binding (liên kết protein)\",\n    \"GO:0005783\": \"endoplasmic reticulum (lưới nội chất)\",\n    \"GO:0009922\": \"fatty acid biosynthetic process (quá trình sinh tổng hợp axit béo)\",\n    \"GO:0005634\": \"nucleus (nhân tế bào)\",\n    \"GO:0005737\": \"cytoplasm (tế bào chất)\",\n    \"GO:0003723\": \"RNA binding (liên kết RNA)\",\n    \"GO:0005829\": \"cytosol (dịch bào tương)\",\n    \"GO:0016020\": \"membrane (màng)\",\n    \"GO:0003677\": \"DNA binding (liên kết DNA)\",\n    \"GO:0005524\": \"ATP binding (liên kết ATP)\"\n}\n\ndef load_fasta(path, limit=None):\n    \"\"\"Đọc file FASTA / Read FASTA file\"\"\"\n    ids, seqs = [], []\n    with open(path, 'r') as f:\n        current_seq = []\n        count = 0\n        for line in f:\n            line = line.strip()\n            if not line: continue\n            if line.startswith(\">\"):\n                if limit and count >= limit: break\n                if current_seq: seqs.append(\"\".join(current_seq))\n                header = line[1:]\n                entry_id = header.split('|')[-2] if '|' in header else header.split()[0]\n                ids.append(entry_id)\n                current_seq = []\n                count += 1\n            else:\n                current_seq.append(line)\n        if current_seq: seqs.append(\"\".join(current_seq))\n    return pd.DataFrame({'EntryID': ids, 'Sequence': seqs})\n\n# 2. Xử lý dữ liệu / Data Processing\nprint(\"\\n[✔] Bước 1: Xử lý dữ liệu & Huấn luyện KNN / Step 1: Processing Data & Training KNN...\")\ntrain_terms = pd.read_csv(f'{DATA_PATH}/Train/train_terms.tsv', sep='\\t')\nall_go_terms = sorted(train_terms['term'].unique())\nall_prot_ids = sorted(train_terms['EntryID'].unique())\nterm_map = {t: i for i, t in enumerate(all_go_terms)}\nprot_map = {p: i for i, p in enumerate(all_prot_ids)}\n\n# Tạo ma trận thưa / Create Sparse Matrix\nY_sparse = csr_matrix((np.ones(len(train_terms)), \n                      (train_terms['EntryID'].map(prot_map), train_terms['term'].map(term_map))), \n                      shape=(len(all_prot_ids), len(all_go_terms)))\n\ntrain_df = load_fasta(f'{DATA_PATH}/Train/train_sequences.fasta', limit=15000)\ntest_df = load_fasta(f'{DATA_PATH}/Test/testsuperset.fasta', limit=500)\n\n# Vector hóa trình tự protein / Vectorize Protein Sequences\nvectorizer = TfidfVectorizer(max_features=4000)\nX_train = vectorizer.fit_transform(train_df['Sequence'].apply(lambda x: \" \".join([x[i:i+3] for i in range(len(x)-2)])))\nX_test = vectorizer.transform(test_df['Sequence'].apply(lambda x: \" \".join([x[i:i+3] for i in range(len(x)-2)])))\n\n# Huấn luyện KNN / Train KNN\nknn = NearestNeighbors(n_neighbors=5, metric='cosine').fit(X_train)\ndistances, indices = knn.kneighbors(X_test)\n\n# 3. Tải mô hình AI / Load AI Model\nprint(\"[✔] Bước 2: Tải mô hình Gemma 4 E2B-it / Step 2: Loading Gemma 4 E2B-it...\")\nprocessor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)\nmodel = AutoModelForCausalLM.from_pretrained(\n    MODEL_ID, \n    trust_remote_code=True, \n    torch_dtype=torch.bfloat16, \n    device_map=\"auto\"\n)\n\n# 4. Hiển thị kết quả song ngữ / Display Bilingual Results\nprint(\"\\n\" + \"=\"*80)\nprint(f\"{'DỰ ĐOÁN & GIẢI THÍCH CHỨC NĂNG PROTEIN (SONG NGỮ EN-VN)':^80}\")\nprint(f\"{'PROTEIN FUNCTION PREDICTION & EXPLANATION (BILINGUAL)':^80}\")\nprint(\"=\"*80)\n\nsubmission_rows = []\ndisplay_indices = random.sample(range(len(test_df)), 10)\n\nfor i in range(len(test_df)):\n    pid = test_df['EntryID'].iloc[i]\n    seq = test_df['Sequence'].iloc[i]\n    neighbor_labels = Y_sparse[[prot_map[train_df['EntryID'].iloc[idx]] for idx in indices[i]]].toarray()\n    scores = np.mean(neighbor_labels, axis=0)\n    top_hits = np.argsort(scores)[-3:][::-1]\n    \n    preds_for_ai = []\n    display_preds = []\n    \n    for idx in top_hits:\n        if scores[idx] > 0.01:\n            term = all_go_terms[idx]\n            conf = round(scores[idx], 2)\n            desc = GO_DESCRIPTIONS.get(term, \"biological function\")\n            preds_for_ai.append(f\"{term} ({desc}) with confidence {conf}\")\n            display_preds.append(f\"{term} ({conf:.2f})\")\n            submission_rows.append([pid, term, conf])\n\n    if i in display_indices:\n        print(f\"\\n[🔬] Mẫu Protein / Protein ID: {pid}\")\n        print(f\"[-] Trình tự / Sequence: {seq[:60]}...\")\n        print(f\"[-] Dự đoán GO / Predicted GO: {', '.join(display_preds)}\")\n        \n        # Yêu cầu AI giải thích song ngữ / Prompt AI for bilingual explanation\n        prompt = (f\"As a bioinformatics expert, explain why protein {pid} (Sequence: {seq[:20]}...) \"\n                  f\"has these functions: {', '.join(preds_for_ai)}. \"\n                  f\"Provide a clear reasoning based on confidence scores in BOTH English and Vietnamese.\")\n        \n        messages = [{\"role\": \"user\", \"content\": prompt}]\n        input_chat = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n        inputs = processor(text=input_chat, return_tensors=\"pt\").to(model.device)\n        \n        with torch.no_grad():\n            out = model.generate(**inputs, max_new_tokens=400, temperature=0.7)\n        \n        insight = processor.decode(out[0], skip_special_tokens=True).split('model')[-1].strip()\n        print(f\"[-] Phân tích từ Gemma 4 / Gemma 4 Insight:\\n{insight}\")\n        print(\"-\" * 50)\n        \n        # Lưu vào file tsv / Save to tsv\n        submission_rows.append([pid, \"Text\", 0.500, insight[:200]])\n\n# Xuất file kết quả / Export results\npd.DataFrame(submission_rows).to_csv('submission.tsv', sep='\\t', index=False, header=False)\nprint(\"\\n[✔] THÀNH CÔNG: Đã tạo file submission.tsv và hiển thị ví dụ!\")\nprint(\"[✔] SUCCESS: submission.tsv generated and examples displayed!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-04T02:18:46.456976Z","iopub.execute_input":"2026-04-04T02:18:46.457264Z","iopub.status.idle":"2026-04-04T02:25:02.628681Z","shell.execute_reply.started":"2026-04-04T02:18:46.457225Z","shell.execute_reply":"2026-04-04T02:25:02.627737Z"}},"outputs":[],"execution_count":null}]}