File size: 5,940 Bytes
c208d89 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e0f555c6-4f5d-4f2d-93ab-8106d2c470dc",
"metadata": {
"jupyter": {
"source_hidden": true
},
"id": "e0f555c6-4f5d-4f2d-93ab-8106d2c470dc"
},
"outputs": [],
"source": [
"!pip install -q accelerate sentencepiece torch transformers"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1mncI66sFR9a",
"metadata": {
"id": "1mncI66sFR9a",
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"!pip install -q --upgrade gradio"
]
},
{
"cell_type": "markdown",
"source": [
"### Inference with Gradio but no streaming"
],
"metadata": {
"id": "0q800RsXd6Nj"
},
"id": "0q800RsXd6Nj"
},
{
"cell_type": "code",
"execution_count": null,
"id": "26153855-215a-4289-b4ed-a1cb935ebe69",
"metadata": {
"jupyter": {
"source_hidden": true
},
"scrolled": true,
"id": "26153855-215a-4289-b4ed-a1cb935ebe69"
},
"outputs": [],
"source": [
"import gradio as gr\n",
"import torch\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
"\n",
"base_model = \"TokenBender/evolvedSeeker_1_3\"\n",
"tokenizer = AutoTokenizer.from_pretrained(base_model)\n",
"model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float16)\n",
"model.config.use_cache = True\n",
"model = model.to('cuda:0')\n",
"\n",
"def predict(message, history):\n",
" history_transformed = [{'role': 'system', 'content': \"You are a helpful coding assistant, provide code based on the given query in context.\\n\"}]\n",
" for msg in history:\n",
" history_transformed.append({'role': 'user', 'content': msg[0]})\n",
" history_transformed.append({'role': 'assistant', 'content': msg[1]})\n",
"\n",
" history_transformed.append({'role': 'user', 'content': message})\n",
"\n",
" inputs = tokenizer.apply_chat_template(history_transformed, return_tensors=\"pt\").to(model.device)\n",
" outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, top_k=10, top_p=0.95, num_return_sequences=1, eos_token_id=32021)\n",
" response = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)\n",
" yield response\n",
"\n",
"gr.ChatInterface(predict).queue().launch(share=True)\n"
]
},
{
"cell_type": "markdown",
"source": [
"### Inference without gradio"
],
"metadata": {
"id": "0gpUWgWtdhOi"
},
"id": "0gpUWgWtdhOi"
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f5f98f1-430e-45a0-b4b3-6a3340b5efcf",
"metadata": {
"id": "7f5f98f1-430e-45a0-b4b3-6a3340b5efcf"
},
"outputs": [],
"source": [
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"tokenizer = AutoTokenizer.from_pretrained(\"TokenBender/evolvedSeeker_1_3\", trust_remote_code=True)\n",
"model = AutoModelForCausalLM.from_pretrained(\"TokenBender/evolvedSeeker_1_3\", trust_remote_code=True).cuda()\n",
"messages=[\n",
" {'role': 'system', 'content': \"You are EvolvedSeeker, a model fine-tuned by TokenBender for coding assistant role. Help the user in a friendly, curious manner.\"},\n",
" { 'role': 'user', 'content': \"Hi, who are you?.\"}\n",
"]\n",
"inputs = tokenizer.apply_chat_template(messages, return_tensors=\"pt\").to(model.device)\n",
"# 32021 is the id of <|EOT|> token\n",
"outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, top_k=10, top_p=0.95, num_return_sequences=1, eos_token_id=32021)\n",
"print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))"
]
},
{
"cell_type": "markdown",
"source": [
"### Chat further"
],
"metadata": {
"id": "TsFjwbtadqsJ"
},
"id": "TsFjwbtadqsJ"
},
{
"cell_type": "code",
"execution_count": null,
"id": "a15a4f07-846f-4b89-bdcc-21b7c182e614",
"metadata": {
"id": "a15a4f07-846f-4b89-bdcc-21b7c182e614"
},
"outputs": [],
"source": [
"messages=[\n",
" {'role': 'system', 'content': \"You are EvolvedSeeker, a model fine-tuned by TokenBender for coding assistant role. Help the user in a friendly, curious manner.\"},\n",
" { 'role': 'user', 'content': \"Write a python program to create a snake game.\"}\n",
"]\n",
"inputs = tokenizer.apply_chat_template(messages, return_tensors=\"pt\").to(model.device)\n",
"# 32021 is the id of <|EOT|> token\n",
"outputs = model.generate(inputs, max_new_tokens=2048, do_sample=False, top_k=10, top_p=0.95, num_return_sequences=1, eos_token_id=32021)\n",
"print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"machine_shape": "hm",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
} |