Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import os | |
import json | |
from elo import update_elo_ratings # Custom function for ELO ratings | |
# Load the chatbot URLs and their respective model names from a JSON file | |
with open('chatbot_urls.json', 'r') as file: | |
chatbots = json.load(file) | |
# Function to read ELO ratings from a file | |
def read_elo_ratings(): | |
try: | |
with open('elo_ratings.json', 'r') as file: | |
return json.load(file) | |
except FileNotFoundError: | |
# Initialize ELO ratings for existing models | |
return {model: 1200 for model in chatbots.keys()} | |
# Function to update ELO ratings in a file | |
def write_elo_ratings(elo_ratings): | |
with open('elo_ratings.json', 'w') as file: | |
json.dump(elo_ratings, file, indent=4) | |
# Load existing ELO ratings or initialize them | |
elo_ratings = read_elo_ratings() | |
def chat_with_bots(user_input, bot1_url, bot2_url): | |
# Function to interact with both chatbots | |
def get_bot_response(url, prompt): | |
payload = { | |
"input": { | |
"prompt": prompt, | |
"sampling_params": { | |
"max_new_tokens": 16, | |
"temperature": 0.7, | |
} | |
} | |
} | |
headers = { | |
"accept": "application/json", | |
"content-type": "application/json", | |
"authorization": os.environ.get("RUNPOD_TOKEN") | |
} | |
response = requests.post(url, json=payload, headers=headers) | |
return response.json() | |
bot1_response = get_bot_response(bot1_url, user_input) | |
bot2_response = get_bot_response(bot2_url, user_input) | |
return bot1_response, bot2_response | |
def update_ratings(winner_model): | |
# Update ELO ratings based on the winner | |
global elo_ratings | |
loser_model = set(elo_ratings.keys()) - {winner_model} | |
elo_ratings = update_elo_ratings(elo_ratings, winner_model, loser_model) | |
write_elo_ratings(elo_ratings) | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=chat_with_bots, | |
inputs=[ | |
gr.inputs.Textbox(label="Your message"), | |
gr.inputs.Radio(list(chatbots.values()), label="Bot 1"), | |
gr.inputs.Radio(list(chatbots.values()), label="Bot 2") | |
], | |
outputs=[ | |
gr.outputs.Textbox(label="Bot 1 Response"), | |
gr.outputs.Textbox(label="Bot 2 Response"), | |
gr.components.Button("Vote for the Best Response", elem_id="vote_button") | |
], | |
live=True | |
) | |
iface.launch() | |