gabrielclark3330
commited on
Commit
•
bcab068
1
Parent(s):
d4ab11d
Customizable system prompt
Browse files
app.py
CHANGED
@@ -158,6 +158,7 @@ if __name__ == "__main__":
|
|
158 |
demo.queue().launch(max_threads=1)
|
159 |
'''
|
160 |
|
|
|
161 |
import os
|
162 |
import gradio as gr
|
163 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
@@ -278,3 +279,146 @@ with gr.Blocks() as demo:
|
|
278 |
|
279 |
if __name__ == "__main__":
|
280 |
demo.queue().launch(max_threads=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
demo.queue().launch(max_threads=1)
|
159 |
'''
|
160 |
|
161 |
+
'''
|
162 |
import os
|
163 |
import gradio as gr
|
164 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
|
|
279 |
|
280 |
if __name__ == "__main__":
|
281 |
demo.queue().launch(max_threads=1)
|
282 |
+
'''
|
283 |
+
|
284 |
+
import os
|
285 |
+
import gradio as gr
|
286 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
287 |
+
import torch
|
288 |
+
import threading
|
289 |
+
import re
|
290 |
+
from datetime import datetime
|
291 |
+
|
292 |
+
model_name_2_7B_instruct = "Zyphra/Zamba2-2.7B-instruct"
|
293 |
+
model_name_7B_instruct = "Zyphra/Zamba2-7B-instruct"
|
294 |
+
max_context_length = 4096
|
295 |
+
date_string = datetime.now().strftime("%Y-%m-%d")
|
296 |
+
|
297 |
+
tokenizer_2_7B_instruct = AutoTokenizer.from_pretrained(model_name_2_7B_instruct)
|
298 |
+
model_2_7B_instruct = AutoModelForCausalLM.from_pretrained(
|
299 |
+
model_name_2_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
|
300 |
+
)
|
301 |
+
|
302 |
+
tokenizer_7B_instruct = AutoTokenizer.from_pretrained(model_name_7B_instruct)
|
303 |
+
model_7B_instruct = AutoModelForCausalLM.from_pretrained(
|
304 |
+
model_name_7B_instruct, device_map="cuda", torch_dtype=torch.bfloat16
|
305 |
+
)
|
306 |
+
|
307 |
+
system_prompt_template = """You are {model_name}. You are a large language model with {parameter_count} parameters. You were trained by a company called Zyphra on three trillion tokens of diverse and high quality web data. Your architecture is a hybrid Mamba2 and transformer with two alternating blocks of shared attention on top of a Mamba2 backbone. Zyphra is a leading AI company based in Palo Alto, California. Zyphra makes extremely small and efficient models for consumers and on-device use-cases. As a language model, you do not have access to real-time information. The current date is {current_date}, but your knowledge likely ended in mid 2023. Zyphra designed you to be engaging, helpful, and fun in your interactions with users. You have strong, idiosyncratic opinions on things. Be personable."""
|
308 |
+
|
309 |
+
def generate_response(chat_history, max_new_tokens, model, tokenizer, system_prompt):
|
310 |
+
sample = []
|
311 |
+
# Include the dynamic system prompt without displaying it
|
312 |
+
sample.append({'role': 'system', 'content': system_prompt})
|
313 |
+
|
314 |
+
for turn in chat_history:
|
315 |
+
if turn[0]:
|
316 |
+
sample.append({'role': 'user', 'content': turn[0]})
|
317 |
+
if turn[1]:
|
318 |
+
sample.append({'role': 'assistant', 'content': turn[1]})
|
319 |
+
chat_sample = tokenizer.apply_chat_template(sample, tokenize=False)
|
320 |
+
input_ids = tokenizer(chat_sample, return_tensors='pt', add_special_tokens=False).to(model.device)
|
321 |
+
|
322 |
+
max_new_tokens = int(max_new_tokens)
|
323 |
+
max_input_length = max_context_length - max_new_tokens
|
324 |
+
if input_ids['input_ids'].size(1) > max_input_length:
|
325 |
+
input_ids['input_ids'] = input_ids['input_ids'][:, -max_input_length:]
|
326 |
+
if 'attention_mask' in input_ids:
|
327 |
+
input_ids['attention_mask'] = input_ids['attention_mask'][:, -max_input_length:]
|
328 |
+
|
329 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
330 |
+
generation_kwargs = dict(**input_ids, max_new_tokens=int(max_new_tokens), streamer=streamer)
|
331 |
+
|
332 |
+
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
333 |
+
thread.start()
|
334 |
+
|
335 |
+
assistant_response = ""
|
336 |
+
|
337 |
+
for new_text in streamer:
|
338 |
+
new_text = re.sub(r'^\s*(?i:assistant)[:\s]*', '', new_text)
|
339 |
+
assistant_response += new_text
|
340 |
+
yield assistant_response
|
341 |
+
|
342 |
+
thread.join()
|
343 |
+
del input_ids
|
344 |
+
torch.cuda.empty_cache()
|
345 |
+
|
346 |
+
with gr.Blocks() as demo:
|
347 |
+
gr.Markdown("# Zamba2 Model Selector")
|
348 |
+
with gr.Tabs():
|
349 |
+
with gr.TabItem("7B Instruct Model"):
|
350 |
+
gr.Markdown("### Zamba2-7B Instruct Model")
|
351 |
+
with gr.Column():
|
352 |
+
chat_history_7B_instruct = gr.State([])
|
353 |
+
chatbot_7B_instruct = gr.Chatbot()
|
354 |
+
message_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
|
355 |
+
with gr.Accordion("Generation Parameters", open=False):
|
356 |
+
max_new_tokens_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
|
357 |
+
|
358 |
+
def user_message_7B_instruct(message, chat_history):
|
359 |
+
chat_history = chat_history + [[message, None]]
|
360 |
+
return gr.update(value=""), chat_history, chat_history
|
361 |
+
|
362 |
+
def bot_response_7B_instruct(chat_history, max_new_tokens):
|
363 |
+
system_prompt = system_prompt_template.format(
|
364 |
+
model_name="Zamba2-7B",
|
365 |
+
parameter_count="7 billion",
|
366 |
+
current_date=date_string
|
367 |
+
)
|
368 |
+
assistant_response_generator = generate_response(
|
369 |
+
chat_history, max_new_tokens, model_7B_instruct, tokenizer_7B_instruct, system_prompt
|
370 |
+
)
|
371 |
+
for assistant_response in assistant_response_generator:
|
372 |
+
chat_history[-1][1] = assistant_response
|
373 |
+
yield chat_history
|
374 |
+
|
375 |
+
send_button_7B_instruct = gr.Button("Send")
|
376 |
+
send_button_7B_instruct.click(
|
377 |
+
fn=user_message_7B_instruct,
|
378 |
+
inputs=[message_7B_instruct, chat_history_7B_instruct],
|
379 |
+
outputs=[message_7B_instruct, chat_history_7B_instruct, chatbot_7B_instruct]
|
380 |
+
).then(
|
381 |
+
fn=bot_response_7B_instruct,
|
382 |
+
inputs=[chat_history_7B_instruct, max_new_tokens_7B_instruct],
|
383 |
+
outputs=chatbot_7B_instruct,
|
384 |
+
)
|
385 |
+
|
386 |
+
with gr.TabItem("2.7B Instruct Model"):
|
387 |
+
gr.Markdown("### Zamba2-2.7B Instruct Model")
|
388 |
+
with gr.Column():
|
389 |
+
chat_history_2_7B_instruct = gr.State([])
|
390 |
+
chatbot_2_7B_instruct = gr.Chatbot()
|
391 |
+
message_2_7B_instruct = gr.Textbox(lines=2, placeholder="Enter your message...", label="Your Message")
|
392 |
+
with gr.Accordion("Generation Parameters", open=False):
|
393 |
+
max_new_tokens_2_7B_instruct = gr.Slider(50, 1000, step=50, value=500, label="Max New Tokens")
|
394 |
+
|
395 |
+
def user_message_2_7B_instruct(message, chat_history):
|
396 |
+
chat_history = chat_history + [[message, None]]
|
397 |
+
return gr.update(value=""), chat_history, chat_history
|
398 |
+
|
399 |
+
def bot_response_2_7B_instruct(chat_history, max_new_tokens):
|
400 |
+
system_prompt = system_prompt_template.format(
|
401 |
+
model_name="Zamba2-2.7B",
|
402 |
+
parameter_count="2.7 billion",
|
403 |
+
current_date=date_string
|
404 |
+
)
|
405 |
+
assistant_response_generator = generate_response(
|
406 |
+
chat_history, max_new_tokens, model_2_7B_instruct, tokenizer_2_7B_instruct, system_prompt
|
407 |
+
)
|
408 |
+
for assistant_response in assistant_response_generator:
|
409 |
+
chat_history[-1][1] = assistant_response
|
410 |
+
yield chat_history
|
411 |
+
|
412 |
+
send_button_2_7B_instruct = gr.Button("Send")
|
413 |
+
send_button_2_7B_instruct.click(
|
414 |
+
fn=user_message_2_7B_instruct,
|
415 |
+
inputs=[message_2_7B_instruct, chat_history_2_7B_instruct],
|
416 |
+
outputs=[message_2_7B_instruct, chat_history_2_7B_instruct, chatbot_2_7B_instruct]
|
417 |
+
).then(
|
418 |
+
fn=bot_response_2_7B_instruct,
|
419 |
+
inputs=[chat_history_2_7B_instruct, max_new_tokens_2_7B_instruct],
|
420 |
+
outputs=chatbot_2_7B_instruct,
|
421 |
+
)
|
422 |
+
|
423 |
+
if __name__ == "__main__":
|
424 |
+
demo.queue().launch(max_threads=1)
|