7 changed files with 367 additions and 285 deletions
@ -1,267 +1,277 @@ |
|||||||
{ |
{ |
||||||
"nbformat": 4, |
"cells": [ |
||||||
"nbformat_minor": 0, |
{ |
||||||
"metadata": { |
"cell_type": "code", |
||||||
"colab": { |
"execution_count": null, |
||||||
"provenance": [], |
"metadata": { |
||||||
"gpuType": "T4" |
"id": "kU2JrcPlhwd9" |
||||||
}, |
}, |
||||||
"kernelspec": { |
"outputs": [], |
||||||
"name": "python3", |
"source": [ |
||||||
"display_name": "Python 3" |
"!pip install -q requests torch bitsandbytes transformers sentencepiece accelerate gradio" |
||||||
}, |
] |
||||||
"language_info": { |
|
||||||
"name": "python" |
|
||||||
}, |
|
||||||
"accelerator": "GPU" |
|
||||||
}, |
}, |
||||||
"cells": [ |
{ |
||||||
{ |
"cell_type": "markdown", |
||||||
"cell_type": "code", |
"metadata": { |
||||||
"source": [ |
"id": "lAMIVT4iwNg0" |
||||||
"!pip install -q requests torch bitsandbytes transformers sentencepiece accelerate gradio" |
}, |
||||||
], |
"source": [ |
||||||
"metadata": { |
"**Imports**" |
||||||
"id": "kU2JrcPlhwd9" |
] |
||||||
}, |
}, |
||||||
"execution_count": null, |
{ |
||||||
"outputs": [] |
"cell_type": "code", |
||||||
}, |
"execution_count": 2, |
||||||
{ |
"metadata": { |
||||||
"cell_type": "markdown", |
"id": "-Apd7-p-hyLk" |
||||||
"source": [ |
}, |
||||||
"**Imports**" |
"outputs": [], |
||||||
], |
"source": [ |
||||||
"metadata": { |
"import os\n", |
||||||
"id": "lAMIVT4iwNg0" |
"import requests\n", |
||||||
} |
"from google.colab import drive\n", |
||||||
}, |
"from huggingface_hub import login\n", |
||||||
{ |
"from google.colab import userdata\n", |
||||||
"cell_type": "code", |
"from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer, BitsAndBytesConfig\n", |
||||||
"source": [ |
"import torch\n", |
||||||
"import os\n", |
"import gradio as gr\n", |
||||||
"import requests\n", |
"\n", |
||||||
"from google.colab import drive\n", |
"hf_token = userdata.get('HF_TOKEN')\n", |
||||||
"from huggingface_hub import login\n", |
"login(hf_token, add_to_git_credential=True)" |
||||||
"from google.colab import userdata\n", |
] |
||||||
"from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer, BitsAndBytesConfig\n", |
}, |
||||||
"import torch\n", |
{ |
||||||
"import gradio as gr\n", |
"cell_type": "markdown", |
||||||
"\n", |
"metadata": { |
||||||
"hf_token = userdata.get('HF_TOKEN')\n", |
"id": "xa0qYqZrwQ66" |
||||||
"login(hf_token, add_to_git_credential=True)" |
}, |
||||||
], |
"source": [ |
||||||
"metadata": { |
"**Model**" |
||||||
"id": "-Apd7-p-hyLk" |
] |
||||||
}, |
}, |
||||||
"execution_count": 2, |
{ |
||||||
"outputs": [] |
"cell_type": "code", |
||||||
}, |
"execution_count": null, |
||||||
{ |
"metadata": { |
||||||
"cell_type": "markdown", |
"id": "z5enGmuKjtJu" |
||||||
"source": [ |
}, |
||||||
"**Model**" |
"outputs": [], |
||||||
], |
"source": [ |
||||||
"metadata": { |
"model_name = \"meta-llama/Meta-Llama-3.1-8B-Instruct\"\n", |
||||||
"id": "xa0qYqZrwQ66" |
"quant_config = BitsAndBytesConfig(\n", |
||||||
} |
" load_in_4bit=True,\n", |
||||||
}, |
" bnb_4bit_use_double_quant=True,\n", |
||||||
{ |
" bnb_4bit_compute_dtype=torch.bfloat16,\n", |
||||||
"cell_type": "code", |
" bnb_4bit_quant_type=\"nf4\"\n", |
||||||
"source": [ |
")\n", |
||||||
"model_name = \"meta-llama/Meta-Llama-3.1-8B-Instruct\"\n", |
"\n", |
||||||
"quant_config = BitsAndBytesConfig(\n", |
"model = AutoModelForCausalLM.from_pretrained(\n", |
||||||
" load_in_4bit=True,\n", |
" model_name,\n", |
||||||
" bnb_4bit_use_double_quant=True,\n", |
" device_map=\"auto\",\n", |
||||||
" bnb_4bit_compute_dtype=torch.bfloat16,\n", |
" quantization_config=quant_config\n", |
||||||
" bnb_4bit_quant_type=\"nf4\"\n", |
")" |
||||||
")\n", |
] |
||||||
"\n", |
}, |
||||||
"model = AutoModelForCausalLM.from_pretrained(\n", |
{ |
||||||
" model_name,\n", |
"cell_type": "markdown", |
||||||
" device_map=\"auto\",\n", |
"metadata": { |
||||||
" quantization_config=quant_config\n", |
"id": "y1hUSmWlwSbp" |
||||||
")" |
}, |
||||||
], |
"source": [ |
||||||
"metadata": { |
"**Tokenizer**" |
||||||
"id": "z5enGmuKjtJu" |
] |
||||||
}, |
}, |
||||||
"execution_count": null, |
{ |
||||||
"outputs": [] |
"cell_type": "code", |
||||||
}, |
"execution_count": 4, |
||||||
{ |
"metadata": { |
||||||
"cell_type": "markdown", |
"id": "WjxNWW6bvdgj" |
||||||
"source": [ |
}, |
||||||
"**Tokenizer**" |
"outputs": [], |
||||||
], |
"source": [ |
||||||
"metadata": { |
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n", |
||||||
"id": "y1hUSmWlwSbp" |
"tokenizer.pad_token = tokenizer.eos_token" |
||||||
} |
] |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
"cell_type": "code", |
"cell_type": "markdown", |
||||||
"source": [ |
"metadata": { |
||||||
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n", |
"id": "1pg2U-B3wbIK" |
||||||
"tokenizer.pad_token = tokenizer.eos_token" |
}, |
||||||
], |
"source": [ |
||||||
"metadata": { |
"**Functions**" |
||||||
"id": "WjxNWW6bvdgj" |
] |
||||||
}, |
}, |
||||||
"execution_count": 4, |
{ |
||||||
"outputs": [] |
"cell_type": "code", |
||||||
}, |
"execution_count": 12, |
||||||
{ |
"metadata": { |
||||||
"cell_type": "markdown", |
"id": "ZvljDKdji8iV" |
||||||
"source": [ |
}, |
||||||
"**Functions**" |
"outputs": [], |
||||||
], |
"source": [ |
||||||
"metadata": { |
"def generate_dataset(topic, number_of_data, inst1, resp1, inst2, resp2, inst3, resp3):\n", |
||||||
"id": "1pg2U-B3wbIK" |
" # Convert user inputs into multi-shot examples\n", |
||||||
} |
" multi_shot_examples = [\n", |
||||||
}, |
" {\"instruction\": inst1, \"response\": resp1},\n", |
||||||
{ |
" {\"instruction\": inst2, \"response\": resp2},\n", |
||||||
"cell_type": "code", |
" {\"instruction\": inst3, \"response\": resp3}\n", |
||||||
"source": [ |
" ]\n", |
||||||
"def generate_dataset(topic, number_of_data, inst1, resp1, inst2, resp2, inst3, resp3):\n", |
"\n", |
||||||
" # Convert user inputs into multi-shot examples\n", |
" # System prompt\n", |
||||||
" multi_shot_examples = [\n", |
" system_prompt = f\"\"\"\n", |
||||||
" {\"instruction\": inst1, \"response\": resp1},\n", |
" You are a helpful assistant whose main purpose is to generate datasets.\n", |
||||||
" {\"instruction\": inst2, \"response\": resp2},\n", |
" Topic: {topic}\n", |
||||||
" {\"instruction\": inst3, \"response\": resp3}\n", |
" Return the dataset in JSON format. Use examples with simple, fun, and easy-to-understand instructions for kids.\n", |
||||||
" ]\n", |
" Include the following examples: {multi_shot_examples}\n", |
||||||
"\n", |
" Return {number_of_data} examples each time.\n", |
||||||
" # System prompt\n", |
" Do not repeat the provided examples.\n", |
||||||
" system_prompt = f\"\"\"\n", |
" \"\"\"\n", |
||||||
" You are a helpful assistant whose main purpose is to generate datasets.\n", |
"\n", |
||||||
" Topic: {topic}\n", |
" # Example Messages\n", |
||||||
" Return the dataset in JSON format. Use examples with simple, fun, and easy-to-understand instructions for kids.\n", |
" messages = [\n", |
||||||
" Include the following examples: {multi_shot_examples}\n", |
" {\"role\": \"system\", \"content\": system_prompt},\n", |
||||||
" Return {number_of_data} examples each time.\n", |
" {\"role\": \"user\", \"content\": f\"Please generate my dataset for {topic}\"}\n", |
||||||
" Do not repeat the provided examples.\n", |
" ]\n", |
||||||
" \"\"\"\n", |
"\n", |
||||||
"\n", |
" # Tokenize Input\n", |
||||||
" # Example Messages\n", |
" inputs = tokenizer.apply_chat_template(messages, return_tensors=\"pt\").to(\"cuda\")\n", |
||||||
" messages = [\n", |
" streamer = TextStreamer(tokenizer)\n", |
||||||
" {\"role\": \"system\", \"content\": system_prompt},\n", |
"\n", |
||||||
" {\"role\": \"user\", \"content\": f\"Please generate my dataset for {topic}\"}\n", |
" # Generate Output\n", |
||||||
" ]\n", |
" outputs = model.generate(inputs, max_new_tokens=2000, streamer=streamer)\n", |
||||||
"\n", |
"\n", |
||||||
" # Tokenize Input\n", |
" # Decode and Return\n", |
||||||
" inputs = tokenizer.apply_chat_template(messages, return_tensors=\"pt\").to(\"cuda\")\n", |
" return tokenizer.decode(outputs[0], skip_special_tokens=True)\n", |
||||||
" streamer = TextStreamer(tokenizer)\n", |
"\n", |
||||||
"\n", |
"\n", |
||||||
" # Generate Output\n", |
"def gradio_interface(topic, number_of_data, inst1, resp1, inst2, resp2, inst3, resp3):\n", |
||||||
" outputs = model.generate(inputs, max_new_tokens=2000, streamer=streamer)\n", |
" return generate_dataset(topic, number_of_data, inst1, resp1, inst2, resp2, inst3, resp3)" |
||||||
"\n", |
] |
||||||
" # Decode and Return\n", |
}, |
||||||
" return tokenizer.decode(outputs[0], skip_special_tokens=True)\n", |
{ |
||||||
"\n", |
"cell_type": "markdown", |
||||||
"\n", |
"metadata": { |
||||||
"def gradio_interface(topic, number_of_data, inst1, resp1, inst2, resp2, inst3, resp3):\n", |
"id": "_WDZ5dvRwmng" |
||||||
" return generate_dataset(topic, number_of_data, inst1, resp1, inst2, resp2, inst3, resp3)" |
}, |
||||||
], |
"source": [ |
||||||
"metadata": { |
"**Default Values**" |
||||||
"id": "ZvljDKdji8iV" |
] |
||||||
}, |
}, |
||||||
"execution_count": 12, |
{ |
||||||
"outputs": [] |
"cell_type": "code", |
||||||
}, |
"execution_count": 13, |
||||||
{ |
"metadata": { |
||||||
"cell_type": "markdown", |
"id": "JAdfqYXnvEDE" |
||||||
"source": [ |
}, |
||||||
"**Default Values**" |
"outputs": [], |
||||||
], |
"source": [ |
||||||
"metadata": { |
"default_topic = \"Talking to a (5-8) years old and teaching them manners.\"\n", |
||||||
"id": "_WDZ5dvRwmng" |
"default_number_of_data = 10\n", |
||||||
} |
"default_multi_shot_examples = [\n", |
||||||
}, |
" {\n", |
||||||
{ |
" \"instruction\": \"Why do I have to say please when I want something?\",\n", |
||||||
"cell_type": "code", |
" \"response\": \"Because it’s like magic! It shows you’re nice, and people want to help you more.\"\n", |
||||||
"source": [ |
" },\n", |
||||||
"default_topic = \"Talking to a (5-8) years old and teaching them manners.\"\n", |
" {\n", |
||||||
"default_number_of_data = 10\n", |
" \"instruction\": \"What should I say if someone gives me a toy?\",\n", |
||||||
"default_multi_shot_examples = [\n", |
" \"response\": \"You say, 'Thank you!' because it makes them happy you liked it.\"\n", |
||||||
" {\n", |
" },\n", |
||||||
" \"instruction\": \"Why do I have to say please when I want something?\",\n", |
" {\n", |
||||||
" \"response\": \"Because it’s like magic! It shows you’re nice, and people want to help you more.\"\n", |
" \"instruction\": \"why should I listen to my parents?\",\n", |
||||||
" },\n", |
" \"response\": \"Because parents want the best for you and they love you the most.\"\n", |
||||||
" {\n", |
" }\n", |
||||||
" \"instruction\": \"What should I say if someone gives me a toy?\",\n", |
"]" |
||||||
" \"response\": \"You say, 'Thank you!' because it makes them happy you liked it.\"\n", |
] |
||||||
" },\n", |
}, |
||||||
" {\n", |
{ |
||||||
" \"instruction\": \"why should I listen to my parents?\",\n", |
"cell_type": "markdown", |
||||||
" \"response\": \"Because parents want the best for you and they love you the most.\"\n", |
"metadata": { |
||||||
" }\n", |
"id": "JwZtD032wuK8" |
||||||
"]" |
}, |
||||||
], |
"source": [ |
||||||
"metadata": { |
"**Init gradio**" |
||||||
"id": "JAdfqYXnvEDE" |
] |
||||||
}, |
}, |
||||||
"execution_count": 13, |
{ |
||||||
"outputs": [] |
"cell_type": "code", |
||||||
}, |
"execution_count": 14, |
||||||
{ |
"metadata": { |
||||||
"cell_type": "markdown", |
"id": "xy2RP5T-vxXg" |
||||||
"source": [ |
}, |
||||||
"**Init gradio**" |
"outputs": [], |
||||||
], |
"source": [ |
||||||
"metadata": { |
"gr_interface = gr.Interface(\n", |
||||||
"id": "JwZtD032wuK8" |
" fn=gradio_interface,\n", |
||||||
} |
" inputs=[\n", |
||||||
}, |
" gr.Textbox(label=\"Topic\", value=default_topic, lines=2),\n", |
||||||
{ |
" gr.Number(label=\"Number of Examples\", value=default_number_of_data, precision=0),\n", |
||||||
"cell_type": "code", |
" gr.Textbox(label=\"Instruction 1\", value=default_multi_shot_examples[0][\"instruction\"]),\n", |
||||||
"source": [ |
" gr.Textbox(label=\"Response 1\", value=default_multi_shot_examples[0][\"response\"]),\n", |
||||||
"gr_interface = gr.Interface(\n", |
" gr.Textbox(label=\"Instruction 2\", value=default_multi_shot_examples[1][\"instruction\"]),\n", |
||||||
" fn=gradio_interface,\n", |
" gr.Textbox(label=\"Response 2\", value=default_multi_shot_examples[1][\"response\"]),\n", |
||||||
" inputs=[\n", |
" gr.Textbox(label=\"Instruction 3\", value=default_multi_shot_examples[2][\"instruction\"]),\n", |
||||||
" gr.Textbox(label=\"Topic\", value=default_topic, lines=2),\n", |
" gr.Textbox(label=\"Response 3\", value=default_multi_shot_examples[2][\"response\"]),\n", |
||||||
" gr.Number(label=\"Number of Examples\", value=default_number_of_data, precision=0),\n", |
" ],\n", |
||||||
" gr.Textbox(label=\"Instruction 1\", value=default_multi_shot_examples[0][\"instruction\"]),\n", |
" outputs=gr.Textbox(label=\"Generated Dataset\")\n", |
||||||
" gr.Textbox(label=\"Response 1\", value=default_multi_shot_examples[0][\"response\"]),\n", |
")" |
||||||
" gr.Textbox(label=\"Instruction 2\", value=default_multi_shot_examples[1][\"instruction\"]),\n", |
] |
||||||
" gr.Textbox(label=\"Response 2\", value=default_multi_shot_examples[1][\"response\"]),\n", |
}, |
||||||
" gr.Textbox(label=\"Instruction 3\", value=default_multi_shot_examples[2][\"instruction\"]),\n", |
{ |
||||||
" gr.Textbox(label=\"Response 3\", value=default_multi_shot_examples[2][\"response\"]),\n", |
"cell_type": "markdown", |
||||||
" ],\n", |
"metadata": { |
||||||
" outputs=gr.Textbox(label=\"Generated Dataset\")\n", |
"id": "HZx-mm9Uw3Ph" |
||||||
")" |
}, |
||||||
], |
"source": [ |
||||||
"metadata": { |
"**Run the app**" |
||||||
"id": "xy2RP5T-vxXg" |
] |
||||||
}, |
}, |
||||||
"execution_count": 14, |
{ |
||||||
"outputs": [] |
"cell_type": "code", |
||||||
}, |
"execution_count": null, |
||||||
{ |
"metadata": { |
||||||
"cell_type": "markdown", |
"id": "bfGs5ip8mndg" |
||||||
"source": [ |
}, |
||||||
"**Run the app**" |
"outputs": [], |
||||||
], |
"source": [ |
||||||
"metadata": { |
"gr_interface.launch()" |
||||||
"id": "HZx-mm9Uw3Ph" |
] |
||||||
} |
}, |
||||||
}, |
{ |
||||||
{ |
"cell_type": "code", |
||||||
"cell_type": "code", |
"execution_count": null, |
||||||
"source": [ |
"metadata": { |
||||||
"gr_interface.launch()" |
"id": "Cveqx392x7Mm" |
||||||
], |
}, |
||||||
"metadata": { |
"outputs": [], |
||||||
"id": "bfGs5ip8mndg" |
"source": [] |
||||||
}, |
} |
||||||
"execution_count": null, |
], |
||||||
"outputs": [] |
"metadata": { |
||||||
}, |
"accelerator": "GPU", |
||||||
{ |
"colab": { |
||||||
"cell_type": "code", |
"gpuType": "T4", |
||||||
"source": [], |
"provenance": [] |
||||||
"metadata": { |
}, |
||||||
"id": "Cveqx392x7Mm" |
"kernelspec": { |
||||||
}, |
"display_name": "Python 3 (ipykernel)", |
||||||
"execution_count": null, |
"language": "python", |
||||||
"outputs": [] |
"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.11.11" |
||||||
|
} |
||||||
|
}, |
||||||
|
"nbformat": 4, |
||||||
|
"nbformat_minor": 4 |
||||||
} |
} |
Loading…
Reference in new issue