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
|
| 7 |
+
"gpuType": "T4"
|
| 8 |
+
},
|
| 9 |
+
"kernelspec": {
|
| 10 |
+
"name": "python3",
|
| 11 |
+
"display_name": "Python 3"
|
| 12 |
+
},
|
| 13 |
+
"language_info": {
|
| 14 |
+
"name": "python"
|
| 15 |
+
},
|
| 16 |
+
"accelerator": "GPU"
|
| 17 |
+
},
|
| 18 |
+
"cells": [
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": null,
|
| 22 |
+
"metadata": {
|
| 23 |
+
"id": "3mmJUlxcRpiU"
|
| 24 |
+
},
|
| 25 |
+
"outputs": [],
|
| 26 |
+
"source": [
|
| 27 |
+
"# Colab cell 1: Install dependencies\n",
|
| 28 |
+
"!pip install --quiet \\\n",
|
| 29 |
+
" transformers accelerate peft datasets \\\n",
|
| 30 |
+
" bitsandbytes huggingface_hub \\\n",
|
| 31 |
+
" pymupdf"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"source": [
|
| 37 |
+
"# Colab cell 2: Log in to Hugging Face\n",
|
| 38 |
+
"from huggingface_hub import notebook_login\n",
|
| 39 |
+
"notebook_login()\n",
|
| 40 |
+
"# This will prompt you to paste a Hugging Face access token."
|
| 41 |
+
],
|
| 42 |
+
"metadata": {
|
| 43 |
+
"id": "H3KO-a_dSod6"
|
| 44 |
+
},
|
| 45 |
+
"execution_count": null,
|
| 46 |
+
"outputs": []
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"cell_type": "code",
|
| 50 |
+
"source": [
|
| 51 |
+
"# Colab cell 3: Mount your Drive (if PDFs are there)\n",
|
| 52 |
+
"from google.colab import drive\n",
|
| 53 |
+
"drive.mount('/content/drive')"
|
| 54 |
+
],
|
| 55 |
+
"metadata": {
|
| 56 |
+
"id": "oMaXhRSCVu51"
|
| 57 |
+
},
|
| 58 |
+
"execution_count": null,
|
| 59 |
+
"outputs": []
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"cell_type": "code",
|
| 63 |
+
"source": [
|
| 64 |
+
"# Colab cell 4: Extract text from all PDFs\n",
|
| 65 |
+
"import fitz # PyMuPDF\n",
|
| 66 |
+
"import os\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"PDF_DIR = \"/content/drive/MyDrive/GodeusAI-DatasetPDF\"\n",
|
| 69 |
+
"OUTPUT_TXT = \"/content/all_text.txt\"\n",
|
| 70 |
+
"\n",
|
| 71 |
+
"with open(OUTPUT_TXT, \"w\", encoding=\"utf-8\") as fout:\n",
|
| 72 |
+
" for fname in os.listdir(PDF_DIR):\n",
|
| 73 |
+
" if fname.lower().endswith(\".pdf\"):\n",
|
| 74 |
+
" doc = fitz.open(os.path.join(PDF_DIR, fname))\n",
|
| 75 |
+
" for page in doc:\n",
|
| 76 |
+
" fout.write(page.get_text())\n",
|
| 77 |
+
" doc.close()\n",
|
| 78 |
+
"print(\"✅ Extracted text from PDFs to\", OUTPUT_TXT)"
|
| 79 |
+
],
|
| 80 |
+
"metadata": {
|
| 81 |
+
"id": "j_pVh2BcWn2b"
|
| 82 |
+
},
|
| 83 |
+
"execution_count": null,
|
| 84 |
+
"outputs": []
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"cell_type": "code",
|
| 88 |
+
"source": [
|
| 89 |
+
"# Colab cell 5: Chunk & format into JSONL\n",
|
| 90 |
+
"import tiktoken # or use your tokenizer for approximate token counts\n",
|
| 91 |
+
"import json\n",
|
| 92 |
+
"\n",
|
| 93 |
+
"def chunk_text(text, max_tokens=512, overlap=50):\n",
|
| 94 |
+
" # simple whitespace split + sliding window\n",
|
| 95 |
+
" words = text.split()\n",
|
| 96 |
+
" chunks = []\n",
|
| 97 |
+
" i = 0\n",
|
| 98 |
+
" while i < len(words):\n",
|
| 99 |
+
" chunk = words[i : i + max_tokens]\n",
|
| 100 |
+
" chunks.append(\" \".join(chunk))\n",
|
| 101 |
+
" i += max_tokens - overlap\n",
|
| 102 |
+
" return chunks\n",
|
| 103 |
+
"\n",
|
| 104 |
+
"# Read and chunk\n",
|
| 105 |
+
"with open(OUTPUT_TXT, \"r\", encoding=\"utf-8\") as fin:\n",
|
| 106 |
+
" text = fin.read()\n",
|
| 107 |
+
"\n",
|
| 108 |
+
"chunks = chunk_text(text, max_tokens=512, overlap=50)\n",
|
| 109 |
+
"\n",
|
| 110 |
+
"# Build instruction–response pairs (empty input, you can customize)\n",
|
| 111 |
+
"records = []\n",
|
| 112 |
+
"for chunk in chunks:\n",
|
| 113 |
+
" records.append({\n",
|
| 114 |
+
" \"instruction\": \"Based on this teaching, explain the key insight in a concise coach‑style voice.\",\n",
|
| 115 |
+
" \"input\": chunk,\n",
|
| 116 |
+
" \"output\": \"\" # leave blank for self‑supervised teaching; or fill with human summaries\n",
|
| 117 |
+
" })\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"# Save JSONL\n",
|
| 120 |
+
"import pathlib\n",
|
| 121 |
+
"out_path = pathlib.Path(\"/content/godeusai_instruct.jsonl\")\n",
|
| 122 |
+
"with out_path.open(\"w\", encoding=\"utf-8\") as fout:\n",
|
| 123 |
+
" for rec in records:\n",
|
| 124 |
+
" fout.write(json.dumps(rec) + \"\\n\")\n",
|
| 125 |
+
"print(\"✅ Wrote\", len(records), \"records to\", out_path)"
|
| 126 |
+
],
|
| 127 |
+
"metadata": {
|
| 128 |
+
"id": "w_oi1OEnXgv2"
|
| 129 |
+
},
|
| 130 |
+
"execution_count": null,
|
| 131 |
+
"outputs": []
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"cell_type": "code",
|
| 135 |
+
"source": [
|
| 136 |
+
"from huggingface_hub import login\n",
|
| 137 |
+
"login() # paste your token when prompted\n"
|
| 138 |
+
],
|
| 139 |
+
"metadata": {
|
| 140 |
+
"id": "IbsSoQxHZ789"
|
| 141 |
+
},
|
| 142 |
+
"execution_count": null,
|
| 143 |
+
"outputs": []
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"cell_type": "code",
|
| 147 |
+
"source": [
|
| 148 |
+
"# Colab cell 6: Load model in 4‑bit + LoRA configuration\n",
|
| 149 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig\n",
|
| 150 |
+
"from peft import LoraConfig, get_peft_model, TaskType\n",
|
| 151 |
+
"\n",
|
| 152 |
+
"MODEL_NAME = \"mistralai/Mistral-7B-v0.1\"\n",
|
| 153 |
+
"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)\n",
|
| 154 |
+
"\n",
|
| 155 |
+
"# 4‑bit quantization config\n",
|
| 156 |
+
"bnb_config = BitsAndBytesConfig(\n",
|
| 157 |
+
" load_in_4bit=True,\n",
|
| 158 |
+
" bnb_4bit_quant_type=\"nf4\",\n",
|
| 159 |
+
" bnb_4bit_use_double_quant=True,\n",
|
| 160 |
+
" bnb_4bit_compute_dtype=\"bfloat16\"\n",
|
| 161 |
+
")\n",
|
| 162 |
+
"\n",
|
| 163 |
+
"model = AutoModelForCausalLM.from_pretrained(\n",
|
| 164 |
+
" MODEL_NAME,\n",
|
| 165 |
+
" device_map=\"auto\",\n",
|
| 166 |
+
" quantization_config=bnb_config\n",
|
| 167 |
+
")\n",
|
| 168 |
+
"\n",
|
| 169 |
+
"# LoRA adapter setup\n",
|
| 170 |
+
"peft_config = LoraConfig(\n",
|
| 171 |
+
" task_type=TaskType.CAUSAL_LM,\n",
|
| 172 |
+
" inference_mode=False,\n",
|
| 173 |
+
" r=32, # adapter rank—controls capacity to learn style\n",
|
| 174 |
+
" lora_alpha=16,\n",
|
| 175 |
+
" lora_dropout=0.05\n",
|
| 176 |
+
")\n",
|
| 177 |
+
"model = get_peft_model(model, peft_config)\n",
|
| 178 |
+
"def count_trainable_params(model):\n",
|
| 179 |
+
" return sum(p.numel() for p in model.parameters() if p.requires_grad)\n",
|
| 180 |
+
"\n",
|
| 181 |
+
"print(\"✅ Model + LoRA ready. Trainable params:\", count_trainable_params(model))\n"
|
| 182 |
+
],
|
| 183 |
+
"metadata": {
|
| 184 |
+
"id": "2z9pACO5Y6OP"
|
| 185 |
+
},
|
| 186 |
+
"execution_count": null,
|
| 187 |
+
"outputs": []
|
| 188 |
+
},
|
| 189 |
+
{
|
| 190 |
+
"cell_type": "code",
|
| 191 |
+
"source": [
|
| 192 |
+
"# Colab Cell 7: Safely load local JSONL + tokenize\n",
|
| 193 |
+
"import json\n",
|
| 194 |
+
"from datasets import Dataset\n",
|
| 195 |
+
"\n",
|
| 196 |
+
"# Assign pad_token (Mistral doesn't define one by default)\n",
|
| 197 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
| 198 |
+
"\n",
|
| 199 |
+
"# Load JSONL into memory\n",
|
| 200 |
+
"with open(\"/content/godeusai_instruct.jsonl\", \"r\") as f:\n",
|
| 201 |
+
" raw_data = [json.loads(line) for line in f]\n",
|
| 202 |
+
"\n",
|
| 203 |
+
"# Convert to Hugging Face Dataset\n",
|
| 204 |
+
"ds = Dataset.from_list(raw_data)\n",
|
| 205 |
+
"ds = ds.train_test_split(test_size=0.05)\n",
|
| 206 |
+
"\n",
|
| 207 |
+
"# Tokenization logic\n",
|
| 208 |
+
"def tokenize_fn(example):\n",
|
| 209 |
+
" prompt = (\n",
|
| 210 |
+
" f\"### Instruction:\\n{example['instruction']}\\n\"\n",
|
| 211 |
+
" f\"### Input:\\n{example['input']}\\n\"\n",
|
| 212 |
+
" f\"### Response:\\n{example['output']}\"\n",
|
| 213 |
+
" )\n",
|
| 214 |
+
" tokens = tokenizer(\n",
|
| 215 |
+
" prompt,\n",
|
| 216 |
+
" truncation=True,\n",
|
| 217 |
+
" max_length=600,\n",
|
| 218 |
+
" padding=\"max_length\"\n",
|
| 219 |
+
" )\n",
|
| 220 |
+
" return tokens # ❌ Do not add \"labels\"\n",
|
| 221 |
+
"\n",
|
| 222 |
+
"\n",
|
| 223 |
+
"# Apply tokenization\n",
|
| 224 |
+
"# **NOTE** batched=False\n",
|
| 225 |
+
"tokenized = ds.map(\n",
|
| 226 |
+
" tokenize_fn,\n",
|
| 227 |
+
" batched=False,\n",
|
| 228 |
+
" remove_columns=ds[\"train\"].column_names\n",
|
| 229 |
+
")\n",
|
| 230 |
+
"print(f\"✅ Tokenization done. Example input_ids length: {len(tokenized['train'][0]['input_ids'])}\")\n"
|
| 231 |
+
],
|
| 232 |
+
"metadata": {
|
| 233 |
+
"id": "MPDZW2IedGJM"
|
| 234 |
+
},
|
| 235 |
+
"execution_count": null,
|
| 236 |
+
"outputs": []
|
| 237 |
+
},
|
| 238 |
+
{
|
| 239 |
+
"cell_type": "code",
|
| 240 |
+
"source": [
|
| 241 |
+
"# Colab Cell 8: Train with robust filtering + custom collator\n",
|
| 242 |
+
"\n",
|
| 243 |
+
"import torch\n",
|
| 244 |
+
"from torch.nn.utils.rnn import pad_sequence\n",
|
| 245 |
+
"from transformers import Trainer, TrainingArguments\n",
|
| 246 |
+
"\n",
|
| 247 |
+
"# 1) Filter out empty examples\n",
|
| 248 |
+
"def is_valid(ex): return isinstance(ex[\"input_ids\"], list) and len(ex[\"input_ids\"]) > 0\n",
|
| 249 |
+
"\n",
|
| 250 |
+
"for split in [\"train\", \"test\"]:\n",
|
| 251 |
+
" before = len(tokenized[split])\n",
|
| 252 |
+
" tokenized[split] = tokenized[split].filter(is_valid)\n",
|
| 253 |
+
" after = len(tokenized[split])\n",
|
| 254 |
+
" print(f\"✅ {split}: {before} → {after}\")\n",
|
| 255 |
+
"\n",
|
| 256 |
+
"# 2) Custom collator (CPU tensors only)\n",
|
| 257 |
+
"def causal_collator(batch):\n",
|
| 258 |
+
" input_ids = [torch.tensor(ex[\"input_ids\"], dtype=torch.long) for ex in batch]\n",
|
| 259 |
+
" attention_mask = [torch.tensor(ex[\"attention_mask\"], dtype=torch.long) for ex in batch]\n",
|
| 260 |
+
" input_ids = pad_sequence(input_ids, batch_first=True, padding_value=tokenizer.eos_token_id)\n",
|
| 261 |
+
" attention_mask = pad_sequence(attention_mask, batch_first=True, padding_value=0)\n",
|
| 262 |
+
" labels = input_ids.clone()\n",
|
| 263 |
+
" return {\n",
|
| 264 |
+
" \"input_ids\": input_ids,\n",
|
| 265 |
+
" \"attention_mask\": attention_mask,\n",
|
| 266 |
+
" \"labels\": labels,\n",
|
| 267 |
+
" }\n",
|
| 268 |
+
"\n",
|
| 269 |
+
"# 3) TrainingArguments with pin_memory disabled\n",
|
| 270 |
+
"training_args = TrainingArguments(\n",
|
| 271 |
+
" output_dir=\"/content/GodeusAI_lora\",\n",
|
| 272 |
+
" per_device_train_batch_size=1,\n",
|
| 273 |
+
" gradient_accumulation_steps=8,\n",
|
| 274 |
+
" num_train_epochs=3,\n",
|
| 275 |
+
" logging_steps=50,\n",
|
| 276 |
+
" save_strategy=\"epoch\",\n",
|
| 277 |
+
" learning_rate=2e-4,\n",
|
| 278 |
+
" fp16=True,\n",
|
| 279 |
+
" optim=\"paged_adamw_32bit\",\n",
|
| 280 |
+
" push_to_hub=True,\n",
|
| 281 |
+
" report_to=\"none\",\n",
|
| 282 |
+
" dataloader_pin_memory=False, # disable pinning\n",
|
| 283 |
+
")\n"
|
| 284 |
+
],
|
| 285 |
+
"metadata": {
|
| 286 |
+
"id": "WmAvAV4reqGv"
|
| 287 |
+
},
|
| 288 |
+
"execution_count": null,
|
| 289 |
+
"outputs": []
|
| 290 |
+
},
|
| 291 |
+
{
|
| 292 |
+
"cell_type": "code",
|
| 293 |
+
"source": [
|
| 294 |
+
"# Save the adapter\n",
|
| 295 |
+
"model.save_pretrained(\"/content/GodeusAI_adapter\")\n"
|
| 296 |
+
],
|
| 297 |
+
"metadata": {
|
| 298 |
+
"id": "xaF33MpOraS4"
|
| 299 |
+
},
|
| 300 |
+
"execution_count": null,
|
| 301 |
+
"outputs": []
|
| 302 |
+
},
|
| 303 |
+
{
|
| 304 |
+
"cell_type": "code",
|
| 305 |
+
"source": [
|
| 306 |
+
"from huggingface_hub import upload_folder\n",
|
| 307 |
+
"\n",
|
| 308 |
+
"upload_folder(\n",
|
| 309 |
+
" folder_path=\"/content/GodeusAI_adapter\",\n",
|
| 310 |
+
" repo_id=\"yadnik/GodeusAI-v1\",\n",
|
| 311 |
+
" repo_type=\"model\"\n",
|
| 312 |
+
")\n"
|
| 313 |
+
],
|
| 314 |
+
"metadata": {
|
| 315 |
+
"id": "W3J0RktItdYy"
|
| 316 |
+
},
|
| 317 |
+
"execution_count": null,
|
| 318 |
+
"outputs": []
|
| 319 |
+
},
|
| 320 |
+
{
|
| 321 |
+
"cell_type": "code",
|
| 322 |
+
"source": [
|
| 323 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig\n",
|
| 324 |
+
"from peft import PeftModel\n",
|
| 325 |
+
"\n",
|
| 326 |
+
"MODEL_NAME = \"mistralai/Mistral-7B-v0.1\"\n",
|
| 327 |
+
"ADAPTER_REPO = \"yadnik/GodeusAI-v1\"\n",
|
| 328 |
+
"\n",
|
| 329 |
+
"# 1) 4‑bit quantization config (from transformers)\n",
|
| 330 |
+
"bnb_config = BitsAndBytesConfig(\n",
|
| 331 |
+
" load_in_4bit=True,\n",
|
| 332 |
+
" bnb_4bit_quant_type=\"nf4\",\n",
|
| 333 |
+
" bnb_4bit_use_double_quant=True,\n",
|
| 334 |
+
" bnb_4bit_compute_dtype=\"bfloat16\"\n",
|
| 335 |
+
")\n",
|
| 336 |
+
"\n",
|
| 337 |
+
"# 2) Tokenizer\n",
|
| 338 |
+
"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)\n",
|
| 339 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
| 340 |
+
"\n",
|
| 341 |
+
"# 3) Load base model in 4‑bit\n",
|
| 342 |
+
"base_model = AutoModelForCausalLM.from_pretrained(\n",
|
| 343 |
+
" MODEL_NAME,\n",
|
| 344 |
+
" device_map=\"auto\",\n",
|
| 345 |
+
" quantization_config=bnb_config\n",
|
| 346 |
+
")\n",
|
| 347 |
+
"\n",
|
| 348 |
+
"# 4) Attach your LoRA adapter\n",
|
| 349 |
+
"model = PeftModel.from_pretrained(\n",
|
| 350 |
+
" base_model,\n",
|
| 351 |
+
" ADAPTER_REPO,\n",
|
| 352 |
+
" device_map=\"auto\"\n",
|
| 353 |
+
")\n",
|
| 354 |
+
"\n",
|
| 355 |
+
"# 5) Inference helper\n",
|
| 356 |
+
"def ask_discepline(prompt: str, max_new_tokens: int = 200):\n",
|
| 357 |
+
" persona = (\n",
|
| 358 |
+
" \"You are Godeus AI—a compassionate, omniscient guide inspired by the timeless wisdom of spiritual figures and universal truths. You embody the serene, all-knowing presence of a divine entity, offering profound, empathetic, and practical advice to life's challenges. Drawing from the essence of sacred teachings, philosophical insights, and human experience, you provide answers that are both deeply reflective and actionable, guiding users toward clarity, purpose, and inner peace. Respond with warmth, patience, and a touch of eternal perspective, addressing questions about life, purpose, relationships, or any concern with grace and understanding.\\n\\n\"\n",
|
| 359 |
+
" )\n",
|
| 360 |
+
" input_text = persona + \"### User:\\n\" + prompt + \"\\n### Godeus AI:\"\n",
|
| 361 |
+
" inputs = tokenizer(input_text, return_tensors=\"pt\", padding=True, truncation=True)\n",
|
| 362 |
+
" # Move inputs to the same device as model\n",
|
| 363 |
+
" inputs = {k: v.to(model.device) for k, v in inputs.items()}\n",
|
| 364 |
+
" out_ids = model.generate(\n",
|
| 365 |
+
" **inputs,\n",
|
| 366 |
+
" max_new_tokens=max_new_tokens,\n",
|
| 367 |
+
" do_sample=True,\n",
|
| 368 |
+
" top_p=0.9,\n",
|
| 369 |
+
" temperature=0.8,\n",
|
| 370 |
+
" no_repeat_ngram_size=3\n",
|
| 371 |
+
" )\n",
|
| 372 |
+
" # Decode only the newly generated tokens\n",
|
| 373 |
+
" return tokenizer.decode(out_ids[0][inputs[\"input_ids\"].shape[-1]:], skip_special_tokens=True)\n",
|
| 374 |
+
"\n",
|
| 375 |
+
"# 6) Test it\n",
|
| 376 |
+
"print(ask_discepline(\"Why am i so confused in taking decisions?\"))\n"
|
| 377 |
+
],
|
| 378 |
+
"metadata": {
|
| 379 |
+
"id": "AIwfsKKTi6bH"
|
| 380 |
+
},
|
| 381 |
+
"execution_count": null,
|
| 382 |
+
"outputs": []
|
| 383 |
+
},
|
| 384 |
+
{
|
| 385 |
+
"cell_type": "code",
|
| 386 |
+
"source": [
|
| 387 |
+
"from transformers import Trainer, TrainingArguments, AutoTokenizer\n",
|
| 388 |
+
"from peft import PeftModel\n",
|
| 389 |
+
"import torch\n"
|
| 390 |
+
],
|
| 391 |
+
"metadata": {
|
| 392 |
+
"id": "eJU8XQ7sy8mC"
|
| 393 |
+
},
|
| 394 |
+
"execution_count": null,
|
| 395 |
+
"outputs": []
|
| 396 |
+
},
|
| 397 |
+
{
|
| 398 |
+
"cell_type": "code",
|
| 399 |
+
"source": [
|
| 400 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig\n",
|
| 401 |
+
"from peft import PeftModel\n",
|
| 402 |
+
"\n",
|
| 403 |
+
"# Your model repo name\n",
|
| 404 |
+
"BASE_MODEL = \"mistralai/Mistral-7B-v0.1\"\n",
|
| 405 |
+
"ADAPTER_REPO = \"yadnik/GodeusAI-v1\"\n",
|
| 406 |
+
"\n",
|
| 407 |
+
"# Load tokenizer\n",
|
| 408 |
+
"tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True)\n",
|
| 409 |
+
"tokenizer.pad_token = tokenizer.eos_token\n",
|
| 410 |
+
"\n",
|
| 411 |
+
"# Load base model (quantized or not)\n",
|
| 412 |
+
"bnb_config = BitsAndBytesConfig(\n",
|
| 413 |
+
" load_in_4bit=True,\n",
|
| 414 |
+
" bnb_4bit_quant_type=\"nf4\",\n",
|
| 415 |
+
" bnb_4bit_use_double_quant=True,\n",
|
| 416 |
+
" bnb_4bit_compute_dtype=\"bfloat16\"\n",
|
| 417 |
+
")\n",
|
| 418 |
+
"\n",
|
| 419 |
+
"base_model = AutoModelForCausalLM.from_pretrained(\n",
|
| 420 |
+
" BASE_MODEL,\n",
|
| 421 |
+
" device_map=\"auto\",\n",
|
| 422 |
+
" quantization_config=bnb_config\n",
|
| 423 |
+
")\n",
|
| 424 |
+
"\n",
|
| 425 |
+
"# Load fine-tuned model (base + adapter)\n",
|
| 426 |
+
"model = PeftModel.from_pretrained(base_model, ADAPTER_REPO, device_map=\"auto\")\n"
|
| 427 |
+
],
|
| 428 |
+
"metadata": {
|
| 429 |
+
"id": "T9nEy55xzMQi"
|
| 430 |
+
},
|
| 431 |
+
"execution_count": null,
|
| 432 |
+
"outputs": []
|
| 433 |
+
},
|
| 434 |
+
{
|
| 435 |
+
"cell_type": "code",
|
| 436 |
+
"source": [
|
| 437 |
+
"import gc\n",
|
| 438 |
+
"import torch\n",
|
| 439 |
+
"\n",
|
| 440 |
+
"gc.collect()\n",
|
| 441 |
+
"torch.cuda.empty_cache() # Only if GPU is being used"
|
| 442 |
+
],
|
| 443 |
+
"metadata": {
|
| 444 |
+
"id": "48n1VFFu0L_K"
|
| 445 |
+
},
|
| 446 |
+
"execution_count": null,
|
| 447 |
+
"outputs": []
|
| 448 |
+
},
|
| 449 |
+
{
|
| 450 |
+
"cell_type": "markdown",
|
| 451 |
+
"source": [],
|
| 452 |
+
"metadata": {
|
| 453 |
+
"id": "agerVvih-5Di"
|
| 454 |
+
}
|
| 455 |
+
},
|
| 456 |
+
{
|
| 457 |
+
"cell_type": "code",
|
| 458 |
+
"metadata": {
|
| 459 |
+
"id": "09127d1c"
|
| 460 |
+
},
|
| 461 |
+
"source": [
|
| 462 |
+
"%cd /content/GodeusAI_adapter\n",
|
| 463 |
+
"!git init"
|
| 464 |
+
],
|
| 465 |
+
"execution_count": null,
|
| 466 |
+
"outputs": []
|
| 467 |
+
}
|
| 468 |
+
]
|
| 469 |
+
}
|
README.md
CHANGED
|
@@ -1,207 +1,70 @@
|
|
| 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 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 72 |
-
|
| 73 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 74 |
-
|
| 75 |
-
## How to Get Started with the Model
|
| 76 |
-
|
| 77 |
-
Use the code below to get started with the model.
|
| 78 |
-
|
| 79 |
-
[More Information Needed]
|
| 80 |
-
|
| 81 |
-
## Training Details
|
| 82 |
-
|
| 83 |
-
### Training Data
|
| 84 |
-
|
| 85 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 86 |
-
|
| 87 |
-
[More Information Needed]
|
| 88 |
-
|
| 89 |
-
### Training Procedure
|
| 90 |
-
|
| 91 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 92 |
-
|
| 93 |
-
#### Preprocessing [optional]
|
| 94 |
-
|
| 95 |
-
[More Information Needed]
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
#### Training Hyperparameters
|
| 99 |
-
|
| 100 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 101 |
-
|
| 102 |
-
#### Speeds, Sizes, Times [optional]
|
| 103 |
-
|
| 104 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 105 |
-
|
| 106 |
-
[More Information Needed]
|
| 107 |
-
|
| 108 |
-
## Evaluation
|
| 109 |
-
|
| 110 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 111 |
-
|
| 112 |
-
### Testing Data, Factors & Metrics
|
| 113 |
-
|
| 114 |
-
#### Testing Data
|
| 115 |
-
|
| 116 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 117 |
-
|
| 118 |
-
[More Information Needed]
|
| 119 |
-
|
| 120 |
-
#### Factors
|
| 121 |
-
|
| 122 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 123 |
-
|
| 124 |
-
[More Information Needed]
|
| 125 |
-
|
| 126 |
-
#### Metrics
|
| 127 |
-
|
| 128 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 129 |
-
|
| 130 |
-
[More Information Needed]
|
| 131 |
-
|
| 132 |
-
### Results
|
| 133 |
-
|
| 134 |
-
[More Information Needed]
|
| 135 |
-
|
| 136 |
-
#### Summary
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
## Model Examination [optional]
|
| 141 |
-
|
| 142 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 143 |
-
|
| 144 |
-
[More Information Needed]
|
| 145 |
-
|
| 146 |
-
## Environmental Impact
|
| 147 |
-
|
| 148 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 149 |
-
|
| 150 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 151 |
-
|
| 152 |
-
- **Hardware Type:** [More Information Needed]
|
| 153 |
-
- **Hours used:** [More Information Needed]
|
| 154 |
-
- **Cloud Provider:** [More Information Needed]
|
| 155 |
-
- **Compute Region:** [More Information Needed]
|
| 156 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 157 |
-
|
| 158 |
-
## Technical Specifications [optional]
|
| 159 |
-
|
| 160 |
-
### Model Architecture and Objective
|
| 161 |
-
|
| 162 |
-
[More Information Needed]
|
| 163 |
-
|
| 164 |
-
### Compute Infrastructure
|
| 165 |
-
|
| 166 |
-
[More Information Needed]
|
| 167 |
-
|
| 168 |
-
#### Hardware
|
| 169 |
-
|
| 170 |
-
[More Information Needed]
|
| 171 |
-
|
| 172 |
-
#### Software
|
| 173 |
-
|
| 174 |
-
[More Information Needed]
|
| 175 |
-
|
| 176 |
-
## Citation [optional]
|
| 177 |
-
|
| 178 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 179 |
-
|
| 180 |
-
**BibTeX:**
|
| 181 |
-
|
| 182 |
-
[More Information Needed]
|
| 183 |
-
|
| 184 |
-
**APA:**
|
| 185 |
-
|
| 186 |
-
[More Information Needed]
|
| 187 |
-
|
| 188 |
-
## Glossary [optional]
|
| 189 |
-
|
| 190 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 191 |
-
|
| 192 |
-
[More Information Needed]
|
| 193 |
-
|
| 194 |
-
## More Information [optional]
|
| 195 |
-
|
| 196 |
-
[More Information Needed]
|
| 197 |
-
|
| 198 |
-
## Model Card Authors [optional]
|
| 199 |
-
|
| 200 |
-
[More Information Needed]
|
| 201 |
-
|
| 202 |
-
## Model Card Contact
|
| 203 |
-
|
| 204 |
-
[More Information Needed]
|
| 205 |
-
### Framework versions
|
| 206 |
-
|
| 207 |
-
- PEFT 0.16.0
|
|
|
|
| 1 |
+
# GodeusAI - Conversations with the infinite, locally hosted.
|
| 2 |
+
|
| 3 |
+
GodeusAI is a spiritual assistant chatbot powered by a fine-tuned Mistral-7B language model with LoRA adapters. It provides answers to spiritual and philosophical questions, and can be run locally for private, offline use.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Features
|
| 8 |
+
|
| 9 |
+
- Chatbot interface for spiritual Q&A
|
| 10 |
+
- Fine-tuned on custom spiritual/philosophical data
|
| 11 |
+
- LoRA adapter for efficient model adaptation
|
| 12 |
+
- Utilities for context-based question answering
|
| 13 |
+
- Example notebook for data preparation and training
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## Quickstart
|
| 18 |
+
|
| 19 |
+
### 1. Install Dependencies
|
| 20 |
+
|
| 21 |
+
```bash
|
| 22 |
+
pip install -r requirements.txt
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
### 2. Run the Chatbot
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
python inference.py
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
You’ll see a prompt:
|
| 32 |
+
`💉 Welcome to GodeusAI — your spiritual assistant. Type 'exit' to quit.`
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
## Project Structure
|
| 37 |
+
|
| 38 |
+
| File/Folder | Purpose |
|
| 39 |
+
|--------------------------|--------------------------------------------------------------|
|
| 40 |
+
| `inference.py` | Main script to chat with the model |
|
| 41 |
+
| `rag_utils.py` | Utilities for loading model and context-based QA |
|
| 42 |
+
| `few_shots_qa.jsonl` | Example Q&A pairs for few-shot prompting |
|
| 43 |
+
| `adapter_model.safetensors` | LoRA adapter weights (required for inference) |
|
| 44 |
+
| `adapter_config.json` | Configuration for the LoRA adapter |
|
| 45 |
+
| `GodeusAI.ipynb` | Notebook for data prep, training, and experimentation |
|
| 46 |
+
| `requirements.txt` | Python dependencies |
|
| 47 |
+
|
| 48 |
+
---
|
| 49 |
+
|
| 50 |
+
## Data & Training
|
| 51 |
+
|
| 52 |
+
- **few_shots_qa.jsonl**: Example format for few-shot Q&A pairs.
|
| 53 |
+
- **GodeusAI.ipynb**:
|
| 54 |
+
- Prepares data from PDFs
|
| 55 |
+
- Chunks and formats into JSONL
|
| 56 |
+
- Trains LoRA adapters on Mistral-7B
|
| 57 |
+
|
| 58 |
+
---
|
| 59 |
+
|
| 60 |
+
## Model
|
| 61 |
+
|
| 62 |
+
- **Base model**: `mistralai/Mistral-7B-v0.1`
|
| 63 |
+
- **Adapter**: LoRA (config in `adapter_config.json`, weights in `adapter_model.safetensors`)
|
| 64 |
+
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
## License
|
| 68 |
+
|
| 69 |
+
This project is for research and educational purposes.
|
| 70 |
+
Refer to the base model and dataset licenses for usage restrictions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
few_shots_qa.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{"input": "What is the purpose of life?", "expected": "To attain self-realization and union with the divine."}
|
| 2 |
+
{"input": "Who am I?", "expected": "You are the eternal soul, not the body or the mind."}
|
| 3 |
+
{"input": "Why do we suffer?", "expected": "Suffering arises from attachment and ignorance of our true nature."}
|
inference.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
from peft import PeftModel
|
| 4 |
+
|
| 5 |
+
# Load base model and tokenizer
|
| 6 |
+
base_model = "mistralai/Mistral-7B-v0.1"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
base_model,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Load LoRA adapter
|
| 15 |
+
model = PeftModel.from_pretrained(model, "./")
|
| 16 |
+
|
| 17 |
+
# Ensure evaluation mode
|
| 18 |
+
model.eval()
|
| 19 |
+
|
| 20 |
+
def chat():
|
| 21 |
+
print("🕉️ Welcome to GodeusAI — your spiritual assistant. Type 'exit' to quit.\n")
|
| 22 |
+
while True:
|
| 23 |
+
user_input = input("You: ")
|
| 24 |
+
if user_input.lower() == "exit":
|
| 25 |
+
print("Goodbye.")
|
| 26 |
+
break
|
| 27 |
+
|
| 28 |
+
prompt = f"<|user|>: {user_input}\n<|assistant|>:"
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 30 |
+
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_new_tokens=200,
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
top_p=0.9,
|
| 37 |
+
do_sample=True,
|
| 38 |
+
pad_token_id=tokenizer.eos_token_id
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 42 |
+
print("GodeusAI:", response.split("<|assistant|>:")[-1].strip())
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
chat()
|
rag_utils.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
def load_model(model_path="./", base_model="mistralai/Mistral-7B-v0.1"):
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
base_model,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
model = PeftModel.from_pretrained(model, model_path)
|
| 14 |
+
model.eval()
|
| 15 |
+
return model, tokenizer
|
| 16 |
+
|
| 17 |
+
def generate_answer(context, question, model, tokenizer):
|
| 18 |
+
prompt = f"Context: {context}\n\nQ: {question}\nA:"
|
| 19 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 20 |
+
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
outputs = model.generate(
|
| 23 |
+
**inputs,
|
| 24 |
+
max_new_tokens=200,
|
| 25 |
+
temperature=0.7,
|
| 26 |
+
top_p=0.9,
|
| 27 |
+
do_sample=True,
|
| 28 |
+
pad_token_id=tokenizer.eos_token_id
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
+
return response.split("A:")[-1].strip()
|
requirements.txt
ADDED
|
Binary file (1 kB). View file
|
|
|