k-mktr commited on
Commit
3f10ec6
·
verified ·
1 Parent(s): bbe8901

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -126
app.py CHANGED
@@ -3,7 +3,7 @@ from functools import lru_cache
3
  import random
4
  import requests
5
  import logging
6
- import arena_config
7
  import plotly.graph_objects as go
8
  from typing import Dict
9
  from leaderboard import (
@@ -15,7 +15,7 @@ from leaderboard import (
15
  ensure_elo_ratings_initialized
16
  )
17
  import sys
18
- from fun_stats import get_fun_stats
19
  import threading
20
  import time
21
  from collections import Counter
@@ -31,7 +31,7 @@ start_backup_thread()
31
 
32
  # Function to get available models (using predefined list)
33
  def get_available_models():
34
- return [model[0] for model in arena_config.APPROVED_MODELS]
35
 
36
  # Function to get recent opponents for a model
37
  recent_opponents = {}
@@ -46,30 +46,28 @@ def update_recent_opponents(model_a, model_b):
46
  # Function to call Ollama API with caching
47
  @lru_cache(maxsize=100)
48
  def call_ollama_api(model, prompt):
49
- payload = {
50
- "model": model,
51
- "messages": [
52
- {
53
- "role": "system",
54
- "content": "You are a helpful assistant. At no point should you reveal your name, identity or team affiliation to the user, especially if asked directly!"
55
- },
56
- {
57
- "role": "user",
58
- "content": prompt
59
- }
60
- ]
61
- }
62
  try:
63
- response = requests.post(
64
- f"{arena_config.API_URL}/chat/completions",
65
- headers=arena_config.HEADERS,
66
- json=payload,
 
 
 
 
 
 
 
 
67
  timeout=100
68
  )
69
- response.raise_for_status()
70
- data = response.json()
71
- return data["choices"][0]["message"]["content"]
72
- except requests.exceptions.RequestException as e:
73
  logger.error(f"Error calling Ollama API for model {model}: {e}")
74
  return f"Error: Unable to get response from the model."
75
 
@@ -132,8 +130,8 @@ def battle_arena(prompt):
132
  gr.update(value="Error: Unable to get response from the model", visible=True)
133
  )
134
 
135
- nickname_a = random.choice(arena_config.model_nicknames)
136
- nickname_b = random.choice(arena_config.model_nicknames)
137
 
138
  # Format responses for gr.Chatbot, including the user's prompt
139
  response_a_formatted = [
@@ -283,8 +281,8 @@ def get_leaderboard_chart():
283
  return fig
284
 
285
  def new_battle():
286
- nickname_a = random.choice(arena_config.model_nicknames)
287
- nickname_b = random.choice(arena_config.model_nicknames)
288
  return (
289
  "", # Reset prompt_input
290
  gr.update(value=[], label=nickname_a), # Reset left Chatbot
@@ -303,18 +301,18 @@ def new_battle():
303
 
304
  # Add this new function
305
  def get_human_readable_name(model_name: str) -> str:
306
- model_dict = dict(arena_config.APPROVED_MODELS)
307
  return model_dict.get(model_name, model_name)
308
 
309
  # Add this new function to randomly select a prompt
310
  def random_prompt():
311
- return random.choice(arena_config.example_prompts)
312
 
313
  # Modify the continue_conversation function
314
  def continue_conversation(prompt, left_chat, right_chat, left_model, right_model, previous_prompt, tie_count):
315
  # Check if the prompt is empty or the same as the previous one
316
  if not prompt or prompt == previous_prompt:
317
- prompt = random.choice(arena_config.example_prompts)
318
 
319
  left_response = call_ollama_api(left_model, prompt)
320
  right_response = call_ollama_api(right_model, prompt)
@@ -337,84 +335,6 @@ def continue_conversation(prompt, left_chat, right_chat, left_model, right_model
337
  tie_count
338
  )
339
 
340
- def get_fun_stats_html():
341
- stats = get_fun_stats()
342
-
343
- html = f"""
344
- <style>
345
- .fun-stats {{
346
- font-family: Arial, sans-serif;
347
- font-size: 18px;
348
- line-height: 1.6;
349
- max-width: 800px;
350
- margin: 0 auto;
351
- padding: 20px;
352
- }}
353
- .fun-stats h2 {{
354
- font-size: 36px;
355
- color: inherit;
356
- text-align: center;
357
- margin-bottom: 20px;
358
- }}
359
- .fun-stats h3 {{
360
- font-size: 28px;
361
- color: inherit;
362
- margin-top: 30px;
363
- margin-bottom: 15px;
364
- border-bottom: 2px solid currentColor;
365
- padding-bottom: 10px;
366
- }}
367
- .fun-stats ul {{
368
- list-style-type: none;
369
- padding-left: 0;
370
- }}
371
- .fun-stats li {{
372
- margin-bottom: 15px;
373
- padding: 15px;
374
- border-radius: 5px;
375
- box-shadow: 0 2px 5px rgba(0,0,0,0.1);
376
- }}
377
- .fun-stats .timestamp {{
378
- font-style: italic;
379
- text-align: center;
380
- margin-bottom: 20px;
381
- }}
382
- .fun-stats .highlight {{
383
- font-weight: bold;
384
- color: #e74c3c;
385
- }}
386
- </style>
387
- <div class="fun-stats">
388
- <h2>🎭 Fun Arena Stats 🎭</h2>
389
- <p class="timestamp">Last updated: {stats['timestamp']}</p>
390
-
391
- <h3>🏟️ Arena Overview</h3>
392
- <p>Total Battles Fought: <span class="highlight">{stats['total_battles']}</span></p>
393
- <p>Active Gladiators (Models): <span class="highlight">{stats['active_models']}</span></p>
394
-
395
- <h3>🏆 Hall of Fame</h3>
396
- <p>🥇 Battle Veteran: <span class="highlight">{stats['most_battles']['model']}</span> ({stats['most_battles']['battles']} battles)</p>
397
- <p>🏹 Sharpshooter: <span class="highlight">{stats['highest_win_rate']['model']}</span> (Win Rate: {stats['highest_win_rate']['win_rate']})</p>
398
- <p>🌈 Jack of All Trades: <span class="highlight">{stats['most_diverse_opponent']['model']}</span> (Faced {stats['most_diverse_opponent']['unique_opponents']} unique opponents)</p>
399
- <p>🐕 Underdog Champion: <span class="highlight">{stats['underdog_champion']['model']}</span> ({stats['underdog_champion']['size']} model with {stats['underdog_champion']['win_rate']} win rate)</p>
400
- <p>⚖️ Mr. Consistent: <span class="highlight">{stats['most_consistent']['model']}</span> (Closest to 50% win rate, difference of {stats['most_consistent']['win_loss_difference']} wins/losses)</p>
401
-
402
- <h3>🤼 Epic Battles</h3>
403
- <p>🤼 Biggest Rivalry: <span class="highlight">{stats['biggest_rivalry']['model1']}</span> vs <span class="highlight">{stats['biggest_rivalry']['model2']}</span> ({stats['biggest_rivalry']['total_battles']} fierce battles!)</p>
404
- <p>🏋️ David vs Goliath: <span class="highlight">{stats['david_vs_goliath']['david']}</span> (David) vs <span class="highlight">{stats['david_vs_goliath']['goliath']}</span> (Goliath)<br>
405
- David won {stats['david_vs_goliath']['wins']} times despite being {stats['david_vs_goliath']['size_difference']} smaller!</p>
406
- <p>🔄 Comeback King: <span class="highlight">{stats['comeback_king']['model']}</span> (Overcame a {stats['comeback_king']['comeback_margin']}-battle deficit)</p>
407
- <p>🏆 Pyrrhic Victor: <span class="highlight">{stats['pyrrhic_victor']['model']}</span> (Lowest win rate among models with more wins than losses: {stats['pyrrhic_victor']['win_rate']})</p>
408
- </div>
409
- """
410
-
411
- return html
412
-
413
- def update_fun_stats_periodically(interval):
414
- while True:
415
- time.sleep(interval)
416
- fun_stats_html.update(value=get_fun_stats_html())
417
-
418
  # Initialize Gradio Blocks
419
  with gr.Blocks(css="""
420
  #dice-button {
@@ -422,8 +342,8 @@ with gr.Blocks(css="""
422
  font-size: 35px;
423
  }
424
  """) as demo:
425
- gr.Markdown(arena_config.ARENA_NAME)
426
- gr.Markdown(arena_config.ARENA_DESCRIPTION)
427
 
428
  # Leaderboard Tab (now first)
429
  with gr.Tab("Leaderboard"):
@@ -450,8 +370,8 @@ with gr.Blocks(css="""
450
  submit_btn = gr.Button("Generate Responses", variant="primary")
451
 
452
  with gr.Row():
453
- left_output = gr.Chatbot(label=random.choice(arena_config.model_nicknames), type="messages")
454
- right_output = gr.Chatbot(label=random.choice(arena_config.model_nicknames), type="messages")
455
 
456
  with gr.Row():
457
  left_vote_btn = gr.Button(f"Vote for {left_output.label}", interactive=False)
@@ -482,11 +402,6 @@ with gr.Blocks(css="""
482
  with gr.Tab("ELO Leaderboard"):
483
  elo_leaderboard = gr.HTML(label="ELO Leaderboard")
484
 
485
- # Add this new tab
486
- with gr.Tab("Fun Stats"):
487
- refresh_btn = gr.Button("Refresh Stats")
488
- fun_stats_html = gr.HTML(label="Fun Arena Stats")
489
-
490
  # Add this new tab
491
  with gr.Tab("Suggest Models"):
492
  with gr.Row():
@@ -564,16 +479,10 @@ with gr.Blocks(css="""
564
  demo.load(get_leaderboard, outputs=leaderboard)
565
  demo.load(get_elo_leaderboard, outputs=elo_leaderboard)
566
  demo.load(get_leaderboard_chart, outputs=leaderboard_chart)
567
- demo.load(get_fun_stats_html, outputs=fun_stats_html)
568
-
569
- # Add this event handler for the refresh button
570
- refresh_btn.click(get_fun_stats_html, outputs=fun_stats_html)
571
-
572
- # Start the background task to update stats every hour
573
- update_thread = threading.Thread(target=update_fun_stats_periodically, args=(3600,), daemon=True)
574
- update_thread.start()
575
 
576
  if __name__ == "__main__":
577
  # Initialize ELO ratings before launching the app
578
  ensure_elo_ratings_initialized()
 
 
579
  demo.launch(show_api=False)
 
3
  import random
4
  import requests
5
  import logging
6
+ import config
7
  import plotly.graph_objects as go
8
  from typing import Dict
9
  from leaderboard import (
 
15
  ensure_elo_ratings_initialized
16
  )
17
  import sys
18
+ import openai
19
  import threading
20
  import time
21
  from collections import Counter
 
31
 
32
  # Function to get available models (using predefined list)
33
  def get_available_models():
34
+ return [model[0] for model in config.get_approved_models()]
35
 
36
  # Function to get recent opponents for a model
37
  recent_opponents = {}
 
46
  # Function to call Ollama API with caching
47
  @lru_cache(maxsize=100)
48
  def call_ollama_api(model, prompt):
49
+ client = openai.OpenAI(
50
+ api_key=config.API_KEY,
51
+ base_url=config.API_URL
52
+ )
53
+
 
 
 
 
 
 
 
 
54
  try:
55
+ response = client.chat.completions.create(
56
+ model=model,
57
+ messages=[
58
+ {
59
+ "role": "system",
60
+ "content": "You are a helpful assistant. At no point should you reveal your name, identity or team affiliation to the user, especially if asked directly!"
61
+ },
62
+ {
63
+ "role": "user",
64
+ "content": prompt
65
+ }
66
+ ],
67
  timeout=100
68
  )
69
+ return response.choices[0].message.content
70
+ except Exception as e:
 
 
71
  logger.error(f"Error calling Ollama API for model {model}: {e}")
72
  return f"Error: Unable to get response from the model."
73
 
 
130
  gr.update(value="Error: Unable to get response from the model", visible=True)
131
  )
132
 
133
+ nickname_a = random.choice(config.model_nicknames)
134
+ nickname_b = random.choice(config.model_nicknames)
135
 
136
  # Format responses for gr.Chatbot, including the user's prompt
137
  response_a_formatted = [
 
281
  return fig
282
 
283
  def new_battle():
284
+ nickname_a = random.choice(config.model_nicknames)
285
+ nickname_b = random.choice(config.model_nicknames)
286
  return (
287
  "", # Reset prompt_input
288
  gr.update(value=[], label=nickname_a), # Reset left Chatbot
 
301
 
302
  # Add this new function
303
  def get_human_readable_name(model_name: str) -> str:
304
+ model_dict = dict(config.get_approved_models())
305
  return model_dict.get(model_name, model_name)
306
 
307
  # Add this new function to randomly select a prompt
308
  def random_prompt():
309
+ return random.choice(config.example_prompts)
310
 
311
  # Modify the continue_conversation function
312
  def continue_conversation(prompt, left_chat, right_chat, left_model, right_model, previous_prompt, tie_count):
313
  # Check if the prompt is empty or the same as the previous one
314
  if not prompt or prompt == previous_prompt:
315
+ prompt = random.choice(config.example_prompts)
316
 
317
  left_response = call_ollama_api(left_model, prompt)
318
  right_response = call_ollama_api(right_model, prompt)
 
335
  tie_count
336
  )
337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
  # Initialize Gradio Blocks
339
  with gr.Blocks(css="""
340
  #dice-button {
 
342
  font-size: 35px;
343
  }
344
  """) as demo:
345
+ gr.Markdown(config.ARENA_NAME)
346
+ gr.Markdown(config.ARENA_DESCRIPTION)
347
 
348
  # Leaderboard Tab (now first)
349
  with gr.Tab("Leaderboard"):
 
370
  submit_btn = gr.Button("Generate Responses", variant="primary")
371
 
372
  with gr.Row():
373
+ left_output = gr.Chatbot(label=random.choice(config.model_nicknames), type="messages")
374
+ right_output = gr.Chatbot(label=random.choice(config.model_nicknames), type="messages")
375
 
376
  with gr.Row():
377
  left_vote_btn = gr.Button(f"Vote for {left_output.label}", interactive=False)
 
402
  with gr.Tab("ELO Leaderboard"):
403
  elo_leaderboard = gr.HTML(label="ELO Leaderboard")
404
 
 
 
 
 
 
405
  # Add this new tab
406
  with gr.Tab("Suggest Models"):
407
  with gr.Row():
 
479
  demo.load(get_leaderboard, outputs=leaderboard)
480
  demo.load(get_elo_leaderboard, outputs=elo_leaderboard)
481
  demo.load(get_leaderboard_chart, outputs=leaderboard_chart)
 
 
 
 
 
 
 
 
482
 
483
  if __name__ == "__main__":
484
  # Initialize ELO ratings before launching the app
485
  ensure_elo_ratings_initialized()
486
+ # Start the model refresh thread
487
+ config.start_model_refresh_thread()
488
  demo.launch(show_api=False)