Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,36 @@ from elo import update_elo_ratings # Custom function for ELO ratings
|
|
10 |
enable_btn = gr.Button.update(interactive=True)
|
11 |
|
12 |
import sqlite3
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
from pymongo.mongo_client import MongoClient
|
15 |
from pymongo.server_api import ServerApi
|
16 |
async def direct_regenerate(model, user_input, chatbot, character_name, character_description, user_name):
|
@@ -28,21 +57,10 @@ password=os.environ.get("MONGODB")
|
|
28 |
def init_database():
|
29 |
uri = f"mongodb+srv://new-user:{password}@cluster0.xb2urf6.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
30 |
client = MongoClient(uri)
|
31 |
-
db = client["
|
32 |
-
collection = db["
|
33 |
return collection
|
34 |
|
35 |
-
def get_user_elo_ratings(collection):
|
36 |
-
rows = list(collection.find())
|
37 |
-
if rows:
|
38 |
-
return {row['bot_name']: {'elo_rating': row['elo_rating'], 'games_played': row['games_played']} for row in rows}
|
39 |
-
else:
|
40 |
-
return {"default": {'elo_rating': 1200, 'games_played': 0}}
|
41 |
-
|
42 |
-
def update_elo_rating(collection, updated_ratings, winner, loser):
|
43 |
-
collection.update_one({"bot_name": winner}, {"$set": {"elo_rating": updated_ratings[winner]['elo_rating'], "games_played": updated_ratings[winner]['games_played']}}, upsert=True)
|
44 |
-
collection.update_one({"bot_name": loser}, {"$set": {"elo_rating": updated_ratings[loser]['elo_rating'], "games_played": updated_ratings[loser]['games_played']}}, upsert=True)
|
45 |
-
|
46 |
import json
|
47 |
|
48 |
with open('chatbots.txt', 'r') as file:
|
@@ -142,7 +160,7 @@ async def chat_with_bots(user_input, state, character_name, character_descriptio
|
|
142 |
get_bot_response(bot2_adapter, user_input, state, 1, character_name, character_description, user_name)
|
143 |
)
|
144 |
return bot1_response.replace("<|im_end|>",""), bot2_response.replace("<|im_end|>","")
|
145 |
-
def update_ratings(state, winner_index, collection):
|
146 |
elo_ratings = get_user_elo_ratings(collection)
|
147 |
winner_adapter = state['last_bots'][winner_index]
|
148 |
loser_adapter = state['last_bots'][1 - winner_index]
|
@@ -150,24 +168,28 @@ def update_ratings(state, winner_index, collection):
|
|
150 |
winner = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == winner_adapter)
|
151 |
loser = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == loser_adapter)
|
152 |
|
153 |
-
elo_ratings = update_elo_ratings(elo_ratings, winner_adapter, loser_adapter)
|
154 |
-
update_elo_rating(collection, elo_ratings, winner_adapter, loser_adapter)
|
155 |
return [('Winner: ', winner), ('Loser: ', loser)]
|
156 |
|
157 |
-
def vote_up_model(state, chatbot, chatbot2):
|
158 |
collection = init_database()
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
162 |
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
163 |
|
164 |
-
def vote_down_model(state, chatbot, chatbot2):
|
165 |
collection = init_database()
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
169 |
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
170 |
-
|
171 |
async def user_ask(state, chatbot1, chatbot2, textbox, character_name, character_description, user_name):
|
172 |
if character_name and len(character_name) > 20:
|
173 |
character_name = character_name[:20] # Limit character name to 20 characters
|
@@ -230,16 +252,39 @@ def submit_model(model_name):
|
|
230 |
else:
|
231 |
return "Discord webhook URL not configured."
|
232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
def generate_leaderboard(collection):
|
234 |
rows = list(collection.find())
|
235 |
-
leaderboard_data =
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
|
|
242 |
|
|
|
|
|
|
|
|
|
243 |
def refresh_leaderboard():
|
244 |
collection = init_database()
|
245 |
leaderboard_data = generate_leaderboard(collection)
|
@@ -308,9 +353,8 @@ with gr.Blocks() as demo:
|
|
308 |
textbox.submit(user_ask, inputs=[state, chatbot1, chatbot2, textbox, character_name, character_description, user_name], outputs=[state, chatbot1, chatbot2, textbox, upvote_btn_a, upvote_btn_b], queue=True)
|
309 |
collection = init_database()
|
310 |
|
311 |
-
upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1, chatbot2], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
|
312 |
-
upvote_btn_b.click(vote_down_model, inputs=[state, chatbot1, chatbot2], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
|
313 |
-
|
314 |
with gr.Tab("💬 Direct Chat"):
|
315 |
gr.Markdown("## 🗣️ Chat directly with a model!")
|
316 |
|
|
|
10 |
enable_btn = gr.Button.update(interactive=True)
|
11 |
|
12 |
import sqlite3
|
13 |
+
import requests
|
14 |
|
15 |
+
def classify_vote(user_input):
|
16 |
+
url = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
|
17 |
+
headers = {
|
18 |
+
"accept": "*/*",
|
19 |
+
"accept-language": "en-US,en;q=0.9",
|
20 |
+
"content-type": "application/json",
|
21 |
+
"sec-ch-ua": "\"Not A(Brand\";v=\"99\", \"Microsoft Edge\";v=\"121\", \"Chromium\";v=\"121\"",
|
22 |
+
"sec-ch-ua-mobile": "?0",
|
23 |
+
"sec-ch-ua-platform": "\"macOS\"",
|
24 |
+
"sec-fetch-dest": "empty",
|
25 |
+
"sec-fetch-mode": "cors",
|
26 |
+
"sec-fetch-site": "same-site"
|
27 |
+
}
|
28 |
+
payload = {
|
29 |
+
"inputs": user_input,
|
30 |
+
"parameters": {
|
31 |
+
"candidate_labels": "information and knowledge,problem solving,creative expression,personal development and guidance,entertainment, niche topics",
|
32 |
+
"multi_class": True
|
33 |
+
}
|
34 |
+
}
|
35 |
+
response = requests.post(url, headers=headers, json=payload)
|
36 |
+
if response.status_code == 200:
|
37 |
+
response_data = response.json()
|
38 |
+
top_category = response_data["labels"][0].strip()
|
39 |
+
return top_category
|
40 |
+
else:
|
41 |
+
print(f"Error: {response.status_code}")
|
42 |
+
return None
|
43 |
from pymongo.mongo_client import MongoClient
|
44 |
from pymongo.server_api import ServerApi
|
45 |
async def direct_regenerate(model, user_input, chatbot, character_name, character_description, user_name):
|
|
|
57 |
def init_database():
|
58 |
uri = f"mongodb+srv://new-user:{password}@cluster0.xb2urf6.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
59 |
client = MongoClient(uri)
|
60 |
+
db = client["elo_ratings2"]
|
61 |
+
collection = db["elo_ratings2"]
|
62 |
return collection
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
import json
|
65 |
|
66 |
with open('chatbots.txt', 'r') as file:
|
|
|
160 |
get_bot_response(bot2_adapter, user_input, state, 1, character_name, character_description, user_name)
|
161 |
)
|
162 |
return bot1_response.replace("<|im_end|>",""), bot2_response.replace("<|im_end|>","")
|
163 |
+
def update_ratings(state, winner_index, collection, category):
|
164 |
elo_ratings = get_user_elo_ratings(collection)
|
165 |
winner_adapter = state['last_bots'][winner_index]
|
166 |
loser_adapter = state['last_bots'][1 - winner_index]
|
|
|
168 |
winner = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == winner_adapter)
|
169 |
loser = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == loser_adapter)
|
170 |
|
171 |
+
elo_ratings = update_elo_ratings(elo_ratings, winner_adapter, loser_adapter, category)
|
172 |
+
update_elo_rating(collection, elo_ratings, winner_adapter, loser_adapter, category)
|
173 |
return [('Winner: ', winner), ('Loser: ', loser)]
|
174 |
|
175 |
+
def vote_up_model(state, chatbot, chatbot2, user_input):
|
176 |
collection = init_database()
|
177 |
+
top_category = classify_vote(user_input)
|
178 |
+
if top_category:
|
179 |
+
update_message = update_ratings(state, 0, collection, top_category)
|
180 |
+
chatbot.append(update_message[0])
|
181 |
+
chatbot2.append(update_message[1])
|
182 |
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
183 |
|
184 |
+
def vote_down_model(state, chatbot, chatbot2, user_input):
|
185 |
collection = init_database()
|
186 |
+
top_category = classify_vote(user_input)
|
187 |
+
if top_category:
|
188 |
+
update_message = update_ratings(state, 1, collection, top_category)
|
189 |
+
chatbot2.append(update_message[0])
|
190 |
+
chatbot.append(update_message[1])
|
191 |
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
192 |
+
|
193 |
async def user_ask(state, chatbot1, chatbot2, textbox, character_name, character_description, user_name):
|
194 |
if character_name and len(character_name) > 20:
|
195 |
character_name = character_name[:20] # Limit character name to 20 characters
|
|
|
252 |
else:
|
253 |
return "Discord webhook URL not configured."
|
254 |
|
255 |
+
def get_user_elo_ratings(collection):
|
256 |
+
rows = list(collection.find())
|
257 |
+
if rows:
|
258 |
+
elo_ratings = {}
|
259 |
+
for row in rows:
|
260 |
+
bot_name = row['bot_name']
|
261 |
+
if bot_name not in elo_ratings:
|
262 |
+
elo_ratings[bot_name] = {}
|
263 |
+
for category in row['categories']:
|
264 |
+
elo_ratings[bot_name][category] = {'elo_rating': row['categories'][category]['elo_rating'], 'games_played': row['categories'][category]['games_played']}
|
265 |
+
return elo_ratings
|
266 |
+
else:
|
267 |
+
return {"default": {'overall': {'elo_rating': 1200, 'games_played': 0}}}
|
268 |
+
|
269 |
+
def update_elo_rating(collection, updated_ratings, winner, loser, category):
|
270 |
+
collection.update_one({"bot_name": winner}, {"$set": {f"categories.{category}.elo_rating": updated_ratings[winner][category]['elo_rating'], f"categories.{category}.games_played": updated_ratings[winner][category]['games_played']}}, upsert=True)
|
271 |
+
collection.update_one({"bot_name": loser}, {"$set": {f"categories.{category}.elo_rating": updated_ratings[loser][category]['elo_rating'], f"categories.{category}.games_played": updated_ratings[loser][category]['games_played']}}, upsert=True)
|
272 |
+
|
273 |
def generate_leaderboard(collection):
|
274 |
rows = list(collection.find())
|
275 |
+
leaderboard_data = []
|
276 |
+
for row in rows:
|
277 |
+
bot_name = row['bot_name']
|
278 |
+
original_model = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == bot_name)
|
279 |
+
total_elo = sum(category['elo_rating'] for category in row['categories'].values())
|
280 |
+
total_games = sum(category['games_played'] for category in row['categories'].values())
|
281 |
+
avg_elo = total_elo / len(row['categories']) if len(row['categories']) > 0 else 0
|
282 |
+
leaderboard_data.append([original_model, avg_elo, total_games])
|
283 |
|
284 |
+
leaderboard_data = pd.DataFrame(leaderboard_data, columns=['Chatbot', 'Avg ELO Score', 'Total Games Played'])
|
285 |
+
leaderboard_data['Avg ELO Score'] = leaderboard_data['Avg ELO Score'].round().astype(int)
|
286 |
+
leaderboard_data = leaderboard_data.sort_values('Avg ELO Score', ascending=False)
|
287 |
+
return leaderboard_data
|
288 |
def refresh_leaderboard():
|
289 |
collection = init_database()
|
290 |
leaderboard_data = generate_leaderboard(collection)
|
|
|
353 |
textbox.submit(user_ask, inputs=[state, chatbot1, chatbot2, textbox, character_name, character_description, user_name], outputs=[state, chatbot1, chatbot2, textbox, upvote_btn_a, upvote_btn_b], queue=True)
|
354 |
collection = init_database()
|
355 |
|
356 |
+
upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1, chatbot2, textbox], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
|
357 |
+
upvote_btn_b.click(vote_down_model, inputs=[state, chatbot1, chatbot2, textbox], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
|
|
|
358 |
with gr.Tab("💬 Direct Chat"):
|
359 |
gr.Markdown("## 🗣️ Chat directly with a model!")
|
360 |
|