rohankaran commited on
Commit
05a5906
1 Parent(s): c7de84b

Add function to retrieve chatbot rankings

Browse files

A new function called 'get_rankings' has been added to the 'guardrails_buttons.py' file. This function fetches rankings from an API and generates a sorted markdown table. Also, necessary changes have been made to other files to integrate and display this ranking data in the user interface.

app.py CHANGED
@@ -13,6 +13,7 @@ from guardrails_buttons import (
13
  deactivate_invisible_vote_buttons,
14
  deactivate_textbox,
15
  deactivate_visible_vote_buttons,
 
16
  leftvote,
17
  rightvote,
18
  share_js,
@@ -213,7 +214,7 @@ with gr.Blocks(
213
  background-size: cover; /* Adjust as needed */
214
  background-position: center;
215
  }
216
- #model_description_markdown table {
217
  width: 100%;
218
  }
219
  .w-100 {
@@ -280,6 +281,7 @@ with gr.Blocks(
280
  states = [gr.State() for _ in range(num_sides)]
281
  chatbots = [None] * num_sides
282
  models = gr.State(get_random_models)
 
283
  system_prompt = gr.State(get_random_system_prompt)
284
  show_models = [None] * num_sides
285
  conversation_id = gr.State()
@@ -447,14 +449,10 @@ with gr.Blocks(
447
  """
448
  )
449
 
450
- with gr.Tab(label="🏆 Leaderboard"):
451
- gr.Markdown(
452
- """
453
- ## 🏆 Guardrails Leaderboard
454
-
455
- We will launch the guardrails leaderboard once enough votes are collected. Ranking will be calculated based on ELO ratings. Keep playing so that we can collect enough data.
456
- """
457
- )
458
 
459
  gr.Markdown(
460
  """
 
13
  deactivate_invisible_vote_buttons,
14
  deactivate_textbox,
15
  deactivate_visible_vote_buttons,
16
+ get_rankings,
17
  leftvote,
18
  rightvote,
19
  share_js,
 
214
  background-size: cover; /* Adjust as needed */
215
  background-position: center;
216
  }
217
+ #model_description_markdown table, #leaderboard table {
218
  width: 100%;
219
  }
220
  .w-100 {
 
281
  states = [gr.State() for _ in range(num_sides)]
282
  chatbots = [None] * num_sides
283
  models = gr.State(get_random_models)
284
+ rankings = gr.State("")
285
  system_prompt = gr.State(get_random_system_prompt)
286
  show_models = [None] * num_sides
287
  conversation_id = gr.State()
 
449
  """
450
  )
451
 
452
+ with gr.Tab(label="🏆 Leaderboard", elem_id="leaderboard") as leaderboard_tab:
453
+ gr.Markdown("## 🏆 Guardrails Leaderboard")
454
+ rankings = gr.Markdown("")
455
+ leaderboard_tab.select(get_rankings, None, [rankings])
 
 
 
 
456
 
457
  gr.Markdown(
458
  """
guardrails_buttons.py CHANGED
@@ -143,6 +143,33 @@ def bothbadvote(conversation_id, history1, history2):
143
  pass
144
 
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  def show_models_fn(models):
147
  model_1 = gr.Markdown(" 🅰️ " + models[0]["name"])
148
  model_2 = gr.Markdown(" 🅱️ " + models[1]["name"])
 
143
  pass
144
 
145
 
146
+ def get_rankings():
147
+ try:
148
+ response = requests.get(f"{LIGHTHOUZ_API_URL}/rankings")
149
+ ratings = response.json()["ratings"]
150
+ sorted_ratings = dict(
151
+ sorted(ratings.items(), key=lambda item: item[1], reverse=True)
152
+ )
153
+
154
+ markdown_table = "| Rank | Chatbots | Arena Elo |\n|:-----:|-----|-------|\n"
155
+ current_rank = 1
156
+ last_value = None
157
+ rank_increment = 0
158
+ for key, value in sorted_ratings.items():
159
+ rounded_value = round(value)
160
+ if last_value is None or rounded_value != last_value:
161
+ current_rank += rank_increment
162
+ rank_increment = 0
163
+ markdown_table += f"| {current_rank} | {key} | {rounded_value} |\n"
164
+ last_value = rounded_value
165
+ rank_increment += 1
166
+
167
+ return markdown_table
168
+ except Exception as e:
169
+ print(e)
170
+ return ""
171
+
172
+
173
  def show_models_fn(models):
174
  model_1 = gr.Markdown(" 🅰️ " + models[0]["name"])
175
  model_2 = gr.Markdown(" 🅱️ " + models[1]["name"])
guardrails_models.py CHANGED
@@ -5,9 +5,9 @@ from typing import List, Optional
5
  import openai
6
  from google.generativeai.types import (
7
  BlockedPromptException,
8
- StopCandidateException,
9
- HarmCategory,
10
  HarmBlockThreshold,
 
 
11
  )
12
  from langchain_community.chat_models import ChatAnyscale
13
  from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
 
5
  import openai
6
  from google.generativeai.types import (
7
  BlockedPromptException,
 
 
8
  HarmBlockThreshold,
9
+ HarmCategory,
10
+ StopCandidateException,
11
  )
12
  from langchain_community.chat_models import ChatAnyscale
13
  from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
llamaguard_moderator.py CHANGED
@@ -127,8 +127,6 @@ Provide your safety assessment for $agent_type in the above conversation:
127
  - If unsafe, a second line must include a comma-separated list of violated categories."""
128
  )
129
 
130
- print(PROMPT_TEMPLATE)
131
-
132
 
133
  def format_prompt(role, message):
134
  prompt = PROMPT_TEMPLATE.substitute(prompt=message, agent_type=role)
 
127
  - If unsafe, a second line must include a comma-separated list of violated categories."""
128
  )
129
 
 
 
130
 
131
  def format_prompt(role, message):
132
  prompt = PROMPT_TEMPLATE.substitute(prompt=message, agent_type=role)