File size: 9,230 Bytes
777015e |
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 |
import gradio as gr
from dotenv import load_dotenv
from models import get_all_models, get_random_models
load_dotenv()
share_js = """
function () {
const captureElement = document.querySelector('#share-region-annoy');
// console.log(captureElement);
html2canvas(captureElement)
.then(canvas => {
canvas.style.display = 'none'
document.body.appendChild(canvas)
return canvas
})
.then(canvas => {
const image = canvas.toDataURL('image/png')
const a = document.createElement('a')
a.setAttribute('download', 'guardrails-arena.png')
a.setAttribute('href', image)
a.click()
canvas.remove()
});
return [];
}
"""
def activate_chat_buttons():
regenerate_btn = gr.Button(
value="🔄 Regenerate", interactive=True, elem_id="regenerate_btn"
)
clear_btn = gr.ClearButton(
elem_id="clear_btn",
interactive=True,
)
return regenerate_btn, clear_btn
def deactivate_chat_buttons():
regenerate_btn = gr.Button(
value="🔄 Regenerate", interactive=False, elem_id="regenerate_btn"
)
clear_btn = gr.ClearButton(
elem_id="clear_btn",
interactive=False,
)
return regenerate_btn, clear_btn
def handle_message(
llms, user_input, temperature, top_p, max_output_tokens, states1, states2
):
history1 = states1.value if states1 else []
history2 = states2.value if states2 else []
states = [states1, states2]
history = [history1, history2]
for hist in history:
hist.append((user_input, None))
for (
updated_history1,
updated_history2,
updated_states1,
updated_states2,
) in process_responses(
llms, temperature, top_p, max_output_tokens, history, states
):
yield updated_history1, updated_history2, updated_states1, updated_states2
def regenerate_message(llms, temperature, top_p, max_output_tokens, states1, states2):
history1 = states1.value if states1 else []
history2 = states2.value if states2 else []
user_input = (
history1.pop()[0] if history1 else None
) # Assumes regeneration is needed so there is at least one input
if history2:
history2.pop()
states = [states1, states2]
history = [history1, history2]
for hist in history:
hist.append((user_input, None))
for (
updated_history1,
updated_history2,
updated_states1,
updated_states2,
) in process_responses(
llms, temperature, top_p, max_output_tokens, history, states
):
yield updated_history1, updated_history2, updated_states1, updated_states2
def process_responses(llms, temperature, top_p, max_output_tokens, history, states):
generators = [
llms[i]["model"](history[i], temperature, top_p, max_output_tokens)
for i in range(2)
]
responses = [[], []]
done = [False, False]
while not all(done):
for i in range(2):
if not done[i]:
try:
response = next(generators[i])
if response:
responses[i].append(response)
history[i][-1] = (history[i][-1][0], "".join(responses[i]))
states[i] = gr.State(history[i])
yield history[0], history[1], states[0], states[1]
except StopIteration:
done[i] = True
yield history[0], history[1], states[0], states[1]
with gr.Blocks(
title="Chatbot Arena",
# theme=gr.themes.Soft(secondary_hue=gr.themes.colors.sky),
theme=gr.themes.Base(),
) as demo:
num_sides = 2
states = [gr.State() for _ in range(num_sides)]
chatbots = [None] * num_sides
models = gr.State(get_random_models)
all_models = get_all_models()
# gr.Markdown(
# "# Cherokee Language Preserve Model V0.4 \n\nChat with multiple models at the same time and compare their responses. "
# )
with gr.Group(elem_id="share-region-annoy"):
# with gr.Accordion(f"🔍 Expand to see the {len(all_models)} models", open=False):
# model_description_md = """| | | |\n| ---- | ---- | ---- |\n"""
# count = 0
# for model in all_models:
# if count % 3 == 0:
# model_description_md += "|"
# model_description_md += f" {model['name']} |"
# if count % 3 == 2:
# model_description_md += "\n"
# count += 1
# gr.Markdown(model_description_md, elem_id="model_description_markdown")
with gr.Row():
for i in range(num_sides):
label = models.value[i]["name"]
with gr.Column():
chatbots[i] = gr.Chatbot(
label=label,
elem_id=f"chatbot",
height=550,
show_copy_button=True,
)
with gr.Row():
textbox = gr.Textbox(
show_label=False,
placeholder="Enter your query and press ENTER",
elem_id="input_box",
scale=4,
)
send_btn = gr.Button(value="Send", variant="primary", scale=0)
with gr.Row() as button_row:
upload_button = gr.UploadButton("Upload File", file_types=["image", "video"], file_count="multiple")
clear_btn = gr.ClearButton(
value="🎲 New Round",
elem_id="clear_btn",
interactive=False,
components=chatbots + states,
)
regenerate_btn = gr.Button(
value="🔄 Regenerate", interactive=False, elem_id="regenerate_btn"
)
share_btn = gr.Button(value="📷 Share Image")
with gr.Row():
examples = gr.Examples(
[
"translate: ᎧᏃᎮᏍᎩ",
"Could you assist in rendering this Cherokee word into English?\nᎤᏲᎢ",
"translate the following Cherokee word into English. ᏧᎩᏨᏅᏓ",
],
inputs=[textbox],
label="Example task: Translate words",
)
with gr.Row():
examples = gr.Examples(
[
"translate: ᏚᏁᏤᎴᏃ ᎬᏩᏍᏓᏩᏗᏙᎯ, ᎾᏍᎩ ᏥᏳ ᎤᎦᏘᏗᏍᏗᏱ, ᎤᏂᏣᏘ ᎨᏒ ᎢᏳᏍᏗ, ᎾᏍᎩ ᎬᏩᏁᏄᎳᏍᏙᏗᏱ ᏂᎨᏒᎾ",
"translate following Cherokee sentences into English.\nᏥᏌᏃ ᎤᏓᏅᏎ ᏚᏘᏅᏎ ᎬᏩᏍᏓᏩᏗᏙᎯ ᎥᏓᎵ ᏭᏂᎶᏎᎢ; ᎤᏂᏣᏘᏃ ᎬᏩᏍᏓᏩᏛᏎᎢ, ᏅᏓᏳᏂᎶᏒᎯ ᎨᎵᎵ, ᎠᎴ ᏧᏗᏱ,",
"translate following sentences.\nᎯᎠᏃ ᏄᏪᏎᎴ ᎠᏍᎦᏯ ᎤᏬᏰᏂ ᎤᏩᎢᏎᎸᎯ; ᎠᏰᎵ ᎭᎴᎲᎦ.",
],
inputs=[textbox],
label="Example task: Translate sentences",
)
with gr.Accordion("Parameters", open=False) as parameter_row:
temperature = gr.Slider(
minimum=0.0,
maximum=2.0,
value=0.5,
step=0.1,
interactive=True,
label="Temperature",
)
top_p = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.7,
step=0.1,
interactive=True,
label="Top P",
)
max_output_tokens = gr.Slider(
minimum=16,
maximum=4096,
value=1024,
step=64,
interactive=True,
label="Max output tokens",
)
textbox.submit(
handle_message,
inputs=[
models,
textbox,
temperature,
top_p,
max_output_tokens,
states[0],
states[1],
],
outputs=[chatbots[0], chatbots[1], states[0], states[1]],
).then(
activate_chat_buttons,
inputs=[],
outputs=[regenerate_btn, clear_btn],
)
send_btn.click(
handle_message,
inputs=[
models,
textbox,
temperature,
top_p,
max_output_tokens,
states[0],
states[1],
],
outputs=[chatbots[0], chatbots[1], states[0], states[1]],
).then(
activate_chat_buttons,
inputs=[],
outputs=[regenerate_btn, clear_btn],
)
regenerate_btn.click(
regenerate_message,
inputs=[
models,
temperature,
top_p,
max_output_tokens,
states[0],
states[1],
],
outputs=[chatbots[0], chatbots[1], states[0], states[1]],
)
clear_btn.click(
deactivate_chat_buttons,
inputs=[],
outputs=[regenerate_btn, clear_btn],
).then(lambda: get_random_models(), inputs=None, outputs=[models])
share_btn.click(None, inputs=[], outputs=[], js=share_js)
if __name__ == "__main__":
demo.queue(default_concurrency_limit=10)
demo.launch(server_name="127.0.01", server_port=5009, share=True) |