rwitz commited on
Commit
5c13efa
1 Parent(s): 79132a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -30
app.py CHANGED
@@ -8,69 +8,75 @@ from elo import update_elo_ratings # Custom function for ELO ratings
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()
 
8
  with open('chatbot_urls.json', 'r') as file:
9
  chatbots = json.load(file)
10
 
11
+ # Load existing ELO ratings or initialize them
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
  return {model: 1200 for model in chatbots.keys()}
18
 
19
+ # Update ELO ratings in a file
20
  def write_elo_ratings(elo_ratings):
21
  with open('elo_ratings.json', 'w') as file:
22
  json.dump(elo_ratings, file, indent=4)
23
 
 
24
  elo_ratings = read_elo_ratings()
25
 
26
+ def get_bot_response(url, prompt):
27
+ payload = {
28
+ "input": {
29
+ "prompt": prompt,
30
+ "sampling_params": {
31
+ "max_new_tokens": 16,
32
+ "temperature": 0.7,
 
 
 
33
  }
34
  }
35
+ }
36
+ headers = {
37
+ "accept": "application/json",
38
+ "content-type": "application/json",
39
+ "authorization": os.environ.get("RUNPOD_TOKEN")
40
+ }
41
+ response = requests.post(url, json=payload, headers=headers)
42
+ return response.json()
43
+
44
+ def chat_with_bots(user_input):
45
+ bot1_url = chatbots[list(chatbots.keys())[0]]
46
+ bot2_url = chatbots[list(chatbots.keys())[1]]
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(bot_index):
 
54
  global elo_ratings
55
+ bot_names = list(chatbots.keys())
56
+ winner = bot_names[bot_index]
57
+ loser = bot_names[1 - bot_index]
58
+
59
+ elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
60
  write_elo_ratings(elo_ratings)
61
+
62
+ return f"Bot 1 is {bot_names[0]} with ELO {elo_ratings[bot_names[0]]}\nBot 2 is {bot_names[1]} with ELO {elo_ratings[bot_names[1]]}"
63
 
64
  # Gradio Interface
65
  iface = gr.Interface(
66
  fn=chat_with_bots,
67
  inputs=[
68
+ gr.inputs.Textbox(label="Your message")
 
 
69
  ],
70
  outputs=[
71
  gr.outputs.Textbox(label="Bot 1 Response"),
72
+ gr.outputs.Textbox(label="Bot 2 Response")
 
73
  ],
74
  live=True
75
  )
76
 
77
+ iface.add_component(gr.inputs.Radio(["Bot 1", "Bot 2"], label="Vote for the Best Response"), "vote")
78
+ iface.add_component(gr.outputs.Textbox(label="Voting Result"), "vote_result")
79
+
80
+ iface.update(value=update_ratings, component_name="vote", outputs=["vote_result"])
81
+
82
  iface.launch()