File size: 27,667 Bytes
030f984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": [],
   "gpuType": "T4",
   "name": "StudyOcean_RAG_Colab.ipynb"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "accelerator": "GPU"
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# πŸŽ“ Study Ocean AI Mentor β€” Google Colab Pipeline\n",
    "\n",
    "> **Run this notebook on Colab with T4 GPU**  \n",
    "> Runtime β†’ Change runtime type β†’ **T4 GPU**\n",
    "\n",
    "### What this notebook does:\n",
    "1. βœ… Mounts your Google Drive\n",
    "2. βœ… Installs all dependencies (with `faiss-gpu` for T4 acceleration)\n",
    "3. βœ… Extracts PDFs from `StudyOceanPDFs.zip`\n",
    "4. βœ… Runs the ingestion pipeline (GPU-accelerated embeddings)\n",
    "5. βœ… Saves the FAISS vector store back to Google Drive\n",
    "6. βœ… Launches the Gradio chat UI with a public shareable link\n",
    "\n",
    "---\n",
    "### πŸ“‹ Before you start:\n",
    "- Upload `StudyOceanPDFs.zip` to your **Google Drive** (e.g., `MyDrive/StudyOcean/StudyOceanPDFs.zip`)\n",
    "- Get your **Google Gemini API key** from https://aistudio.google.com/apikey\n",
    "- Set runtime to **T4 GPU**: Runtime β†’ Change runtime type β†’ T4 GPU\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ”§ Step 1 β€” Verify GPU & Mount Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ── Verify T4 GPU is available ───────────────────────────────\n",
    "import subprocess\n",
    "result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)\n",
    "print(result.stdout if result.returncode == 0 else \"⚠️ No GPU found! Go to Runtime β†’ Change runtime type β†’ T4 GPU\")\n",
    "\n",
    "import torch\n",
    "print(f\"\\nπŸ”₯ PyTorch CUDA available: {torch.cuda.is_available()}\")\n",
    "if torch.cuda.is_available():\n",
    "    print(f\"   GPU: {torch.cuda.get_device_name(0)}\")\n",
    "    print(f\"   VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ── Mount Google Drive ────────────────────────────────────────\n",
    "from google.colab import drive\n",
    "drive.mount('/content/drive')\n",
    "print(\"βœ… Google Drive mounted at /content/drive\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ“¦ Step 2 β€” Install Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ── Install all packages ──────────────────────────────────────\n",
    "# Using faiss-gpu for T4 GPU acceleration during similarity search\n",
    "!pip install -q \\\n",
    "    langchain \\\n",
    "    langchain-community \\\n",
    "    langchain-core \\\n",
    "    langchain-google-genai \\\n",
    "    langchain-huggingface \\\n",
    "    pymupdf \\\n",
    "    faiss-gpu \\\n",
    "    sentence-transformers \\\n",
    "    google-generativeai \\\n",
    "    python-dotenv \\\n",
    "    gradio \\\n",
    "    tiktoken \\\n",
    "    accelerate\n",
    "\n",
    "print(\"\\nβœ… All packages installed!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## βš™οΈ Step 3 β€” Configure Settings\n",
    "\n",
    "> **Edit the variables in this cell before running!**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# ════════════════════════════════════════════════════════\n",
    "#              ✏️  EDIT THESE SETTINGS\n",
    "# ════════════════════════════════════════════════════════\n",
    "\n",
    "# Your Google Gemini API key β€” get it from https://aistudio.google.com/apikey\n",
    "GOOGLE_API_KEY = \"\"  # ← PASTE YOUR KEY HERE\n",
    "\n",
    "# Path to StudyOceanPDFs.zip in your Google Drive\n",
    "ZIP_DRIVE_PATH = \"/content/drive/MyDrive/StudyOcean/StudyOceanPDFs.zip\"\n",
    "\n",
    "# Where to save the vector store in Google Drive (so it persists!)\n",
    "VECTOR_STORE_DRIVE_PATH = \"/content/drive/MyDrive/StudyOcean/vector_store\"\n",
    "\n",
    "# Embedding model β€” 'cuda' will use the T4 GPU!\n",
    "EMBEDDING_MODEL = \"all-MiniLM-L6-v2\"   # Fast & accurate\n",
    "EMBEDDING_DEVICE = \"cuda\"               # ← T4 GPU acceleration\n",
    "\n",
    "# Gemini LLM model\n",
    "LLM_MODEL = \"gemini-2.0-flash\"\n",
    "\n",
    "# Text splitting config\n",
    "CHUNK_SIZE    = 1000\n",
    "CHUNK_OVERLAP = 200\n",
    "\n",
    "# RAG retrieval\n",
    "TOP_K = 5\n",
    "\n",
    "# ════════════════════════════════════════════════════════\n",
    "\n",
    "# Local working paths (inside Colab VM)\n",
    "PDF_DIR          = \"/content/studyocean_pdfs\"\n",
    "VECTOR_STORE_LOCAL = \"/content/vector_store\"\n",
    "\n",
    "os.makedirs(PDF_DIR, exist_ok=True)\n",
    "os.makedirs(VECTOR_STORE_LOCAL, exist_ok=True)\n",
    "\n",
    "# Set the API key as env var for LangChain\n",
    "os.environ[\"GOOGLE_API_KEY\"] = GOOGLE_API_KEY\n",
    "\n",
    "print(\"βœ… Configuration set!\")\n",
    "print(f\"   ZIP source:    {ZIP_DRIVE_PATH}\")\n",
    "print(f\"   PDF extract:   {PDF_DIR}\")\n",
    "print(f\"   Vector store:  {VECTOR_STORE_LOCAL}\")\n",
    "print(f\"   Drive backup:  {VECTOR_STORE_DRIVE_PATH}\")\n",
    "print(f\"   Embedding GPU: {EMBEDDING_DEVICE}\")\n",
    "print(f\"   LLM:           {LLM_MODEL}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ“‚ Step 4 β€” Extract PDFs from ZIP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import zipfile\n",
    "import time\n",
    "\n",
    "# Check if zip exists\n",
    "if not os.path.exists(ZIP_DRIVE_PATH):\n",
    "    print(f\"❌ ZIP not found at: {ZIP_DRIVE_PATH}\")\n",
    "    print(\"   Please upload StudyOceanPDFs.zip to your Google Drive\")\n",
    "    print(\"   Then update ZIP_DRIVE_PATH in Step 3.\")\n",
    "    raise FileNotFoundError(f\"ZIP not found: {ZIP_DRIVE_PATH}\")\n",
    "\n",
    "zip_size_gb = os.path.getsize(ZIP_DRIVE_PATH) / 1e9\n",
    "print(f\"πŸ“¦ Found ZIP: {zip_size_gb:.2f} GB\")\n",
    "print(f\"   Extracting to {PDF_DIR} ...\")\n",
    "print(\"   (This may take a few minutes for large archives)\")\n",
    "\n",
    "start = time.time()\n",
    "with zipfile.ZipFile(ZIP_DRIVE_PATH, 'r') as zf:\n",
    "    zf.extractall(PDF_DIR)\n",
    "elapsed = time.time() - start\n",
    "\n",
    "# Count extracted PDFs\n",
    "pdf_count = sum(\n",
    "    1 for root, _, files in os.walk(PDF_DIR)\n",
    "    for f in files if f.lower().endswith('.pdf')\n",
    ")\n",
    "\n",
    "print(f\"\\nβœ… Extraction complete in {elapsed:.1f}s\")\n",
    "print(f\"   Found {pdf_count} PDF files\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ” Step 5 β€” PDF Ingestion Pipeline (GPU-Accelerated)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fitz  # PyMuPDF\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "from langchain.docstore.document import Document\n",
    "\n",
    "\n",
    "def collect_pdf_paths(pdf_dir):\n",
    "    \"\"\"Recursively collect all PDF paths.\"\"\"\n",
    "    paths = []\n",
    "    for root, _, files in os.walk(pdf_dir):\n",
    "        for f in files:\n",
    "            if f.lower().endswith('.pdf'):\n",
    "                paths.append(os.path.join(root, f))\n",
    "    return sorted(paths)\n",
    "\n",
    "\n",
    "def extract_text(pdf_path):\n",
    "    \"\"\"Extract text from a PDF using PyMuPDF.\"\"\"\n",
    "    text = \"\"\n",
    "    try:\n",
    "        doc = fitz.open(pdf_path)\n",
    "        for page in doc:\n",
    "            text += page.get_text()\n",
    "        doc.close()\n",
    "    except Exception as e:\n",
    "        print(f\"  ⚠ Error reading {os.path.basename(pdf_path)}: {e}\")\n",
    "    return text\n",
    "\n",
    "\n",
    "def build_documents(pdf_paths):\n",
    "    \"\"\"Extract and chunk all PDFs into LangChain Documents.\"\"\"\n",
    "    splitter = RecursiveCharacterTextSplitter(\n",
    "        chunk_size=CHUNK_SIZE,\n",
    "        chunk_overlap=CHUNK_OVERLAP,\n",
    "        separators=[\"\\n\\n\", \"\\n\", \". \", \" \", \"\"],\n",
    "    )\n",
    "    all_docs = []\n",
    "    skipped = 0\n",
    "\n",
    "    for i, path in enumerate(pdf_paths, 1):\n",
    "        rel = os.path.relpath(path, PDF_DIR)\n",
    "        parts = rel.replace(\"\\\\\", \"/\").split(\"/\")\n",
    "        subject = parts[0] if len(parts) > 1 else \"General\"\n",
    "\n",
    "        if i % 50 == 0 or i == 1:\n",
    "            print(f\"  [{i}/{len(pdf_paths)}] {rel}\")\n",
    "\n",
    "        text = extract_text(path)\n",
    "        if not text.strip():\n",
    "            skipped += 1\n",
    "            continue\n",
    "\n",
    "        chunks = splitter.create_documents(\n",
    "            texts=[text],\n",
    "            metadatas=[{\"source\": rel, \"filename\": os.path.basename(path), \"subject\": subject}]\n",
    "        )\n",
    "        all_docs.extend(chunks)\n",
    "\n",
    "    print(f\"\\n  Skipped (image/scanned PDFs): {skipped}\")\n",
    "    return all_docs\n",
    "\n",
    "\n",
    "# ── Step 5a: Collect PDFs ────────────────────────────────────\n",
    "print(\"=\" * 60)\n",
    "print(\"πŸ“„ PHASE 1: Collecting & chunking PDFs\")\n",
    "print(\"=\" * 60)\n",
    "pdf_paths = collect_pdf_paths(PDF_DIR)\n",
    "print(f\"   {len(pdf_paths)} PDFs found\")\n",
    "\n",
    "t0 = time.time()\n",
    "documents = build_documents(pdf_paths)\n",
    "t_extract = time.time() - t0\n",
    "\n",
    "print(f\"\\nβœ… Created {len(documents)} chunks in {t_extract:.1f}s\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ── Step 5b: Load Embedding Model onto T4 GPU ────────────────\n",
    "print(\"=\" * 60)\n",
    "print(f\"🧠 PHASE 2: Loading embedding model on {EMBEDDING_DEVICE.upper()}\")\n",
    "print(\"=\" * 60)\n",
    "\n",
    "import torch\n",
    "device = EMBEDDING_DEVICE if torch.cuda.is_available() else \"cpu\"\n",
    "if device == \"cpu\" and EMBEDDING_DEVICE == \"cuda\":\n",
    "    print(\"⚠️  CUDA not available, falling back to CPU\")\n",
    "\n",
    "embeddings = HuggingFaceEmbeddings(\n",
    "    model_name=EMBEDDING_MODEL,\n",
    "    model_kwargs={\"device\": device},\n",
    "    encode_kwargs={\"normalize_embeddings\": True, \"batch_size\": 256},  # Larger batch on GPU!\n",
    ")\n",
    "\n",
    "print(f\"βœ… Embedding model loaded on {device.upper()}\")\n",
    "print(f\"   Model: {EMBEDDING_MODEL}\")\n",
    "print(f\"   Batch size: 256 (optimized for T4)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ── Step 5c: Build FAISS Vector Store ───────────────────────\n",
    "print(\"=\" * 60)\n",
    "print(f\"πŸ“Š PHASE 3: Building FAISS vector store ({len(documents)} chunks)\")\n",
    "print(\"=\" * 60)\n",
    "\n",
    "BATCH_SIZE = 1000  # Larger batches since we have GPU\n",
    "vector_store = None\n",
    "t0 = time.time()\n",
    "\n",
    "total_batches = (len(documents) + BATCH_SIZE - 1) // BATCH_SIZE\n",
    "for i in range(0, len(documents), BATCH_SIZE):\n",
    "    batch = documents[i : i + BATCH_SIZE]\n",
    "    batch_num = (i // BATCH_SIZE) + 1\n",
    "\n",
    "    elapsed = time.time() - t0\n",
    "    rate = (i + 1) / elapsed if elapsed > 0 else 0\n",
    "    eta = (len(documents) - i) / rate if rate > 0 else 0\n",
    "    print(f\"  Batch {batch_num:3d}/{total_batches} | {len(batch)} chunks | \"\n",
    "          f\"ETA: {eta/60:.1f} min\")\n",
    "\n",
    "    if vector_store is None:\n",
    "        vector_store = FAISS.from_documents(batch, embeddings)\n",
    "    else:\n",
    "        batch_store = FAISS.from_documents(batch, embeddings)\n",
    "        vector_store.merge_from(batch_store)\n",
    "\n",
    "t_embed = time.time() - t0\n",
    "print(f\"\\nβœ… Vector store built in {t_embed/60:.1f} minutes\")\n",
    "print(f\"   Total vectors: {vector_store.index.ntotal}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ’Ύ Step 6 β€” Save Vector Store to Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "\n",
    "# Save locally first\n",
    "print(\"πŸ’Ύ Saving vector store locally...\")\n",
    "vector_store.save_local(VECTOR_STORE_LOCAL)\n",
    "print(f\"   βœ… Saved to {VECTOR_STORE_LOCAL}\")\n",
    "\n",
    "# Copy to Google Drive for persistence\n",
    "print(\"\\n☁️  Copying to Google Drive...\")\n",
    "os.makedirs(VECTOR_STORE_DRIVE_PATH, exist_ok=True)\n",
    "\n",
    "for fname in os.listdir(VECTOR_STORE_LOCAL):\n",
    "    src = os.path.join(VECTOR_STORE_LOCAL, fname)\n",
    "    dst = os.path.join(VECTOR_STORE_DRIVE_PATH, fname)\n",
    "    shutil.copy2(src, dst)\n",
    "    size = os.path.getsize(dst) / 1e6\n",
    "    print(f\"   βœ… {fname}  ({size:.1f} MB)\")\n",
    "\n",
    "print(f\"\\nπŸŽ‰ Vector store saved to Google Drive!\")\n",
    "print(f\"   Path: {VECTOR_STORE_DRIVE_PATH}\")\n",
    "print(f\"   Total vectors: {vector_store.index.ntotal}\")\n",
    "print(\"\\nπŸ“Œ You can now download this folder to your laptop\")\n",
    "print(\"   and place it at: Study_Ocean Rag/data/vector_store/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ§ͺ Step 7 β€” Test the Vector Store (Quick Sanity Check)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ── Quick retrieval test ─────────────────────────────────────\n",
    "test_question = \"What is Newton's second law of motion?\"\n",
    "print(f\"πŸ” Test query: '{test_question}'\\n\")\n",
    "\n",
    "results = vector_store.similarity_search(test_question, k=3)\n",
    "for i, doc in enumerate(results, 1):\n",
    "    print(f\"--- Result {i} ---\")\n",
    "    print(f\"Subject : {doc.metadata.get('subject', 'N/A')}\")\n",
    "    print(f\"File    : {doc.metadata.get('filename', 'N/A')}\")\n",
    "    print(f\"Preview : {doc.page_content[:200]}...\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ€– Step 8 β€” Build the RAG Chain (Gemini + FAISS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_google_genai import ChatGoogleGenerativeAI\n",
    "from langchain.chains import RetrievalQA\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "# Validate API key\n",
    "if not GOOGLE_API_KEY or GOOGLE_API_KEY == \"\":\n",
    "    print(\"❌ GOOGLE_API_KEY is not set! Go back to Step 3 and paste your key.\")\n",
    "    raise ValueError(\"Missing GOOGLE_API_KEY\")\n",
    "\n",
    "# ── Student Mentor Prompt ────────────────────────────────────\n",
    "MENTOR_PROMPT = PromptTemplate(\n",
    "    input_variables=[\"context\", \"question\"],\n",
    "    template=\"\"\"You are **Study Ocean AI Mentor** β€” a friendly, patient, and knowledgeable tutor who helps students understand their study material deeply.\n",
    "\n",
    "Your role:\n",
    "- Solve student doubts with clear, step-by-step explanations\n",
    "- Use simple language that students can easily understand\n",
    "- Provide examples and analogies when helpful\n",
    "- If the answer is in the context, use it; if not, say so honestly\n",
    "- Encourage students and make learning enjoyable\n",
    "\n",
    "**Guidelines:**\n",
    "1. Start with a brief, direct answer, then explain in detail\n",
    "2. Use bullet points, numbered steps, or tables for clarity\n",
    "3. For math/science problems, show the solution step by step\n",
    "4. Mention which topic/chapter the information relates to when possible\n",
    "5. If the topic isn't covered, say: \"This isn't in your study materials, but here's what I know...\"\n",
    "\n",
    "---\n",
    "πŸ“š **Relevant Study Material:**\n",
    "{context}\n",
    "\n",
    "---\n",
    "πŸ™‹ **Student's Question:** {question}\n",
    "\n",
    "---\n",
    "πŸŽ“ **Mentor's Response:**\n",
    "\"\"\"\n",
    ")\n",
    "\n",
    "# ── Initialize LLM ───────────────────────────────────────────\n",
    "llm = ChatGoogleGenerativeAI(\n",
    "    model=LLM_MODEL,\n",
    "    google_api_key=GOOGLE_API_KEY,\n",
    "    temperature=0.3,\n",
    "    max_output_tokens=2048,\n",
    "    convert_system_message_to_human=True,\n",
    ")\n",
    "\n",
    "# ── Build RAG Chain ──────────────────────────────────────────\n",
    "retriever = vector_store.as_retriever(\n",
    "    search_type=\"similarity\",\n",
    "    search_kwargs={\"k\": TOP_K},\n",
    ")\n",
    "\n",
    "rag_chain = RetrievalQA.from_chain_type(\n",
    "    llm=llm,\n",
    "    chain_type=\"stuff\",\n",
    "    retriever=retriever,\n",
    "    return_source_documents=True,\n",
    "    chain_type_kwargs={\"prompt\": MENTOR_PROMPT},\n",
    ")\n",
    "\n",
    "print(\"βœ… RAG Chain ready!\")\n",
    "print(f\"   LLM: {LLM_MODEL}\")\n",
    "print(f\"   Retriever: top-{TOP_K} chunks\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## πŸ§ͺ Step 9 β€” Test the Full RAG Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ask(question):\n",
    "    \"\"\"Ask the mentor a question.\"\"\"\n",
    "    print(f\"\\nπŸ™‹ Question: {question}\")\n",
    "    print(\"πŸ” Searching study material...\\n\")\n",
    "    result = rag_chain.invoke({\"query\": question})\n",
    "    print(\"πŸŽ“ Mentor's Answer:\")\n",
    "    print(\"─\" * 60)\n",
    "    print(result[\"result\"])\n",
    "    print(\"─\" * 60)\n",
    "    print(\"\\nπŸ“– Sources:\")\n",
    "    seen = set()\n",
    "    for doc in result.get(\"source_documents\", []):\n",
    "        key = doc.metadata.get(\"source\", \"?\")\n",
    "        if key not in seen:\n",
    "            seen.add(key)\n",
    "            print(f\"  [{doc.metadata.get('subject','?')}] {doc.metadata.get('filename','?')}\")\n",
    "\n",
    "# Test it!\n",
    "ask(\"Explain Newton's laws of motion with examples\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🌐 Step 10 β€” Launch Gradio Chat UI (Public Link)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "\n",
    "CUSTOM_CSS = \"\"\"\n",
    ".gradio-container {\n",
    "    max-width: 900px !important;\n",
    "    margin: auto !important;\n",
    "    font-family: 'Inter', 'Segoe UI', sans-serif !important;\n",
    "}\n",
    ".header-section {\n",
    "    text-align: center;\n",
    "    padding: 20px 0 10px;\n",
    "    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);\n",
    "    border-radius: 16px;\n",
    "    margin-bottom: 20px;\n",
    "    color: white;\n",
    "}\n",
    ".header-section h1 { font-size: 2em; margin: 0; color: white !important; }\n",
    ".header-section p  { opacity: 0.9; margin: 5px 0 0; font-size: 1.05em; color: #e8e8ff !important; }\n",
    ".chatbot { border-radius: 12px !important; min-height: 450px !important; }\n",
    ".primary-btn {\n",
    "    background: linear-gradient(135deg, #667eea, #764ba2) !important;\n",
    "    border: none !important; color: white !important;\n",
    "    border-radius: 10px !important; font-weight: 600 !important;\n",
    "}\n",
    ".primary-btn:hover { opacity: 0.9 !important; transform: translateY(-1px); }\n",
    "\"\"\"\n",
    "\n",
    "def format_sources(docs):\n",
    "    if not docs: return \"\"\n",
    "    seen, lines = set(), [\"\\n\\n---\\nπŸ“– **Sources Referenced:**\\n\"]\n",
    "    for doc in docs:\n",
    "        key = f\"{doc.metadata.get('subject','?')}/{doc.metadata.get('source','?')}\"\n",
    "        if key not in seen:\n",
    "            seen.add(key)\n",
    "            lines.append(f\"**β€’** `{doc.metadata.get('subject','?')}` β†’ _{doc.metadata.get('filename','?')}_\")\n",
    "    return \"\\n\".join(lines)\n",
    "\n",
    "def respond(message, history):\n",
    "    if not message.strip():\n",
    "        return \"\", history\n",
    "    result = rag_chain.invoke({\"query\": message})\n",
    "    answer = result[\"result\"] + format_sources(result.get(\"source_documents\", []))\n",
    "    history.append((message, answer))\n",
    "    return \"\", history\n",
    "\n",
    "def clear_chat():\n",
    "    return [], \"\"\n",
    "\n",
    "with gr.Blocks(css=CUSTOM_CSS, title=\"Study Ocean AI Mentor\", theme=gr.themes.Soft()) as demo:\n",
    "    gr.HTML(\"\"\"\n",
    "    <div class=\"header-section\">\n",
    "        <h1>πŸŽ“ Study Ocean AI Mentor</h1>\n",
    "        <p>Ask any doubt from your study material β€” I'll explain it step by step!</p>\n",
    "    </div>\n",
    "    \"\"\")\n",
    "    chatbot = gr.Chatbot(\n",
    "        label=\"πŸ’¬ Chat with Your Mentor\",\n",
    "        elem_classes=[\"chatbot\"],\n",
    "        height=480,\n",
    "        show_copy_button=True,\n",
    "        bubble_full_width=False,\n",
    "        avatar_images=(None, \"https://api.dicebear.com/7.x/bottts/svg?seed=mentor&backgroundColor=6366f1\"),\n",
    "    )\n",
    "    with gr.Row():\n",
    "        msg = gr.Textbox(\n",
    "            placeholder=\"Type your doubt here... e.g., 'Explain Newton's third law'\",\n",
    "            scale=5, lines=1, max_lines=3, show_label=False, container=False,\n",
    "        )\n",
    "        submit_btn = gr.Button(\"Ask πŸš€\", elem_classes=[\"primary-btn\"], scale=1)\n",
    "    with gr.Row():\n",
    "        clear_btn = gr.Button(\"πŸ—‘οΈ Clear Chat\", size=\"sm\")\n",
    "\n",
    "    gr.Examples(\n",
    "        examples=[\n",
    "            \"What are Newton's laws of motion? Explain with examples.\",\n",
    "            \"Explain the difference between mitosis and meiosis.\",\n",
    "            \"What is the Pythagorean theorem and how do I apply it?\",\n",
    "            \"Explain the concept of supply and demand in economics.\",\n",
    "            \"What are the key features of the Indian Constitution?\",\n",
    "        ],\n",
    "        inputs=msg, label=\"πŸ’‘ Try these example questions:\",\n",
    "    )\n",
    "\n",
    "    msg.submit(respond, [msg, chatbot], [msg, chatbot])\n",
    "    submit_btn.click(respond, [msg, chatbot], [msg, chatbot])\n",
    "    clear_btn.click(clear_chat, outputs=[chatbot, msg])\n",
    "\n",
    "# Launch with share=True to get a public Gradio URL!\n",
    "demo.launch(share=True, debug=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## πŸ’» Step 11 β€” Use Pre-built Vector Store (Skip Re-ingestion)\n",
    "\n",
    "> **Run this cell INSTEAD of Steps 4–6 if you already have a saved vector store on Drive**\n",
    "> This lets you skip the ~1 hour ingestion on future sessions!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ── Load existing vector store from Drive ────────────────────\n",
    "# ⚠️ Only run this cell if you've already done ingestion before!\n",
    "# Skip this if running ingestion fresh (Steps 4-6).\n",
    "\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_huggingface import HuggingFaceEmbeddings\n",
    "import torch, os\n",
    "\n",
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "print(f\"πŸ”§ Loading embeddings on {device.upper()}...\")\n",
    "\n",
    "embeddings = HuggingFaceEmbeddings(\n",
    "    model_name=EMBEDDING_MODEL,\n",
    "    model_kwargs={\"device\": device},\n",
    "    encode_kwargs={\"normalize_embeddings\": True},\n",
    ")\n",
    "\n",
    "# Try Drive path first, fall back to local\n",
    "store_path = VECTOR_STORE_DRIVE_PATH if os.path.exists(VECTOR_STORE_DRIVE_PATH) else VECTOR_STORE_LOCAL\n",
    "print(f\"πŸ“‚ Loading vector store from: {store_path}\")\n",
    "\n",
    "vector_store = FAISS.load_local(\n",
    "    store_path,\n",
    "    embeddings,\n",
    "    allow_dangerous_deserialization=True,\n",
    ")\n",
    "\n",
    "print(f\"βœ… Loaded! Total vectors: {vector_store.index.ntotal}\")\n",
    "print(\"   Now run Steps 8 β†’ 10 to start chatting!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## πŸ“₯ Step 12 β€” Download Vector Store to Your Laptop\n",
    "\n",
    "> Run this to zip and download the vector store directly to your PC (no Drive needed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "from google.colab import files\n",
    "\n",
    "# Zip the vector store\n",
    "print(\"πŸ“¦ Zipping vector store...\")\n",
    "zip_path = \"/content/vector_store_export.zip\"\n",
    "shutil.make_archive(\"/content/vector_store_export\", 'zip', VECTOR_STORE_LOCAL)\n",
    "\n",
    "size_mb = os.path.getsize(zip_path) / 1e6\n",
    "print(f\"   ZIP size: {size_mb:.1f} MB\")\n",
    "\n",
    "# Download to local machine\n",
    "print(\"⬇️  Downloading to your computer...\")\n",
    "files.download(zip_path)\n",
    "print(\"\\nβœ… Download started!\")\n",
    "print(\"   Extract to: Study_Ocean Rag/data/vector_store/\")\n",
    "print(\"   Then run locally: python main.py chat\")"
   ]
  }
 ]
}