rwitz commited on
Commit
4097bac
1 Parent(s): 6a61b6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -36
app.py CHANGED
@@ -7,6 +7,18 @@ import random
7
  from elo import update_elo_ratings # Custom function for ELO ratings
8
  enable_btn = gr.Button.update(interactive=True)
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # Load chatbot URLs and model names from a JSON file
11
  # Load chatbot model adapter names from a text file
12
  with open('chatbots.txt', 'r') as file:
@@ -28,25 +40,24 @@ import requests
28
  import os
29
 
30
  def get_user_elo_ratings():
31
- global global_elo_ratings
32
- try:
33
- dataset = load_dataset("rwitz/mistral-elo-ratings", download_mode="force_redownload")
34
- global_elo_ratings = dataset['train'] # or the relevant split
35
- except:
36
- global_elo_ratings=Dataset.from_dict({"bot_name":["default"],"elo_rating":[1200],"games_played":[0]})
37
- return global_elo_ratings
38
-
39
- def update_elo_rating(new_rating,winner,loser):
40
- # Fetch the current dataset
41
- elo_ratings = get_user_elo_ratings()
42
-
43
- # Function to update the rating of a specific player
44
-
45
- # Update the dataset
46
- # Convert updated dataset to a dictionary for pushing
47
- updated_ratings=update_elo_ratings(elo_ratings,winner,loser)
48
- updated_ratings.push_to_hub("rwitz/mistral-elo-ratings",token=os.environ.get("huggingface_token"))
49
-
50
  # Function to get bot response
51
  def format_alpaca_prompt(state):
52
  alpaca_prompt = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
@@ -152,23 +163,14 @@ import pandas as pd
152
  # Function to generate leaderboard data
153
 
154
  def generate_leaderboard():
155
- global global_elo_ratings
156
- global_elo_ratings = get_user_elo_ratings()
157
-
158
- # Convert the Hugging Face dataset to a pandas DataFrame
159
- leaderboard_data = pd.DataFrame(global_elo_ratings)
160
-
161
- # Rename columns to 'Chatbot' and 'ELO Score'
162
- leaderboard_data.columns = ['Chatbot', 'ELO Score','Games Played']
163
-
164
- # Round the ELO Score to the nearest whole number
165
- leaderboard_data['ELO Score'] = leaderboard_data['ELO Score'].round()
166
-
167
- # Sort the DataFrame based on the ELO Score in descending order
168
- leaderboard_data = leaderboard_data.sort_values('ELO Score', ascending=False)
169
-
170
  return leaderboard_data
171
-
172
  def refresh_leaderboard():
173
  return generate_leaderboard()
174
  # Gradio interface setup
@@ -208,4 +210,5 @@ with gr.Blocks() as demo:
208
 
209
  # Launch the Gradio interface
210
  if __name__ == "__main__":
211
- demo.launch(share=False)
 
 
7
  from elo import update_elo_ratings # Custom function for ELO ratings
8
  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
24
  with open('chatbots.txt', 'r') as file:
 
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."
 
163
  # Function to generate leaderboard data
164
 
165
  def generate_leaderboard():
166
+ conn = sqlite3.connect('elo_ratings.db')
167
+ c = conn.cursor()
168
+ c.execute("SELECT bot_name, elo_rating, games_played FROM elo_ratings ORDER BY elo_rating DESC")
169
+ rows = c.fetchall()
170
+ conn.close()
171
+ leaderboard_data = pd.DataFrame(rows, columns=['Chatbot', 'ELO Score', 'Games Played'])
 
 
 
 
 
 
 
 
 
172
  return leaderboard_data
173
+
174
  def refresh_leaderboard():
175
  return generate_leaderboard()
176
  # Gradio interface setup
 
210
 
211
  # Launch the Gradio interface
212
  if __name__ == "__main__":
213
+ init_database()
214
+ demo.launch(share=False)