Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
elo_ratings = get_user_elo_ratings()
|
68 |
bot_names = list(chatbots.keys())
|
69 |
-
winner =
|
70 |
-
loser =
|
71 |
|
72 |
elo_ratings = update_elo_ratings(elo_ratings, winner, loser)
|
73 |
write_elo_ratings(elo_ratings)
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|