File size: 11,264 Bytes
2ca0c5e
 
40912b5
 
 
 
 
 
 
 
 
 
 
2ca0c5e
 
80e88ee
2ca0c5e
 
 
 
4ef1969
2ca0c5e
 
 
 
 
 
 
 
4ef1969
 
 
 
 
 
 
2ca0c5e
 
a2b7758
ba64cc5
 
 
 
 
 
 
 
 
2ca0c5e
b637f0b
ba64cc5
4ef1969
 
ba64cc5
 
2ca0c5e
 
4ef1969
2ca0c5e
4ef1969
2ca0c5e
 
 
 
 
 
4ef1969
 
2ca0c5e
 
80e88ee
2ca0c5e
 
 
 
 
 
80e88ee
2ca0c5e
 
 
80e88ee
2ca0c5e
4ef1969
 
80e88ee
4ef1969
 
 
2ca0c5e
 
 
4ef1969
2ca0c5e
 
 
 
4ef1969
 
 
 
 
2ca0c5e
 
 
 
 
 
40912b5
2ca0c5e
 
 
 
 
 
 
 
4ef1969
 
 
2ca0c5e
4ef1969
 
 
 
 
 
2ca0c5e
4ef1969
 
 
 
 
2ca0c5e
4ef1969
2ca0c5e
d78f59f
061118b
4ef1969
2ca0c5e
 
 
4ef1969
 
 
 
 
 
 
 
 
 
 
 
2ca0c5e
4ef1969
 
 
 
061118b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ef1969
 
2ca0c5e
 
4ef1969
 
 
 
c16851d
4ef1969
 
 
 
2ca0c5e
061118b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ef1969
 
 
 
2ca0c5e
4ef1969
061118b
4ef1969
 
 
 
 
2ca0c5e
 
 
 
 
 
 
 
 
 
d78f59f
2ca0c5e
40912b5
2ca0c5e
 
 
64919f8
2ca0c5e
3a9ff40
 
 
2ca0c5e
 
4ef1969
 
d78f59f
2ca0c5e
4ef1969
 
 
 
 
 
 
 
 
061118b
 
 
 
 
 
 
 
 
 
 
 
 
4ef1969
 
 
 
 
 
061118b
 
 
 
 
 
 
 
4ef1969
 
 
061118b
 
 
 
 
 
 
4ef1969
 
061118b
 
4ef1969
061118b
4ef1969
 
 
 
 
 
 
 
 
2ca0c5e
4ef1969
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ca0c5e
 
4ef1969
 
 
 
 
 
 
2ca0c5e
 
 
4ef1969
 
 
2ca0c5e
4ef1969
 
 
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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
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 enum import Enum
from models import (
    model_presets,
    load_model,
    model_labels,
    preset_to_website_url,
    get_appropriate_chat_template,
)


class TextRoute(Enum):
    LEFT = 0
    RIGHT = 1
    BOTH = 2


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"
#     "../misc-code/ari_tiny_llama3"
# )
# models = [model, model, model, model, model]


def chat_turn_assistant(
    message,
    sel,
    history,
    system_message,
    # max_tokens,
    # temperature,
    # top_p,
):
    model = models[sel]
    preset = model_presets[sel]
    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_both_assistant(
    message, sel1, sel2, history1, history2, system_message
):
    return (
        chat_turn_assistant(message, sel1, history1, system_message),
        chat_turn_assistant(message, sel2, history2, system_message),
    )


def chat_turn_user(message, history):
    history.append(ChatMessage(role="user", content=message))
    return history


def chat_turn_both_user(message, history1, history2):
    return (
        chat_turn_user(message, history1),
        chat_turn_user(message, 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_select_box(sel, model_labels):
    return gr.Dropdown(
        choices=[(name, i) for i, name in enumerate(model_labels)],
        show_label=False,
        value=sel,
        info="<span style='color:black'>Selected model:</span> <a href='"
        + preset_to_website_url(model_presets[sel])
        + "'>"
        + preset_to_website_url(model_presets[sel])
        + "</a>",
    )


def instantiate_chatbot(sel, key):
    model_name = model_presets[sel]
    return gr.Chatbot(
        type="messages",
        key=key,
        show_label=False,
        show_share_button=False,
        show_copy_all_button=True,
        avatar_images=("img/usr.png", bot_icon_select(model_name)),
    )


def instantiate_arrow_button(route, text_route):
    icons = {
        TextRoute.LEFT: "img/arrowL.png",
        TextRoute.RIGHT: "img/arrowR.png",
        TextRoute.BOTH: "img/arrowRL.png",
    }
    button = gr.Button(
        "",
        size="sm",
        scale=0,
        min_width=40,
        icon=icons[route],
    )
    button.click(lambda: route, outputs=[text_route])
    return button


def instantiate_retry_button(route):
    return gr.Button(
        "",
        size="sm",
        scale=0,
        min_width=40,
        icon="img/retry.png",
    )


def instantiate_trash_button():
    return gr.Button(
        "",
        size="sm",
        scale=0,
        min_width=40,
        icon="img/trash.png",
    )


def instantiate_text_box():
    return gr.Textbox(label="Your message:", submit_btn=True, key="msg")


def instantiate_additional_settings():
    with gr.Accordion("Additional settings", open=False):
        system_message = gr.Textbox(
            label="Sytem prompt",
            key="system_prompt",
            value="You are a helpful assistant and your name is Eliza.",
        )
    return system_message


def retry_fn(history):
    if len(history) >= 2:
        msg = history.pop(-1)  # assistant message
        msg = history.pop(-1)  # user message
        return msg["content"], history
    else:
        return gr.skip(), gr.skip()


def retry_fn_both(history1, history2):
    msg1, history1 = retry_fn(history1)
    msg2, history2 = retry_fn(history2)
    if isinstance(msg1, str) and isinstance(msg2, str):
        if msg1 == msg2:
            msg = msg1
        else:
            msg = msg1 + " / " + msg2
    elif isinstance(msg1, str):
        msg = msg1
    elif isinstance(msg2, str):
        msg = msg2
    else:
        msg = msg1
    return msg, history1, history2


sel1 = instantiate_select_box(0, model_labels_list)
sel2 = instantiate_select_box(1, model_labels_list)
chatbot1 = instantiate_chatbot(sel1.value, "chat1")
chatbot2 = instantiate_chatbot(sel2.value, "chat2")

# to correctly align the left/right arrows
CSS = ".stick-to-the-right {align-items: end; justify-content: end}"

with gr.Blocks(fill_width=True, title="Keras demo", css=CSS) as demo:

    # Where do messages go
    text_route = gr.State(TextRoute.BOTH)

    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>Keras chatbot arena  - running with JAX on TPU</H2>"
            + "All the models are loaded into the TPU memory. "
            + "You can call any of them and compare their answers. "
            + "The entire chat<br/>history is fed to the models at every submission. "
            + "This demo is runnig on a Google TPU v5e 2x4 (8 cores) in bfloat16 precision."
        )
    with gr.Row():
        sel1.render(),
        sel2.render(),

    with gr.Row():
        chatbot1.render()
        chatbot2.render()

    @gr.render(inputs=text_route)
    def render_text_area(route):

        if route == TextRoute.BOTH:
            with gr.Row():
                msg = instantiate_text_box()
                with gr.Column(scale=0, min_width=100):
                    with gr.Row():
                        instantiate_arrow_button(TextRoute.LEFT, text_route)
                        retry = instantiate_retry_button(route)
                    with gr.Row():
                        instantiate_arrow_button(TextRoute.RIGHT, text_route)
                        trash = instantiate_trash_button()
            retry.click(
                retry_fn_both,
                inputs=[chatbot1, chatbot2],
                outputs=[msg, chatbot1, chatbot2],
            )
            trash.click(lambda: ("", [], []), outputs=[msg, chatbot1, chatbot2])

        elif route == TextRoute.LEFT:
            with gr.Row():
                with gr.Column(scale=1):
                    msg = instantiate_text_box()
                with gr.Column(scale=1):
                    with gr.Row():
                        instantiate_arrow_button(TextRoute.RIGHT, text_route)
                        retry = instantiate_retry_button(route)
                    with gr.Row():
                        instantiate_arrow_button(TextRoute.BOTH, text_route)
                        trash = instantiate_trash_button()
            retry.click(retry_fn, inputs=[chatbot1], outputs=[msg, chatbot1])
            trash.click(lambda: ("", []), outputs=[msg, chatbot1])

        elif route == TextRoute.RIGHT:
            with gr.Row():
                with gr.Column(scale=1, elem_classes="stick-to-the-right"):
                    with gr.Row(elem_classes="stick-to-the-right"):
                        retry = instantiate_retry_button(route)
                        instantiate_arrow_button(TextRoute.LEFT, text_route)
                    with gr.Row(elem_classes="stick-to-the-right"):
                        trash = instantiate_trash_button()
                        instantiate_arrow_button(TextRoute.BOTH, text_route)
                with gr.Column(scale=1):
                    msg = instantiate_text_box()
            retry.click(retry_fn, inputs=[chatbot2], outputs=[msg, chatbot2])
            trash.click(lambda: ("", []), outputs=[msg, chatbot2])

        system_message = instantiate_additional_settings()

        # Route the submitted message to the left, right or both chatbots
        if route == TextRoute.LEFT:
            submission = msg.submit(
                chat_turn_user, inputs=[msg, chatbot1], outputs=[chatbot1]
            ).then(
                chat_turn_assistant,
                [msg, sel1, chatbot1, system_message],
                outputs=[chatbot1],
            )
        elif route == TextRoute.RIGHT:
            submission = msg.submit(
                chat_turn_user, inputs=[msg, chatbot2], outputs=[chatbot2]
            ).then(
                chat_turn_assistant,
                [msg, sel2, chatbot2, system_message],
                outputs=[chatbot2],
            )
        elif route == TextRoute.BOTH:
            submission = msg.submit(
                chat_turn_both_user,
                inputs=[msg, chatbot1, chatbot2],
                outputs=[chatbot1, chatbot2],
            ).then(
                chat_turn_both_assistant,
                [msg, sel1, sel2, chatbot1, chatbot2, system_message],
                outputs=[chatbot1, chatbot2],
            )
        # In all cases reset text box after submission
        submission.then(lambda: "", outputs=msg)

    sel1.select(
        lambda sel: instantiate_chatbot(sel, "chat1"),
        inputs=[sel1],
        outputs=[chatbot1],
    ).then(
        lambda sel: instantiate_select_box(sel, model_labels_list),
        inputs=[sel1],
        outputs=[sel1],
    )

    sel2.select(
        lambda sel: instantiate_chatbot(sel, "chat2"),
        inputs=[sel2],
        outputs=[chatbot2],
    ).then(
        lambda sel: instantiate_select_box(sel, model_labels_list),
        inputs=[sel2],
        outputs=[sel2],
    )


if __name__ == "__main__":
    demo.launch()