Spaces:
Runtime error
Runtime error
File size: 13,425 Bytes
9423b6b a5b48a7 df24d9c a5b48a7 ffb29d4 0b9c45d a5b48a7 c003b8d a5b48a7 4097bac d51f0d6 30ed04f 8e00642 30ed04f 8e00642 30ed04f d51f0d6 30ed04f 5158e04 d51f0d6 4097bac d51f0d6 ec9d3d3 e2a026e 4097bac 65c4df1 7ca6d3a 65c4df1 1823a15 e9bb75c 40c75e9 038196b 40c75e9 038196b 40c75e9 e9bb75c 40c75e9 7d3d77d b4b5ce5 7d3d77d a272cb9 7d3d77d e2a026e 5b3f957 45d1701 3c0c20b 45d1701 0f925b8 45d1701 3c0c20b 45d1701 3c0c20b 45d1701 dfb47fb e675f6b 7178e21 e675f6b 7ca6d3a 64776a6 9504740 7178e21 7ca6d3a 7178e21 dfb47fb b153149 583cdf6 dfb47fb e675f6b dfb47fb e675f6b e265ae5 e9bb75c 7ca6d3a 3421166 7ca6d3a 3421166 8909c53 a5b48a7 4f778fa e2a026e 65c4df1 5c13efa 65c4df1 e2a026e 8e50330 e2a026e fa1169f 628ad2b 8e50330 e2a026e fa1169f 628ad2b 66afc6f 62664c3 41b12a8 ffef722 5b3f957 0b25d1f c25a9e9 5b3f957 c25a9e9 360577c 6eb3c68 a2df623 5b3f957 62664c3 6eb3c68 5b3f957 6eb3c68 25a4d3c 0b25d1f 5b3f957 cf47250 5b3f957 66afc6f 4db4ec7 dad0315 0d45e75 e2a026e 65c4df1 e2a026e 227a710 dad0315 65c4df1 e5f7a31 43c28d5 cbc3047 65c4df1 c5494d3 65c4df1 cbc3047 64776a6 e1ddfc1 5b3f957 628ad2b 0b25d1f 5c74d7e f1d1dd8 5c74d7e f1d1dd8 5c74d7e 30ed04f 5c74d7e 83df2c0 4609e4a d611593 5c74d7e 30ed04f 5c74d7e 628ad2b 5c74d7e 628ad2b 5c74d7e 8e50330 b33cc37 5c74d7e 65c4df1 bde2561 5c74d7e 83df2c0 4609e4a c4c018d 5c74d7e 30ed04f 5c74d7e 30ed04f 64776a6 5c74d7e b33cc37 5c74d7e 3704691 f7dd537 3704691 628ad2b 5c74d7e 43c28d5 f1d1dd8 628ad2b ca86104 4097bac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
import gradio as gr
import requests
import os
import pandas as pd
import json
import ssl
import random
from elo import update_elo_ratings # Custom function for ELO ratings
enable_btn = gr.Button.update(interactive=True)
import sqlite3
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
async def direct_regenerate(model, user_input, chatbot):
adapter = next(entry['adapter'] for entry in chatbots_data if entry['original_model'] == model)
temp_state = {
"history": [
[{"role": "user", "content": user_input}]
]
}
response = await get_bot_response(adapter, user_input, temp_state, 0)
chatbot[-1] = (user_input, response)
return "", chatbot
password=os.environ.get("MONGODB")
def init_database():
uri = f"mongodb+srv://new-user:{password}@cluster0.xb2urf6.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
client = MongoClient(uri)
db = client["elo_ratings"]
collection = db["elo_ratings"]
return collection
def get_user_elo_ratings(collection):
rows = list(collection.find())
if rows:
return {row['bot_name']: {'elo_rating': row['elo_rating'], 'games_played': row['games_played']} for row in rows}
else:
return {"default": {'elo_rating': 1200, 'games_played': 0}}
def update_elo_rating(collection, updated_ratings, winner, loser):
collection.update_one({"bot_name": winner}, {"$set": {"elo_rating": updated_ratings[winner]['elo_rating'], "games_played": updated_ratings[winner]['games_played']}}, upsert=True)
collection.update_one({"bot_name": loser}, {"$set": {"elo_rating": updated_ratings[loser]['elo_rating'], "games_played": updated_ratings[loser]['games_played']}}, upsert=True)
import json
with open('chatbots.txt', 'r') as file:
chatbots_data = json.load(file)
chatbots = [entry['adapter'] for entry in chatbots_data]
def clear_chat(state):
# Reset state including the chatbot order
state = {} if state is not None else state
# Initialize the collection object
collection = init_database()
# Get the list of adapter names
adapter_names = [entry['adapter'] for entry in chatbots_data]
# Randomly select two new adapters
selected_adapters = random.sample(adapter_names, 2)
state['last_bots'] = selected_adapters
# Reset other components
return state, [], [], gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(value='', interactive=True), gr.Button.update(interactive=True)
from datasets import load_dataset,DatasetDict,Dataset
import requests
import os
# Function to get bot response
def format_chatml_prompt(state):
chatml_prompt = "<|im_start|>system You are a helpful assistant, who can think outside the box.<|im_end|>"
chatml_prompt2 = "<|im_start|>system You are a helpful assistant, who can think outside the box.<|im_end|>"
for message in state["history"][0]:
if message['role'] == 'user':
chatml_prompt += "\n<|im_start|>user " + message['content'] + "<|im_end|>"
else:
chatml_prompt += "\n<|im_start|>assistant " + message['content'] + "<|im_end|>"
for message in state["history"][1]:
if message['role'] == 'user':
chatml_prompt2 += "\n<|im_start|>user " + message['content'] + "<|im_end|>"
else:
chatml_prompt2 += "\n<|im_start|>assistant " + message['content'] + "<|im_end|>"
return [chatml_prompt + "\n<|im_start|>assistant", chatml_prompt2 + "\n<|im_start|>assistant"]
import aiohttp
import asyncio
from tenacity import retry, stop_after_attempt, wait_exponential
async def get_bot_response(adapter_id, prompt, state, bot_index):
chatml_prompt = format_chatml_prompt(state)
print(chatml_prompt)
payload = {
"inputs": chatml_prompt[bot_index],
"parameters": {
"adapter_id": adapter_id,
"adapter_source": "hub",
"temperature": 0.7,
"max_new_tokens": 100
}
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('PREDIBASE_TOKEN')}"
}
async with aiohttp.ClientSession() as session:
try:
async with session.post("https://serving.app.predibase.com/79957f/deployments/v2/llms/mistral-7b/generate",
json=payload, headers=headers, timeout=30) as response:
if response.status == 200:
response_data = await response.json()
response_text = response_data.get('generated_text', '')
else:
error_text = await response.text()
print(error_text)
response_text = "Sorry, I couldn't generate a response."
except (aiohttp.ClientError, asyncio.TimeoutError):
response_text = "Sorry, I couldn't generate a response."
return response_text.split('<|im_end|>')[0].strip()
async def chat_with_bots(user_input, state):
# Use existing bot order from state if available, otherwise shuffle and initialize
if 'last_bots' not in state or not state['last_bots']:
random.shuffle(chatbots)
state['last_bots'] = [chatbots[0], chatbots[1]]
bot1_adapter, bot2_adapter = state['last_bots'][0], state['last_bots'][1]
bot1_response, bot2_response = await asyncio.gather(
get_bot_response(bot1_adapter, user_input, state, 0),
get_bot_response(bot2_adapter, user_input, state, 1)
)
return bot1_response.replace("<|im_end|>",""), bot2_response.replace("<|im_end|>","")
def update_ratings(state, winner_index, collection):
elo_ratings = get_user_elo_ratings(collection)
winner_adapter = state['last_bots'][winner_index]
loser_adapter = state['last_bots'][1 - winner_index]
winner = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == winner_adapter)
loser = next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == loser_adapter)
elo_ratings = update_elo_ratings(elo_ratings, winner_adapter, loser_adapter)
update_elo_rating(collection, elo_ratings, winner_adapter, loser_adapter)
return [('Winner: ', winner), ('Loser: ', loser)]
def vote_up_model(state, chatbot, chatbot2):
collection = init_database()
update_message = update_ratings(state, 0, collection)
chatbot.append(update_message[0])
chatbot2.append(update_message[1])
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
def vote_down_model(state, chatbot, chatbot2):
collection = init_database()
update_message = update_ratings(state, 1, collection)
chatbot2.append(update_message[0])
chatbot.append(update_message[1])
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
async def user_ask(state, chatbot1, chatbot2, textbox):
global enable_btn
user_input = textbox
if len(user_input) > 200:
user_input = user_input[:200] # Limit user input to 200 characters
collection = init_database() # Initialize the collection object
# Updating state with the current ELO ratings
state["elo_ratings"] = get_user_elo_ratings(collection)
if "history" not in state:
state.update({'history': [[],[]]})
state["history"][0].extend([
{"role": "user", "content": user_input}])
state["history"][1].extend([
{"role": "user", "content": user_input}])
# Chat with bots
bot1_response, bot2_response = await chat_with_bots(user_input, state)
state["history"][0].extend([
{"role": "bot1", "content": bot1_response},
])
state["history"][1].extend([
{"role": "bot2", "content": bot2_response},
])
chatbot1.append((user_input,bot1_response))
chatbot2.append((user_input,bot2_response))
# Keep only the last 10 messages in history
state["history"] = state["history"][-10:]
# Format the conversation in ChatML format
return state, chatbot1, chatbot2, gr.update(value=''),enable_btn,enable_btn
import pandas as pd
# Function to generate leaderboard data
def generate_leaderboard(collection):
rows = list(collection.find())
leaderboard_data = pd.DataFrame(rows, columns=['bot_name', 'elo_rating', 'games_played'])
leaderboard_data['original_model'] = leaderboard_data['bot_name'].apply(lambda x: next(entry['original_model'] for entry in chatbots_data if entry['adapter'] == x))
leaderboard_data = leaderboard_data[['original_model', 'elo_rating', 'games_played']]
leaderboard_data.columns = ['Chatbot', 'ELO Score', 'Games Played']
leaderboard_data['ELO Score'] = leaderboard_data['ELO Score'].round().astype(int)
leaderboard_data = leaderboard_data.sort_values('ELO Score', ascending=False)
return leaderboard_data
def refresh_leaderboard():
collection = init_database()
return generate_leaderboard(collection)
async def direct_chat(model, user_input, chatbot):
adapter = next(entry['adapter'] for entry in chatbots_data if entry['original_model'] == model)
temp_state = {
"history": [
[{"role": "user", "content": user_input}],
[{"role": "user", "content": user_input}]
]
}
response = await get_bot_response(adapter, user_input, temp_state, 0)
chatbot.append((user_input, response))
return "", chatbot
def reset_direct_chat():
return "", [], gr.Dropdown.update(value=model_dropdown.value)
refresh_leaderboard()
# Gradio interface setup
# Gradio interface setup
with gr.Blocks() as demo:
state = gr.State({})
with gr.Tab("π€ Chatbot Arena"):
gr.Markdown("## π₯ Let's see which chatbot wins!")
with gr.Row():
with gr.Column():
chatbot1 = gr.Chatbot(label='π€ Model A').style(height=500)
upvote_btn_a = gr.Button(value="π Upvote A", interactive=False).style(full_width=True)
with gr.Column():
chatbot2 = gr.Chatbot(label='π€ Model B').style(height=500)
upvote_btn_b = gr.Button(value="π Upvote B", interactive=False).style(full_width=True)
with gr.Row():
with gr.Column(scale=5):
textbox = gr.Textbox(placeholder="π€ Enter your prompt (up to 200 characters)")
submit_btn = gr.Button(value="Submit")
with gr.Row():
reset_btn = gr.Button(value="ποΈ Reset")
# ...
reset_btn.click(clear_chat, inputs=[state], outputs=[state, chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
submit_btn.click(user_ask, inputs=[state, chatbot1, chatbot2, textbox], outputs=[state, chatbot1, chatbot2, textbox, upvote_btn_a, upvote_btn_b], queue=True)
collection = init_database()
upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1, chatbot2], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
upvote_btn_b.click(vote_down_model, inputs=[state, chatbot1, chatbot2], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
textbox.submit(user_ask, inputs=[state, chatbot1, chatbot2, textbox], outputs=[state, chatbot1, chatbot2, textbox, upvote_btn_a, upvote_btn_b], queue=True)
with gr.Tab("π¬ Direct Chat"):
gr.Markdown("## π£οΈ Chat directly with a model!")
with gr.Row():
model_dropdown = gr.Dropdown(choices=[entry['original_model'] for entry in chatbots_data], value=chatbots_data[0]['original_model'], label="π€ Select a model")
with gr.Row():
direct_chatbot = gr.Chatbot(label="π¬ Direct Chat").style(height=500)
with gr.Row():
with gr.Column(scale=5):
direct_textbox = gr.Textbox(placeholder="π Enter your message")
direct_submit_btn = gr.Button(value="Submit")
with gr.Row():
direct_regenerate_btn = gr.Button(value="π Regenerate")
direct_reset_btn = gr.Button(value="ποΈ Reset Chat")
# ...
direct_regenerate_btn.click(direct_regenerate, inputs=[model_dropdown, direct_textbox, direct_chatbot], outputs=[direct_textbox, direct_chatbot])
direct_textbox.submit(direct_chat, inputs=[model_dropdown, direct_textbox, direct_chatbot], outputs=[direct_textbox, direct_chatbot])
direct_submit_btn.click(direct_chat, inputs=[model_dropdown, direct_textbox, direct_chatbot], outputs=[direct_textbox, direct_chatbot])
direct_reset_btn.click(reset_direct_chat, None, [direct_textbox, direct_chatbot, model_dropdown])
with gr.Tab("π Leaderboard"):
gr.Markdown("## π Check out the top-performing models!")
try:
leaderboard = gr.Dataframe(refresh_leaderboard())
except:
leaderboard = gr.Dataframe()
with gr.Row():
refresh_btn = gr.Button("π Refresh Leaderboard")
refresh_btn.click(refresh_leaderboard, outputs=[leaderboard])
# Launch the Gradio interface
if __name__ == "__main__":
demo.launch(share=False) |