Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -29,9 +29,8 @@ 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["elo_ratings"]
|
32 |
-
|
33 |
-
|
34 |
-
return elo_collection, user_collection
|
35 |
|
36 |
def get_user_elo_ratings(collection):
|
37 |
rows = list(collection.find())
|
@@ -176,7 +175,7 @@ async def user_ask(state, chatbot1, chatbot2, textbox):
|
|
176 |
if len(user_input) > 200:
|
177 |
user_input = user_input[:200] # Limit user input to 200 characters
|
178 |
|
179 |
-
|
180 |
|
181 |
# Updating state with the current ELO ratings
|
182 |
state["elo_ratings"] = get_user_elo_ratings(collection)
|
@@ -202,12 +201,6 @@ async def user_ask(state, chatbot1, chatbot2, textbox):
|
|
202 |
state["history"] = state["history"][-10:]
|
203 |
|
204 |
# Format the conversation in ChatML format
|
205 |
-
if user_id is not None:
|
206 |
-
user_collection.update_one(
|
207 |
-
{"user_id": user_id},
|
208 |
-
{"$push": {"chat_logs": {"user_input": user_input, "bot1_response": bot1_response, "bot2_response": bot2_response}}},
|
209 |
-
upsert=True
|
210 |
-
)
|
211 |
|
212 |
return state, chatbot1, chatbot2, gr.update(value=''),enable_btn,enable_btn
|
213 |
import pandas as pd
|
@@ -229,8 +222,8 @@ def submit_model(model_name):
|
|
229 |
else:
|
230 |
return "Discord webhook URL not configured."
|
231 |
|
232 |
-
def generate_leaderboard(
|
233 |
-
rows = list(
|
234 |
leaderboard_data = pd.DataFrame(rows, columns=['bot_name', 'elo_rating', 'games_played'])
|
235 |
leaderboard_data['original_model'] = leaderboard_data['bot_name'].apply(lambda x: next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == x))
|
236 |
leaderboard_data = leaderboard_data[['original_model', 'elo_rating', 'games_played']]
|
@@ -240,8 +233,8 @@ def generate_leaderboard(elo_collection):
|
|
240 |
return leaderboard_data
|
241 |
|
242 |
def refresh_leaderboard():
|
243 |
-
|
244 |
-
return generate_leaderboard(
|
245 |
async def direct_chat(model, user_input, chatbot):
|
246 |
adapter = next(entry['adapter'] for entry in chatbots_data if entry['original_model'] == model)
|
247 |
temp_state = {
|
@@ -255,91 +248,12 @@ async def direct_chat(model, user_input, chatbot):
|
|
255 |
return "", chatbot
|
256 |
def reset_direct_chat():
|
257 |
return "", [], gr.Dropdown.update(value=model_dropdown.value)
|
258 |
-
def save_character(user_id, character_name, character_data):
|
259 |
-
if user_id is None:
|
260 |
-
return "Please log in to save characters."
|
261 |
-
elo_collection, user_collection = init_database()
|
262 |
-
user_collection.update_one(
|
263 |
-
{"user_id": user_id},
|
264 |
-
{"$set": {f"characters.{character_name}": character_data}},
|
265 |
-
upsert=True
|
266 |
-
)
|
267 |
-
return f"Character '{character_name}' saved successfully!"
|
268 |
-
|
269 |
-
def load_character(user_id, character_name):
|
270 |
-
if user_id is None:
|
271 |
-
return None
|
272 |
-
elo_collection, user_collection = init_database()
|
273 |
-
user_data = user_collection.find_one({"user_id": user_id})
|
274 |
-
if user_data and character_name in user_data.get("characters", {}):
|
275 |
-
return user_data["characters"][character_name]
|
276 |
-
else:
|
277 |
-
return None
|
278 |
-
|
279 |
-
def delete_character(user_id, character_name):
|
280 |
-
if user_id is None:
|
281 |
-
return "Please log in to delete characters."
|
282 |
-
elo_collection, user_collection = init_database()
|
283 |
-
user_collection.update_one(
|
284 |
-
{"user_id": user_id},
|
285 |
-
{"$unset": {f"characters.{character_name}": ""}}
|
286 |
-
)
|
287 |
-
return f"Character '{character_name}' deleted successfully!"
|
288 |
-
|
289 |
-
def load_chat_logs(user_id):
|
290 |
-
if user_id is None:
|
291 |
-
return []
|
292 |
-
elo_collection, user_collection = init_database()
|
293 |
-
user_data = user_collection.find_one({"user_id": user_id})
|
294 |
-
if user_data:
|
295 |
-
return user_data.get("chat_logs", [])
|
296 |
-
else:
|
297 |
-
return []
|
298 |
-
|
299 |
-
def delete_chat_log(user_id, chat_log_index):
|
300 |
-
if user_id is None:
|
301 |
-
return "Please log in to delete chat logs."
|
302 |
-
elo_collection, user_collection = init_database()
|
303 |
-
user_collection.update_one(
|
304 |
-
{"user_id": user_id},
|
305 |
-
{"$unset": {f"chat_logs.{chat_log_index}": ""}}
|
306 |
-
)
|
307 |
-
user_collection.update_one(
|
308 |
-
{"user_id": user_id},
|
309 |
-
{"$pull": {"chat_logs": None}}
|
310 |
-
)
|
311 |
-
return "Chat log deleted successfully!"
|
312 |
-
|
313 |
-
def delete_message(user_id, chat_log_index, message_index):
|
314 |
-
if user_id is None:
|
315 |
-
return "Please log in to delete messages."
|
316 |
-
elo_collection, user_collection = init_database()
|
317 |
-
user_collection.update_one(
|
318 |
-
{"user_id": user_id},
|
319 |
-
{"$unset": {f"chat_logs.{chat_log_index}.{message_index}": ""}}
|
320 |
-
)
|
321 |
-
user_collection.update_one(
|
322 |
-
{"user_id": user_id},
|
323 |
-
{"$pull": {f"chat_logs.{chat_log_index}": None}}
|
324 |
-
)
|
325 |
-
return "Message deleted successfully!"
|
326 |
-
|
327 |
-
def authorize_user(hf_token):
|
328 |
-
headers = {"Authorization": f"Bearer {hf_token}"}
|
329 |
-
response = requests.get("https://huggingface.co/api/whoami", headers=headers)
|
330 |
-
if response.status_code == 200:
|
331 |
-
user_data = response.json()
|
332 |
-
user_id = user_data["id"]
|
333 |
-
return user_id
|
334 |
-
else:
|
335 |
-
return None
|
336 |
-
|
337 |
refresh_leaderboard()
|
338 |
# Gradio interface setup
|
339 |
# Gradio interface setup
|
340 |
with gr.Blocks() as demo:
|
341 |
state = gr.State({})
|
342 |
-
|
343 |
with gr.Tab("π€ Chatbot Arena"):
|
344 |
gr.Markdown("## π₯ Let's see which chatbot wins!")
|
345 |
|
@@ -388,13 +302,14 @@ with gr.Blocks() as demo:
|
|
388 |
direct_regenerate_btn = gr.Button(value="π Regenerate")
|
389 |
direct_reset_btn = gr.Button(value="ποΈ Reset Chat")
|
390 |
|
|
|
|
|
391 |
direct_regenerate_btn.click(direct_regenerate, inputs=[model_dropdown, direct_textbox, direct_chatbot], outputs=[direct_textbox, direct_chatbot])
|
392 |
-
direct_textbox.submit(direct_chat, inputs=[model_dropdown, direct_textbox, direct_chatbot
|
393 |
-
direct_submit_btn.click(direct_chat, inputs=[model_dropdown, direct_textbox, direct_chatbot
|
394 |
direct_reset_btn.click(reset_direct_chat, None, [direct_textbox, direct_chatbot, model_dropdown])
|
395 |
|
396 |
|
397 |
-
|
398 |
with gr.Tab("π Leaderboard"):
|
399 |
gr.Markdown("## π Check out the top-performing models!")
|
400 |
|
@@ -415,40 +330,9 @@ with gr.Blocks() as demo:
|
|
415 |
model_name_input = gr.Textbox(placeholder="Enter the model name")
|
416 |
submit_model_btn = gr.Button(value="Submit Model")
|
417 |
submit_model_btn.click(submit_model, inputs=[model_name_input], outputs=[model_name_input])
|
418 |
-
with gr.Tab("π€ User Account"):
|
419 |
-
gr.Markdown("## π€ Manage your account")
|
420 |
-
|
421 |
-
with gr.Row():
|
422 |
-
hf_token = gr.Textbox(placeholder="Enter your Hugging Face token")
|
423 |
-
authorize_btn = gr.Button(value="Authorize")
|
424 |
-
|
425 |
-
with gr.Row():
|
426 |
-
character_name = gr.Textbox(placeholder="Enter character name")
|
427 |
-
character_data = gr.Textbox(placeholder="Enter character data (JSON)")
|
428 |
-
save_character_btn = gr.Button(value="Save Character")
|
429 |
-
|
430 |
-
with gr.Row():
|
431 |
-
load_character_dropdown = gr.Dropdown(label="Select character to load")
|
432 |
-
load_character_btn = gr.Button(value="Load Character")
|
433 |
-
delete_character_btn = gr.Button(value="Delete Character")
|
434 |
-
|
435 |
-
with gr.Row():
|
436 |
-
chat_logs_dropdown = gr.Dropdown(label="Select chat log")
|
437 |
-
delete_chat_log_btn = gr.Button(value="Delete Chat Log")
|
438 |
-
with gr.Row():
|
439 |
-
chat_log_index = gr.Number(label="Chat Log Index")
|
440 |
-
message_index = gr.Number(label="Message Index")
|
441 |
-
delete_message_btn = gr.Button(value="Delete Message")
|
442 |
-
|
443 |
-
authorize_btn.click(authorize_user, inputs=[hf_token], outputs=[user_id])
|
444 |
-
save_character_btn.click(save_character, inputs=[user_id, character_name, character_data], outputs=[])
|
445 |
-
load_character_btn.click(load_character, inputs=[user_id, load_character_dropdown], outputs=[character_data])
|
446 |
-
delete_character_btn.click(delete_character, inputs=[user_id, load_character_dropdown], outputs=[])
|
447 |
-
chat_logs_dropdown.change(load_chat_logs, inputs=[user_id], outputs=[chat_logs_dropdown])
|
448 |
-
delete_chat_log_btn.click(delete_chat_log, inputs=[user_id, chat_logs_dropdown], outputs=[])
|
449 |
-
delete_message_btn.click(delete_message, inputs=[user_id, chat_log_index, message_index], outputs=[])
|
450 |
|
451 |
|
452 |
# Launch the Gradio interface
|
453 |
if __name__ == "__main__":
|
454 |
-
demo.launch(share=False)
|
|
|
|
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["elo_ratings"]
|
32 |
+
collection = db["elo_ratings"]
|
33 |
+
return collection
|
|
|
34 |
|
35 |
def get_user_elo_ratings(collection):
|
36 |
rows = list(collection.find())
|
|
|
175 |
if len(user_input) > 200:
|
176 |
user_input = user_input[:200] # Limit user input to 200 characters
|
177 |
|
178 |
+
collection = init_database() # Initialize the collection object
|
179 |
|
180 |
# Updating state with the current ELO ratings
|
181 |
state["elo_ratings"] = get_user_elo_ratings(collection)
|
|
|
201 |
state["history"] = state["history"][-10:]
|
202 |
|
203 |
# Format the conversation in ChatML format
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
return state, chatbot1, chatbot2, gr.update(value=''),enable_btn,enable_btn
|
206 |
import pandas as pd
|
|
|
222 |
else:
|
223 |
return "Discord webhook URL not configured."
|
224 |
|
225 |
+
def generate_leaderboard(collection):
|
226 |
+
rows = list(collection.find())
|
227 |
leaderboard_data = pd.DataFrame(rows, columns=['bot_name', 'elo_rating', 'games_played'])
|
228 |
leaderboard_data['original_model'] = leaderboard_data['bot_name'].apply(lambda x: next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == x))
|
229 |
leaderboard_data = leaderboard_data[['original_model', 'elo_rating', 'games_played']]
|
|
|
233 |
return leaderboard_data
|
234 |
|
235 |
def refresh_leaderboard():
|
236 |
+
collection = init_database()
|
237 |
+
return generate_leaderboard(collection)
|
238 |
async def direct_chat(model, user_input, chatbot):
|
239 |
adapter = next(entry['adapter'] for entry in chatbots_data if entry['original_model'] == model)
|
240 |
temp_state = {
|
|
|
248 |
return "", chatbot
|
249 |
def reset_direct_chat():
|
250 |
return "", [], gr.Dropdown.update(value=model_dropdown.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
refresh_leaderboard()
|
252 |
# Gradio interface setup
|
253 |
# Gradio interface setup
|
254 |
with gr.Blocks() as demo:
|
255 |
state = gr.State({})
|
256 |
+
|
257 |
with gr.Tab("π€ Chatbot Arena"):
|
258 |
gr.Markdown("## π₯ Let's see which chatbot wins!")
|
259 |
|
|
|
302 |
direct_regenerate_btn = gr.Button(value="π Regenerate")
|
303 |
direct_reset_btn = gr.Button(value="ποΈ Reset Chat")
|
304 |
|
305 |
+
# ...
|
306 |
+
|
307 |
direct_regenerate_btn.click(direct_regenerate, inputs=[model_dropdown, direct_textbox, direct_chatbot], outputs=[direct_textbox, direct_chatbot])
|
308 |
+
direct_textbox.submit(direct_chat, inputs=[model_dropdown, direct_textbox, direct_chatbot], outputs=[direct_textbox, direct_chatbot])
|
309 |
+
direct_submit_btn.click(direct_chat, inputs=[model_dropdown, direct_textbox, direct_chatbot], outputs=[direct_textbox, direct_chatbot])
|
310 |
direct_reset_btn.click(reset_direct_chat, None, [direct_textbox, direct_chatbot, model_dropdown])
|
311 |
|
312 |
|
|
|
313 |
with gr.Tab("π Leaderboard"):
|
314 |
gr.Markdown("## π Check out the top-performing models!")
|
315 |
|
|
|
330 |
model_name_input = gr.Textbox(placeholder="Enter the model name")
|
331 |
submit_model_btn = gr.Button(value="Submit Model")
|
332 |
submit_model_btn.click(submit_model, inputs=[model_name_input], outputs=[model_name_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|
334 |
|
335 |
# Launch the Gradio interface
|
336 |
if __name__ == "__main__":
|
337 |
+
demo.launch(share=False)
|
338 |
+
|