Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,29 +6,27 @@ import random
|
|
6 |
from elo import update_elo_ratings # Custom function for ELO ratings
|
7 |
enable_btn = gr.Button.update(interactive=True)
|
8 |
|
9 |
-
# Load
|
10 |
with open('chatbot_urls.json', 'r') as file:
|
11 |
chatbots = json.load(file)
|
12 |
|
13 |
-
# Thread-local storage for user-specific data
|
14 |
-
|
15 |
# Initialize or get user-specific ELO ratings
|
16 |
def get_user_elo_ratings(state):
|
17 |
-
return state
|
18 |
|
19 |
-
# Read ELO ratings
|
20 |
def read_elo_ratings():
|
21 |
try:
|
22 |
with open('elo_ratings.json', 'r') as file:
|
23 |
-
|
24 |
except FileNotFoundError:
|
25 |
-
|
26 |
-
|
27 |
-
# Write ELO ratings to file (thread-safe)
|
28 |
def write_elo_ratings(elo_ratings):
|
29 |
with open('elo_ratings.json', 'w') as file:
|
30 |
json.dump(elo_ratings, file, indent=4)
|
31 |
|
|
|
32 |
def get_bot_response(url, prompt):
|
33 |
payload = {
|
34 |
"input": {
|
@@ -45,7 +43,7 @@ def get_bot_response(url, prompt):
|
|
45 |
"authorization": os.environ.get("RUNPOD_TOKEN")
|
46 |
}
|
47 |
response = requests.post(url, json=payload, headers=headers)
|
48 |
-
return response.json()['output'][0]['generated_text'].replace(prompt,"")
|
49 |
|
50 |
def chat_with_bots(user_input, state):
|
51 |
bot_names = list(chatbots.keys())
|
@@ -79,47 +77,53 @@ def vote_down_model(state, chatbot):
|
|
79 |
update_message = update_ratings(state, 1)
|
80 |
chatbot.append(update_message)
|
81 |
return chatbot
|
82 |
-
def user_ask(state, chatbot1, chatbot2, textbox
|
83 |
-
global enable_btn
|
84 |
-
state.update({"elo_ratings":read_elo_ratings()})
|
85 |
user_input = textbox
|
86 |
-
|
|
|
87 |
|
88 |
-
|
|
|
89 |
|
90 |
-
|
|
|
91 |
|
92 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
|
95 |
-
state
|
96 |
-
return state, chatbot1, chatbot2,textbox, enable_btn,enable_btn
|
97 |
|
98 |
-
#
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
with gr.Blocks() as demo:
|
102 |
state = gr.State({})
|
|
|
103 |
with gr.Row():
|
104 |
-
# First column for Model A
|
105 |
with gr.Column():
|
106 |
chatbot1 = gr.Chatbot(label='Model A').style(height=600)
|
107 |
-
upvote_btn_a = gr.Button(value="π Upvote A"
|
108 |
|
109 |
-
# Second column for Model B
|
110 |
with gr.Column():
|
111 |
chatbot2 = gr.Chatbot(label='Model B').style(height=600)
|
112 |
-
upvote_btn_b = gr.Button(value="π Upvote B"
|
113 |
|
114 |
-
|
115 |
-
textbox = gr.Textbox(placeholder="Enter your prompt and press ENTER")
|
116 |
submit_btn = gr.Button(value="Send")
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
submit_btn.click(user_ask, inputs=[state, chatbot1, chatbot2, textbox], outputs=[state, chatbot1, chatbot2,textbox,upvote_btn_a,upvote_btn_b])
|
121 |
upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1], outputs=[chatbot1])
|
122 |
-
upvote_btn_b.click(
|
123 |
|
124 |
-
# Start the interface
|
125 |
demo.launch()
|
|
|
6 |
from elo import update_elo_ratings # Custom function for ELO ratings
|
7 |
enable_btn = gr.Button.update(interactive=True)
|
8 |
|
9 |
+
# Load chatbot URLs and model names from a JSON file
|
10 |
with open('chatbot_urls.json', 'r') as file:
|
11 |
chatbots = json.load(file)
|
12 |
|
|
|
|
|
13 |
# Initialize or get user-specific ELO ratings
|
14 |
def get_user_elo_ratings(state):
|
15 |
+
return state.get('elo_ratings', {model: 1200 for model in chatbots.keys()})
|
16 |
|
17 |
+
# Read and write ELO ratings to file (thread-safe)
|
18 |
def read_elo_ratings():
|
19 |
try:
|
20 |
with open('elo_ratings.json', 'r') as file:
|
21 |
+
return json.load(file)
|
22 |
except FileNotFoundError:
|
23 |
+
return {model: 1200 for model in chatbots.keys()}
|
24 |
+
|
|
|
25 |
def write_elo_ratings(elo_ratings):
|
26 |
with open('elo_ratings.json', 'w') as file:
|
27 |
json.dump(elo_ratings, file, indent=4)
|
28 |
|
29 |
+
# Function to get bot response
|
30 |
def get_bot_response(url, prompt):
|
31 |
payload = {
|
32 |
"input": {
|
|
|
43 |
"authorization": os.environ.get("RUNPOD_TOKEN")
|
44 |
}
|
45 |
response = requests.post(url, json=payload, headers=headers)
|
46 |
+
return response.json()['output'][0]['generated_text'].replace(prompt, "")
|
47 |
|
48 |
def chat_with_bots(user_input, state):
|
49 |
bot_names = list(chatbots.keys())
|
|
|
77 |
update_message = update_ratings(state, 1)
|
78 |
chatbot.append(update_message)
|
79 |
return chatbot
|
80 |
+
def user_ask(state, chatbot1, chatbot2, textbox):
|
|
|
|
|
81 |
user_input = textbox
|
82 |
+
if len(user_input) > 200:
|
83 |
+
user_input = user_input[:200] # Limit user input to 200 characters
|
84 |
|
85 |
+
# Updating state with the current ELO ratings
|
86 |
+
state["elo_ratings"] = read_elo_ratings()
|
87 |
|
88 |
+
# Chat with bots
|
89 |
+
bot1_response, bot2_response = chat_with_bots(user_input, state)
|
90 |
|
91 |
+
# Update chat history in state
|
92 |
+
if "history" not in state:
|
93 |
+
state["history"] = []
|
94 |
+
state["history"].extend([
|
95 |
+
{"role": "user", "content": user_input},
|
96 |
+
{"role": "bot1", "content": bot1_response},
|
97 |
+
{"role": "bot2", "content": bot2_response}
|
98 |
+
])
|
99 |
|
100 |
+
# Keep only the last 10 messages in history
|
101 |
+
state["history"] = state["history"][-10:]
|
|
|
102 |
|
103 |
+
# Format the conversation in ChatML format
|
104 |
+
chatml_prompt = format_chatml_prompt(state)
|
105 |
|
106 |
+
return state, chatbot1, chatbot2, textbox
|
107 |
|
108 |
+
# Gradio interface setup
|
109 |
with gr.Blocks() as demo:
|
110 |
state = gr.State({})
|
111 |
+
|
112 |
with gr.Row():
|
|
|
113 |
with gr.Column():
|
114 |
chatbot1 = gr.Chatbot(label='Model A').style(height=600)
|
115 |
+
upvote_btn_a = gr.Button(value="π Upvote A")
|
116 |
|
|
|
117 |
with gr.Column():
|
118 |
chatbot2 = gr.Chatbot(label='Model B').style(height=600)
|
119 |
+
upvote_btn_b = gr.Button(value="π Upvote B")
|
120 |
|
121 |
+
textbox = gr.Textbox(placeholder="Enter your prompt (up to 200 characters)", max_chars=200)
|
|
|
122 |
submit_btn = gr.Button(value="Send")
|
123 |
|
124 |
+
textbox.submit(user_ask, inputs=[state, chatbot1, chatbot2, textbox], outputs=[state, chatbot1, chatbot2, textbox])
|
125 |
+
submit_btn.click(user_ask, inputs=[state, chatbot1, chatbot2, textbox], outputs=[state, chatbot1, chatbot2, textbox])
|
|
|
126 |
upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1], outputs=[chatbot1])
|
127 |
+
upvote_btn_b.click(vote_down_model, inputs=[state, chatbot2], outputs=[chatbot2])
|
128 |
|
|
|
129 |
demo.launch()
|