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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"attachments": {},
|
| 5 |
+
"cell_type": "markdown",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
" # Install Dependencies"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": null,
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"outputs": [],
|
| 16 |
+
"source": [
|
| 17 |
+
"!pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 --upgrade"
|
| 18 |
+
]
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"cell_type": "code",
|
| 22 |
+
"execution_count": null,
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [],
|
| 25 |
+
"source": [
|
| 26 |
+
"!pip install langchain einops accelerate transformers bitsandbytes"
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"attachments": {},
|
| 31 |
+
"cell_type": "markdown",
|
| 32 |
+
"metadata": {},
|
| 33 |
+
"source": [
|
| 34 |
+
"# Import Dependencies"
|
| 35 |
+
]
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"cell_type": "code",
|
| 39 |
+
"execution_count": 10,
|
| 40 |
+
"metadata": {},
|
| 41 |
+
"outputs": [
|
| 42 |
+
{
|
| 43 |
+
"name": "stderr",
|
| 44 |
+
"output_type": "stream",
|
| 45 |
+
"text": [
|
| 46 |
+
"d:\\YouTube\\6-06-2023 - Falcon\\falcon\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 47 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 48 |
+
]
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
"source": [
|
| 52 |
+
"from langchain import HuggingFacePipeline\n",
|
| 53 |
+
"from langchain import PromptTemplate, LLMChain\n",
|
| 54 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
|
| 55 |
+
"import transformers\n",
|
| 56 |
+
"import os \n",
|
| 57 |
+
"import torch"
|
| 58 |
+
]
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"cell_type": "code",
|
| 62 |
+
"execution_count": 11,
|
| 63 |
+
"metadata": {},
|
| 64 |
+
"outputs": [
|
| 65 |
+
{
|
| 66 |
+
"data": {
|
| 67 |
+
"text/plain": [
|
| 68 |
+
"True"
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
"execution_count": 11,
|
| 72 |
+
"metadata": {},
|
| 73 |
+
"output_type": "execute_result"
|
| 74 |
+
}
|
| 75 |
+
],
|
| 76 |
+
"source": [
|
| 77 |
+
"# Check if cuda is available \n",
|
| 78 |
+
"torch.cuda.is_available()"
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"attachments": {},
|
| 83 |
+
"cell_type": "markdown",
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"source": [
|
| 86 |
+
"# Build the Pipeline"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "code",
|
| 91 |
+
"execution_count": null,
|
| 92 |
+
"metadata": {},
|
| 93 |
+
"outputs": [],
|
| 94 |
+
"source": [
|
| 95 |
+
"# Define Model ID\n",
|
| 96 |
+
"model_id = \"tiiuae/falcon-40b-instruct\"\n",
|
| 97 |
+
"# Load Tokenizer\n",
|
| 98 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
|
| 99 |
+
"# Load Model \n",
|
| 100 |
+
"model = AutoModelForCausalLM.from_pretrained(model_id, cache_dir='./workspace/', \n",
|
| 101 |
+
" torch_dtype=torch.bfloat16, trust_remote_code=True, device_map=\"auto\", offload_folder=\"offload\")\n",
|
| 102 |
+
"# Set PT model to inference mode\n",
|
| 103 |
+
"model.eval()\n",
|
| 104 |
+
"# Build HF Transformers pipeline \n",
|
| 105 |
+
"pipeline = transformers.pipeline(\n",
|
| 106 |
+
" \"text-generation\", \n",
|
| 107 |
+
" model=model,\n",
|
| 108 |
+
" tokenizer=tokenizer,\n",
|
| 109 |
+
" device_map=\"auto\",\n",
|
| 110 |
+
" max_length=400,\n",
|
| 111 |
+
" do_sample=True,\n",
|
| 112 |
+
" top_k=10,\n",
|
| 113 |
+
" num_return_sequences=1,\n",
|
| 114 |
+
" eos_token_id=tokenizer.eos_token_id\n",
|
| 115 |
+
")"
|
| 116 |
+
]
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
"cell_type": "code",
|
| 120 |
+
"execution_count": null,
|
| 121 |
+
"metadata": {},
|
| 122 |
+
"outputs": [],
|
| 123 |
+
"source": [
|
| 124 |
+
"# Test out the pipeline\n",
|
| 125 |
+
"pipeline('who is kim kardashian?')"
|
| 126 |
+
]
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"attachments": {},
|
| 130 |
+
"cell_type": "markdown",
|
| 131 |
+
"metadata": {},
|
| 132 |
+
"source": [
|
| 133 |
+
"# Pass it to Langchain"
|
| 134 |
+
]
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"cell_type": "code",
|
| 138 |
+
"execution_count": null,
|
| 139 |
+
"metadata": {},
|
| 140 |
+
"outputs": [],
|
| 141 |
+
"source": [
|
| 142 |
+
"# Setup prompt template\n",
|
| 143 |
+
"template = PromptTemplate(input_variables=['input'], template='{input}') \n",
|
| 144 |
+
"# Pass hugging face pipeline to langchain class\n",
|
| 145 |
+
"llm = HuggingFacePipeline(pipeline=pipeline) \n",
|
| 146 |
+
"# Build stacked LLM chain i.e. prompt-formatting + LLM\n",
|
| 147 |
+
"chain = LLMChain(llm=llm, prompt=template)"
|
| 148 |
+
]
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"cell_type": "code",
|
| 152 |
+
"execution_count": null,
|
| 153 |
+
"metadata": {},
|
| 154 |
+
"outputs": [],
|
| 155 |
+
"source": [
|
| 156 |
+
"# Test LLMChain \n",
|
| 157 |
+
"response = chain.run('who is kim kardashian?')"
|
| 158 |
+
]
|
| 159 |
+
},
|
| 160 |
+
{
|
| 161 |
+
"attachments": {},
|
| 162 |
+
"cell_type": "markdown",
|
| 163 |
+
"metadata": {},
|
| 164 |
+
"source": [
|
| 165 |
+
"# Build Gradio App"
|
| 166 |
+
]
|
| 167 |
+
},
|
| 168 |
+
{
|
| 169 |
+
"cell_type": "code",
|
| 170 |
+
"execution_count": null,
|
| 171 |
+
"metadata": {},
|
| 172 |
+
"outputs": [],
|
| 173 |
+
"source": [
|
| 174 |
+
"# Install Gradio for the UI component\n",
|
| 175 |
+
"!pip install gradio"
|
| 176 |
+
]
|
| 177 |
+
},
|
| 178 |
+
{
|
| 179 |
+
"cell_type": "code",
|
| 180 |
+
"execution_count": null,
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"outputs": [],
|
| 183 |
+
"source": [
|
| 184 |
+
"# Import gradio for UI\n",
|
| 185 |
+
"import gradio as gr"
|
| 186 |
+
]
|
| 187 |
+
},
|
| 188 |
+
{
|
| 189 |
+
"cell_type": "code",
|
| 190 |
+
"execution_count": null,
|
| 191 |
+
"metadata": {},
|
| 192 |
+
"outputs": [],
|
| 193 |
+
"source": [
|
| 194 |
+
"# Create generate function - this will be called when a user runs the gradio app \n",
|
| 195 |
+
"def generate(prompt): \n",
|
| 196 |
+
" # The prompt will get passed to the LLM Chain!\n",
|
| 197 |
+
" return chain.run(prompt)\n",
|
| 198 |
+
" # And will return responses "
|
| 199 |
+
]
|
| 200 |
+
},
|
| 201 |
+
{
|
| 202 |
+
"cell_type": "code",
|
| 203 |
+
"execution_count": null,
|
| 204 |
+
"metadata": {},
|
| 205 |
+
"outputs": [],
|
| 206 |
+
"source": [
|
| 207 |
+
"# Define a string variable to hold the title of the app\n",
|
| 208 |
+
"title = 'π¦π Falcon-40b-Instruct'\n",
|
| 209 |
+
"# Define another string variable to hold the description of the app\n",
|
| 210 |
+
"description = 'This application demonstrates the use of the open-source `Falcon-40b-Instruct` LLM.'\n",
|
| 211 |
+
"# pls subscribe π"
|
| 212 |
+
]
|
| 213 |
+
},
|
| 214 |
+
{
|
| 215 |
+
"cell_type": "code",
|
| 216 |
+
"execution_count": null,
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"outputs": [],
|
| 219 |
+
"source": [
|
| 220 |
+
"# Build gradio interface, define inputs and outputs...just text in this\n",
|
| 221 |
+
"gr.Interface(fn=generate, inputs=[\"text\"], outputs=[\"text\"], \n",
|
| 222 |
+
" # Pass through title and description\n",
|
| 223 |
+
" title=title, description=description, \n",
|
| 224 |
+
" # Set theme and launch parameters\n",
|
| 225 |
+
" theme='finlaymacklon/boxy_violet').launch(server_port=8080, share=True)"
|
| 226 |
+
]
|
| 227 |
+
}
|
| 228 |
+
],
|
| 229 |
+
"metadata": {
|
| 230 |
+
"kernelspec": {
|
| 231 |
+
"display_name": "falcon",
|
| 232 |
+
"language": "python",
|
| 233 |
+
"name": "python3"
|
| 234 |
+
},
|
| 235 |
+
"language_info": {
|
| 236 |
+
"codemirror_mode": {
|
| 237 |
+
"name": "ipython",
|
| 238 |
+
"version": 3
|
| 239 |
+
},
|
| 240 |
+
"file_extension": ".py",
|
| 241 |
+
"mimetype": "text/x-python",
|
| 242 |
+
"name": "python",
|
| 243 |
+
"nbconvert_exporter": "python",
|
| 244 |
+
"pygments_lexer": "ipython3",
|
| 245 |
+
"version": "3.9.12"
|
| 246 |
+
},
|
| 247 |
+
"orig_nbformat": 4
|
| 248 |
+
},
|
| 249 |
+
"nbformat": 4,
|
| 250 |
+
"nbformat_minor": 2
|
| 251 |
+
}
|