A ~9M parameter LLM that talks like a small fish.
Python
Split into train + use notebooks, add interactive chat
Arman Hossain96830cbfa7dc
diff --git a/README.md b/README.mdindex 3a989f2..18511c6 100644--- a/README.md+++ b/README.md@@ -8,7 +8,8 @@<p align="center"><a href="https://huggingface.co/datasets/arman-bd/guppylm-60k-generic"><img src="https://img.shields.io/badge/π€_Dataset-guppylm--60k-blue" alt="Dataset"/></a> <a href="https://huggingface.co/arman-bd/guppylm-9M"><img src="https://img.shields.io/badge/π€_Model-guppylm--9M-orange" alt="Model"/></a> - <a href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/guppy_colab.ipynb"><img src="https://img.shields.io/badge/Open_in-Colab-F9AB00?logo=googlecolab" alt="Colab"/></a> + <a href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/train_guppylm.ipynb"><img src="https://img.shields.io/badge/Train_in-Colab-F9AB00?logo=googlecolab" alt="Train"/></a> + <a href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/use_guppylm.ipynb"><img src="https://img.shields.io/badge/Chat_in-Colab-F9AB00?logo=googlecolab" alt="Chat"/></a> <a href="https://github.com/arman-bd/guppylm/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-green" alt="License"/></a></p>@@ -88,12 +89,19 @@ Guppy:## Quick Start-### Train on Colab+### Chat with Guppy (no training needed)-1. Upload [`guppy_colab.ipynb`](guppy_colab.ipynb) to [Google Colab](https://colab.research.google.com)-2. Set runtime to **T4 GPU**-3. **Run all cells** β downloads dataset, trains tokenizer, trains model, tests it-4. Download the trained model (~35 MB)+[](https://colab.research.google.com/github/arman-bd/guppylm/blob/main/use_guppylm.ipynb)++Downloads the pre-trained model from HuggingFace and lets you chat. Just run all cells.++### Train your own++[](https://colab.research.google.com/github/arman-bd/guppylm/blob/main/train_guppylm.ipynb)++1. Set runtime to **T4 GPU**+2. **Run all cells** β downloads dataset, trains tokenizer, trains model, tests it+3. Upload to HuggingFace or download locally### Chat locallydiff --git a/tools/dataset_card.md b/tools/dataset_card.mdindex 5317219..a5f6fa4 100644--- a/tools/dataset_card.md+++ b/tools/dataset_card.md@@ -21,7 +21,7 @@ pretty_name: GuppyLM Chat<p align="center"><a href="https://github.com/arman-bd/guppylm"><img src="https://img.shields.io/badge/GitHub-guppylm-181717?logo=github" alt="GitHub"/></a> <a href="https://huggingface.co/arman-bd/guppylm-9M"><img src="https://img.shields.io/badge/π€_Model-guppylm--9M-orange" alt="Model"/></a> - <a href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/guppy_colab.ipynb"><img src="https://img.shields.io/badge/Open_in-Colab-F9AB00?logo=googlecolab" alt="Colab"/></a>+ <a href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/train_guppylm.ipynb"><img src="https://img.shields.io/badge/Open_in-Colab-F9AB00?logo=googlecolab" alt="Colab"/></a></p># GuppyLM Chat Datasetdiff --git a/tools/make_colab.py b/tools/make_colab.pyindex fe3cdc6..e2a99e4 100644--- a/tools/make_colab.py+++ b/tools/make_colab.py@@ -83,7 +83,7 @@ def build():))cells.append(code(- "!pip install -q torch tokenizers tqdm numpy datasets\n"+ "!pip install -q torch tokenizers tqdm numpy datasets huggingface_hub\n""\n""import torch\n""print(f'PyTorch {torch.__version__}')\n"@@ -287,21 +287,52 @@ def build():))# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ- # 7. DOWNLOAD+ # 7. UPLOAD TO HUGGINGFACE# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββcells.append(md(- "## 7. Download\n"+ "## 7. Upload to HuggingFace\n""\n"- "Package the trained model for local use.\n"+ "Push the trained model to HuggingFace Hub.\n"+ "Set your token and repo below."+ ))++ cells.append(code(+ "from huggingface_hub import HfApi, login\n"+ "import os\n""\n"- "```python\n"- "# Run locally after downloading:\n"- "from inference import GuppyInference\n"- "engine = GuppyInference('checkpoints/best_model.pt', 'data/tokenizer.json')\n"- "r = engine.chat_completion([{'role': 'user', 'content': 'hi guppy'}])\n"- "print(r['choices'][0]['message']['content'])\n"- "```"+ "HF_TOKEN = '' # Paste your HF token here\n"+ "HF_REPO = 'arman-bd/guppylm-9M' # Change to your repo\n"+ "\n"+ "if not HF_TOKEN:\n"+ " print('Set HF_TOKEN above to upload. Skipping.')\n"+ "else:\n"+ " login(token=HF_TOKEN)\n"+ " api = HfApi()\n"+ " api.create_repo(HF_REPO, exist_ok=True)\n"+ "\n"+ " # Upload model checkpoint, tokenizer, and source files\n"+ " for path in ['checkpoints/best_model.pt', 'checkpoints/config.json',\n"+ " 'data/tokenizer.json', 'model.py', 'config.py', 'inference.py']:\n"+ " if os.path.exists(path):\n"+ " api.upload_file(\n"+ " path_or_fileobj=path,\n"+ " path_in_repo=path,\n"+ " repo_id=HF_REPO,\n"+ " )\n"+ " print(f' Uploaded {path}')\n"+ "\n"+ " print(f'\\nDone! https://huggingface.co/{HF_REPO}')"+ ))++ # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+ # 8. DOWNLOAD+ # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ++ cells.append(md(+ "## 8. Download\n"+ "\n"+ "Or download the model locally as a tar.gz."))cells.append(code(@@ -330,7 +361,7 @@ def build():return {"nbformat": 4, "nbformat_minor": 0,"metadata": {- "colab": {"provenance": [], "gpuType": "T4", "name": "GuppyLM β Train a Fish"},+ "colab": {"provenance": [], "gpuType": "T4", "name": "GuppyLM β Train"},"kernelspec": {"name": "python3", "display_name": "Python 3"},"language_info": {"name": "python"},"accelerator": "GPU",@@ -339,11 +370,161 @@ def build():}-if __name__ == "__main__":- nb = build()- out = os.path.join(PROJECT_ROOT, "guppy_colab.ipynb")+def build_use():+ """Build the use_guppylm notebook β download model from HF and chat."""+ cells = []++ cells.append(md(+ "# GuppyLM β Chat with a Fish\n"+ "\n"+ "Download the pre-trained GuppyLM model from HuggingFace and chat with it.\n"+ "No training needed β just run all cells.\n"+ "\n"+ "**Model:** [arman-bd/guppylm-9M](https://huggingface.co/arman-bd/guppylm-9M) (8.7M params)"+ ))++ # ββ Setup ββββββββββββββββββββββββββββββββββββββββββββββββββββββ++ cells.append(md("## 1. Setup"))++ cells.append(code(+ "!pip install -q torch tokenizers huggingface_hub\n"+ "\n"+ "import torch\n"+ "print(f'PyTorch {torch.__version__}')\n"+ "print(f'CUDA: {torch.cuda.is_available()}')"+ ))++ cells.append(code(+ "import os, shutil\n"+ "if os.path.exists('/content/guppy'):\n"+ " shutil.rmtree('/content/guppy')\n"+ "os.makedirs('/content/guppy')\n"+ "os.chdir('/content/guppy')\n"+ "print(f'Working dir: {os.getcwd()}')"+ ))++ # ββ Download model βββββββββββββββββββββββββββββββββββββββββββββ++ cells.append(md(+ "## 2. Download Model\n"+ "\n"+ "Download the pre-trained model, tokenizer, and inference code from HuggingFace."+ ))++ cells.append(code(+ "from huggingface_hub import hf_hub_download\n"+ "import os\n"+ "\n"+ "HF_REPO = 'arman-bd/guppylm-9M'\n"+ "\n"+ "files = [\n"+ " ('checkpoints/best_model.pt', 'checkpoints'),\n"+ " ('checkpoints/config.json', 'checkpoints'),\n"+ " ('data/tokenizer.json', 'data'),\n"+ " ('model.py', '.'),\n"+ " ('config.py', '.'),\n"+ " ('inference.py', '.'),\n"+ "]\n"+ "\n"+ "for filename, subdir in files:\n"+ " os.makedirs(subdir, exist_ok=True)\n"+ " hf_hub_download(\n"+ " repo_id=HF_REPO,\n"+ " filename=filename,\n"+ " local_dir='.',\n"+ " )\n"+ " print(f' Downloaded {filename}')\n"+ "\n"+ "print(f'\\nModel ready from {HF_REPO}')"+ ))++ # ββ Chat βββββββββββββββββββββββββββββββββββββββββββββββββββββββ++ cells.append(md(+ "## 3. Chat\n"+ "\n"+ "Talk to Guppy. Each message is independent (single-turn)."+ ))++ cells.append(code(+ "from inference import GuppyInference\n"+ "import torch\n"+ "\n"+ "engine = GuppyInference(\n"+ " 'checkpoints/best_model.pt', 'data/tokenizer.json',\n"+ " device='cuda' if torch.cuda.is_available() else 'cpu'\n"+ ")\n"+ "\n"+ "def chat(prompt):\n"+ " r = engine.chat_completion([{'role': 'user', 'content': prompt}], max_tokens=64)\n"+ " return r['choices'][0]['message'].get('content', '').strip()"+ ))++ cells.append(code(+ "# Try different topics\n"+ "tests = [\n"+ " 'hi guppy',\n"+ " 'are you hungry',\n"+ " 'it is really hot today',\n"+ " 'do you like bubbles',\n"+ " 'what is the meaning of life',\n"+ " 'tell me a joke',\n"+ " 'the cat is looking at you',\n"+ " 'do you love me',\n"+ " 'what is the internet',\n"+ " 'goodnight guppy',\n"+ "]\n"+ "\n"+ "for prompt in tests:\n"+ " reply = chat(prompt)\n"+ " print(f'You> {prompt}')\n"+ " print(f'Guppy> {reply}')\n"+ " print()"+ ))++ cells.append(md(+ "## 4. Interactive Chat\n"+ "\n"+ "Type your own messages. Type `quit` to stop."+ ))++ cells.append(code(+ "print('Chat with Guppy (type quit to stop)')\n"+ "print('=' * 40)\n"+ "while True:\n"+ " try:\n"+ " prompt = input('You> ').strip()\n"+ " except (KeyboardInterrupt, EOFError):\n"+ " break\n"+ " if not prompt or prompt.lower() in ('quit', 'exit', 'q'):\n"+ " print('Guppy> bye. i will continue being a fish.')\n"+ " break\n"+ " reply = chat(prompt)\n"+ " print(f'Guppy> {reply}')\n"+ " print()"+ ))++ return {+ "nbformat": 4, "nbformat_minor": 0,+ "metadata": {+ "colab": {"provenance": [], "name": "GuppyLM β Chat"},+ "kernelspec": {"name": "python3", "display_name": "Python 3"},+ "language_info": {"name": "python"},+ },+ "cells": cells,+ }+++def write_notebook(nb, filename):+ out = os.path.join(PROJECT_ROOT, filename)with open(out, "w") as f:json.dump(nb, f, indent=1)n = len(nb["cells"])sz = os.path.getsize(out) / 1024print(f"Generated {out}: {n} cells, {sz:.1f} KB")+++if __name__ == "__main__":+ write_notebook(build(), "train_guppylm.ipynb")+ write_notebook(build_use(), "use_guppylm.ipynb")diff --git a/tools/model_card.md b/tools/model_card.mdindex 22693ad..56e623e 100644--- a/tools/model_card.md+++ b/tools/model_card.md@@ -18,7 +18,7 @@ pipeline_tag: text-generation<p align="center"><a href="https://github.com/arman-bd/guppylm"><img src="https://img.shields.io/badge/GitHub-guppylm-181717?logo=github" alt="GitHub"/></a> <a href="https://huggingface.co/datasets/arman-bd/guppylm-60k-generic"><img src="https://img.shields.io/badge/π€_Dataset-guppylm--60k-blue" alt="Dataset"/></a> - <a href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/guppy_colab.ipynb"><img src="https://img.shields.io/badge/Open_in-Colab-F9AB00?logo=googlecolab" alt="Colab"/></a>+ <a href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/use_guppylm.ipynb"><img src="https://img.shields.io/badge/Open_in-Colab-F9AB00?logo=googlecolab" alt="Colab"/></a></p># GuppyLM β 9M Parameter Fish Chat Modeldiff --git a/guppy_colab.ipynb b/train_guppylm.ipynbsimilarity index 54%rename from guppy_colab.ipynbrename to train_guppylm.ipynbindex 17dc9c4..b3d7600 100644--- a/guppy_colab.ipynb+++ b/train_guppylm.ipynb@@ -1,10 +1,27 @@{+ "nbformat": 4,+ "nbformat_minor": 0,+ "metadata": {+ "colab": {+ "provenance": [],+ "gpuType": "T4",+ "name": "GuppyLM \u2014 Train"+ },+ "kernelspec": {+ "name": "python3",+ "display_name": "Python 3"+ },+ "language_info": {+ "name": "python"+ },+ "accelerator": "GPU"+ },"cells": [{"cell_type": "markdown","metadata": {},"source": [- "# GuppyLM β Your Friendly Fish\n",+ "# GuppyLM \u2014 Your Friendly Fish\n","\n","Train a ~9M parameter LLM that talks like a small fish.\n","\n",@@ -32,52 +49,34 @@},{"cell_type": "code",- "execution_count": 1,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "PyTorch 2.10.0+cu128\n",- "CUDA: True\n",- "GPU: Tesla T4\n"- ]- }- ],"source": [- "!pip install -q torch tokenizers tqdm numpy datasets\n",+ "!pip install -q torch tokenizers tqdm numpy datasets huggingface_hub\n","\n","import torch\n","print(f'PyTorch {torch.__version__}')\n","print(f'CUDA: {torch.cuda.is_available()}')\n","if torch.cuda.is_available():\n"," print(f'GPU: {torch.cuda.get_device_name(0)}')"- ]+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "code",- "execution_count": 2,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Working dir: /content/guppy\n"- ]- }- ],"source": ["import os, shutil\n","\n",- "# Start fresh β removes stale files from previous runs\n",+ "# Start fresh \u2014 removes stale files from previous runs\n","if os.path.exists('/content/guppy'):\n"," shutil.rmtree('/content/guppy')\n","os.makedirs('/content/guppy')\n","os.chdir('/content/guppy')\n","print(f'Working dir: {os.getcwd()}')"- ]+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "markdown",@@ -86,26 +85,16 @@"## 2. Source Files\n","\n","Write the model code to disk. These are the only files needed:\n",- "- `config.py` β model and training hyperparameters\n",- "- `model.py` β transformer architecture\n",- "- `dataset.py` β data loading and batching\n",- "- `train.py` β training loop\n",- "- `inference.py` β chat interface"+ "- `config.py` \u2014 model and training hyperparameters\n",+ "- `model.py` \u2014 transformer architecture\n",+ "- `dataset.py` \u2014 data loading and batching\n",+ "- `train.py` \u2014 training loop\n",+ "- `inference.py` \u2014 chat interface"]},{"cell_type": "code",- "execution_count": 3,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Writing config.py\n"- ]- }- ],"source": ["%%writefile config.py\n","\"\"\"GuppyLM configuration.\"\"\"\n",@@ -143,26 +132,19 @@" device: str = \"auto\"\n"," seed: int = 42\n"," data_dir: str = \"data\"\n",- " output_dir: str = \"checkpoints\"\n"- ]+ " output_dir: str = \"checkpoints\"\n",+ ""+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "code",- "execution_count": 4,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Writing model.py\n"- ]- }- ],"source": ["%%writefile model.py\n","\"\"\"\n",- "GuppyLM β a tiny fish brain.\n",+ "GuppyLM \u2014 a tiny fish brain.\n","\n","Vanilla transformer: multi-head attention, ReLU FFN, LayerNorm, learned positional embeddings.\n","No GQA, no SwiGLU, no parallel residual, no RoPE. As simple as it gets.\n",@@ -289,22 +271,15 @@"\n"," def param_summary(self):\n"," total, _ = self.param_count()\n",- " return f\"GuppyLM: {total:,} params ({total/1e6:.1f}M)\"\n"- ]+ " return f\"GuppyLM: {total:,} params ({total/1e6:.1f}M)\"\n",+ ""+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "code",- "execution_count": 5,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Writing dataset.py\n"- ]- }- ],"source": ["%%writefile dataset.py\n","\"\"\"GuppyLM dataset loading.\"\"\"\n",@@ -357,22 +332,15 @@" return DataLoader(\n"," dataset, batch_size=batch_size, shuffle=shuffle,\n"," collate_fn=collate_fn, num_workers=0, pin_memory=True,\n",- " )\n"- ]+ " )\n",+ ""+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "code",- "execution_count": 6,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Writing train.py\n"- ]- }- ],"source": ["%%writefile train.py\n","\"\"\"GuppyLM training loop.\"\"\"\n",@@ -535,25 +503,18 @@"\n","\n","if __name__ == \"__main__\":\n",- " train()\n"- ]+ " train()\n",+ ""+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "code",- "execution_count": 7,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Writing inference.py\n"- ]- }- ],"source": ["%%writefile inference.py\n",- "\"\"\"GuppyLM inference β simple chat.\"\"\"\n",+ "\"\"\"GuppyLM inference \u2014 simple chat.\"\"\"\n","\n","import json\n","import time\n",@@ -584,7 +545,7 @@"\n"," def chat_completion(self, messages, temperature=0.7, max_tokens=64,\n"," top_k=50, **kwargs):\n",- " \"\"\"Chat completion β takes messages, returns response.\"\"\"\n",+ " \"\"\"Chat completion \u2014 takes messages, returns response.\"\"\"\n"," prompt = self._format_prompt(messages)\n"," input_ids = self.tokenizer.encode(prompt).ids\n"," prompt_tokens = len(input_ids)\n",@@ -592,7 +553,7 @@"\n"," output_t, _ = self.model.generate(input_t, max_tokens, temperature, top_k)\n"," output_text = self.tokenizer.decode(output_t[0].tolist()[prompt_tokens:])\n",- " # Truncate at first <|im_end|> β don't let the model leak into the next turn\n",+ " # Truncate at first <|im_end|> \u2014 don't let the model leak into the next turn\n"," if \"<|im_end|>\" in output_text:\n"," output_text = output_text.split(\"<|im_end|>\")[0]\n"," # Also strip any <|im_start|> fragments\n",@@ -642,8 +603,11 @@"\n","\n","if __name__ == \"__main__\":\n",- " main()\n"- ]+ " main()\n",+ ""+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "markdown",@@ -667,126 +631,18 @@},{"cell_type": "code",- "execution_count": 8,"metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "/usr/local/lib/python3.12/dist-packages/huggingface_hub/utils/_auth.py:104: UserWarning: \n",- "Error while fetching `HF_TOKEN` secret value from your vault: 'Requesting secret HF_TOKEN timed out. Secrets can only be fetched when running from the Colab UI.'.\n",- "You are not authenticated with the Hugging Face Hub in this notebook.\n",- "If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).\n",- " warnings.warn(\n"- ]- },- {- "data": {- "application/vnd.jupyter.widget-view+json": {- "model_id": "e9ab300612fd4b9b8cef97a653463677",- "version_major": 2,- "version_minor": 0- },- "text/plain": [- "README.md: 0.00B [00:00, ?B/s]"- ]- },- "metadata": {},- "output_type": "display_data"- },- {- "name": "stderr",- "output_type": "stream",- "text": [- "Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n",- "WARNING:huggingface_hub.utils._http:Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n"- ]- },- {- "data": {- "application/vnd.jupyter.widget-view+json": {- "model_id": "00f09b71d216412089184ac027fec981",- "version_major": 2,- "version_minor": 0- },- "text/plain": [- "data/train-00000-of-00001.parquet: 0%| | 0.00/557k [00:00<?, ?B/s]"- ]- },- "metadata": {},- "output_type": "display_data"- },- {- "data": {- "application/vnd.jupyter.widget-view+json": {- "model_id": "063737a546b9414da8c2bac2e7621692",- "version_major": 2,- "version_minor": 0- },- "text/plain": [- "data/test-00000-of-00001.parquet: 0%| | 0.00/57.9k [00:00<?, ?B/s]"- ]- },- "metadata": {},- "output_type": "display_data"- },- {- "data": {- "application/vnd.jupyter.widget-view+json": {- "model_id": "aa23e29731c949d2b819fa1967ef15e6",- "version_major": 2,- "version_minor": 0- },- "text/plain": [- "Generating train split: 0%| | 0/57000 [00:00<?, ? examples/s]"- ]- },- "metadata": {},- "output_type": "display_data"- },- {- "data": {- "application/vnd.jupyter.widget-view+json": {- "model_id": "ad886029d1084ca7b60f234e96a77d9d",- "version_major": 2,- "version_minor": 0- },- "text/plain": [- "Generating test split: 0%| | 0/3000 [00:00<?, ? examples/s]"- ]- },- "metadata": {},- "output_type": "display_data"- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "Downloaded: 57,000 train, 3,000 test samples\n",- " data/train.jsonl: 57,000 samples\n",- " data/eval.jsonl: 3,000 samples\n",- " Tokenizer: 2418 tokens\n",- "\n",- "Sample (temp_cold):\n",- "<|im_start|>user\n",- "i need a heater<|im_end|>\n",- "<|im_start|>assistant\n",- "the water feels cool today. i just hide and wait.<|im_end|>\n"- ]- }- ],"source": ["import json, os\n","from datasets import load_dataset\n","from tokenizers import Tokenizer, models, trainers, pre_tokenizers, decoders, processors\n","\n",- "# ββ Download from HuggingFace ββ\n",+ "# \u2500\u2500 Download from HuggingFace \u2500\u2500\n","HF_DATASET = 'arman-bd/guppylm-60k-generic'\n","ds = load_dataset(HF_DATASET)\n","print(f'Downloaded: {len(ds[\"train\"]):,} train, {len(ds[\"test\"]):,} test samples')\n","\n",- "# ββ Format into ChatML and save as JSONL ββ\n",+ "# \u2500\u2500 Format into ChatML and save as JSONL \u2500\u2500\n","os.makedirs('data', exist_ok=True)\n","texts = []\n","\n",@@ -801,7 +657,7 @@" texts.append(text)\n"," print(f' {path}: {len(ds[split]):,} samples')\n","\n",- "# ββ Train BPE tokenizer on the data ββ\n",+ "# \u2500\u2500 Train BPE tokenizer on the data \u2500\u2500\n","tokenizer = Tokenizer(models.BPE())\n","tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False)\n","tokenizer.decoder = decoders.ByteLevel()\n",@@ -817,11 +673,13 @@"tokenizer.save('data/tokenizer.json')\n","print(f' Tokenizer: {tokenizer.get_vocab_size()} tokens')\n","\n",- "# ββ Preview ββ\n",+ "# \u2500\u2500 Preview \u2500\u2500\n","with open('data/train.jsonl') as f:\n"," sample = json.loads(f.readline())\n","print(f'\\nSample ({sample[\"category\"]}):\\n{sample[\"text\"]}')"- ]+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "markdown",@@ -829,25 +687,12 @@"source": ["## 4. Verify Architecture\n","\n",- "Quick sanity check β build the model, print param count, run a dummy forward pass."+ "Quick sanity check \u2014 build the model, print param count, run a dummy forward pass."]},{"cell_type": "code",- "execution_count": 9,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "GuppyLM: 8,726,016 params (8.7M)\n",- " Layers: 6, Heads: 6, FFN: 768\n",- " Vocab: 4096, Max seq: 128\n",- " Forward pass: torch.Size([2, 32]) -> torch.Size([2, 32, 4096]) OK\n"- ]- }- ],"source": ["from config import GuppyConfig\n","from model import GuppyLM\n",@@ -864,7 +709,9 @@"logits, _ = model(x)\n","print(f' Forward pass: {x.shape} -> {logits.shape} OK')\n","del model"- ]+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "markdown",@@ -883,220 +730,13 @@},{"cell_type": "code",- "execution_count": 10,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Device: cuda\n",- "GuppyLM: 8,726,016 params (8.7M)\n",- "Train: 57,000, Eval: 3,000\n",- "\n",- "Training for 10000 steps...\n",- " Step | LR | Train | Eval | Time\n",- "--------------------------------------------------------\n",- " 0 | 0.000000 | 8.4972 | -- | 1.0s\n",- " 100 | 0.000150 | 5.7581 | -- | 3.5s\n",- " 200 | 0.000300 | 3.2231 | -- | 5.9s\n",- " 200 | 0.000300 | 4.4906 | 2.7493 | 6.3s\n",- " -> Best model (eval=2.7493)\n",- " 300 | 0.000300 | 2.4972 | -- | 8.8s\n",- " 400 | 0.000300 | 2.0159 | -- | 11.8s\n",- " 400 | 0.000300 | 2.2566 | 1.7105 | 12.3s\n",- " -> Best model (eval=1.7105)\n",- " 500 | 0.000299 | 1.6539 | -- | 14.8s\n",- " 600 | 0.000299 | 1.3577 | -- | 17.3s\n",- " 600 | 0.000299 | 1.5058 | 1.1083 | 17.8s\n",- " -> Best model (eval=1.1083)\n",- " 700 | 0.000298 | 1.1433 | -- | 20.2s\n",- " 800 | 0.000298 | 0.9510 | -- | 23.0s\n",- " 800 | 0.000298 | 1.0472 | 0.7740 | 23.5s\n",- " -> Best model (eval=0.7740)\n",- " 900 | 0.000297 | 0.8354 | -- | 26.2s\n",- " 1000 | 0.000296 | 0.7302 | -- | 28.6s\n",- " 1000 | 0.000296 | 0.7828 | 0.6078 | 29.1s\n",- " -> Best model (eval=0.6078)\n",- " 1100 | 0.000294 | 0.6600 | -- | 31.7s\n",- " 1200 | 0.000293 | 0.6254 | -- | 34.3s\n",- " 1200 | 0.000293 | 0.6427 | 0.5268 | 34.8s\n",- " -> Best model (eval=0.5268)\n",- " 1300 | 0.000292 | 0.5897 | -- | 37.8s\n",- " 1400 | 0.000290 | 0.5569 | -- | 40.3s\n",- " 1400 | 0.000290 | 0.5733 | 0.4896 | 40.8s\n",- " -> Best model (eval=0.4896)\n",- " 1500 | 0.000288 | 0.5441 | -- | 43.3s\n",- " 1600 | 0.000287 | 0.5200 | -- | 45.7s\n",- " 1600 | 0.000287 | 0.5320 | 0.4802 | 46.2s\n",- " -> Best model (eval=0.4802)\n",- " 1700 | 0.000285 | 0.5175 | -- | 49.4s\n",- " 1800 | 0.000283 | 0.4995 | -- | 51.9s\n",- " 1800 | 0.000283 | 0.5085 | 0.4589 | 52.3s\n",- " -> Best model (eval=0.4589)\n",- " 1900 | 0.000280 | 0.4888 | -- | 54.9s\n",- " 2000 | 0.000278 | 0.4814 | -- | 57.3s\n",- " 2000 | 0.000278 | 0.4851 | 0.4499 | 57.8s\n",- " -> Best model (eval=0.4499)\n",- " 2100 | 0.000276 | 0.4791 | -- | 61.0s\n",- " 2200 | 0.000273 | 0.4718 | -- | 63.4s\n",- " 2200 | 0.000273 | 0.4755 | 0.4438 | 63.9s\n",- " -> Best model (eval=0.4438)\n",- " 2300 | 0.000271 | 0.4645 | -- | 66.5s\n",- " 2400 | 0.000268 | 0.4665 | -- | 69.1s\n",- " 2400 | 0.000268 | 0.4655 | 0.4378 | 69.6s\n",- " -> Best model (eval=0.4378)\n",- " 2500 | 0.000265 | 0.4660 | -- | 72.7s\n",- " 2600 | 0.000262 | 0.4555 | -- | 75.2s\n",- " 2600 | 0.000262 | 0.4607 | 0.4372 | 75.7s\n",- " -> Best model (eval=0.4372)\n",- " 2700 | 0.000259 | 0.4556 | -- | 78.2s\n",- " 2800 | 0.000256 | 0.4497 | -- | 80.6s\n",- " 2800 | 0.000256 | 0.4526 | 0.4305 | 81.1s\n",- " -> Best model (eval=0.4305)\n",- " 2900 | 0.000253 | 0.4472 | -- | 83.8s\n",- " 3000 | 0.000249 | 0.4479 | -- | 86.5s\n",- " 3000 | 0.000249 | 0.4476 | 0.4276 | 87.0s\n",- " -> Best model (eval=0.4276)\n",- " 3100 | 0.000246 | 0.4459 | -- | 89.6s\n",- " 3200 | 0.000242 | 0.4427 | -- | 92.0s\n",- " 3200 | 0.000242 | 0.4443 | 0.4226 | 92.5s\n",- " -> Best model (eval=0.4226)\n",- " 3300 | 0.000239 | 0.4441 | -- | 95.2s\n",- " 3400 | 0.000235 | 0.4389 | -- | 98.1s\n",- " 3400 | 0.000235 | 0.4415 | 0.4226 | 98.6s\n",- " -> Best model (eval=0.4226)\n",- " 3500 | 0.000231 | 0.4349 | -- | 101.1s\n",- " 3600 | 0.000227 | 0.4331 | -- | 103.5s\n",- " 3600 | 0.000227 | 0.4340 | 0.4203 | 104.0s\n",- " -> Best model (eval=0.4203)\n",- " 3700 | 0.000224 | 0.4359 | -- | 106.5s\n",- " 3800 | 0.000220 | 0.4273 | -- | 109.6s\n",- " 3800 | 0.000220 | 0.4316 | 0.4188 | 110.1s\n",- " -> Best model (eval=0.4188)\n",- " 3900 | 0.000216 | 0.4258 | -- | 112.6s\n",- " 4000 | 0.000212 | 0.4291 | -- | 115.1s\n",- " 4000 | 0.000212 | 0.4274 | 0.4149 | 115.5s\n",- " -> Best model (eval=0.4149)\n",- " 4100 | 0.000208 | 0.4254 | -- | 118.2s\n",- " 4200 | 0.000203 | 0.4248 | -- | 121.0s\n",- " 4200 | 0.000203 | 0.4251 | 0.4150 | 121.5s\n",- " 4300 | 0.000199 | 0.4205 | -- | 124.0s\n",- " 4400 | 0.000195 | 0.4175 | -- | 126.3s\n",- " 4400 | 0.000195 | 0.4190 | 0.4093 | 126.8s\n",- " -> Best model (eval=0.4093)\n",- " 4500 | 0.000191 | 0.4170 | -- | 129.4s\n",- " 4600 | 0.000187 | 0.4173 | -- | 132.0s\n",- " 4600 | 0.000187 | 0.4171 | 0.4087 | 132.6s\n",- " -> Best model (eval=0.4087)\n",- " 4700 | 0.000182 | 0.4183 | -- | 135.3s\n",- " 4800 | 0.000178 | 0.4173 | -- | 137.8s\n",- " 4800 | 0.000178 | 0.4178 | 0.4086 | 138.3s\n",- " -> Best model (eval=0.4086)\n",- " 4900 | 0.000174 | 0.4158 | -- | 140.8s\n",- " 5000 | 0.000169 | 0.4178 | -- | 143.2s\n",- " 5000 | 0.000169 | 0.4168 | 0.4057 | 143.7s\n",- " -> Best model (eval=0.4057)\n",- " 5100 | 0.000165 | 0.4119 | -- | 146.8s\n",- " 5200 | 0.000161 | 0.4166 | -- | 149.2s\n",- " 5200 | 0.000161 | 0.4143 | 0.4022 | 149.7s\n",- " -> Best model (eval=0.4022)\n",- " 5300 | 0.000156 | 0.4146 | -- | 152.2s\n",- " 5400 | 0.000152 | 0.4071 | -- | 154.5s\n",- " 5400 | 0.000152 | 0.4108 | 0.4017 | 155.0s\n",- " -> Best model (eval=0.4017)\n",- " 5500 | 0.000148 | 0.4087 | -- | 158.0s\n",- " 5600 | 0.000143 | 0.4037 | -- | 160.5s\n",- " 5600 | 0.000143 | 0.4062 | 0.4006 | 161.0s\n",- " -> Best model (eval=0.4006)\n",- " 5700 | 0.000139 | 0.4086 | -- | 163.5s\n",- " 5800 | 0.000135 | 0.4064 | -- | 165.9s\n",- " 5800 | 0.000135 | 0.4075 | 0.3999 | 166.4s\n",- " -> Best model (eval=0.3999)\n",- " 5900 | 0.000131 | 0.4018 | -- | 169.2s\n",- " 6000 | 0.000127 | 0.4031 | -- | 172.0s\n",- " 6000 | 0.000127 | 0.4024 | 0.3972 | 172.4s\n",- " -> Best model (eval=0.3972)\n",- " 6100 | 0.000122 | 0.4047 | -- | 175.0s\n",- " 6200 | 0.000118 | 0.4024 | -- | 177.4s\n",- " 6200 | 0.000118 | 0.4035 | 0.3944 | 177.9s\n",- " -> Best model (eval=0.3944)\n",- " 6300 | 0.000114 | 0.4050 | -- | 180.5s\n",- " 6400 | 0.000110 | 0.4022 | -- | 183.3s\n",- " 6400 | 0.000110 | 0.4036 | 0.3966 | 183.8s\n",- " 6500 | 0.000106 | 0.4007 | -- | 186.2s\n",- " 6600 | 0.000103 | 0.3983 | -- | 188.6s\n",- " 6600 | 0.000103 | 0.3995 | 0.3933 | 189.1s\n",- " -> Best model (eval=0.3933)\n",- " 6700 | 0.000099 | 0.3969 | -- | 191.6s\n",- " 6800 | 0.000095 | 0.3954 | -- | 194.6s\n",- " 6800 | 0.000095 | 0.3961 | 0.3947 | 195.0s\n",- " 6900 | 0.000091 | 0.3981 | -- | 197.5s\n",- " 7000 | 0.000088 | 0.4002 | -- | 200.0s\n",- " 7000 | 0.000088 | 0.3991 | 0.3928 | 200.5s\n",- " -> Best model (eval=0.3928)\n",- " 7100 | 0.000084 | 0.3980 | -- | 203.1s\n",- " 7200 | 0.000081 | 0.3916 | -- | 205.9s\n",- " 7200 | 0.000081 | 0.3948 | 0.3912 | 206.4s\n",- " -> Best model (eval=0.3912)\n",- " 7300 | 0.000077 | 0.3913 | -- | 208.9s\n",- " 7400 | 0.000074 | 0.3922 | -- | 211.4s\n",- " 7400 | 0.000074 | 0.3917 | 0.3926 | 211.8s\n",- " 7500 | 0.000071 | 0.3922 | -- | 214.3s\n",- " 7600 | 0.000068 | 0.3920 | -- | 216.8s\n",- " 7600 | 0.000068 | 0.3921 | 0.3896 | 217.3s\n",- " -> Best model (eval=0.3896)\n",- " 7700 | 0.000065 | 0.3904 | -- | 220.2s\n",- " 7800 | 0.000062 | 0.3916 | -- | 222.6s\n",- " 7800 | 0.000062 | 0.3910 | 0.3891 | 223.1s\n",- " -> Best model (eval=0.3891)\n",- " 7900 | 0.000059 | 0.3895 | -- | 225.6s\n",- " 8000 | 0.000057 | 0.3886 | -- | 228.0s\n",- " 8000 | 0.000057 | 0.3890 | 0.3887 | 228.5s\n",- " -> Best model (eval=0.3887)\n",- " 8100 | 0.000054 | 0.3935 | -- | 231.7s\n",- " 8200 | 0.000052 | 0.3919 | -- | 234.1s\n",- " 8200 | 0.000052 | 0.3927 | 0.3884 | 234.6s\n",- " -> Best model (eval=0.3884)\n",- " 8300 | 0.000050 | 0.3906 | -- | 237.0s\n",- " 8400 | 0.000047 | 0.3886 | -- | 239.5s\n",- " 8400 | 0.000047 | 0.3896 | 0.3869 | 240.0s\n",- " -> Best model (eval=0.3869)\n",- " 8500 | 0.000045 | 0.3926 | -- | 243.0s\n",- " 8600 | 0.000043 | 0.3909 | -- | 245.5s\n",- " 8600 | 0.000043 | 0.3917 | 0.3872 | 246.0s\n",- " 8700 | 0.000042 | 0.3910 | -- | 248.4s\n",- " 8800 | 0.000040 | 0.3915 | -- | 250.8s\n",- " 8800 | 0.000040 | 0.3912 | 0.3859 | 251.3s\n",- " -> Best model (eval=0.3859)\n",- " 8900 | 0.000038 | 0.3874 | -- | 254.1s\n",- " 9000 | 0.000037 | 0.3853 | -- | 256.9s\n",- " 9000 | 0.000037 | 0.3864 | 0.3866 | 257.4s\n",- " 9100 | 0.000036 | 0.3843 | -- | 260.0s\n",- " 9200 | 0.000034 | 0.3882 | -- | 262.5s\n",- " 9200 | 0.000034 | 0.3863 | 0.3861 | 263.0s\n",- " 9300 | 0.000033 | 0.3871 | -- | 265.7s\n",- " 9400 | 0.000032 | 0.3858 | -- | 268.7s\n",- " 9400 | 0.000032 | 0.3864 | 0.3847 | 269.2s\n",- " -> Best model (eval=0.3847)\n",- " 9500 | 0.000032 | 0.3833 | -- | 271.8s\n",- " 9600 | 0.000031 | 0.3853 | -- | 274.3s\n",- " 9600 | 0.000031 | 0.3843 | 0.3847 | 274.8s\n",- " -> Best model (eval=0.3847)\n",- " 9700 | 0.000031 | 0.3851 | -- | 277.4s\n",- " 9800 | 0.000030 | 0.3850 | -- | 280.4s\n",- " 9800 | 0.000030 | 0.3850 | 0.3844 | 280.9s\n",- " -> Best model (eval=0.3844)\n",- " 9900 | 0.000030 | 0.3871 | -- | 283.4s\n",- "\n",- "Done! 286s, best eval: 0.3844\n"- ]- }- ],"source": ["from train import train\n","train()"- ]+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "markdown",@@ -1109,34 +749,7 @@},{"cell_type": "code",- "execution_count": 11,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "GuppyLM loaded: 8.7M params\n",- "Topic You Guppy\n",- "====================================================================================================\n",- "greeting hi guppy oh hello. i'm near the rock. the temperature feels nice.\n",- "food are you hungry yes. always yes. i've been exploring the cave waiting for this moment. thank you in advance.\n",- "temperature it is really hot today too warm is not good. i am small and i overheat easy. can you do something about it.\n",- "water how is the water the water feels a little cloudy right now. i'll swim around behind the plant to feel the new water. the clarity is just right.\n",- "bubbles do you like bubbles bubbles are great. i swim through them in the corner.\n",- "confused what is the internet sometimes the temperature changes suddenly. that scares me.\n",- "lonely do you get lonely i talk to the heater tube sometimes. it doesn't answer. i'm ok alone. fish are used to it. maybe a shrimp. they don't eat the re\n",- "cat the cat is looking at you tell it i'm not food. please.\n",- "joke tell me a joke i don't know jokes. but i fell off a rock once. that was funny.\n",- "dreams what do you dream about maybe fish dream. maybe it's just dark with occasional thoughts.\n",- "love do you love me if love is wanting someone to stay and bring food then yes.\n",- "meaning what is the meaning of life i don't think about purpose. i think about food.\n",- "glass_tap sorry i tapped the glass i know you don't mean it but it scares me.\n",- "rain it is raining outside tell the rain i said hi.\n",- "night goodnight guppy goodnight. my little body need a break. goodnight tank. goodnight water.\n"- ]- }- ],"source": ["from inference import GuppyInference\n","import torch\n",@@ -1173,59 +786,67 @@"print('=' * 100)\n","for prompt, topic in tests:\n"," reply = chat(prompt)\n",- " print(f'{topic:<12s} {prompt:<35s} {reply[:128]}')\n"- ]+ " print(f'{topic:<12s} {prompt:<35s} {reply[:128]}')\n",+ ""+ ],+ "outputs": [],+ "execution_count": null},{"cell_type": "markdown","metadata": {},"source": [- "## 7. Download\n",- "\n",- "Package the trained model for local use.\n",+ "## 7. Upload to HuggingFace\n","\n",- "```python\n",- "# Run locally after downloading:\n",- "from inference import GuppyInference\n",- "engine = GuppyInference('checkpoints/best_model.pt', 'data/tokenizer.json')\n",- "r = engine.chat_completion([{'role': 'user', 'content': 'hi guppy'}])\n",- "print(r['choices'][0]['message']['content'])\n",- "```"+ "Push the trained model to HuggingFace Hub.\n",+ "Set your token and repo below."]},{"cell_type": "code",- "execution_count": 12,"metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Package: /content/guppylm.tar.gz (32.4 MB)\n"- ]- },- {- "data": {- "application/javascript": "\n async function download(id, filename, size) {\n if (!google.colab.kernel.accessAllowed) {\n return;\n }\n const div = document.createElement('div');\n const label = document.createElement('label');\n label.textContent = `Downloading \"${filename}\": `;\n div.appendChild(label);\n const progress = document.createElement('progress');\n progress.max = size;\n div.appendChild(progress);\n document.body.appendChild(div);\n\n const buffers = [];\n let downloaded = 0;\n\n const channel = await google.colab.kernel.comms.open(id);\n // Send a message to notify the kernel that we're ready.\n channel.send({})\n\n for await (const message of channel.messages) {\n // Send a message to notify the kernel that we're ready.\n channel.send({})\n if (message.buffers) {\n for (const buffer of message.buffers) {\n buffers.push(buffer);\n downloaded += buffer.byteLength;\n progress.value = downloaded;\n }\n }\n }\n const blob = new Blob(buffers, {type: 'application/binary'});\n const a = document.createElement('a');\n a.href = window.URL.createObjectURL(blob);\n a.download = filename;\n div.appendChild(a);\n a.click();\n div.remove();\n }\n ",- "text/plain": [- "<IPython.core.display.Javascript object>"- ]- },- "metadata": {},- "output_type": "display_data"- },- {- "data": {- "application/javascript": "download(\"download_9644de2b-5ab7-4a28-a5bf-6741f7ef1cc6\", \"guppylm.tar.gz\", 32436475)",- "text/plain": [- "<IPython.core.display.Javascript object>"- ]- },- "metadata": {},- "output_type": "display_data"- }+ "source": [+ "from huggingface_hub import HfApi, login\n",+ "import os\n",+ "\n",+ "HF_TOKEN = '' # Paste your HF token here\n",+ "HF_REPO = 'arman-bd/guppylm-9M' # Change to your repo\n",+ "\n",+ "if not HF_TOKEN:\n",+ " print('Set HF_TOKEN above to upload. Skipping.')\n",+ "else:\n",+ " login(token=HF_TOKEN)\n",+ " api = HfApi()\n",+ " api.create_repo(HF_REPO, exist_ok=True)\n",+ "\n",+ " # Upload model checkpoint, tokenizer, and source files\n",+ " for path in ['checkpoints/best_model.pt', 'checkpoints/config.json',\n",+ " 'data/tokenizer.json', 'model.py', 'config.py', 'inference.py']:\n",+ " if os.path.exists(path):\n",+ " api.upload_file(\n",+ " path_or_fileobj=path,\n",+ " path_in_repo=path,\n",+ " repo_id=HF_REPO,\n",+ " )\n",+ " print(f' Uploaded {path}')\n",+ "\n",+ " print(f'\\nDone! https://huggingface.co/{HF_REPO}')"],+ "outputs": [],+ "execution_count": null+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## 8. Download\n",+ "\n",+ "Or download the model locally as a tar.gz."+ ]+ },+ {+ "cell_type": "code",+ "metadata": {},"source": ["import os\n","\n",@@ -1244,35 +865,10 @@" from google.colab import files\n"," files.download('/content/guppylm.tar.gz')\n","except ImportError:\n",- " print('Not in Colab β download manually from the file browser.')"- ]- }- ],- "metadata": {- "accelerator": "GPU",- "colab": {- "gpuType": "T4",- "name": "GuppyLM β Train a Fish",- "provenance": []- },- "kernelspec": {- "display_name": "Python 3 (ipykernel)",- "language": "python",- "name": "python3"- },- "language_info": {- "codemirror_mode": {- "name": "ipython",- "version": 3- },- "file_extension": ".py",- "mimetype": "text/x-python",- "name": "python",- "nbconvert_exporter": "python",- "pygments_lexer": "ipython3",- "version": "3.12.13"+ " print('Not in Colab \u2014 download manually from the file browser.')"+ ],+ "outputs": [],+ "execution_count": null}- },- "nbformat": 4,- "nbformat_minor": 0-}+ ]+}\ No newline at end of filediff --git a/use_guppylm.ipynb b/use_guppylm.ipynbnew file mode 100644index 0000000..01efc68--- /dev/null+++ b/use_guppylm.ipynb@@ -0,0 +1,191 @@+{+ "nbformat": 4,+ "nbformat_minor": 0,+ "metadata": {+ "colab": {+ "provenance": [],+ "name": "GuppyLM \u2014 Chat"+ },+ "kernelspec": {+ "name": "python3",+ "display_name": "Python 3"+ },+ "language_info": {+ "name": "python"+ }+ },+ "cells": [+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "# GuppyLM \u2014 Chat with a Fish\n",+ "\n",+ "Download the pre-trained GuppyLM model from HuggingFace and chat with it.\n",+ "No training needed \u2014 just run all cells.\n",+ "\n",+ "**Model:** [arman-bd/guppylm-9M](https://huggingface.co/arman-bd/guppylm-9M) (8.7M params)"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## 1. Setup"+ ]+ },+ {+ "cell_type": "code",+ "metadata": {},+ "source": [+ "!pip install -q torch tokenizers huggingface_hub\n",+ "\n",+ "import torch\n",+ "print(f'PyTorch {torch.__version__}')\n",+ "print(f'CUDA: {torch.cuda.is_available()}')"+ ],+ "outputs": [],+ "execution_count": null+ },+ {+ "cell_type": "code",+ "metadata": {},+ "source": [+ "import os, shutil\n",+ "if os.path.exists('/content/guppy'):\n",+ " shutil.rmtree('/content/guppy')\n",+ "os.makedirs('/content/guppy')\n",+ "os.chdir('/content/guppy')\n",+ "print(f'Working dir: {os.getcwd()}')"+ ],+ "outputs": [],+ "execution_count": null+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## 2. Download Model\n",+ "\n",+ "Download the pre-trained model, tokenizer, and inference code from HuggingFace."+ ]+ },+ {+ "cell_type": "code",+ "metadata": {},+ "source": [+ "from huggingface_hub import hf_hub_download\n",+ "import os\n",+ "\n",+ "HF_REPO = 'arman-bd/guppylm-9M'\n",+ "\n",+ "files = [\n",+ " ('checkpoints/best_model.pt', 'checkpoints'),\n",+ " ('checkpoints/config.json', 'checkpoints'),\n",+ " ('data/tokenizer.json', 'data'),\n",+ " ('model.py', '.'),\n",+ " ('config.py', '.'),\n",+ " ('inference.py', '.'),\n",+ "]\n",+ "\n",+ "for filename, subdir in files:\n",+ " os.makedirs(subdir, exist_ok=True)\n",+ " hf_hub_download(\n",+ " repo_id=HF_REPO,\n",+ " filename=filename,\n",+ " local_dir='.',\n",+ " )\n",+ " print(f' Downloaded {filename}')\n",+ "\n",+ "print(f'\\nModel ready from {HF_REPO}')"+ ],+ "outputs": [],+ "execution_count": null+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## 3. Chat\n",+ "\n",+ "Talk to Guppy. Each message is independent (single-turn)."+ ]+ },+ {+ "cell_type": "code",+ "metadata": {},+ "source": [+ "from inference import GuppyInference\n",+ "import torch\n",+ "\n",+ "engine = GuppyInference(\n",+ " 'checkpoints/best_model.pt', 'data/tokenizer.json',\n",+ " device='cuda' if torch.cuda.is_available() else 'cpu'\n",+ ")\n",+ "\n",+ "def chat(prompt):\n",+ " r = engine.chat_completion([{'role': 'user', 'content': prompt}], max_tokens=64)\n",+ " return r['choices'][0]['message'].get('content', '').strip()"+ ],+ "outputs": [],+ "execution_count": null+ },+ {+ "cell_type": "code",+ "metadata": {},+ "source": [+ "# Try different topics\n",+ "tests = [\n",+ " 'hi guppy',\n",+ " 'are you hungry',\n",+ " 'it is really hot today',\n",+ " 'do you like bubbles',\n",+ " 'what is the meaning of life',\n",+ " 'tell me a joke',\n",+ " 'the cat is looking at you',\n",+ " 'do you love me',\n",+ " 'what is the internet',\n",+ " 'goodnight guppy',\n",+ "]\n",+ "\n",+ "for prompt in tests:\n",+ " reply = chat(prompt)\n",+ " print(f'You> {prompt}')\n",+ " print(f'Guppy> {reply}')\n",+ " print()"+ ],+ "outputs": [],+ "execution_count": null+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## 4. Interactive Chat\n",+ "\n",+ "Type your own messages. Type `quit` to stop."+ ]+ },+ {+ "cell_type": "code",+ "metadata": {},+ "source": [+ "print('Chat with Guppy (type quit to stop)')\n",+ "print('=' * 40)\n",+ "while True:\n",+ " try:\n",+ " prompt = input('You> ').strip()\n",+ " except (KeyboardInterrupt, EOFError):\n",+ " break\n",+ " if not prompt or prompt.lower() in ('quit', 'exit', 'q'):\n",+ " print('Guppy> bye. i will continue being a fish.')\n",+ " break\n",+ " reply = chat(prompt)\n",+ " print(f'Guppy> {reply}')\n",+ " print()"+ ],+ "outputs": [],+ "execution_count": null+ }+ ]+}\ No newline at end of file