Upload Gradio_Interface_Llama3_1_Inference_Notebook.ipynb
Browse files
Gradio_Interface_Llama3_1_Inference_Notebook.ipynb
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "gdf-gWhrxVLc"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"(Loading can take a long time. Please be patient.)"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "code",
|
14 |
+
"execution_count": 1,
|
15 |
+
"metadata": {
|
16 |
+
"id": "RPA4EuwJHudN"
|
17 |
+
},
|
18 |
+
"outputs": [],
|
19 |
+
"source": [
|
20 |
+
"# @title Installing Requirements\n",
|
21 |
+
"%%capture\n",
|
22 |
+
"!pip install gradio\n",
|
23 |
+
"# Installs Unsloth, Xformers (Flash Attention) and all other packages!\n",
|
24 |
+
"!pip install \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\"\n",
|
25 |
+
"\n",
|
26 |
+
"# We have to check which Torch version for Xformers (2.3 -> 0.0.27)\n",
|
27 |
+
"from torch import __version__; from packaging.version import Version as V\n",
|
28 |
+
"xformers = \"xformers==0.0.27\" if V(__version__) < V(\"2.4.0\") else \"xformers\"\n",
|
29 |
+
"!pip install --no-deps {xformers} trl peft accelerate bitsandbytes triton\n",
|
30 |
+
"\n",
|
31 |
+
"from unsloth import FastLanguageModel\n",
|
32 |
+
"import torch\n",
|
33 |
+
"max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!\n",
|
34 |
+
"dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+\n",
|
35 |
+
"load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.\n",
|
36 |
+
"\n",
|
37 |
+
"# 4bit pre quantized models we support for 4x faster downloading + no OOMs.\n",
|
38 |
+
"fourbit_models = [\n",
|
39 |
+
" #\"unsloth/Meta-Llama-3.1-8B-bnb-4bit\", # Llama-3.1 15 trillion tokens model 2x faster!\n",
|
40 |
+
" \"Solshine/mergekit-passthrough-aogjzpb\",\n",
|
41 |
+
" #\"unsloth/Meta-Llama-3.1-70B-bnb-4bit\",\n",
|
42 |
+
" #\"unsloth/Meta-Llama-3.1-405B-bnb-4bit\", # We also uploaded 4bit for 405b!\n",
|
43 |
+
" #\"unsloth/Mistral-Nemo-Base-2407-bnb-4bit\", # New Mistral 12b 2x faster!\n",
|
44 |
+
" #\"unsloth/Mistral-Nemo-Instruct-2407-bnb-4bit\",\n",
|
45 |
+
" #\"unsloth/mistral-7b-v0.3-bnb-4bit\", # Mistral v3 2x faster!\n",
|
46 |
+
" #\"unsloth/mistral-7b-instruct-v0.3-bnb-4bit\",\n",
|
47 |
+
" #\"unsloth/Phi-3.5-mini-instruct\", # Phi-3.5 2x faster!\n",
|
48 |
+
" #\"unsloth/Phi-3-medium-4k-instruct\",\n",
|
49 |
+
" #\"unsloth/gemma-2-9b-bnb-4bit\",\n",
|
50 |
+
" #\"unsloth/gemma-2-27b-bnb-4bit\", # Gemma 2x faster!\n",
|
51 |
+
"] # More models at https://huggingface.co/unsloth\n",
|
52 |
+
"\n",
|
53 |
+
"model, tokenizer = FastLanguageModel.from_pretrained(\n",
|
54 |
+
" model_name = \"Solshine/mergekit-passthrough-aogjzpb\",\n",
|
55 |
+
" max_seq_length = max_seq_length,\n",
|
56 |
+
" dtype = dtype,\n",
|
57 |
+
" load_in_4bit = load_in_4bit,\n",
|
58 |
+
" token = \"XXXXX\", # use one if using gated models like meta-llama/Llama-2-7b-hf\n",
|
59 |
+
"\n",
|
60 |
+
")\n",
|
61 |
+
"\n",
|
62 |
+
"alpaca_prompt = \"\"\"Below is an instruction that describes a task. Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags. If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags. Write a response that appropriately completes the request.\n",
|
63 |
+
"\n",
|
64 |
+
"### Instruction:\n",
|
65 |
+
"{}\n",
|
66 |
+
"\n",
|
67 |
+
"### Response:\n",
|
68 |
+
"{}\"\"\"\n",
|
69 |
+
"\n",
|
70 |
+
"EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN\n",
|
71 |
+
"def formatting_prompts_func(examples):\n",
|
72 |
+
" instructions = examples[\"prompt\"]\n",
|
73 |
+
" outputs = examples[\"response\"]\n",
|
74 |
+
" texts = []\n",
|
75 |
+
" for instruction, output in zip(instructions, outputs):\n",
|
76 |
+
" # Must add EOS_TOKEN, otherwise your generation will go on forever!\n",
|
77 |
+
" text = alpaca_prompt.format(instruction, output) + EOS_TOKEN\n",
|
78 |
+
" texts.append(text)\n",
|
79 |
+
" return {\"text\": texts}"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"cell_type": "code",
|
84 |
+
"execution_count": 5,
|
85 |
+
"metadata": {
|
86 |
+
"id": "Rh4hKp8AglIY"
|
87 |
+
},
|
88 |
+
"outputs": [],
|
89 |
+
"source": [
|
90 |
+
"# Ensure model is prepared for inference\n",
|
91 |
+
"model = FastLanguageModel.for_inference(model)"
|
92 |
+
]
|
93 |
+
},
|
94 |
+
{
|
95 |
+
"cell_type": "code",
|
96 |
+
"execution_count": null,
|
97 |
+
"metadata": {
|
98 |
+
"colab": {
|
99 |
+
"base_uri": "https://localhost:8080/",
|
100 |
+
"height": 610
|
101 |
+
},
|
102 |
+
"id": "u9SeBZ6WoHD0",
|
103 |
+
"outputId": "8950b091-4b4d-4353-a6aa-ed0699cd7955"
|
104 |
+
},
|
105 |
+
"outputs": [
|
106 |
+
{
|
107 |
+
"name": "stdout",
|
108 |
+
"output_type": "stream",
|
109 |
+
"text": [
|
110 |
+
"Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
|
111 |
+
"Running on public URL: https://88a900ce9e5af38193.gradio.live\n",
|
112 |
+
"\n",
|
113 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
|
114 |
+
]
|
115 |
+
},
|
116 |
+
{
|
117 |
+
"data": {
|
118 |
+
"text/html": [
|
119 |
+
"<div><iframe src=\"https://88a900ce9e5af38193.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
120 |
+
],
|
121 |
+
"text/plain": [
|
122 |
+
"<IPython.core.display.HTML object>"
|
123 |
+
]
|
124 |
+
},
|
125 |
+
"metadata": {},
|
126 |
+
"output_type": "display_data"
|
127 |
+
}
|
128 |
+
],
|
129 |
+
"source": [
|
130 |
+
"# @title Create Gradio User Inferface\n",
|
131 |
+
"import gradio as gr\n",
|
132 |
+
"\n",
|
133 |
+
"MASTER_PROMPT = \"\"\"You are an assistant who answers all questions by first thinking internally using thinking tags, then reflects on your thinking, then answers, and then reflects on your answer offering any needed corrections. You are concise and accurate.\"\"\"\n",
|
134 |
+
"\n",
|
135 |
+
"# Add this function for the chatbot\n",
|
136 |
+
"def chatbot(message, history):\n",
|
137 |
+
" # Combine history and master prompt and new message\n",
|
138 |
+
" full_prompt = MASTER_PROMPT + \"\\n\\n\" + \"\\n\".join([f\"Human: {h[0]}\\nAI: {h[1]}\" for h in history])\n",
|
139 |
+
" full_prompt += f\"\\nHuman: {message}\\nAI:\"\n",
|
140 |
+
"\n",
|
141 |
+
" inputs = tokenizer([alpaca_prompt.format(full_prompt, \"\")], return_tensors=\"pt\").to(\"cuda\")\n",
|
142 |
+
"\n",
|
143 |
+
" output = model.generate(**inputs, max_new_tokens=1000, temperature=0.7)\n",
|
144 |
+
" response = tokenizer.decode(output[0], skip_special_tokens=True)\n",
|
145 |
+
"\n",
|
146 |
+
" # Extract the AI's response\n",
|
147 |
+
" ai_response = response.split(\"AI:\")[-1].strip()\n",
|
148 |
+
"\n",
|
149 |
+
" return ai_response\n",
|
150 |
+
"\n",
|
151 |
+
"# Create the Gradio interface\n",
|
152 |
+
"iface = gr.ChatInterface(\n",
|
153 |
+
" chatbot,\n",
|
154 |
+
" title=\"AI Assistant\",\n",
|
155 |
+
" description=\"Ask questions, get reflections.\",\n",
|
156 |
+
" theme=\"soft\",\n",
|
157 |
+
" examples=[\n",
|
158 |
+
" \"How do I make fertilizer for tomatoes in Oregon?\",\n",
|
159 |
+
" \"What is Korean Natural Farming?\",\n",
|
160 |
+
" \"Can you explain the philosophy behind quantum physics?\",\n",
|
161 |
+
" \"Please explain quantum entanglement in simple terms.\",\n",
|
162 |
+
" ],\n",
|
163 |
+
")\n",
|
164 |
+
"\n",
|
165 |
+
"# Launch the interface\n",
|
166 |
+
"iface.launch(share=True, debug=True)\n"
|
167 |
+
]
|
168 |
+
}
|
169 |
+
],
|
170 |
+
"metadata": {
|
171 |
+
"accelerator": "GPU",
|
172 |
+
"colab": {
|
173 |
+
"gpuType": "T4",
|
174 |
+
"provenance": []
|
175 |
+
},
|
176 |
+
"kernelspec": {
|
177 |
+
"display_name": "Python 3",
|
178 |
+
"name": "python3"
|
179 |
+
},
|
180 |
+
"language_info": {
|
181 |
+
"name": "python"
|
182 |
+
}
|
183 |
+
},
|
184 |
+
"nbformat": 4,
|
185 |
+
"nbformat_minor": 0
|
186 |
+
}
|