rwitz commited on
Commit
65c4df1
β€’
1 Parent(s): 1def0cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -52,10 +52,12 @@ def update_elo_rating(collection, updated_ratings, winner, loser):
52
  collection.update_one({"bot_name": winner}, {"$set": {"elo_rating": updated_ratings[winner]['elo_rating'], "games_played": updated_ratings[winner]['games_played']}}, upsert=True)
53
  collection.update_one({"bot_name": loser}, {"$set": {"elo_rating": updated_ratings[loser]['elo_rating'], "games_played": updated_ratings[loser]['games_played']}}, upsert=True)
54
 
55
- # Load chatbot URLs and model names from a JSON file
56
- # Load chatbot model adapter names from a text file
57
  with open('chatbots.txt', 'r') as file:
58
- chatbots = file.read().splitlines()
 
 
59
  def clear_chat(state):
60
  # Reset state including the chatbot order
61
  state = {} if state is not None else state
@@ -64,7 +66,7 @@ def clear_chat(state):
64
  collection = init_database()
65
 
66
  # Get the list of chatbot names
67
- bot_names = list(get_user_elo_ratings(collection).keys())
68
 
69
  # Randomly select two new Loras
70
  selected_bots = random.sample(bot_names, 2)
@@ -147,12 +149,15 @@ async def chat_with_bots(user_input, state):
147
  return bot1_response, bot2_response
148
  def update_ratings(state, winner_index, collection):
149
  elo_ratings = get_user_elo_ratings(collection)
150
- winner = state['last_bots'][winner_index]
151
- loser = state['last_bots'][1 - winner_index]
152
 
153
- elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
154
- update_elo_rating(collection, elo_ratings, winner, loser)
155
- return [('Winner: ', winner.replace('rwitz/','').replace('-lora','')), ('Loser: ', loser.replace('rwitz/','').replace('-lora',''))]
 
 
 
156
 
157
  def vote_up_model(state, chatbot, chatbot2):
158
  collection = init_database()
@@ -209,24 +214,27 @@ import pandas as pd
209
  def generate_leaderboard(collection):
210
  rows = list(collection.find())
211
  leaderboard_data = pd.DataFrame(rows, columns=['bot_name', 'elo_rating', 'games_played'])
 
 
212
  leaderboard_data.columns = ['Chatbot', 'ELO Score', 'Games Played']
213
  leaderboard_data['ELO Score'] = leaderboard_data['ELO Score'].round().astype(int)
214
  leaderboard_data = leaderboard_data.sort_values('ELO Score', ascending=False)
215
  return leaderboard_data
 
216
  def refresh_leaderboard():
217
  collection = init_database()
218
  return generate_leaderboard(collection)
219
  async def direct_chat(model, user_input, chatbot):
 
220
  temp_state = {
221
  "history": [
222
  [{"role": "user", "content": user_input}],
223
  [{"role": "user", "content": user_input}]
224
  ]
225
  }
226
- response = await get_bot_response(model, user_input, temp_state, 0)
227
  chatbot.append((user_input, response))
228
  return "", chatbot
229
- # ...
230
  def reset_direct_chat():
231
  return "", [], gr.Dropdown.update(value=model_dropdown.value)
232
  refresh_leaderboard()
@@ -272,7 +280,7 @@ with gr.Blocks() as demo:
272
  gr.Markdown("## πŸ—£οΈ Chat directly with a model!")
273
 
274
  with gr.Row():
275
- model_dropdown = gr.Dropdown(choices=chatbots, value="rwitz/go-bruins-v2-lora", label="πŸ€– Select a model")
276
  with gr.Row():
277
  direct_chatbot = gr.Chatbot(label="πŸ’¬ Direct Chat").style(height=500)
278
 
 
52
  collection.update_one({"bot_name": winner}, {"$set": {"elo_rating": updated_ratings[winner]['elo_rating'], "games_played": updated_ratings[winner]['games_played']}}, upsert=True)
53
  collection.update_one({"bot_name": loser}, {"$set": {"elo_rating": updated_ratings[loser]['elo_rating'], "games_played": updated_ratings[loser]['games_played']}}, upsert=True)
54
 
55
+ import json
56
+
57
  with open('chatbots.txt', 'r') as file:
58
+ chatbots_data = json.load(file)
59
+ chatbots = [entry['adapter'] for entry in chatbots_data]
60
+
61
  def clear_chat(state):
62
  # Reset state including the chatbot order
63
  state = {} if state is not None else state
 
66
  collection = init_database()
67
 
68
  # Get the list of chatbot names
69
+ bot_names = [entry['original_model'] for entry in chatbots_data]
70
 
71
  # Randomly select two new Loras
72
  selected_bots = random.sample(bot_names, 2)
 
149
  return bot1_response, bot2_response
150
  def update_ratings(state, winner_index, collection):
151
  elo_ratings = get_user_elo_ratings(collection)
152
+ winner_adapter = state['last_bots'][winner_index]
153
+ loser_adapter = state['last_bots'][1 - winner_index]
154
 
155
+ winner = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == winner_adapter)
156
+ loser = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == loser_adapter)
157
+
158
+ elo_ratings = update_elo_ratings(elo_ratings, winner_adapter, loser_adapter)
159
+ update_elo_rating(collection, elo_ratings, winner_adapter, loser_adapter)
160
+ return [('Winner: ', winner), ('Loser: ', loser)]
161
 
162
  def vote_up_model(state, chatbot, chatbot2):
163
  collection = init_database()
 
214
  def generate_leaderboard(collection):
215
  rows = list(collection.find())
216
  leaderboard_data = pd.DataFrame(rows, columns=['bot_name', 'elo_rating', 'games_played'])
217
+ leaderboard_data['original_model'] = leaderboard_data['bot_name'].apply(lambda x: next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == x))
218
+ leaderboard_data = leaderboard_data[['original_model', 'elo_rating', 'games_played']]
219
  leaderboard_data.columns = ['Chatbot', 'ELO Score', 'Games Played']
220
  leaderboard_data['ELO Score'] = leaderboard_data['ELO Score'].round().astype(int)
221
  leaderboard_data = leaderboard_data.sort_values('ELO Score', ascending=False)
222
  return leaderboard_data
223
+
224
  def refresh_leaderboard():
225
  collection = init_database()
226
  return generate_leaderboard(collection)
227
  async def direct_chat(model, user_input, chatbot):
228
+ adapter = next(entry['adapter'] for entry in chatbots_data if entry['original_model'] == model)
229
  temp_state = {
230
  "history": [
231
  [{"role": "user", "content": user_input}],
232
  [{"role": "user", "content": user_input}]
233
  ]
234
  }
235
+ response = await get_bot_response(adapter, user_input, temp_state, 0)
236
  chatbot.append((user_input, response))
237
  return "", chatbot
 
238
  def reset_direct_chat():
239
  return "", [], gr.Dropdown.update(value=model_dropdown.value)
240
  refresh_leaderboard()
 
280
  gr.Markdown("## πŸ—£οΈ Chat directly with a model!")
281
 
282
  with gr.Row():
283
+ model_dropdown = gr.Dropdown(choices=[entry['original_model'] for entry in chatbots_data], value=chatbots_data[0]['original_model'], label="πŸ€– Select a model")
284
  with gr.Row():
285
  direct_chatbot = gr.Chatbot(label="πŸ’¬ Direct Chat").style(height=500)
286