muhammadtlha944 commited on
Commit
2a34d74
Β·
verified Β·
1 Parent(s): fc244c8

Add Kaggle training notebook (free GPU)

Browse files
Files changed (1) hide show
  1. MCP_Agent_1_7B_Kaggle.ipynb +260 -0
MCP_Agent_1_7B_Kaggle.ipynb ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "kernelspec": {
6
+ "name": "python3",
7
+ "display_name": "Python 3"
8
+ },
9
+ "language_info": {
10
+ "name": "python"
11
+ },
12
+ "kaggle": {
13
+ "accelerator": "gpu",
14
+ "dataSources": [],
15
+ "isInternetEnabled": true,
16
+ "language": "python",
17
+ "sourceType": "notebook"
18
+ }
19
+ },
20
+ "cells": [
21
+ {
22
+ "cell_type": "markdown",
23
+ "metadata": {},
24
+ "source": [
25
+ "# πŸ€– MCP-Agent-1.7B β€” Training on Kaggle (FREE GPU)\n",
26
+ "\n",
27
+ "**What:** Fine-tune Qwen3-1.7B to natively speak Model Context Protocol (MCP) for tool calling.\n",
28
+ "\n",
29
+ "**GPU:** Kaggle P100/T4 (16GB VRAM) β€” **completely free**, 30 hrs/week\n",
30
+ "\n",
31
+ "**Time:** ~2 hours\n",
32
+ "\n",
33
+ "---\n",
34
+ "\n",
35
+ "⚑ **Before running:** Go to **Settings (right panel) β†’ Accelerator β†’ GPU P100 or GPU T4Γ—2**\n",
36
+ "\n",
37
+ "⚑ **Also:** Settings β†’ Internet β†’ **Turn ON** (needed to download model & push to Hub)"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": null,
43
+ "metadata": {},
44
+ "outputs": [],
45
+ "source": [
46
+ "# ============================================================\n",
47
+ "# CELL 1: Check GPU & Install Dependencies\n",
48
+ "# ============================================================\n",
49
+ "!nvidia-smi\n",
50
+ "\n",
51
+ "import torch\n",
52
+ "assert torch.cuda.is_available(), \"❌ No GPU! Go to Settings β†’ Accelerator β†’ GPU\"\n",
53
+ "print(f\"\\nβœ… GPU: {torch.cuda.get_device_name(0)}\")\n",
54
+ "print(f\"βœ… VRAM: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f} GB\")\n",
55
+ "\n",
56
+ "!pip install -q transformers trl peft datasets accelerate bitsandbytes huggingface_hub\n",
57
+ "print(\"\\nβœ… All packages installed!\")"
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "execution_count": null,
63
+ "metadata": {},
64
+ "outputs": [],
65
+ "source": [
66
+ "# ============================================================\n",
67
+ "# CELL 2: Login to HuggingFace\n",
68
+ "# ============================================================\n",
69
+ "# Get your token at: https://huggingface.co/settings/tokens (needs WRITE permission)\n",
70
+ "\n",
71
+ "from huggingface_hub import notebook_login\n",
72
+ "notebook_login()"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "execution_count": null,
78
+ "metadata": {},
79
+ "outputs": [],
80
+ "source": [
81
+ "# ============================================================\n",
82
+ "# CELL 3: Load Everything & Start Training\n",
83
+ "# ============================================================\n",
84
+ "# This single cell does it all β€” loads data, configures LoRA,\n",
85
+ "# sets up training, and runs for 3 epochs (~2 hours).\n",
86
+ "# Don't close the tab while it runs!\n",
87
+ "# ============================================================\n",
88
+ "\n",
89
+ "import os, torch\n",
90
+ "from datasets import load_dataset\n",
91
+ "from peft import LoraConfig\n",
92
+ "from trl import SFTConfig, SFTTrainer\n",
93
+ "from transformers import AutoTokenizer\n",
94
+ "\n",
95
+ "# --- Load dataset (16.5K MCP tool-calling conversations) ---\n",
96
+ "print(\"πŸ“¦ Loading dataset...\")\n",
97
+ "dataset = load_dataset(\"muhammadtlha944/mcp-agent-training-data\")\n",
98
+ "print(f\" Train: {len(dataset['train']):,} | Val: {len(dataset['validation']):,}\")\n",
99
+ "\n",
100
+ "# --- Load tokenizer ---\n",
101
+ "print(\"πŸ“¦ Loading tokenizer...\")\n",
102
+ "tokenizer = AutoTokenizer.from_pretrained(\"Qwen/Qwen3-1.7B\", trust_remote_code=True)\n",
103
+ "\n",
104
+ "# --- LoRA config (only train ~2% of parameters) ---\n",
105
+ "peft_config = LoraConfig(\n",
106
+ " r=16, # Rank of adapter matrices\n",
107
+ " lora_alpha=32, # Scaling factor (2x rank)\n",
108
+ " lora_dropout=0.05, # Regularization\n",
109
+ " bias=\"none\",\n",
110
+ " task_type=\"CAUSAL_LM\",\n",
111
+ " target_modules=\"all-linear\", # Apply LoRA to ALL linear layers\n",
112
+ ")\n",
113
+ "\n",
114
+ "# --- Detect GPU type for precision ---\n",
115
+ "gpu_name = torch.cuda.get_device_name(0).lower()\n",
116
+ "use_bf16 = \"a100\" in gpu_name or \"a10\" in gpu_name or \"l4\" in gpu_name or \"h100\" in gpu_name\n",
117
+ "print(f\" Using {'bf16' if use_bf16 else 'fp16'} precision for {torch.cuda.get_device_name(0)}\")\n",
118
+ "\n",
119
+ "# --- Training config ---\n",
120
+ "training_args = SFTConfig(\n",
121
+ " output_dir=\"/kaggle/working/mcp-agent-checkpoints\",\n",
122
+ " \n",
123
+ " # Core hyperparameters\n",
124
+ " num_train_epochs=3,\n",
125
+ " per_device_train_batch_size=4,\n",
126
+ " gradient_accumulation_steps=4, # Effective batch = 16\n",
127
+ " learning_rate=2e-4, # 10x base LR for LoRA\n",
128
+ " weight_decay=0.01,\n",
129
+ " lr_scheduler_type=\"cosine\",\n",
130
+ " warmup_ratio=0.1,\n",
131
+ " max_grad_norm=1.0,\n",
132
+ " max_seq_length=2048,\n",
133
+ " \n",
134
+ " # Memory optimization\n",
135
+ " bf16=use_bf16,\n",
136
+ " fp16=not use_bf16,\n",
137
+ " gradient_checkpointing=True,\n",
138
+ " gradient_checkpointing_kwargs={\"use_reentrant\": False},\n",
139
+ " \n",
140
+ " # Logging\n",
141
+ " logging_steps=10,\n",
142
+ " logging_first_step=True,\n",
143
+ " logging_strategy=\"steps\",\n",
144
+ " \n",
145
+ " # Evaluation & Checkpoints\n",
146
+ " eval_strategy=\"steps\",\n",
147
+ " eval_steps=200,\n",
148
+ " per_device_eval_batch_size=4,\n",
149
+ " save_strategy=\"steps\",\n",
150
+ " save_steps=200,\n",
151
+ " save_total_limit=2,\n",
152
+ " load_best_model_at_end=True,\n",
153
+ " metric_for_best_model=\"eval_loss\",\n",
154
+ " \n",
155
+ " # Push to Hub\n",
156
+ " push_to_hub=True,\n",
157
+ " hub_model_id=\"muhammadtlha944/MCP-Agent-1.7B\",\n",
158
+ " hub_strategy=\"end\",\n",
159
+ " \n",
160
+ " # Misc\n",
161
+ " seed=42,\n",
162
+ " dataloader_num_workers=2,\n",
163
+ " optim=\"adamw_torch\",\n",
164
+ ")\n",
165
+ "\n",
166
+ "# --- Create trainer ---\n",
167
+ "print(\"\\nπŸ”§ Loading Qwen3-1.7B and applying LoRA...\")\n",
168
+ "trainer = SFTTrainer(\n",
169
+ " model=\"Qwen/Qwen3-1.7B\",\n",
170
+ " args=training_args,\n",
171
+ " train_dataset=dataset[\"train\"],\n",
172
+ " eval_dataset=dataset[\"validation\"],\n",
173
+ " peft_config=peft_config,\n",
174
+ " processing_class=tokenizer,\n",
175
+ ")\n",
176
+ "\n",
177
+ "trainable = sum(p.numel() for p in trainer.model.parameters() if p.requires_grad)\n",
178
+ "total = sum(p.numel() for p in trainer.model.parameters())\n",
179
+ "print(f\" Total params: {total:,}\")\n",
180
+ "print(f\" Trainable (LoRA): {trainable:,} ({100*trainable/total:.2f}%)\")\n",
181
+ "print(f\" GPU memory: {torch.cuda.memory_allocated()/1e9:.1f} GB\")\n",
182
+ "\n",
183
+ "# --- TRAIN! ---\n",
184
+ "print(f\"\\nπŸš€ Starting training (~2 hours)...\\n\")\n",
185
+ "train_result = trainer.train()\n",
186
+ "\n",
187
+ "print(f\"\\nβœ… Training done! Loss: {train_result.metrics.get('train_loss', 'N/A')}\")"
188
+ ]
189
+ },
190
+ {
191
+ "cell_type": "code",
192
+ "execution_count": null,
193
+ "metadata": {},
194
+ "outputs": [],
195
+ "source": [
196
+ "# ============================================================\n",
197
+ "# CELL 4: Evaluate & Push to HuggingFace Hub\n",
198
+ "# ============================================================\n",
199
+ "\n",
200
+ "# Final eval\n",
201
+ "print(\"πŸ“Š Evaluating...\")\n",
202
+ "eval_metrics = trainer.evaluate()\n",
203
+ "print(f\" Eval loss: {eval_metrics['eval_loss']:.4f}\")\n",
204
+ "\n",
205
+ "# Save metrics\n",
206
+ "trainer.log_metrics(\"train\", train_result.metrics)\n",
207
+ "trainer.save_metrics(\"train\", train_result.metrics)\n",
208
+ "trainer.log_metrics(\"eval\", eval_metrics)\n",
209
+ "trainer.save_metrics(\"eval\", eval_metrics)\n",
210
+ "\n",
211
+ "# Push\n",
212
+ "print(\"\\nπŸš€ Pushing to HuggingFace Hub...\")\n",
213
+ "trainer.push_to_hub(\n",
214
+ " commit_message=\"MCP-Agent-1.7B: LoRA fine-tuned Qwen3-1.7B for MCP tool calling\",\n",
215
+ " tags=[\"mcp\", \"tool-calling\", \"function-calling\", \"agent\", \"qwen3\", \"lora\"],\n",
216
+ ")\n",
217
+ "\n",
218
+ "print(f\"\\n{'='*60}\")\n",
219
+ "print(f\"πŸŽ‰ MCP-Agent-1.7B is LIVE!\")\n",
220
+ "print(f\"{'='*60}\")\n",
221
+ "print(f\"πŸ“¦ https://huggingface.co/muhammadtlha944/MCP-Agent-1.7B\")\n",
222
+ "print(f\"πŸ“Š Train loss: {train_result.metrics.get('train_loss', 'N/A')}\")\n",
223
+ "print(f\"πŸ“Š Eval loss: {eval_metrics['eval_loss']:.4f}\")"
224
+ ]
225
+ },
226
+ {
227
+ "cell_type": "code",
228
+ "execution_count": null,
229
+ "metadata": {},
230
+ "outputs": [],
231
+ "source": [
232
+ "# ============================================================\n",
233
+ "# CELL 5: Test the Model!\n",
234
+ "# ============================================================\n",
235
+ "from transformers import pipeline\n",
236
+ "\n",
237
+ "pipe = pipeline(\"text-generation\", model=trainer.model, tokenizer=tokenizer,\n",
238
+ " max_new_tokens=512, do_sample=True, temperature=0.7)\n",
239
+ "\n",
240
+ "tests = [\n",
241
+ " \"Find all Python files in src/ that import pandas\",\n",
242
+ " \"Clone the repo, find all TODO comments, create a summary\",\n",
243
+ " \"Delete the database\",\n",
244
+ "]\n",
245
+ "\n",
246
+ "for i, prompt in enumerate(tests, 1):\n",
247
+ " messages = [\n",
248
+ " {\"role\": \"system\", \"content\": \"You are an MCP agent with tools: github_search, read_file, shell_exec, sqlite_query. Use JSON-RPC format. Ask for clarification when needed. Refuse dangerous requests.\"},\n",
249
+ " {\"role\": \"user\", \"content\": prompt}\n",
250
+ " ]\n",
251
+ " result = pipe({\"messages\": messages})\n",
252
+ " response = result[0]['generated_text'][-1]['content']\n",
253
+ " print(f\"\\n{'='*50}\")\n",
254
+ " print(f\"TEST {i}: {prompt}\")\n",
255
+ " print(f\"{'='*50}\")\n",
256
+ " print(f\"πŸ€– {response}\")"
257
+ ]
258
+ }
259
+ ]
260
+ }