purplesquirrelnetworks commited on
Commit
c706ad1
·
verified ·
1 Parent(s): 4d9a09e

Upload purple_squirrel_r1_demo.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. purple_squirrel_r1_demo.ipynb +181 -0
purple_squirrel_r1_demo.ipynb ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Purple Squirrel R1 - Demo Notebook\n",
8
+ "\n",
9
+ "This notebook demonstrates how to use the Purple Squirrel R1 model, a fine-tuned version of DeepSeek-R1-Distill-Llama-8B specialized for Purple Squirrel platform questions.\n",
10
+ "\n",
11
+ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/purplesquirrelnetworks/purple-squirrel-r1/blob/main/purple_squirrel_r1_demo.ipynb)"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "metadata": {},
17
+ "source": [
18
+ "## Option 1: Use the REST API (Recommended)\n",
19
+ "\n",
20
+ "The fastest way to use the model - no GPU required!"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "import requests\n",
30
+ "\n",
31
+ "API_URL = \"https://purplesquirrelnetworks--purple-squirrel-r1-api-purplesqu-1e3c01.modal.run\"\n",
32
+ "\n",
33
+ "def ask_purple_squirrel(prompt, max_tokens=256, temperature=0.3):\n",
34
+ " response = requests.post(\n",
35
+ " API_URL,\n",
36
+ " json={\"prompt\": prompt, \"max_tokens\": max_tokens, \"temperature\": temperature}\n",
37
+ " )\n",
38
+ " return response.json()[\"response\"]\n",
39
+ "\n",
40
+ "# Test it out\n",
41
+ "print(ask_purple_squirrel(\"What is Purple Squirrel?\"))"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": null,
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "# More examples\n",
51
+ "questions = [\n",
52
+ " \"Can I mint NFTs with Purple Squirrel?\",\n",
53
+ " \"What AI capabilities does Purple Squirrel have?\",\n",
54
+ " \"How does Purple Squirrel handle video transcription?\",\n",
55
+ "]\n",
56
+ "\n",
57
+ "for q in questions:\n",
58
+ " print(f\"Q: {q}\")\n",
59
+ " print(f\"A: {ask_purple_squirrel(q)}\")\n",
60
+ " print(\"-\" * 50)"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "markdown",
65
+ "metadata": {},
66
+ "source": [
67
+ "## Option 2: Load Model Locally (Requires GPU)\n",
68
+ "\n",
69
+ "For local inference with full control. Requires ~16GB VRAM or use 4-bit quantization."
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": null,
75
+ "metadata": {},
76
+ "outputs": [],
77
+ "source": [
78
+ "# Install dependencies\n",
79
+ "!pip install -q torch transformers accelerate bitsandbytes"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": null,
85
+ "metadata": {},
86
+ "outputs": [],
87
+ "source": [
88
+ "import torch\n",
89
+ "from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig\n",
90
+ "\n",
91
+ "MODEL_ID = \"purplesquirrelnetworks/purple-squirrel-r1\"\n",
92
+ "\n",
93
+ "# 4-bit quantization for lower memory usage\n",
94
+ "quantization_config = BitsAndBytesConfig(\n",
95
+ " load_in_4bit=True,\n",
96
+ " bnb_4bit_compute_dtype=torch.bfloat16,\n",
97
+ " bnb_4bit_use_double_quant=True,\n",
98
+ " bnb_4bit_quant_type=\"nf4\",\n",
99
+ ")\n",
100
+ "\n",
101
+ "print(\"Loading tokenizer...\")\n",
102
+ "tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)\n",
103
+ "\n",
104
+ "print(\"Loading model with 4-bit quantization...\")\n",
105
+ "model = AutoModelForCausalLM.from_pretrained(\n",
106
+ " MODEL_ID,\n",
107
+ " quantization_config=quantization_config,\n",
108
+ " device_map=\"auto\",\n",
109
+ ")\n",
110
+ "print(\"Model loaded!\")"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": null,
116
+ "metadata": {},
117
+ "outputs": [],
118
+ "source": [
119
+ "def generate_response(prompt, max_tokens=256, temperature=0.3):\n",
120
+ " messages = [{\"role\": \"user\", \"content\": prompt}]\n",
121
+ " formatted = tokenizer.apply_chat_template(\n",
122
+ " messages, tokenize=False, add_generation_prompt=True\n",
123
+ " )\n",
124
+ " inputs = tokenizer(formatted, return_tensors=\"pt\").to(model.device)\n",
125
+ " \n",
126
+ " outputs = model.generate(\n",
127
+ " **inputs,\n",
128
+ " max_new_tokens=max_tokens,\n",
129
+ " temperature=temperature,\n",
130
+ " do_sample=True,\n",
131
+ " pad_token_id=tokenizer.eos_token_id,\n",
132
+ " repetition_penalty=1.1,\n",
133
+ " )\n",
134
+ " \n",
135
+ " response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
136
+ " # Extract assistant response\n",
137
+ " if \"<|Assistant|>\" in response:\n",
138
+ " response = response.split(\"<|Assistant|>\")[-1]\n",
139
+ " if \"</think>\" in response:\n",
140
+ " response = response.split(\"</think>\")[-1].strip()\n",
141
+ " return response\n",
142
+ "\n",
143
+ "# Test\n",
144
+ "print(generate_response(\"What is Purple Squirrel?\"))"
145
+ ]
146
+ },
147
+ {
148
+ "cell_type": "markdown",
149
+ "metadata": {},
150
+ "source": [
151
+ "## Model Info\n",
152
+ "\n",
153
+ "| Attribute | Value |\n",
154
+ "|-----------|-------|\n",
155
+ "| Base Model | DeepSeek-R1-Distill-Llama-8B |\n",
156
+ "| Parameters | 8B |\n",
157
+ "| Training Method | LoRA |\n",
158
+ "| Training Examples | 74 |\n",
159
+ "| Token Accuracy | 91% |\n",
160
+ "\n",
161
+ "## Links\n",
162
+ "\n",
163
+ "- [Model on HuggingFace](https://huggingface.co/purplesquirrelnetworks/purple-squirrel-r1)\n",
164
+ "- [Purple Squirrel Networks](https://purplesquirrel.io)"
165
+ ]
166
+ }
167
+ ],
168
+ "metadata": {
169
+ "kernelspec": {
170
+ "display_name": "Python 3",
171
+ "language": "python",
172
+ "name": "python3"
173
+ },
174
+ "language_info": {
175
+ "name": "python",
176
+ "version": "3.11.0"
177
+ }
178
+ },
179
+ "nbformat": 4,
180
+ "nbformat_minor": 4
181
+ }