rwitz commited on
Commit
a5b48a7
1 Parent(s): 66a7158

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import json
5
+ from elo import update_elo_ratings # Custom function for ELO ratings
6
+
7
+ # Load the chatbot URLs and their respective model names from a JSON file
8
+ with open('chatbot_urls.json', 'r') as file:
9
+ chatbots = json.load(file)
10
+
11
+ # Function to read ELO ratings from a file
12
+ def read_elo_ratings():
13
+ try:
14
+ with open('elo_ratings.json', 'r') as file:
15
+ return json.load(file)
16
+ except FileNotFoundError:
17
+ # Initialize ELO ratings for existing models
18
+ return {model: 1200 for model in chatbots.keys()}
19
+
20
+ # Function to update ELO ratings in a file
21
+ def write_elo_ratings(elo_ratings):
22
+ with open('elo_ratings.json', 'w') as file:
23
+ json.dump(elo_ratings, file, indent=4)
24
+
25
+ # Load existing ELO ratings or initialize them
26
+ elo_ratings = read_elo_ratings()
27
+
28
+ def chat_with_bots(user_input, bot1_url, bot2_url):
29
+ # Function to interact with both chatbots
30
+ def get_bot_response(url, prompt):
31
+ payload = {
32
+ "input": {
33
+ "prompt": prompt,
34
+ "sampling_params": {
35
+ "max_new_tokens": 16,
36
+ "temperature": 0.7,
37
+ }
38
+ }
39
+ }
40
+ headers = {
41
+ "accept": "application/json",
42
+ "content-type": "application/json",
43
+ "authorization": os.environ.get("RUNPOD_TOKEN")
44
+ }
45
+ response = requests.post(url, json=payload, headers=headers)
46
+ return response.json()
47
+
48
+ bot1_response = get_bot_response(bot1_url, user_input)
49
+ bot2_response = get_bot_response(bot2_url, user_input)
50
+
51
+ return bot1_response, bot2_response
52
+
53
+ def update_ratings(winner_model):
54
+ # Update ELO ratings based on the winner
55
+ global elo_ratings
56
+ loser_model = set(elo_ratings.keys()) - {winner_model}
57
+ elo_ratings = update_elo_ratings(elo_ratings, winner_model, loser_model)
58
+ write_elo_ratings(elo_ratings)
59
+
60
+ # Gradio Interface
61
+ iface = gr.Interface(
62
+ fn=chat_with_bots,
63
+ inputs=[
64
+ gr.inputs.Textbox(label="Your message"),
65
+ gr.inputs.Radio(list(chatbots.values()), label="Bot 1"),
66
+ gr.inputs.Radio(list(chatbots.values()), label="Bot 2")
67
+ ],
68
+ outputs=[
69
+ gr.outputs.Textbox(label="Bot 1 Response"),
70
+ gr.outputs.Textbox(label="Bot 2 Response"),
71
+ gr.components.Button("Vote for the Best Response", elem_id="vote_button")
72
+ ],
73
+ live=True
74
+ )
75
+
76
+ iface.launch()