Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,180 +11,180 @@ def update_scores(winner, loser, k_factor=100):
|
|
11 |
loser -= score_difference
|
12 |
return winner, loser
|
13 |
|
14 |
-
def vote_startup(
|
15 |
try:
|
16 |
-
|
17 |
except:
|
18 |
-
|
19 |
-
if len(
|
20 |
-
if len(
|
21 |
slice_size = 4
|
22 |
-
slice = int(len(
|
23 |
-
sample =
|
24 |
-
|
25 |
else:
|
26 |
-
sample =
|
27 |
-
|
28 |
-
if len(
|
29 |
-
# Randomly select a
|
30 |
-
sample =
|
31 |
-
|
32 |
-
if
|
33 |
-
|
34 |
-
first_df =
|
35 |
-
first_string = first_df["
|
36 |
-
second_df =
|
37 |
-
second_string = second_df["
|
38 |
-
return f"Do you like '{
|
39 |
else:
|
40 |
-
return "Add some
|
41 |
|
42 |
def clean_string(string):
|
43 |
string = string.strip().replace(" "," ").lower()
|
44 |
string = " ".join([x[0].upper()+x[1:] for x in string.split()])
|
45 |
return string
|
46 |
|
47 |
-
def add_and_compare(
|
48 |
try:
|
49 |
-
|
50 |
except:
|
51 |
-
|
52 |
-
if
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return "", "", display_rankings(
|
60 |
|
61 |
# Function to update Elo ratings based on user's choice
|
62 |
-
def update_ratings_pos(first_string, second_string,
|
63 |
try:
|
64 |
-
|
65 |
except:
|
66 |
-
|
67 |
-
if len(
|
68 |
-
return "Add some
|
69 |
if first_string != "":
|
70 |
-
|
71 |
-
loser =
|
72 |
-
winner =
|
73 |
# Update Elo scores
|
74 |
winner_score, loser_score = update_scores(winner['elo_score'].values[0], loser['elo_score'].values[0])
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
|
79 |
-
|
80 |
|
81 |
-
if len(
|
82 |
slice_size = 4
|
83 |
-
slice = int(len(
|
84 |
-
sample =
|
85 |
-
|
86 |
else:
|
87 |
-
sample =
|
88 |
-
|
89 |
-
if len(
|
90 |
-
# Randomly select a
|
91 |
-
sample =
|
92 |
-
|
93 |
-
if
|
94 |
-
|
95 |
-
first_df =
|
96 |
-
first_string = first_df["
|
97 |
-
second_df =
|
98 |
-
second_string = second_df["
|
99 |
-
return f"Do you like '{
|
100 |
else:
|
101 |
-
return "Add some
|
102 |
|
103 |
# Function to update Elo ratings based on user's choice
|
104 |
-
def update_ratings_neg(first_string, second_string,
|
105 |
try:
|
106 |
-
|
107 |
except:
|
108 |
-
|
109 |
-
if len(
|
110 |
-
return "Add some
|
111 |
if first_string != "":
|
112 |
-
|
113 |
-
loser =
|
114 |
-
winner =
|
115 |
|
116 |
# Update Elo scores
|
117 |
winner_score, loser_score = update_scores(winner['elo_score'].values[0], loser['elo_score'].values[0])
|
118 |
-
|
119 |
-
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
if len(
|
124 |
slice_size = 4
|
125 |
-
slice = int(len(
|
126 |
-
sample =
|
127 |
-
|
128 |
else:
|
129 |
-
sample =
|
130 |
-
|
131 |
-
if len(
|
132 |
-
# Randomly select a
|
133 |
-
sample =
|
134 |
-
|
135 |
-
if
|
136 |
-
|
137 |
-
first_df =
|
138 |
-
first_string = first_df["
|
139 |
-
second_df =
|
140 |
-
second_string = second_df["
|
141 |
-
return f"Do you like '{
|
142 |
else:
|
143 |
-
return "Add some
|
144 |
|
145 |
-
def display_rankings(
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
return
|
150 |
|
151 |
-
def export_csv(
|
152 |
# Function to export DataFrame to CSV
|
153 |
-
save_df =
|
154 |
-
save_df.to_csv("
|
155 |
-
return "
|
156 |
|
157 |
-
def import_csv(file,
|
158 |
if file is not None:
|
159 |
#file_content = file.decode('utf-8')
|
160 |
new_df = pd.read_csv(file)
|
161 |
try:
|
162 |
-
|
163 |
except:
|
164 |
-
|
165 |
-
new_df = new_df[["elo_score", "
|
166 |
-
|
167 |
|
168 |
-
|
169 |
-
return
|
170 |
|
171 |
|
172 |
-
# Function to remove a
|
173 |
-
def
|
174 |
-
# Find and remove the
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
return
|
179 |
|
180 |
-
def reset_rankings(
|
181 |
-
|
182 |
-
|
183 |
-
return display_rankings(
|
184 |
|
185 |
-
def clear_rankings(
|
186 |
-
|
187 |
-
return display_rankings(
|
188 |
|
189 |
|
190 |
# theme='Taithrah/Minimal'
|
@@ -204,7 +204,7 @@ with gr.Blocks(theme=theme) as app:
|
|
204 |
)
|
205 |
|
206 |
with gr.Row():
|
207 |
-
previews_df = pd.DataFrame(columns=['elo_score', '
|
208 |
previews = gr.DataFrame(value=previews_df, interactive=False, visible=False)
|
209 |
with gr.Column():
|
210 |
gr.Markdown(
|
@@ -229,27 +229,27 @@ with gr.Blocks(theme=theme) as app:
|
|
229 |
"""
|
230 |
)
|
231 |
|
232 |
-
|
233 |
-
rankings = gr.DataFrame(value=
|
234 |
|
235 |
gr.Markdown(
|
236 |
"""### Add Opponents
|
237 |
"""
|
238 |
)
|
239 |
with gr.Row():
|
240 |
-
|
241 |
-
|
242 |
add_button = gr.Button("Add Opponent")
|
243 |
-
add_button.click(add_and_compare, inputs=[
|
244 |
gr.Markdown(
|
245 |
"""### Remove Opponents
|
246 |
"""
|
247 |
)
|
248 |
with gr.Row():
|
249 |
-
|
250 |
-
|
251 |
-
remove_button = gr.Button("Remove
|
252 |
-
remove_button.click(
|
253 |
|
254 |
gr.Markdown(
|
255 |
"""### Import and Export Rankings
|
@@ -274,7 +274,7 @@ with gr.Blocks(theme=theme) as app:
|
|
274 |
clear_button = gr.Button("Clear Table", variant="primary")
|
275 |
clear_button.click(clear_rankings, inputs=[rankings], outputs=rankings)
|
276 |
|
277 |
-
# add_button.click(add_and_compare, inputs=[
|
278 |
yes_button.click(update_ratings_pos, inputs=[compare_index_1, compare_index_2, rankings], outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
279 |
no_button.click(update_ratings_neg, inputs=[compare_index_1, compare_index_2, rankings], outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
280 |
new_vote.click(vote_startup, inputs=[rankings],outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
|
|
11 |
loser -= score_difference
|
12 |
return winner, loser
|
13 |
|
14 |
+
def vote_startup(opponents_df):
|
15 |
try:
|
16 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
17 |
except:
|
18 |
+
opponents_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
19 |
+
if len(opponents_df)>0:
|
20 |
+
if len(opponents_df)>10:
|
21 |
slice_size = 4
|
22 |
+
slice = int(len(opponents_df)/slice_size)
|
23 |
+
sample = opponents_df[slice:(slice_size-1)*slice].sample(frac=1).iloc[0]
|
24 |
+
opponent, descriptor = sample["opponent"], sample["descriptor"]
|
25 |
else:
|
26 |
+
sample = opponents_df.sample(frac=1).iloc[0]
|
27 |
+
opponent, descriptor = sample["opponent"], sample["descriptor"]
|
28 |
+
if len(opponents_df) > 1:
|
29 |
+
# Randomly select a opponent to compare with
|
30 |
+
sample = opponents_df.sample(frac=1)
|
31 |
+
comparison_opponent = sample.iloc[0]
|
32 |
+
if comparison_opponent['opponent'] == opponent and comparison_opponent['descriptor'] == descriptor:
|
33 |
+
comparison_opponent = sample.iloc[1]
|
34 |
+
first_df = opponents_df[opponents_df["opponent"]==opponent][opponents_df["descriptor"]==descriptor]
|
35 |
+
first_string = first_df["opponent"].tolist()[0]+" - "+first_df["descriptor"].tolist()[0]
|
36 |
+
second_df = comparison_opponent
|
37 |
+
second_string = second_df["opponent"]+" - "+second_df["descriptor"]
|
38 |
+
return f"Do you like '{descriptor} - {opponent}' better than '{comparison_opponent['descriptor']} - {comparison_opponent['opponent']}'?", first_string, second_string, display_rankings(opponents_df)
|
39 |
else:
|
40 |
+
return "Add some opponents to start voting!", "", "", display_rankings(opponents_df)
|
41 |
|
42 |
def clean_string(string):
|
43 |
string = string.strip().replace(" "," ").lower()
|
44 |
string = " ".join([x[0].upper()+x[1:] for x in string.split()])
|
45 |
return string
|
46 |
|
47 |
+
def add_and_compare(descriptor, opponent, opponents_df):
|
48 |
try:
|
49 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
50 |
except:
|
51 |
+
opponents_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
52 |
+
if descriptor != "" and opponent != "":
|
53 |
+
descriptor = clean_string(descriptor)
|
54 |
+
opponent = clean_string(opponent)
|
55 |
+
new_opponent = pd.DataFrame({'descriptor': [descriptor], 'opponent': [opponent], 'elo_score': [1000]})
|
56 |
+
opponents_df = pd.concat([opponents_df, new_opponent], ignore_index=True)
|
57 |
+
opponents_df.to_csv("opponents_df.csv")
|
58 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
59 |
+
return "", "", display_rankings(opponents_df)
|
60 |
|
61 |
# Function to update Elo ratings based on user's choice
|
62 |
+
def update_ratings_pos(first_string, second_string, opponents_df):
|
63 |
try:
|
64 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
65 |
except:
|
66 |
+
opponents_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
67 |
+
if len(opponents_df)==0:
|
68 |
+
return "Add some opponents to start voting!", "", "", display_rankings(opponents_df)
|
69 |
if first_string != "":
|
70 |
+
opponents_df["combined"] = opponents_df["opponent"] + " - " + opponents_df["descriptor"]
|
71 |
+
loser = opponents_df[opponents_df["combined"] == second_string]
|
72 |
+
winner = opponents_df[opponents_df["combined"] == first_string]
|
73 |
# Update Elo scores
|
74 |
winner_score, loser_score = update_scores(winner['elo_score'].values[0], loser['elo_score'].values[0])
|
75 |
+
opponents_df.at[winner.index[0], 'elo_score'] = winner_score
|
76 |
+
opponents_df.at[loser.index[0], 'elo_score'] = loser_score
|
77 |
|
78 |
+
opponents_df = opponents_df.sort_values(by='elo_score', ascending=False)
|
79 |
+
opponents_df.to_csv("opponents_df.csv")
|
80 |
|
81 |
+
if len(opponents_df)>10:
|
82 |
slice_size = 4
|
83 |
+
slice = int(len(opponents_df)/slice_size)
|
84 |
+
sample = opponents_df[slice:(slice_size-1)*slice].sample(frac=1).iloc[0]
|
85 |
+
opponent, descriptor = sample["opponent"], sample["descriptor"]
|
86 |
else:
|
87 |
+
sample = opponents_df.sample(frac=1).iloc[0]
|
88 |
+
opponent, descriptor = sample["opponent"], sample["descriptor"]
|
89 |
+
if len(opponents_df) > 1:
|
90 |
+
# Randomly select a opponent to compare with
|
91 |
+
sample = opponents_df.sample(frac=1)
|
92 |
+
comparison_opponent = sample.iloc[0]
|
93 |
+
if comparison_opponent['opponent'] == opponent and comparison_opponent['descriptor'] == descriptor:
|
94 |
+
comparison_opponent = sample.iloc[1]
|
95 |
+
first_df = opponents_df[opponents_df["opponent"]==opponent][opponents_df["descriptor"]==descriptor]
|
96 |
+
first_string = first_df["opponent"].tolist()[0]+" - "+first_df["descriptor"].tolist()[0]
|
97 |
+
second_df = comparison_opponent
|
98 |
+
second_string = second_df["opponent"]+" - "+second_df["descriptor"]
|
99 |
+
return f"Do you like '{descriptor} - {opponent}' better than '{comparison_opponent['descriptor']} - {comparison_opponent['opponent']}'?", first_string, second_string, display_rankings(opponents_df)
|
100 |
else:
|
101 |
+
return "Add some opponents to start voting!", "", "", display_rankings(opponents_df)
|
102 |
|
103 |
# Function to update Elo ratings based on user's choice
|
104 |
+
def update_ratings_neg(first_string, second_string, opponents_df):
|
105 |
try:
|
106 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
107 |
except:
|
108 |
+
opponents_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
109 |
+
if len(opponents_df)==0:
|
110 |
+
return "Add some opponents to start voting!", "", "", display_rankings(opponents_df)
|
111 |
if first_string != "":
|
112 |
+
opponents_df["combined"] = opponents_df["opponent"] + " - " + opponents_df["descriptor"]
|
113 |
+
loser = opponents_df[opponents_df["combined"] == first_string]
|
114 |
+
winner = opponents_df[opponents_df["combined"] == second_string]
|
115 |
|
116 |
# Update Elo scores
|
117 |
winner_score, loser_score = update_scores(winner['elo_score'].values[0], loser['elo_score'].values[0])
|
118 |
+
opponents_df.at[winner.index[0], 'elo_score'] = winner_score
|
119 |
+
opponents_df.at[loser.index[0], 'elo_score'] = loser_score
|
120 |
|
121 |
+
opponents_df = opponents_df.sort_values(by='elo_score', ascending=False)
|
122 |
+
opponents_df.to_csv("opponents_df.csv")
|
123 |
+
if len(opponents_df)>10:
|
124 |
slice_size = 4
|
125 |
+
slice = int(len(opponents_df)/slice_size)
|
126 |
+
sample = opponents_df[slice:(slice_size-1)*slice].sample(frac=1).iloc[0]
|
127 |
+
opponent, descriptor = sample["opponent"], sample["descriptor"]
|
128 |
else:
|
129 |
+
sample = opponents_df.sample(frac=1).iloc[0]
|
130 |
+
opponent, descriptor = sample["opponent"], sample["descriptor"]
|
131 |
+
if len(opponents_df) > 1:
|
132 |
+
# Randomly select a opponent to compare with
|
133 |
+
sample = opponents_df.sample(frac=1)
|
134 |
+
comparison_opponent = sample.iloc[0]
|
135 |
+
if comparison_opponent['opponent'] == opponent and comparison_opponent['descriptor'] == descriptor:
|
136 |
+
comparison_opponent = sample.iloc[1]
|
137 |
+
first_df = opponents_df[opponents_df["opponent"]==opponent][opponents_df["descriptor"]==descriptor]
|
138 |
+
first_string = first_df["opponent"].tolist()[0]+" - "+first_df["descriptor"].tolist()[0]
|
139 |
+
second_df = comparison_opponent
|
140 |
+
second_string = second_df["opponent"]+" - "+second_df["descriptor"]
|
141 |
+
return f"Do you like '{descriptor} - {opponent}' better than '{comparison_opponent['descriptor']} - {comparison_opponent['opponent']}'?", first_string, second_string, display_rankings(opponents_df)
|
142 |
else:
|
143 |
+
return "Add some opponents to start voting!", "", "", display_rankings(opponents_df)
|
144 |
|
145 |
+
def display_rankings(opponents_df=pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])):
|
146 |
+
opponents_df = opponents_df.sort_values(by='elo_score', ascending=False)
|
147 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
148 |
+
opponents_df.to_csv("opponents_df.csv")
|
149 |
+
return opponents_df
|
150 |
|
151 |
+
def export_csv(opponents_df):
|
152 |
# Function to export DataFrame to CSV
|
153 |
+
save_df = opponents_df
|
154 |
+
save_df.to_csv("opponents_df.csv")
|
155 |
+
return "opponents_df.csv"
|
156 |
|
157 |
+
def import_csv(file, opponents_df):
|
158 |
if file is not None:
|
159 |
#file_content = file.decode('utf-8')
|
160 |
new_df = pd.read_csv(file)
|
161 |
try:
|
162 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
163 |
except:
|
164 |
+
opponents_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
165 |
+
new_df = new_df[["elo_score", "descriptor", "opponent"]]
|
166 |
+
opponents_df = pd.concat([opponents_df,new_df])
|
167 |
|
168 |
+
opponents_df = opponents_df.drop_duplicates(subset=['descriptor', 'opponent'])
|
169 |
+
return opponents_df
|
170 |
|
171 |
|
172 |
+
# Function to remove a opponent
|
173 |
+
def remove_opponent(descriptor, opponent, opponents_df):
|
174 |
+
# Find and remove the opponent from the DataFrame
|
175 |
+
descriptor = clean_string(descriptor)
|
176 |
+
opponent = clean_string(opponent)
|
177 |
+
opponents_df = opponents_df[~((opponents_df["descriptor"] == descriptor) & (opponents_df["opponent"] == opponent))]
|
178 |
+
return opponents_df[["elo_score", "descriptor", "opponent"]]
|
179 |
|
180 |
+
def reset_rankings(opponents_df):
|
181 |
+
opponents_df["elo_score"] = [1000]*len(opponents_df)
|
182 |
+
opponents_df = opponents_df[["elo_score", "descriptor", "opponent"]]
|
183 |
+
return display_rankings(opponents_df)
|
184 |
|
185 |
+
def clear_rankings(opponents_df):
|
186 |
+
opponents_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
187 |
+
return display_rankings(opponents_df)
|
188 |
|
189 |
|
190 |
# theme='Taithrah/Minimal'
|
|
|
204 |
)
|
205 |
|
206 |
with gr.Row():
|
207 |
+
previews_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
208 |
previews = gr.DataFrame(value=previews_df, interactive=False, visible=False)
|
209 |
with gr.Column():
|
210 |
gr.Markdown(
|
|
|
229 |
"""
|
230 |
)
|
231 |
|
232 |
+
opponents_df = pd.DataFrame(columns=['elo_score', 'descriptor', 'opponent'])
|
233 |
+
rankings = gr.DataFrame(value=opponents_df, interactive=False, headers=["Score","descriptor", "opponent"])
|
234 |
|
235 |
gr.Markdown(
|
236 |
"""### Add Opponents
|
237 |
"""
|
238 |
)
|
239 |
with gr.Row():
|
240 |
+
descriptor_input = gr.Textbox(label="Type")
|
241 |
+
opponent_input = gr.Textbox(label="Opponent")
|
242 |
add_button = gr.Button("Add Opponent")
|
243 |
+
add_button.click(add_and_compare, inputs=[descriptor_input, opponent_input, rankings], outputs=[descriptor_input, opponent_input, rankings])
|
244 |
gr.Markdown(
|
245 |
"""### Remove Opponents
|
246 |
"""
|
247 |
)
|
248 |
with gr.Row():
|
249 |
+
remove_descriptor_input = gr.Textbox(label="Type")
|
250 |
+
remove_opponent_input = gr.Textbox(label="Opponent")
|
251 |
+
remove_button = gr.Button("Remove opponent")
|
252 |
+
remove_button.click(remove_opponent, inputs=[remove_descriptor_input, remove_opponent_input, rankings], outputs=rankings)
|
253 |
|
254 |
gr.Markdown(
|
255 |
"""### Import and Export Rankings
|
|
|
274 |
clear_button = gr.Button("Clear Table", variant="primary")
|
275 |
clear_button.click(clear_rankings, inputs=[rankings], outputs=rankings)
|
276 |
|
277 |
+
# add_button.click(add_and_compare, inputs=[descriptor_input, opponent_input, rankings], outputs=[descriptor_input, opponent_input, rankings])
|
278 |
yes_button.click(update_ratings_pos, inputs=[compare_index_1, compare_index_2, rankings], outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
279 |
no_button.click(update_ratings_neg, inputs=[compare_index_1, compare_index_2, rankings], outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|
280 |
new_vote.click(vote_startup, inputs=[rankings],outputs=[compare_output, compare_index_1, compare_index_2, rankings])
|