File size: 13,196 Bytes
d4ab11d 9c317f9 bcc5c70 7b8b167 ede06bd bcc5c70 9c317f9 bcc5c70 122bb5c bcc5c70 122bb5c 7b8b167 122bb5c bcc5c70 122bb5c 7eeefc1 459aa64 9c317f9 bcc5c70 7eeefc1 459aa64 7eeefc1 459aa64 7eeefc1 9c317f9 bcc5c70 122bb5c bcc5c70 7b8b167 bcc5c70 459aa64 bcc5c70 459aa64 bcc5c70 459aa64 bcc5c70 459aa64 bcc5c70 459aa64 c941cf9 122bb5c c941cf9 d4ab11d c941cf9 715d92e c941cf9 d4ab11d c941cf9 d4ab11d c941cf9 d4ab11d c941cf9 d4ab11d c941cf9 d4ab11d c941cf9 d4ab11d c941cf9 d4ab11d 7b8b167 d4ab11d c941cf9 d4ab11d c941cf9 d4ab11d 7b8b167 d4ab11d 7b8b167 c941cf9 d4ab11d c941cf9 bcc5c70 459aa64 bcc5c70 d4ab11d 459aa64 bcc5c70 d4ab11d 459aa64 9c317f9 d4ab11d |
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
'''
import os
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_name_2_7B_instruct = "Zyphra/Zamba2-2.7B-instruct"
model_name_7B_instruct = "Zyphra/Zamba2-7B-instruct"
max_context_length = 4096
tokenizer_2_7B_instruct = AutoTokenizer.from_pretrained(model_name_2_7B_instruct)
model_2_7B_instruct = AutoModelForCausalLM.from_pretrained(
model_name_2_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
)
tokenizer_7B_instruct = AutoTokenizer.from_pretrained(model_name_7B_instruct)
model_7B_instruct = AutoModelForCausalLM.from_pretrained(
model_name_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
)
def extract_assistant_response(generated_text):
assistant_token = '<|im_start|> assistant'
end_token = '<|im_end|>'
start_idx = generated_text.rfind(assistant_token)
if start_idx == -1:
# Assistant token not found
return generated_text.strip()
start_idx += len(assistant_token)
end_idx = generated_text.find(end_token, start_idx)
if end_idx == -1:
# End token not found, return from start_idx to end
return generated_text[start_idx:].strip()
else:
return generated_text[start_idx:end_idx].strip()
def generate_response(chat_history, max_new_tokens, model, tokenizer):
sample = []
for turn in chat_history:
if turn[0]:
sample.append({'role': 'user', 'content': turn[0]})
if turn[1]:
sample.append({'role': 'assistant', 'content': turn[1]})
chat_sample = tokenizer.apply_chat_template(sample, tokenize=False)
input_ids = tokenizer(chat_sample, return_tensors='pt', add_special_tokens=False).to(model.device)
max_new_tokens = int(max_new_tokens)
max_input_length = max_context_length - max_new_tokens
if input_ids['input_ids'].size(1) > max_input_length:
input_ids['input_ids'] = input_ids['input_ids'][:, -max_input_length:]
if 'attention_mask' in input_ids:
input_ids['attention_mask'] = input_ids['attention_mask'][:, -max_input_length:]
with torch.no_grad():
outputs = model.generate(**input_ids, max_new_tokens=int(max_new_tokens), return_dict_in_generate=False, output_scores=False, use_cache=True, num_beams=1, do_sample=False)
"""
outputs = model.generate(
input_ids=input_ids,
max_new_tokens=int(max_new_tokens),
do_sample=True,
use_cache=True,
temperature=temperature,
top_k=int(top_k),
top_p=top_p,
repetition_penalty=repetition_penalty,
num_beams=int(num_beams),
length_penalty=length_penalty,
num_return_sequences=1
)
"""
generated_text = tokenizer.decode(outputs[0])
assistant_response = extract_assistant_response(generated_text)
del input_ids
del outputs
torch.cuda.empty_cache()
return assistant_response
with gr.Blocks() as demo:
gr.Markdown("# Zamba2 Model Selector")
with gr.Tabs():
with gr.TabItem("7B Instruct Model"):
gr.Markdown("### Zamba2-7B Instruct Model")
with gr.Column():
chat_history_7B_instruct = gr.State([])
chatbot_7B_instruct = gr.Chatbot()
message_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
with gr.Accordion("Generation Parameters", open=False):
max_new_tokens_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
# temperature_7B_instruct = gr.Slider(0.1, 1.5, step=0.1, value=0.2, label="Temperature")
# top_k_7B_instruct = gr.Slider(1, 100, step=1, value=50, label="Top K")
# top_p_7B_instruct = gr.Slider(0.1, 1.0, step=0.1, value=1.0, label="Top P")
# repetition_penalty_7B_instruct = gr.Slider(1.0, 2.0, step=0.1, value=1.2, label="Repetition Penalty")
# num_beams_7B_instruct = gr.Slider(1, 10, step=1, value=1, label="Number of Beams")
# length_penalty_7B_instruct = gr.Slider(0.0, 2.0, step=0.1, value=1.0, label="Length Penalty")
def user_message_7B_instruct(message, chat_history):
chat_history = chat_history + [[message, None]]
return gr.update(value=""), chat_history, chat_history
def bot_response_7B_instruct(chat_history, max_new_tokens):
response = generate_response(chat_history, max_new_tokens, model_7B_instruct, tokenizer_7B_instruct)
chat_history[-1][1] = response
return chat_history, chat_history
send_button_7B_instruct = gr.Button("Send")
send_button_7B_instruct.click(
fn=user_message_7B_instruct,
inputs=[message_7B_instruct, chat_history_7B_instruct],
outputs=[message_7B_instruct, chat_history_7B_instruct, chatbot_7B_instruct]
).then(
fn=bot_response_7B_instruct,
inputs=[
chat_history_7B_instruct,
max_new_tokens_7B_instruct
],
outputs=[chat_history_7B_instruct, chatbot_7B_instruct]
)
with gr.TabItem("2.7B Instruct Model"):
gr.Markdown("### Zamba2-2.7B Instruct Model")
with gr.Column():
chat_history_2_7B_instruct = gr.State([])
chatbot_2_7B_instruct = gr.Chatbot()
message_2_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
with gr.Accordion("Generation Parameters", open=False):
max_new_tokens_2_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
# temperature_2_7B_instruct = gr.Slider(0.1, 1.5, step=0.1, value=0.2, label="Temperature")
# top_k_2_7B_instruct = gr.Slider(1, 100, step=1, value=50, label="Top K")
# top_p_2_7B_instruct = gr.Slider(0.1, 1.0, step=0.1, value=1.0, label="Top P")
# repetition_penalty_2_7B_instruct = gr.Slider(1.0, 2.0, step=0.1, value=1.2, label="Repetition Penalty")
# num_beams_2_7B_instruct = gr.Slider(1, 10, step=1, value=1, label="Number of Beams")
# length_penalty_2_7B_instruct = gr.Slider(0.0, 2.0, step=0.1, value=1.0, label="Length Penalty")
def user_message_2_7B_instruct(message, chat_history):
chat_history = chat_history + [[message, None]]
return gr.update(value=""), chat_history, chat_history
def bot_response_2_7B_instruct(chat_history, max_new_tokens):
response = generate_response(chat_history, max_new_tokens, model_2_7B_instruct, tokenizer_2_7B_instruct)
chat_history[-1][1] = response
return chat_history, chat_history
send_button_2_7B_instruct = gr.Button("Send")
send_button_2_7B_instruct.click(
fn=user_message_2_7B_instruct,
inputs=[message_2_7B_instruct, chat_history_2_7B_instruct],
outputs=[message_2_7B_instruct, chat_history_2_7B_instruct, chatbot_2_7B_instruct]
).then(
fn=bot_response_2_7B_instruct,
inputs=[
chat_history_2_7B_instruct,
max_new_tokens_2_7B_instruct
],
outputs=[chat_history_2_7B_instruct, chatbot_2_7B_instruct]
)
if __name__ == "__main__":
demo.queue().launch(max_threads=1)
'''
import os
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
import torch
import threading
import re
model_name_2_7B_instruct = "Zyphra/Zamba2-2.7B-instruct"
model_name_7B_instruct = "Zyphra/Zamba2-7B-instruct"
max_context_length = 4096
tokenizer_2_7B_instruct = AutoTokenizer.from_pretrained(model_name_2_7B_instruct)
model_2_7B_instruct = AutoModelForCausalLM.from_pretrained(
model_name_2_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
)
tokenizer_7B_instruct = AutoTokenizer.from_pretrained(model_name_7B_instruct)
model_7B_instruct = AutoModelForCausalLM.from_pretrained(
model_name_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
)
def generate_response(chat_history, max_new_tokens, model, tokenizer):
sample = []
for turn in chat_history:
if turn[0]:
sample.append({'role': 'user', 'content': turn[0]})
if turn[1]:
sample.append({'role': 'assistant', 'content': turn[1]})
chat_sample = tokenizer.apply_chat_template(sample, tokenize=False)
input_ids = tokenizer(chat_sample, return_tensors='pt', add_special_tokens=False).to(model.device)
max_new_tokens = int(max_new_tokens)
max_input_length = max_context_length - max_new_tokens
if input_ids['input_ids'].size(1) > max_input_length:
input_ids['input_ids'] = input_ids['input_ids'][:, -max_input_length:]
if 'attention_mask' in input_ids:
input_ids['attention_mask'] = input_ids['attention_mask'][:, -max_input_length:]
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
generation_kwargs = dict(**input_ids, max_new_tokens=int(max_new_tokens), streamer=streamer)
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
thread.start()
assistant_response = ""
for new_text in streamer:
new_text = re.sub(r'^\s*(?i:assistant)[:\s]*', '', new_text)
assistant_response += new_text
yield assistant_response
thread.join()
del input_ids
torch.cuda.empty_cache()
with gr.Blocks() as demo:
gr.Markdown("# Zamba2 Model Selector")
with gr.Tabs():
with gr.TabItem("7B Instruct Model"):
gr.Markdown("### Zamba2-7B Instruct Model")
with gr.Column():
chat_history_7B_instruct = gr.State([])
chatbot_7B_instruct = gr.Chatbot()
message_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
with gr.Accordion("Generation Parameters", open=False):
max_new_tokens_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
def user_message_7B_instruct(message, chat_history):
chat_history = chat_history + [[message, None]]
return gr.update(value=""), chat_history, chat_history
def bot_response_7B_instruct(chat_history, max_new_tokens):
assistant_response_generator = generate_response(chat_history, max_new_tokens, model_7B_instruct, tokenizer_7B_instruct)
for assistant_response in assistant_response_generator:
chat_history[-1][1] = assistant_response
yield chat_history
send_button_7B_instruct = gr.Button("Send")
send_button_7B_instruct.click(
fn=user_message_7B_instruct,
inputs=[message_7B_instruct, chat_history_7B_instruct],
outputs=[message_7B_instruct, chat_history_7B_instruct, chatbot_7B_instruct]
).then(
fn=bot_response_7B_instruct,
inputs=[chat_history_7B_instruct, max_new_tokens_7B_instruct],
outputs=chatbot_7B_instruct,
)
with gr.TabItem("2.7B Instruct Model"):
gr.Markdown("### Zamba2-2.7B Instruct Model")
with gr.Column():
chat_history_2_7B_instruct = gr.State([])
chatbot_2_7B_instruct = gr.Chatbot()
message_2_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
with gr.Accordion("Generation Parameters", open=False):
max_new_tokens_2_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
def user_message_2_7B_instruct(message, chat_history):
chat_history = chat_history + [[message, None]]
return gr.update(value=""), chat_history, chat_history
def bot_response_2_7B_instruct(chat_history, max_new_tokens):
assistant_response_generator = generate_response(chat_history, max_new_tokens, model_2_7B_instruct, tokenizer_2_7B_instruct)
for assistant_response in assistant_response_generator:
chat_history[-1][1] = assistant_response
yield chat_history
send_button_2_7B_instruct = gr.Button("Send")
send_button_2_7B_instruct.click(
fn=user_message_2_7B_instruct,
inputs=[message_2_7B_instruct, chat_history_2_7B_instruct],
outputs=[message_2_7B_instruct, chat_history_2_7B_instruct, chatbot_2_7B_instruct]
).then(
fn=bot_response_2_7B_instruct,
inputs=[chat_history_2_7B_instruct, max_new_tokens_2_7B_instruct],
outputs=chatbot_2_7B_instruct,
)
if __name__ == "__main__":
demo.queue().launch(max_threads=1)
|