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12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": [],
   "gpuType": "T4"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "accelerator": "GPU"
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 🕷️ WebScrapeAgent — Fine-tune Qwen2.5-7B for Autonomous Web Scraping\n",
    "\n",
    "This notebook fine-tunes **Qwen2.5-7B-Instruct** with **Unsloth + QLoRA** to create an autonomous web scraping agent that:\n",
    "\n",
    "1. **Reads HTML** and understands page structure (tables, lists, forms, nested elements)\n",
    "2. **Decides action sequences** to extract data (navigate, click, scroll, wait)\n",
    "3. **Handles authentication** (cookie replay, form login, token injection, browser profiles)\n",
    "4. **Recovers from failures** (403→headless browser, timeout→JS execution, rate limit→backoff)\n",
    "\n",
    "**Training recipe based on:**\n",
    "- ScrapeGraphAI-100k (arXiv:2602.15189): QLoRA r=16, lr=1e-4, completion-only loss → Key F1=0.887\n",
    "- BrowserAgent (arXiv:2510.10666): Qwen2.5-7B SFT → +20% over baselines\n",
    "- A3-Annotators (arXiv:2604.07776): assistant-token-only loss → 41.5% on WebArena\n",
    "\n",
    "**Free GPU**: Works on Colab T4 (16GB), Kaggle P100/T4, or any 16GB+ GPU.\n",
    "\n",
    "**Training data**: 45K examples from [sukritvemula/webscrape-agent-training-data](https://huggingface.co/datasets/sukritvemula/webscrape-agent-training-data)\n",
    "- 55% real-world HTML→JSON extraction (ScrapeGraphAI-100k)\n",
    "- 44% multi-turn browser interaction sessions (BrowserAgent)\n",
    "- 1% synthetic auth handling, error recovery, and diverse HTML structures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Install Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install unsloth\n",
    "!pip install --no-deps trl peft accelerate bitsandbytes xformers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Configuration\n",
    "\n",
    "Adjust these based on your GPU. Defaults are tuned for **free Colab T4 (16GB VRAM)**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# === EDIT THESE ===\n",
    "HF_USERNAME = \"sukritvemula\"  # Your HuggingFace username\n",
    "OUTPUT_MODEL = f\"{HF_USERNAME}/WebScrapeAgent-7B-v1\"  # Where to push the trained model\n",
    "\n",
    "# === Training hyperparameters (from ScrapeGraphAI + BrowserAgent papers) ===\n",
    "MAX_SEQ_LENGTH = 4096   # Covers 90%+ of examples; increase to 8192 if you have more VRAM\n",
    "LORA_R = 32             # LoRA rank (higher = more capacity for structured output)\n",
    "LORA_ALPHA = 32         # alpha = r (standard)\n",
    "LEARNING_RATE = 1e-4    # QLoRA needs ~10x higher LR than full fine-tuning\n",
    "NUM_EPOCHS = 2          # Both reference papers use 2 epochs\n",
    "BATCH_SIZE = 1          # Per-device (T4-safe)\n",
    "GRAD_ACCUM = 16         # Effective batch = 16\n",
    "\n",
    "# === Model ===\n",
    "MODEL_NAME = \"unsloth/Qwen2.5-7B-Instruct-bnb-4bit\"  # Pre-quantized for fast start\n",
    "DATASET_NAME = \"sukritvemula/webscrape-agent-training-data\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Login to HuggingFace (for pushing model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from huggingface_hub import login\n",
    "login()  # Enter your HF token when prompted"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Load Model + Apply LoRA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# CRITICAL: import unsloth FIRST\n",
    "import unsloth\n",
    "\n",
    "import torch\n",
    "from unsloth import FastLanguageModel, is_bfloat16_supported\n",
    "from unsloth.chat_templates import get_chat_template, train_on_responses_only\n",
    "\n",
    "print(f\"GPU: {torch.cuda.get_device_name()}\")\n",
    "print(f\"VRAM: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f} GB\")\n",
    "\n",
    "# Load model\n",
    "model, tokenizer = FastLanguageModel.from_pretrained(\n",
    "    model_name=MODEL_NAME,\n",
    "    max_seq_length=MAX_SEQ_LENGTH,\n",
    "    dtype=None,         # Auto-detect\n",
    "    load_in_4bit=True,  # QLoRA\n",
    ")\n",
    "\n",
    "# Apply LoRA adapters to all attention + MLP layers\n",
    "model = FastLanguageModel.get_peft_model(\n",
    "    model,\n",
    "    r=LORA_R,\n",
    "    target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n",
    "                    \"gate_proj\", \"up_proj\", \"down_proj\"],\n",
    "    lora_alpha=LORA_ALPHA,\n",
    "    lora_dropout=0.0,\n",
    "    bias=\"none\",\n",
    "    use_gradient_checkpointing=\"unsloth\",  # 30% more memory efficient\n",
    "    random_state=42,\n",
    ")\n",
    "\n",
    "# Set Qwen2.5 chat template\n",
    "tokenizer = get_chat_template(tokenizer, chat_template=\"qwen-2.5\")\n",
    "\n",
    "trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)\n",
    "total = sum(p.numel() for p in model.parameters())\n",
    "print(f\"Trainable: {trainable:,} / {total:,} ({trainable/total*100:.2f}%)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Load & Format Training Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import load_dataset\n",
    "\n",
    "dataset = load_dataset(DATASET_NAME)\n",
    "train_ds = dataset[\"train\"]\n",
    "print(f\"Training examples: {len(train_ds)}\")\n",
    "\n",
    "# Convert messages → ChatML text\n",
    "def format_to_text(examples):\n",
    "    texts = []\n",
    "    for msgs in examples[\"messages\"]:\n",
    "        try:\n",
    "            text = tokenizer.apply_chat_template(\n",
    "                msgs, tokenize=False, add_generation_prompt=False\n",
    "            )\n",
    "            texts.append(text)\n",
    "        except Exception:\n",
    "            # Fallback for any format issues\n",
    "            text = \"\"\n",
    "            for msg in msgs:\n",
    "                text += f\"<|im_start|>{msg['role']}\\n{msg['content']}<|im_end|>\\n\"\n",
    "            texts.append(text)\n",
    "    return {\"text\": texts}\n",
    "\n",
    "train_ds = train_ds.map(format_to_text, batched=True, num_proc=2,\n",
    "                        remove_columns=train_ds.column_names)\n",
    "\n",
    "# Filter sequences that exceed max length\n",
    "def filter_length(example):\n",
    "    tokens = tokenizer(example[\"text\"], truncation=False)\n",
    "    return len(tokens[\"input_ids\"]) <= MAX_SEQ_LENGTH\n",
    "\n",
    "original_len = len(train_ds)\n",
    "train_ds = train_ds.filter(filter_length, num_proc=2)\n",
    "print(f\"After length filter: {len(train_ds)} / {original_len} ({len(train_ds)/original_len*100:.1f}% kept)\")\n",
    "\n",
    "# Show a sample\n",
    "print(f\"\\nSample (first 300 chars):\\n{train_ds[0]['text'][:300]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Train with Completion-Only Loss\n",
    "\n",
    "Key: we only train on assistant tokens (not system/user). This is critical for structured output quality (+15% schema compliance per ScrapeGraphAI paper)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from trl import SFTTrainer, SFTConfig\n",
    "\n",
    "training_args = SFTConfig(\n",
    "    output_dir=\"./webscrape-checkpoints\",\n",
    "    \n",
    "    # Core training\n",
    "    num_train_epochs=NUM_EPOCHS,\n",
    "    per_device_train_batch_size=BATCH_SIZE,\n",
    "    gradient_accumulation_steps=GRAD_ACCUM,\n",
    "    \n",
    "    # Optimizer\n",
    "    optim=\"adamw_8bit\",\n",
    "    learning_rate=LEARNING_RATE,\n",
    "    weight_decay=0.01,\n",
    "    lr_scheduler_type=\"cosine\",\n",
    "    warmup_ratio=0.03,\n",
    "    max_grad_norm=0.3,\n",
    "    \n",
    "    # Precision\n",
    "    fp16=not is_bfloat16_supported(),\n",
    "    bf16=is_bfloat16_supported(),\n",
    "    \n",
    "    # Sequence\n",
    "    max_seq_length=MAX_SEQ_LENGTH,\n",
    "    dataset_text_field=\"text\",\n",
    "    packing=False,  # Must be False for multi-turn chat with response-only masking\n",
    "    \n",
    "    # Logging\n",
    "    logging_steps=10,\n",
    "    logging_first_step=True,\n",
    "    \n",
    "    # Saving\n",
    "    save_strategy=\"steps\",\n",
    "    save_steps=500,\n",
    "    save_total_limit=2,\n",
    "    \n",
    "    # Push to Hub\n",
    "    push_to_hub=True,\n",
    "    hub_model_id=OUTPUT_MODEL,\n",
    "    hub_strategy=\"end\",\n",
    "    \n",
    "    # Misc\n",
    "    seed=42,\n",
    "    dataset_num_proc=2,\n",
    ")\n",
    "\n",
    "trainer = SFTTrainer(\n",
    "    model=model,\n",
    "    tokenizer=tokenizer,\n",
    "    train_dataset=train_ds,\n",
    "    args=training_args,\n",
    ")\n",
    "\n",
    "# CRITICAL: Apply completion-only loss (train only on assistant tokens)\n",
    "trainer = train_on_responses_only(trainer)\n",
    "\n",
    "print(\"Ready to train!\")\n",
    "print(f\"  Model: {MODEL_NAME}\")\n",
    "print(f\"  LoRA: r={LORA_R}, alpha={LORA_ALPHA}\")\n",
    "print(f\"  LR: {LEARNING_RATE}, Epochs: {NUM_EPOCHS}\")\n",
    "print(f\"  Effective batch: {BATCH_SIZE * GRAD_ACCUM}\")\n",
    "print(f\"  Max seq: {MAX_SEQ_LENGTH}\")\n",
    "print(f\"  Output: {OUTPUT_MODEL}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 🚀 TRAIN!\n",
    "trainer_stats = trainer.train()\n",
    "print(f\"\\n✅ Training complete! Loss: {trainer_stats.training_loss:.4f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Save & Push to Hub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save LoRA adapter\n",
    "model.save_pretrained(\"webscrape-agent-lora\")\n",
    "tokenizer.save_pretrained(\"webscrape-agent-lora\")\n",
    "\n",
    "# Push merged 16-bit model to Hub\n",
    "print(\"Pushing merged model to Hub (this takes a few minutes)...\")\n",
    "model.push_to_hub_merged(\n",
    "    OUTPUT_MODEL,\n",
    "    tokenizer,\n",
    "    save_method=\"merged_16bit\",\n",
    ")\n",
    "\n",
    "# Also push LoRA adapter separately (smaller, faster to load)\n",
    "model.push_to_hub(\n",
    "    OUTPUT_MODEL + \"-lora\",\n",
    "    tokenizer,\n",
    ")\n",
    "\n",
    "print(f\"\\n✅ Merged model: https://huggingface.co/{OUTPUT_MODEL}\")\n",
    "print(f\"✅ LoRA adapter: https://huggingface.co/{OUTPUT_MODEL}-lora\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Test the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Switch to inference mode\n",
    "FastLanguageModel.for_inference(model)\n",
    "\n",
    "# Test: HTML extraction\n",
    "test_messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You are WebScrapeAgent, a web data extraction assistant. Given web content and a target schema, extract clean structured JSON. Every value must exist in the source content. Never invent data. Always include extraction status.\"},\n",
    "    {\"role\": \"user\", \"content\": \"\"\"Extract structured data from the following web content.\n",
    "\n",
    "<content>\n",
    "<div class=\\\"product-list\\\">\n",
    "  <div class=\\\"product\\\" data-sku=\\\"WH-1000\\\">\n",
    "    <h3>Sony WH-1000XM5</h3>\n",
    "    <span class=\\\"price\\\">$348.00</span>\n",
    "    <div class=\\\"rating\\\">4.7 out of 5</div>\n",
    "    <span class=\\\"stock in-stock\\\">Available</span>\n",
    "  </div>\n",
    "  <div class=\\\"product\\\" data-sku=\\\"AP-MAX\\\">\n",
    "    <h3>AirPods Max</h3>\n",
    "    <span class=\\\"price\\\">$549.00</span>\n",
    "    <div class=\\\"rating\\\">4.3 out of 5</div>\n",
    "    <span class=\\\"stock limited\\\">Only 2 left</span>\n",
    "  </div>\n",
    "</div>\n",
    "</content>\n",
    "\n",
    "Return as JSON array of products with name, sku, price, rating, and availability.\"\"\"}\n",
    "]\n",
    "\n",
    "inputs = tokenizer.apply_chat_template(\n",
    "    test_messages, tokenize=True, add_generation_prompt=True, return_tensors=\"pt\"\n",
    ").to(\"cuda\")\n",
    "\n",
    "outputs = model.generate(\n",
    "    input_ids=inputs,\n",
    "    max_new_tokens=512,\n",
    "    temperature=0.3,\n",
    "    do_sample=True,\n",
    ")\n",
    "\n",
    "response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)\n",
    "print(\"Model response:\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test: Multi-step scraping with error recovery\n",
    "test_messages_2 = [\n",
    "    {\"role\": \"system\", \"content\": \"\"\"You are WebScrapeAgent, an autonomous web scraping and data extraction system.\n",
    "\n",
    "Available actions:\n",
    "- EXTRACT_JSON, NAVIGATE, FILL_FORM, CLICK, WAIT, SET_COOKIES, SET_HEADERS,\n",
    "  LOAD_BROWSER_PROFILE, EXECUTE_JS, SCROLL, SWITCH_STRATEGY, RETURN_RESULT\n",
    "\n",
    "Rules:\n",
    "- NEVER invent data\n",
    "- ALWAYS include status in RETURN_RESULT: \\\"success\\\", \\\"partial\\\", or \\\"failed\\\"\n",
    "- Think step-by-step in <thought> blocks\n",
    "- Maximum 10 steps per job\"\"\"},\n",
    "    {\"role\": \"user\", \"content\": \"Task: Extract product reviews\\nURL: https://reviews.example.com/product/456\"},\n",
    "    {\"role\": \"assistant\", \"content\": \"<thought>Let me navigate to the reviews page.</thought>\\n\\nACTION: NAVIGATE\\n```json\\n{\\\"url\\\": \\\"https://reviews.example.com/product/456\\\", \\\"method\\\": \\\"GET\\\"}\\n```\"},\n",
    "    {\"role\": \"user\", \"content\": \"Observation: HTTP 403 Forbidden\\n\\n<html><body><h1>Access Denied</h1><p>Bot detection triggered.</p></body></html>\"},\n",
    "]\n",
    "\n",
    "inputs = tokenizer.apply_chat_template(\n",
    "    test_messages_2, tokenize=True, add_generation_prompt=True, return_tensors=\"pt\"\n",
    ").to(\"cuda\")\n",
    "\n",
    "outputs = model.generate(\n",
    "    input_ids=inputs,\n",
    "    max_new_tokens=512,\n",
    "    temperature=0.3,\n",
    "    do_sample=True,\n",
    ")\n",
    "\n",
    "response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)\n",
    "print(\"Model response (error recovery):\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. Optional: Export to GGUF (for llama.cpp / Ollama)\n",
    "\n",
    "Uncomment to export for local deployment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Export to GGUF Q4_K_M (smallest good quality)\n",
    "# model.save_pretrained_gguf(\n",
    "#     \"webscrape-agent-gguf\",\n",
    "#     tokenizer,\n",
    "#     quantization_method=\"q4_k_m\",\n",
    "# )\n",
    "# \n",
    "# # Push GGUF to Hub\n",
    "# model.push_to_hub_gguf(\n",
    "#     OUTPUT_MODEL + \"-GGUF\",\n",
    "#     tokenizer,\n",
    "#     quantization_method=\"q4_k_m\",\n",
    "# )"
   ]
  }
 ]
}