Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,16 @@ import logging
|
|
| 6 |
import arena_config
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
from typing import Dict
|
| 9 |
-
from leaderboard import get_current_leaderboard, update_leaderboard
|
|
|
|
| 10 |
|
| 11 |
# Initialize logging for errors only
|
| 12 |
logging.basicConfig(level=logging.ERROR)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Function to get available models (using predefined list)
|
| 16 |
def get_available_models():
|
| 17 |
return [model[0] for model in arena_config.APPROVED_MODELS]
|
|
@@ -57,9 +61,15 @@ def battle_arena(prompt):
|
|
| 57 |
nickname_a = random.choice(arena_config.model_nicknames)
|
| 58 |
nickname_b = random.choice(arena_config.model_nicknames)
|
| 59 |
|
| 60 |
-
# Format responses for gr.Chatbot
|
| 61 |
-
response_a_formatted = [
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if random.choice([True, False]):
|
| 65 |
return (
|
|
@@ -67,7 +77,10 @@ def battle_arena(prompt):
|
|
| 67 |
gr.update(label=nickname_a, value=response_a_formatted),
|
| 68 |
gr.update(label=nickname_b, value=response_b_formatted),
|
| 69 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 70 |
-
gr.update(interactive=True, value=f"Vote for {nickname_b}")
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
else:
|
| 73 |
return (
|
|
@@ -75,7 +88,10 @@ def battle_arena(prompt):
|
|
| 75 |
gr.update(label=nickname_a, value=response_b_formatted),
|
| 76 |
gr.update(label=nickname_b, value=response_a_formatted),
|
| 77 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 78 |
-
gr.update(interactive=True, value=f"Vote for {nickname_b}")
|
|
|
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
def record_vote(prompt, left_response, right_response, left_model, right_model, choice):
|
|
@@ -108,6 +124,7 @@ def record_vote(prompt, left_response, right_response, left_model, right_model,
|
|
| 108 |
get_leaderboard(), # Update leaderboard
|
| 109 |
gr.update(interactive=False), # Disable left vote button
|
| 110 |
gr.update(interactive=False), # Disable right vote button
|
|
|
|
| 111 |
gr.update(visible=True), # Show model names
|
| 112 |
get_leaderboard_chart() # Update leaderboard chart
|
| 113 |
)
|
|
@@ -182,6 +199,12 @@ def get_leaderboard():
|
|
| 182 |
rank_display = "π₯"
|
| 183 |
elif index == 3:
|
| 184 |
rank_display = "π₯"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
else:
|
| 186 |
rank_display = f"{index}"
|
| 187 |
|
|
@@ -295,10 +318,12 @@ def new_battle():
|
|
| 295 |
None,
|
| 296 |
gr.update(interactive=False, value=f"Vote for {nickname_a}"),
|
| 297 |
gr.update(interactive=False, value=f"Vote for {nickname_b}"),
|
|
|
|
| 298 |
gr.update(value="", visible=False),
|
| 299 |
gr.update(),
|
| 300 |
gr.update(visible=False),
|
| 301 |
-
gr.update()
|
|
|
|
| 302 |
)
|
| 303 |
|
| 304 |
# Add this new function
|
|
@@ -310,6 +335,33 @@ def get_human_readable_name(model_name: str) -> str:
|
|
| 310 |
def random_prompt():
|
| 311 |
return random.choice(arena_config.example_prompts)
|
| 312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
# Initialize Gradio Blocks
|
| 314 |
with gr.Blocks(css="""
|
| 315 |
#dice-button {
|
|
@@ -346,6 +398,7 @@ with gr.Blocks(css="""
|
|
| 346 |
|
| 347 |
with gr.Row():
|
| 348 |
left_vote_btn = gr.Button(f"Vote for {left_output.label}", interactive=False)
|
|
|
|
| 349 |
right_vote_btn = gr.Button(f"Vote for {right_output.label}", interactive=False)
|
| 350 |
|
| 351 |
result = gr.Textbox(label="Result", interactive=False, visible=False)
|
|
@@ -354,6 +407,9 @@ with gr.Blocks(css="""
|
|
| 354 |
left_model = gr.Textbox(label="π΅ Left Model", interactive=False)
|
| 355 |
right_model = gr.Textbox(label="π΄ Right Model", interactive=False)
|
| 356 |
|
|
|
|
|
|
|
|
|
|
| 357 |
new_battle_btn = gr.Button("New Battle")
|
| 358 |
|
| 359 |
# Leaderboard Tab
|
|
@@ -369,28 +425,35 @@ with gr.Blocks(css="""
|
|
| 369 |
battle_arena,
|
| 370 |
inputs=prompt_input,
|
| 371 |
outputs=[left_output, right_output, left_model, right_model,
|
| 372 |
-
left_output, right_output, left_vote_btn, right_vote_btn
|
|
|
|
| 373 |
)
|
| 374 |
|
| 375 |
left_vote_btn.click(
|
| 376 |
lambda *args: record_vote(*args, "Left is better"),
|
| 377 |
inputs=[prompt_input, left_output, right_output, left_model, right_model],
|
| 378 |
outputs=[result, leaderboard, left_vote_btn,
|
| 379 |
-
right_vote_btn, model_names_row, leaderboard_chart]
|
| 380 |
)
|
| 381 |
|
| 382 |
right_vote_btn.click(
|
| 383 |
lambda *args: record_vote(*args, "Right is better"),
|
| 384 |
inputs=[prompt_input, left_output, right_output, left_model, right_model],
|
| 385 |
outputs=[result, leaderboard, left_vote_btn,
|
| 386 |
-
right_vote_btn, model_names_row, leaderboard_chart]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
)
|
| 388 |
|
| 389 |
new_battle_btn.click(
|
| 390 |
new_battle,
|
| 391 |
outputs=[prompt_input, left_output, right_output, left_model,
|
| 392 |
-
|
| 393 |
-
|
| 394 |
)
|
| 395 |
|
| 396 |
# Update leaderboard and chart on launch
|
|
|
|
| 6 |
import arena_config
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
from typing import Dict
|
| 9 |
+
from leaderboard import get_current_leaderboard, update_leaderboard, start_backup_thread
|
| 10 |
+
|
| 11 |
|
| 12 |
# Initialize logging for errors only
|
| 13 |
logging.basicConfig(level=logging.ERROR)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Start the backup thread
|
| 17 |
+
start_backup_thread()
|
| 18 |
+
|
| 19 |
# Function to get available models (using predefined list)
|
| 20 |
def get_available_models():
|
| 21 |
return [model[0] for model in arena_config.APPROVED_MODELS]
|
|
|
|
| 61 |
nickname_a = random.choice(arena_config.model_nicknames)
|
| 62 |
nickname_b = random.choice(arena_config.model_nicknames)
|
| 63 |
|
| 64 |
+
# Format responses for gr.Chatbot, including the user's prompt
|
| 65 |
+
response_a_formatted = [
|
| 66 |
+
{"role": "user", "content": prompt},
|
| 67 |
+
{"role": "assistant", "content": response_a}
|
| 68 |
+
]
|
| 69 |
+
response_b_formatted = [
|
| 70 |
+
{"role": "user", "content": prompt},
|
| 71 |
+
{"role": "assistant", "content": response_b}
|
| 72 |
+
]
|
| 73 |
|
| 74 |
if random.choice([True, False]):
|
| 75 |
return (
|
|
|
|
| 77 |
gr.update(label=nickname_a, value=response_a_formatted),
|
| 78 |
gr.update(label=nickname_b, value=response_b_formatted),
|
| 79 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 80 |
+
gr.update(interactive=True, value=f"Vote for {nickname_b}"),
|
| 81 |
+
gr.update(interactive=True, visible=True), # Enable and show Tie button
|
| 82 |
+
prompt, # Return the original prompt
|
| 83 |
+
0 # Initialize tie count
|
| 84 |
)
|
| 85 |
else:
|
| 86 |
return (
|
|
|
|
| 88 |
gr.update(label=nickname_a, value=response_b_formatted),
|
| 89 |
gr.update(label=nickname_b, value=response_a_formatted),
|
| 90 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 91 |
+
gr.update(interactive=True, value=f"Vote for {nickname_b}"),
|
| 92 |
+
gr.update(interactive=True, visible=True), # Enable and show Tie button
|
| 93 |
+
prompt, # Return the original prompt
|
| 94 |
+
0 # Initialize tie count
|
| 95 |
)
|
| 96 |
|
| 97 |
def record_vote(prompt, left_response, right_response, left_model, right_model, choice):
|
|
|
|
| 124 |
get_leaderboard(), # Update leaderboard
|
| 125 |
gr.update(interactive=False), # Disable left vote button
|
| 126 |
gr.update(interactive=False), # Disable right vote button
|
| 127 |
+
gr.update(interactive=False), # Disable tie button
|
| 128 |
gr.update(visible=True), # Show model names
|
| 129 |
get_leaderboard_chart() # Update leaderboard chart
|
| 130 |
)
|
|
|
|
| 199 |
rank_display = "π₯"
|
| 200 |
elif index == 3:
|
| 201 |
rank_display = "π₯"
|
| 202 |
+
elif index == 18:
|
| 203 |
+
rank_display = "π"
|
| 204 |
+
elif index == 19:
|
| 205 |
+
rank_display = "π°"
|
| 206 |
+
elif index == 20:
|
| 207 |
+
rank_display = "π"
|
| 208 |
else:
|
| 209 |
rank_display = f"{index}"
|
| 210 |
|
|
|
|
| 318 |
None,
|
| 319 |
gr.update(interactive=False, value=f"Vote for {nickname_a}"),
|
| 320 |
gr.update(interactive=False, value=f"Vote for {nickname_b}"),
|
| 321 |
+
gr.update(interactive=False, visible=False), # Reset Tie button
|
| 322 |
gr.update(value="", visible=False),
|
| 323 |
gr.update(),
|
| 324 |
gr.update(visible=False),
|
| 325 |
+
gr.update(),
|
| 326 |
+
0 # Reset tie_count
|
| 327 |
)
|
| 328 |
|
| 329 |
# Add this new function
|
|
|
|
| 335 |
def random_prompt():
|
| 336 |
return random.choice(arena_config.example_prompts)
|
| 337 |
|
| 338 |
+
# Modify the continue_conversation function
|
| 339 |
+
def continue_conversation(prompt, left_chat, right_chat, left_model, right_model, previous_prompt, tie_count):
|
| 340 |
+
# Check if the prompt is empty or the same as the previous one
|
| 341 |
+
if not prompt or prompt == previous_prompt:
|
| 342 |
+
prompt = random.choice(arena_config.example_prompts)
|
| 343 |
+
|
| 344 |
+
left_response = call_ollama_api(left_model, prompt)
|
| 345 |
+
right_response = call_ollama_api(right_model, prompt)
|
| 346 |
+
|
| 347 |
+
left_chat.append({"role": "user", "content": prompt})
|
| 348 |
+
left_chat.append({"role": "assistant", "content": left_response})
|
| 349 |
+
|
| 350 |
+
right_chat.append({"role": "user", "content": prompt})
|
| 351 |
+
right_chat.append({"role": "assistant", "content": right_response})
|
| 352 |
+
|
| 353 |
+
tie_count += 1
|
| 354 |
+
tie_button_state = gr.update(interactive=True) if tie_count < 3 else gr.update(interactive=False, value="Max ties reached. Please vote!")
|
| 355 |
+
|
| 356 |
+
return (
|
| 357 |
+
gr.update(value=left_chat),
|
| 358 |
+
gr.update(value=right_chat),
|
| 359 |
+
gr.update(value=""), # Clear the prompt input
|
| 360 |
+
tie_button_state,
|
| 361 |
+
prompt, # Return the new prompt
|
| 362 |
+
tie_count
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
# Initialize Gradio Blocks
|
| 366 |
with gr.Blocks(css="""
|
| 367 |
#dice-button {
|
|
|
|
| 398 |
|
| 399 |
with gr.Row():
|
| 400 |
left_vote_btn = gr.Button(f"Vote for {left_output.label}", interactive=False)
|
| 401 |
+
tie_btn = gr.Button("Tie π Continue with a new prompt", interactive=False, visible=False)
|
| 402 |
right_vote_btn = gr.Button(f"Vote for {right_output.label}", interactive=False)
|
| 403 |
|
| 404 |
result = gr.Textbox(label="Result", interactive=False, visible=False)
|
|
|
|
| 407 |
left_model = gr.Textbox(label="π΅ Left Model", interactive=False)
|
| 408 |
right_model = gr.Textbox(label="π΄ Right Model", interactive=False)
|
| 409 |
|
| 410 |
+
previous_prompt = gr.State("") # Add this line to store the previous prompt
|
| 411 |
+
tie_count = gr.State(0) # Add this line to keep track of tie count
|
| 412 |
+
|
| 413 |
new_battle_btn = gr.Button("New Battle")
|
| 414 |
|
| 415 |
# Leaderboard Tab
|
|
|
|
| 425 |
battle_arena,
|
| 426 |
inputs=prompt_input,
|
| 427 |
outputs=[left_output, right_output, left_model, right_model,
|
| 428 |
+
left_output, right_output, left_vote_btn, right_vote_btn,
|
| 429 |
+
tie_btn, previous_prompt, tie_count]
|
| 430 |
)
|
| 431 |
|
| 432 |
left_vote_btn.click(
|
| 433 |
lambda *args: record_vote(*args, "Left is better"),
|
| 434 |
inputs=[prompt_input, left_output, right_output, left_model, right_model],
|
| 435 |
outputs=[result, leaderboard, left_vote_btn,
|
| 436 |
+
right_vote_btn, tie_btn, model_names_row, leaderboard_chart]
|
| 437 |
)
|
| 438 |
|
| 439 |
right_vote_btn.click(
|
| 440 |
lambda *args: record_vote(*args, "Right is better"),
|
| 441 |
inputs=[prompt_input, left_output, right_output, left_model, right_model],
|
| 442 |
outputs=[result, leaderboard, left_vote_btn,
|
| 443 |
+
right_vote_btn, tie_btn, model_names_row, leaderboard_chart]
|
| 444 |
+
)
|
| 445 |
+
|
| 446 |
+
tie_btn.click(
|
| 447 |
+
continue_conversation,
|
| 448 |
+
inputs=[prompt_input, left_output, right_output, left_model, right_model, previous_prompt, tie_count],
|
| 449 |
+
outputs=[left_output, right_output, prompt_input, tie_btn, previous_prompt, tie_count]
|
| 450 |
)
|
| 451 |
|
| 452 |
new_battle_btn.click(
|
| 453 |
new_battle,
|
| 454 |
outputs=[prompt_input, left_output, right_output, left_model,
|
| 455 |
+
right_model, left_vote_btn, right_vote_btn, tie_btn,
|
| 456 |
+
result, leaderboard, model_names_row, leaderboard_chart, tie_count]
|
| 457 |
)
|
| 458 |
|
| 459 |
# Update leaderboard and chart on launch
|