rwitz commited on
Commit
7d3d77d
·
1 Parent(s): 81797ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -19
app.py CHANGED
@@ -15,21 +15,48 @@ def clear_chat(state):
15
  return state, None,None,gr.Button.update(interactive=False),gr.Button.update(interactive=False)
16
 
17
 
18
- # Initialize or get user-specific ELO ratings
19
- def get_user_elo_ratings(state):
20
- return state['elo_ratings']
21
-
22
- # Read and write ELO ratings to file (thread-safe)
23
- def read_elo_ratings():
24
- try:
25
- with open('elo_ratings.json', 'r') as file:
26
- return json.load(file)
27
- except FileNotFoundError:
28
- return {model: 1200 for model in chatbots.keys()}
29
-
30
- def write_elo_ratings(elo_ratings):
31
- with open('elo_ratings.json', 'w') as file:
32
- json.dump(elo_ratings, file, indent=4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  # Function to get bot response
35
  def format_alpaca_prompt(state):
@@ -84,13 +111,13 @@ def chat_with_bots(user_input, state):
84
  return bot1_response, bot2_response
85
 
86
  def update_ratings(state, winner_index):
87
- elo_ratings = get_user_elo_ratings(state)
88
  bot_names = list(chatbots.keys())
89
  winner = state['last_bots'][winner_index]
90
  loser = state['last_bots'][1 - winner_index]
91
 
92
  elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
93
- write_elo_ratings(elo_ratings)
94
  return [('Winner: ',state['last_bots'][winner_index]),('Loser: ',state['last_bots'][1 - winner_index])]
95
 
96
  def vote_up_model(state, chatbot,chatbot2):
@@ -111,7 +138,7 @@ def user_ask(state, chatbot1, chatbot2, textbox):
111
  user_input = user_input[:200] # Limit user input to 200 characters
112
 
113
  # Updating state with the current ELO ratings
114
- state["elo_ratings"] = read_elo_ratings()
115
  if "history" not in state:
116
  state.update({'history': [[],[]]})
117
  state["history"][0].extend([
@@ -140,7 +167,7 @@ import pandas as pd
140
 
141
  # Function to generate leaderboard data
142
  def generate_leaderboard():
143
- elo_ratings = read_elo_ratings() # Assuming this function returns a dict of {bot_name: elo_score}
144
  leaderboard_data = pd.DataFrame(list(elo_ratings.items()), columns=['Chatbot', 'ELO Score'])
145
  leaderboard_data = leaderboard_data.sort_values('ELO Score', ascending=False)
146
  return leaderboard_data
 
15
  return state, None,None,gr.Button.update(interactive=False),gr.Button.update(interactive=False)
16
 
17
 
18
+
19
+
20
+
21
+ from datasets import load_dataset
22
+ import requests
23
+
24
+ HUGGINGFACE_DATASET_URL = "https://huggingface.co/datasets/your_username/your_dataset_name"
25
+ API_TOKEN = "your_huggingface_api_token" # Keep this secure
26
+
27
+ def get_user_elo_ratings():
28
+ dataset = load_dataset("rwitz/mistral-elo-ratings", streaming=True)
29
+ elo_ratings = dataset['train'] # or the relevant split
30
+
31
+ # Check and assign default ELO rating
32
+ default_elo = 1200
33
+ for bot in list(chatbots.keys()):
34
+ if bot not in elo_ratings:
35
+ elo_ratings[bot] = default_elo
36
+
37
+ return elo_ratings
38
+
39
+ def update_elo_rating(player_id, new_rating):
40
+ # Fetch the current dataset
41
+ elo_ratings = get_user_elo_ratings()
42
+
43
+ # Update the rating
44
+ elo_ratings = elo_ratings.map(lambda x: {'rating': new_rating})
45
+
46
+ # Push the updated dataset back to Hugging Face
47
+ # This part is more complex as it involves using Hugging Face's dataset push API.
48
+ # You'll need to use the `requests` library to make a POST request to Hugging Face's API.
49
+ # This is a simplified example:
50
+ updated_data = elo_ratings.to_dict()
51
+ response = requests.post(
52
+ "https://huggingface.co/datasets/rwitz/mistral-elo-ratings",
53
+ headers={"Authorization": f"Bearer {os.environ.get("huggingface_token")}"},
54
+ json=updated_data
55
+ )
56
+ if response.status_code == 200:
57
+ print("Successfully updated the dataset")
58
+ else:
59
+ print("Failed to update the dataset")
60
 
61
  # Function to get bot response
62
  def format_alpaca_prompt(state):
 
111
  return bot1_response, bot2_response
112
 
113
  def update_ratings(state, winner_index):
114
+ elo_ratings = get_user_elo_ratings()
115
  bot_names = list(chatbots.keys())
116
  winner = state['last_bots'][winner_index]
117
  loser = state['last_bots'][1 - winner_index]
118
 
119
  elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
120
+ update_elo_rating(elo_ratings)
121
  return [('Winner: ',state['last_bots'][winner_index]),('Loser: ',state['last_bots'][1 - winner_index])]
122
 
123
  def vote_up_model(state, chatbot,chatbot2):
 
138
  user_input = user_input[:200] # Limit user input to 200 characters
139
 
140
  # Updating state with the current ELO ratings
141
+ state["elo_ratings"] = get_user_elo_ratings()
142
  if "history" not in state:
143
  state.update({'history': [[],[]]})
144
  state["history"][0].extend([
 
167
 
168
  # Function to generate leaderboard data
169
  def generate_leaderboard():
170
+ elo_ratings = get_user_elo_ratings() # Assuming this function returns a dict of {bot_name: elo_score}
171
  leaderboard_data = pd.DataFrame(list(elo_ratings.items()), columns=['Chatbot', 'ELO Score'])
172
  leaderboard_data = leaderboard_data.sort_values('ELO Score', ascending=False)
173
  return leaderboard_data