Spaces:
Runtime error
Runtime error
File size: 6,149 Bytes
55810aa |
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 |
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
" # Install Dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 --upgrade"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install langchain einops accelerate transformers bitsandbytes"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Import Dependencies"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"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",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from langchain import HuggingFacePipeline\n",
"from langchain import PromptTemplate, LLMChain\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"import transformers\n",
"import os \n",
"import torch"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if cuda is available \n",
"torch.cuda.is_available()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Build the Pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define Model ID\n",
"model_id = \"tiiuae/falcon-40b-instruct\"\n",
"# Load Tokenizer\n",
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
"# Load Model \n",
"model = AutoModelForCausalLM.from_pretrained(model_id, cache_dir='./workspace/', \n",
" torch_dtype=torch.bfloat16, trust_remote_code=True, device_map=\"auto\", offload_folder=\"offload\")\n",
"# Set PT model to inference mode\n",
"model.eval()\n",
"# Build HF Transformers pipeline \n",
"pipeline = transformers.pipeline(\n",
" \"text-generation\", \n",
" model=model,\n",
" tokenizer=tokenizer,\n",
" device_map=\"auto\",\n",
" max_length=400,\n",
" do_sample=True,\n",
" top_k=10,\n",
" num_return_sequences=1,\n",
" eos_token_id=tokenizer.eos_token_id\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Test out the pipeline\n",
"pipeline('who is kim kardashian?')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pass it to Langchain"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Setup prompt template\n",
"template = PromptTemplate(input_variables=['input'], template='{input}') \n",
"# Pass hugging face pipeline to langchain class\n",
"llm = HuggingFacePipeline(pipeline=pipeline) \n",
"# Build stacked LLM chain i.e. prompt-formatting + LLM\n",
"chain = LLMChain(llm=llm, prompt=template)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Test LLMChain \n",
"response = chain.run('who is kim kardashian?')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Build Gradio App"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install Gradio for the UI component\n",
"!pip install gradio"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import gradio for UI\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create generate function - this will be called when a user runs the gradio app \n",
"def generate(prompt): \n",
" # The prompt will get passed to the LLM Chain!\n",
" return chain.run(prompt)\n",
" # And will return responses "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define a string variable to hold the title of the app\n",
"title = 'π¦π Falcon-40b-Instruct'\n",
"# Define another string variable to hold the description of the app\n",
"description = 'This application demonstrates the use of the open-source `Falcon-40b-Instruct` LLM.'\n",
"# pls subscribe π"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Build gradio interface, define inputs and outputs...just text in this\n",
"gr.Interface(fn=generate, inputs=[\"text\"], outputs=[\"text\"], \n",
" # Pass through title and description\n",
" title=title, description=description, \n",
" # Set theme and launch parameters\n",
" theme='finlaymacklon/boxy_violet').launch(server_port=8080, share=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "falcon",
"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.9.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|