rwitz commited on
Commit
e2a026e
1 Parent(s): a4f568e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -43
app.py CHANGED
@@ -9,15 +9,24 @@ enable_btn = gr.Button.update(interactive=True)
9
 
10
  import sqlite3
11
 
 
 
12
  def init_database():
13
- conn = sqlite3.connect('elo_ratings.db')
14
- c = conn.cursor()
15
- c.execute('''CREATE TABLE IF NOT EXISTS elo_ratings
16
- (bot_name TEXT PRIMARY KEY,
17
- elo_rating INTEGER,
18
- games_played INTEGER)''')
19
- conn.commit()
20
- conn.close()
 
 
 
 
 
 
 
21
 
22
  # Load chatbot URLs and model names from a JSON file
23
  # Load chatbot model adapter names from a text file
@@ -39,25 +48,7 @@ from datasets import load_dataset,DatasetDict,Dataset
39
  import requests
40
  import os
41
 
42
- def get_user_elo_ratings():
43
- conn = sqlite3.connect('elo_ratings.db')
44
- c = conn.cursor()
45
- c.execute("SELECT * FROM elo_ratings")
46
- rows = c.fetchall()
47
- conn.close()
48
- if rows:
49
- return {row[0]: {'elo_rating': row[1], 'games_played': row[2]} for row in rows}
50
- else:
51
- return {"default": {'elo_rating': 1200, 'games_played': 0}}
52
- def update_elo_rating(updated_ratings, winner, loser):
53
- conn = sqlite3.connect('elo_ratings.db')
54
- c = conn.cursor()
55
- c.execute("INSERT OR REPLACE INTO elo_ratings (bot_name, elo_rating, games_played) VALUES (?, ?, ?)",
56
- (winner, updated_ratings[winner]['elo_rating'], updated_ratings[winner]['games_played']))
57
- c.execute("INSERT OR REPLACE INTO elo_ratings (bot_name, elo_rating, games_played) VALUES (?, ?, ?)",
58
- (loser, updated_ratings[loser]['elo_rating'], updated_ratings[loser]['games_played']))
59
- conn.commit()
60
- conn.close()
61
  # Function to get bot response
62
  def format_alpaca_prompt(state):
63
  alpaca_prompt = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
@@ -124,21 +115,22 @@ async def chat_with_bots(user_input, state):
124
  )
125
 
126
  return bot1_response, bot2_response
127
- def update_ratings(state, winner_index):
128
- elo_ratings = get_user_elo_ratings()
129
  winner = state['last_bots'][winner_index]
130
  loser = state['last_bots'][1 - winner_index]
131
 
132
  elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
133
- update_elo_rating(elo_ratings, winner, loser)
134
- return [('Winner: ', winner.replace('rwitz/','').replace('-lora','')), ('Loser: ', lose.replace('rwitz/','').replace('-lora',''))]
135
- def vote_up_model(state, chatbot,chatbot2):
136
- update_message = update_ratings(state, 0)
 
137
  chatbot.append(update_message[0])
138
  chatbot2.append(update_message[1])
139
  return chatbot, chatbot2,gr.Button.update(interactive=False),gr.Button.update(interactive=False),gr.Textbox.update(interactive=False),gr.Button.update(interactive=False) # Disable voting buttons
140
- def vote_down_model(state, chatbot,chatbot2):
141
- update_message = update_ratings(state, 1)
142
  chatbot2.append(update_message[0])
143
  chatbot.append(update_message[1])
144
  return chatbot,chatbot2, gr.Button.update(interactive=False),gr.Button.update(interactive=False),gr.Textbox.update(interactive=False),gr.Button.update(interactive=False) # Disable voting buttons
@@ -179,13 +171,10 @@ import pandas as pd
179
 
180
  # Function to generate leaderboard data
181
 
182
- def generate_leaderboard():
183
- conn = sqlite3.connect('elo_ratings.db')
184
- c = conn.cursor()
185
- c.execute("SELECT bot_name, elo_rating, games_played FROM elo_ratings ORDER BY elo_rating DESC")
186
- rows = c.fetchall()
187
- conn.close()
188
- leaderboard_data = pd.DataFrame(rows, columns=['Chatbot', 'ELO Score', 'Games Played'])
189
  return leaderboard_data
190
 
191
  def refresh_leaderboard():
@@ -227,5 +216,7 @@ with gr.Blocks() as demo:
227
 
228
  # Launch the Gradio interface
229
  if __name__ == "__main__":
230
- init_database()
 
 
231
  demo.launch(share=False)
 
9
 
10
  import sqlite3
11
 
12
+ import pymongo
13
+
14
  def init_database():
15
+ client = pymongo.MongoClient("mongodb+srv://rwitzman:<password>@cluster0.xb2urf6.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")
16
+ db = client["elo_ratings"]
17
+ collection = db["elo_ratings"]
18
+ return collection
19
+
20
+ def get_user_elo_ratings(collection):
21
+ rows = list(collection.find())
22
+ if rows:
23
+ return {row['bot_name']: {'elo_rating': row['elo_rating'], 'games_played': row['games_played']} for row in rows}
24
+ else:
25
+ return {"default": {'elo_rating': 1200, 'games_played': 0}}
26
+
27
+ def update_elo_rating(collection, updated_ratings, winner, loser):
28
+ collection.update_one({"bot_name": winner}, {"$set": {"elo_rating": updated_ratings[winner]['elo_rating'], "games_played": updated_ratings[winner]['games_played']}}, upsert=True)
29
+ collection.update_one({"bot_name": loser}, {"$set": {"elo_rating": updated_ratings[loser]['elo_rating'], "games_played": updated_ratings[loser]['games_played']}}, upsert=True)
30
 
31
  # Load chatbot URLs and model names from a JSON file
32
  # Load chatbot model adapter names from a text file
 
48
  import requests
49
  import os
50
 
51
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  # Function to get bot response
53
  def format_alpaca_prompt(state):
54
  alpaca_prompt = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
 
115
  )
116
 
117
  return bot1_response, bot2_response
118
+ def update_ratings(state, winner_index, collection):
119
+ elo_ratings = get_user_elo_ratings(collection)
120
  winner = state['last_bots'][winner_index]
121
  loser = state['last_bots'][1 - winner_index]
122
 
123
  elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
124
+ update_elo_rating(collection, elo_ratings, winner, loser)
125
+ return [('Winner: ', winner.replace('rwitz/','').replace('-lora','')), ('Loser: ', loser.replace('rwitz/','').replace('-lora',''))]
126
+
127
+ def vote_up_model(state, chatbot, chatbot2, collection):
128
+ update_message = update_ratings(state, 0, collection)
129
  chatbot.append(update_message[0])
130
  chatbot2.append(update_message[1])
131
  return chatbot, chatbot2,gr.Button.update(interactive=False),gr.Button.update(interactive=False),gr.Textbox.update(interactive=False),gr.Button.update(interactive=False) # Disable voting buttons
132
+ def vote_down_model(state, chatbot, chatbot2, collection):
133
+ update_message = update_ratings(state, 1, collection)
134
  chatbot2.append(update_message[0])
135
  chatbot.append(update_message[1])
136
  return chatbot,chatbot2, gr.Button.update(interactive=False),gr.Button.update(interactive=False),gr.Textbox.update(interactive=False),gr.Button.update(interactive=False) # Disable voting buttons
 
171
 
172
  # Function to generate leaderboard data
173
 
174
+ def generate_leaderboard(collection):
175
+ rows = list(collection.find())
176
+ leaderboard_data = pd.DataFrame(rows, columns=['bot_name', 'elo_rating', 'games_played'])
177
+ leaderboard_data.columns = ['Chatbot', 'ELO Score', 'Games Played']
 
 
 
178
  return leaderboard_data
179
 
180
  def refresh_leaderboard():
 
216
 
217
  # Launch the Gradio interface
218
  if __name__ == "__main__":
219
+ collection = init_database()
220
+ upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1, chatbot2, collection], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
221
+ upvote_btn_b.click(vote_down_model, inputs=[state, chatbot1, chatbot2, collection], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
222
  demo.launch(share=False)