Spaces:
Running
on
TPU v5e
Running
on
TPU v5e
File size: 6,979 Bytes
2ca0c5e 40912b5 2ca0c5e 80e88ee 2ca0c5e a2b7758 2ca0c5e 285adea 2ca0c5e b637f0b 2ca0c5e b637f0b 2ca0c5e a2b7758 2ca0c5e 80e88ee 2ca0c5e 80e88ee 2ca0c5e 80e88ee 2ca0c5e 80e88ee 2ca0c5e 80e88ee 2ca0c5e 80e88ee 2ca0c5e 40912b5 2ca0c5e d78f59f 2ca0c5e d78f59f 2ca0c5e d78f59f 2ca0c5e 40912b5 2ca0c5e a2b7758 d78f59f 2ca0c5e d78f59f 2ca0c5e 80e88ee 2ca0c5e 80e88ee |
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 |
import os
# Questions for Gradio
# - Chat share button is enabled by default but thrown an error when clicked.
# - How to add local images in HTML? (https://github.com/gradio-app/gradio/issues/884)
# - How to allow Chatbot to fill the vertical space? (https://github.com/gradio-app/gradio/issues/4001)
# TODO:
# - Add the 1MB models, keras/gemma_1.1_instruct_7b_en
# - Add retry button, for each model individually
# - Add ability to route a message to a single model only.
# - log_applied_layout_map: make it work for Llama3CausalLM and LlamaCausalLM (vicuna)
# - display context length
os.environ["KERAS_BACKEND"] = "jax"
import gradio as gr
from gradio import ChatMessage
import keras_hub
from chatstate import ChatState
from models import (
model_presets,
load_model,
model_labels,
preset_to_website_url,
get_appropriate_chat_template,
)
model_labels_list = list(model_labels)
# load and warm up (compile) all the models
models = []
for preset in model_presets:
model = load_model(preset)
chat_template = get_appropriate_chat_template(preset)
chat_state = ChatState(model, "", chat_template)
prompt, response = chat_state.send_message("Hello")
print("model " + preset + " loaded and initialized.")
print("The model responded: " + response)
models.append(model)
# For local debugging
# model = keras_hub.models.Llama3CausalLM.from_preset(
# "hf://meta-llama/Llama-3.2-1B-Instruct", dtype="bfloat16"
# )
# models = [model, model, model, model, model]
def chat_turn_assistant_1(
model,
message,
history,
system_message,
preset,
# max_tokens,
# temperature,
# top_p,
):
chat_template = get_appropriate_chat_template(preset)
chat_state = ChatState(model, system_message, chat_template)
for msg in history:
msg = ChatMessage(**msg)
if msg.role == "user":
chat_state.add_to_history_as_user(msg.content)
elif msg.role == "assistant":
chat_state.add_to_history_as_model(msg.content)
prompt, response = chat_state.send_message(message)
history.append(ChatMessage(role="assistant", content=response))
return history
def chat_turn_assistant(
message,
sel1,
history1,
sel2,
history2,
system_message,
# max_tokens,
# temperature,
# top_p,
):
history1 = chat_turn_assistant_1(
models[sel1], message, history1, system_message, model_presets[sel1]
)
history2 = chat_turn_assistant_1(
models[sel2], message, history2, system_message, model_presets[sel2]
)
return "", history1, history2
def chat_turn_user_1(message, history):
history.append(ChatMessage(role="user", content=message))
return history
def chat_turn_user(message, history1, history2):
history1 = chat_turn_user_1(message, history1)
history2 = chat_turn_user_1(message, history2)
return "", history1, history2
def bot_icon_select(model_name):
if "gemma" in model_name:
return "img/gemma.png"
elif "llama" in model_name:
return "img/meta.png"
elif "vicuna" in model_name:
return "img/vicuna.png"
elif "mistral" in model_name:
return "img/mistral.png"
# default
return "img/bot.png"
def instantiate_chatbots(sel1, sel2):
model_name1 = model_presets[sel1]
chatbot1 = gr.Chatbot(
type="messages",
show_label=False,
show_share_button=False,
avatar_images=("img/usr.png", bot_icon_select(model_name1)),
)
model_name2 = model_presets[sel2]
chatbot2 = gr.Chatbot(
type="messages",
show_label=False,
show_share_button=False,
avatar_images=("img/usr.png", bot_icon_select(model_name2)),
)
return chatbot1, chatbot2
def instantiate_select_boxes(sel1, sel2, model_labels):
sel1 = gr.Dropdown(
choices=[(name, i) for i, name in enumerate(model_labels)],
show_label=False,
info="<span style='color:black'>Selected model 1:</span> "
+ "<a href='"
+ preset_to_website_url(model_presets[sel1])
+ "'>"
+ preset_to_website_url(model_presets[sel1])
+ "</a>",
value=sel1,
)
sel2 = gr.Dropdown(
choices=[(name, i) for i, name in enumerate(model_labels)],
show_label=False,
info="<span style='color:black'>Selected model 2:</span> "
+ "<a href='"
+ preset_to_website_url(model_presets[sel2])
+ "'>"
+ preset_to_website_url(model_presets[sel2])
+ "</a>",
value=sel2,
)
return sel1, sel2
def instantiate_chatbots_and_select_boxes(sel1, sel2, model_labels):
chatbot1, chatbot2 = instantiate_chatbots(sel1, sel2)
sel1, sel2 = instantiate_select_boxes(sel1, sel2, model_labels)
return sel1, chatbot1, sel2, chatbot2
with gr.Blocks(fill_width=True, title="Keras demo") as demo:
with gr.Row():
gr.Image(
"img/keras_logo_k.png",
width=80,
height=80,
min_width=80,
show_label=False,
show_download_button=False,
show_fullscreen_button=False,
show_share_button=False,
interactive=False,
scale=0,
container=False,
)
gr.HTML(
"<H2> Battle of the Keras chatbots on TPU</H2>"
+ "All the models are loaded into the TPU memory. "
+ "You can call any of them and compare their answers. <br/>"
+ "The entire chat history is fed to the models at every submission. "
+ "This demo is runnig on a Google TPU v5e 2x4 (8 cores).",
)
with gr.Row():
sel1, sel2 = instantiate_select_boxes(0, 1, model_labels_list)
with gr.Row():
chatbot1, chatbot2 = instantiate_chatbots(sel1.value, sel2.value)
msg = gr.Textbox(label="Your message:", submit_btn=True)
with gr.Row():
gr.ClearButton([msg, chatbot1, chatbot2])
with gr.Accordion("Additional settings", open=False):
system_message = gr.Textbox(
label="Sytem prompt",
value="You are a helpful assistant and your name is Eliza.",
)
sel1.select(
lambda sel1, sel2: instantiate_chatbots_and_select_boxes(
sel1, sel2, model_labels_list
),
inputs=[sel1, sel2],
outputs=[sel1, chatbot1, sel2, chatbot2],
)
sel2.select(
lambda sel1, sel2: instantiate_chatbots_and_select_boxes(
sel1, sel2, model_labels_list
),
inputs=[sel1, sel2],
outputs=[sel1, chatbot1, sel2, chatbot2],
)
msg.submit(
chat_turn_user,
inputs=[msg, chatbot1, chatbot2],
outputs=[msg, chatbot1, chatbot2],
).then(
chat_turn_assistant,
[msg, sel1, chatbot1, sel2, chatbot2, system_message],
outputs=[msg, chatbot1, chatbot2],
)
if __name__ == "__main__":
demo.launch()
|