rwitz commited on
Commit
66afc6f
β€’
1 Parent(s): 9b622e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -53,6 +53,7 @@ def get_bot_response(url, prompt):
53
  }
54
  response = requests.post(url, json=payload, headers=headers)
55
  return response.json()
 
56
  def chat_with_bots(user_input):
57
  bot_names = list(chatbots.keys())
58
  random.shuffle(bot_names)
@@ -63,33 +64,53 @@ def chat_with_bots(user_input):
63
 
64
  return bot1_response, bot2_response
65
 
66
- def update_ratings(bot_index):
 
 
 
 
 
 
 
 
 
67
  elo_ratings = get_user_elo_ratings()
68
  bot_names = list(chatbots.keys())
69
- winner = bot_names[bot_index]
70
- loser = bot_names[1 - bot_index]
71
 
72
  elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
73
  write_elo_ratings(elo_ratings)
74
-
75
- 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]]}"
76
-
77
- # Gradio Interface
78
- iface = gr.Interface(
79
- fn=chat_with_bots,
80
- inputs=[
81
- gr.components.Textbox(label="Your message")
82
- ],
83
- outputs=[
84
- gr.components.Textbox(label="Bot 1 Response"),
85
- gr.components.Textbox(label="Bot 2 Response")
86
- ],
87
- live=True
88
- )
89
-
90
- iface.add_component(gr.components.Radio(["Bot 1", "Bot 2"], label="Vote for the Best Response"), "vote")
91
- iface.add_component(gr.components.Textbox(label="Voting Result"), "vote_result")
92
-
93
- iface.update(value=update_ratings, component_name="vote", outputs=["vote_result"])
94
-
95
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
53
  }
54
  response = requests.post(url, json=payload, headers=headers)
55
  return response.json()
56
+
57
  def chat_with_bots(user_input):
58
  bot_names = list(chatbots.keys())
59
  random.shuffle(bot_names)
 
64
 
65
  return bot1_response, bot2_response
66
 
67
+ def user_ask(state, chatbot, textbox):
68
+ user_input = textbox.value
69
+ bot1_response, bot2_response = chat_with_bots(user_input)
70
+ chatbot.append("User: " + user_input)
71
+ chatbot.append("Bot 1: " + bot1_response['output'])
72
+ chatbot.append("Bot 2: " + bot2_response['output'])
73
+ state['last_bots'] = [bot1_response['model_name'], bot2_response['model_name']]
74
+ return state, chatbot, ""
75
+
76
+ def update_ratings(state, winner_index):
77
  elo_ratings = get_user_elo_ratings()
78
  bot_names = list(chatbots.keys())
79
+ winner = state['last_bots'][winner_index]
80
+ loser = state['last_bots'][1 - winner_index]
81
 
82
  elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
83
  write_elo_ratings(elo_ratings)
84
+ return f"Updated ELO ratings:\n{winner}: {elo_ratings[winner]}\n{loser}: {elo_ratings[loser]}"
85
+
86
+ def vote_up_model(state, chatbot):
87
+ update_message = update_ratings(state, 0)
88
+ chatbot.append(update_message)
89
+ return chatbot
90
+
91
+ def vote_down_model(state, chatbot):
92
+ update_message = update_ratings(state, 1)
93
+ chatbot.append(update_message)
94
+ return chatbot
95
+
96
+ with gr.Blocks() as demo:
97
+ state = gr.State({})
98
+
99
+ with gr.Row():
100
+ with gr.Column(scale=0.5):
101
+ chatbot = gr.Chatbot(label='ChatBox')
102
+ with gr.Row():
103
+ textbox = gr.Textbox(placeholder="Enter text and press ENTER")
104
+ submit_btn = gr.Button(value="Submit")
105
+ with gr.Column():
106
+ upvote_btn = gr.Button(value="πŸ‘ Upvote Bot 1")
107
+ downvote_btn = gr.Button(value="πŸ‘Ž Upvote Bot 2")
108
+ clear_btn = gr.Button(value="πŸ—‘οΈ Clear Chat")
109
+
110
+ textbox.submit(user_ask, [state, chatbot, textbox], [state, chatbot, textbox])
111
+ submit_btn.click(user_ask, [state, chatbot, textbox], [state, chatbot, textbox])
112
+ upvote_btn.click(vote_up_model, [state, chatbot], [chatbot])
113
+ downvote_btn.click(vote_down_model, [state, chatbot], [chatbot])
114
+ clear_btn.click(lambda _: chatbot.clear(), inputs=[], outputs=[chatbot])
115
+
116
+ demo.launch(share=True, enable_queue=True, server_name='0.0.0.0', server_port=7860)