helliun commited on
Commit
2b1a636
·
verified ·
1 Parent(s): 6828f15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +300 -121
app.py CHANGED
@@ -1,145 +1,324 @@
1
  import gradio as gr
2
  import pandas as pd
 
 
3
 
4
- def update_scores(winner_score, loser_score, k_factor=100):
5
- score_difference = k_factor / (winner_score / loser_score)
6
- return winner_score + score_difference, loser_score - score_difference
 
 
7
 
8
- def prepare_dataframe(opponents_df, criteria_df, additional_columns=None):
9
- columns = ["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]
10
- if additional_columns:
11
- columns += additional_columns
12
- return opponents_df[columns] if not opponents_df.empty else pd.DataFrame(columns=columns)
 
 
 
13
 
14
- def clean_string(s):
15
- return " ".join(word.capitalize() for word in s.strip().replace(" ", " ").lower().split())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def display_dataframe(df, sort_column, file_name):
18
- df = df.sort_values(by=sort_column, ascending=False)
19
- df.to_csv(file_name, index=False)
20
- return df
21
 
22
- def update_criteria_ratings(first, second, criteria_df, is_positive):
23
- winner, loser = (first, second) if is_positive else (second, first)
24
- winner_score, loser_score = update_scores(
25
- criteria_df.at[winner, 'score'], criteria_df.at[loser, 'score']
26
- )
27
- criteria_df.at[winner, 'score'], criteria_df.at[loser, 'score'] = winner_score, loser_score
28
- return criteria_df
 
 
 
 
 
 
 
 
 
29
 
30
- def update_opponent_ratings(first, second, criterion, opponents_df, criteria_df, is_positive):
31
- winner, loser = (first, second) if is_positive else (second, first)
32
- winner_score, loser_score = update_scores(
33
- opponents_df.at[winner, f"{criterion}_score"], opponents_df.at[loser, f"{criterion}_score"]
34
- )
35
- opponents_df.at[winner, f"{criterion}_score"], opponents_df.at[loser, f"{criterion}_score"] = winner_score, loser_score
36
- return calculate_overall_scores(opponents_df, criteria_df)
 
 
 
 
 
 
 
 
 
 
37
 
38
- def calculate_overall_scores(opponents_df, criteria_df):
39
- criteria_scores = criteria_df.set_index("criteria")["score"]
40
- criteria_list = criteria_df["criteria"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- def compute_score(row):
43
- total_score = sum(row[f"{c}_score"] * criteria_scores[c] for c in criteria_list)
44
- total_weight = sum(criteria_scores[c] for c in criteria_list)
45
- return total_score / total_weight if total_weight else 0
46
 
47
- opponents_df["overall_score"] = opponents_df.apply(compute_score, axis=1)
 
 
 
 
 
 
 
 
 
48
  return opponents_df
49
 
50
- def handle_vote(first, second, criteria, opponents_df, criteria_df, is_symbolic_func, data_file):
51
- data_frame = is_symbolic_func(first, second, criteria, opponents_df, criteria_df)
52
- data_frame = display_dataframe(data_frame, 'overall_score', data_file)
53
- return get_vote_start_data(opponents_df, criteria_df)
54
-
55
- def get_vote_start_data(opponents_df, criteria_df):
56
- if len(opponents_df) > 1 and len(criteria_df) > 0:
57
- sample = opponents_df.sample(n=2)
58
- first_string = sample.iloc[0]["descriptor"] + " - " + sample.iloc[0]["opponent"]
59
- second_string = sample.iloc[1]["descriptor"] + " - " + sample.iloc[1]["opponent"]
60
- criterion = criteria_df.sample(n=1)["criteria"].values[0]
61
- return f"Which better reflects '{criterion}': '{first_string}' or '{second_string}'?", first_string, second_string, criterion
62
- return "Add more options and criteria to start voting!", "", "", ""
63
-
64
- def handle_criteria_vote(first, second, criteria_df, is_positive):
65
- criteria_df = update_criteria_ratings(first, second, criteria_df, is_positive)
66
- criteria_df = display_dataframe(criteria_df, 'score', 'criteria_df.csv')
67
- return get_criteria_vote_start(criteria_df)
68
-
69
- def get_criteria_vote_start(criteria_df):
70
- if len(criteria_df) > 1:
71
- sample = criteria_df.sample(n=2)
72
- first_string, second_string = sample.iloc[0]["criteria"], sample.iloc[1]["criteria"]
73
- return f"Is '{first_string}' more important than '{second_string}'?", first_string, second_string
74
- return "Add more criteria to start ranking!", "", ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  theme = gr.themes.Soft(primary_hue="red", secondary_hue="blue")
77
 
78
  with gr.Blocks(theme=theme) as app:
79
- gr.Markdown("""## Preference-based Elo Ranker""")
80
-
 
 
 
 
 
81
  with gr.Tab("Criteria Ranking"):
82
- gr.Markdown("### Rank Criteria")
83
- criteria_input = gr.Textbox(label="Criteria")
84
- add_criteria_button = gr.Button("Add Criteria")
85
-
86
- # Define the remove_criteria_input TextBox
87
- remove_criteria_input = gr.Textbox(label="Criteria")
88
- remove_criteria_button = gr.Button("Remove Criteria")
89
-
 
 
 
90
  criteria_df = pd.DataFrame(columns=['score', 'criteria'])
91
  criteria_rankings = gr.DataFrame(value=criteria_df, interactive=False, headers=["Score", "Criteria"])
92
- criteria_compare_output = gr.Textbox("Add some criteria to start ranking!", label="Comparison", interactive=False)
93
-
94
- criteria_yes_button = gr.Button("Yes", variant="secondary")
95
- criteria_no_button = gr.Button("No", variant="primary")
96
- criteria_new_vote = gr.Button("New Vote")
97
-
98
- add_criteria_button.click(lambda _: add_criteria(criteria_input, criteria_rankings),
99
- inputs=[criteria_input, criteria_rankings], outputs=[criteria_input, criteria_rankings])
100
- remove_criteria_button.click(lambda _: remove_criteria(clean_string(remove_criteria_input.value), criteria_rankings),
101
- inputs=[remove_criteria_input, criteria_rankings], outputs=criteria_rankings)
102
-
103
- criteria_yes_button.click(lambda first, second: handle_criteria_vote(first, second, criteria_rankings, True),
104
- inputs=[criteria_input, criteria_input], outputs=[criteria_compare_output, criteria_input, criteria_input])
105
- criteria_no_button.click(lambda first, second: handle_criteria_vote(first, second, criteria_rankings, False),
106
- inputs=[criteria_input, criteria_input], outputs=[criteria_compare_output, criteria_input, criteria_input])
107
- criteria_new_vote.click(lambda data_frame: get_criteria_vote_start(data_frame),
108
- inputs=[criteria_rankings], outputs=[criteria_compare_output, criteria_input, criteria_input])
109
 
110
  with gr.Tab("Opponent Ranking"):
111
- opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
112
- rankings = gr.DataFrame(value=opponents_df, interactive=False,
113
- headers=["Descriptor", "Opponent"] + [f"{c} Score" for c in criteria_df["criteria"]] + ["Overall Score"])
114
-
115
- compare_output = gr.Textbox("Add some options to start voting!", label="Comparison", interactive=False)
116
- yes_button = gr.Button("1", variant="secondary")
117
- no_button = gr.Button("2", variant="primary")
118
- criteria_output = gr.Textbox(label="Criteria", interactive=False)
119
-
120
- new_vote = gr.Button("New Vote")
121
- descriptor_input = gr.Textbox(label="Descriptor")
122
- opponent_input = gr.Textbox(label="Opponent")
123
- add_button = gr.Button("Add Opponent")
124
-
125
- add_button.click(lambda: add_and_compare(clean_string(descriptor_input.value), clean_string(opponent_input.value), rankings, criteria_rankings),
126
- inputs=[descriptor_input, opponent_input, rankings, criteria_rankings], outputs=[descriptor_input, opponent_input, rankings])
127
-
128
- remove_descriptor_input = gr.Textbox(label="Descriptor")
129
- remove_opponent_input = gr.Textbox(label="Opponent")
130
- remove_button = gr.Button("Remove Opponent")
131
-
132
- remove_button.click(lambda _, __: remove_opponent(remove_descriptor_input, remove_opponent_input, rankings),
133
- inputs=[remove_descriptor_input, remove_opponent_input, rankings], outputs=rankings)
134
-
135
- yes_button.click(lambda first, second, crit, opp_df, crit_df: handle_vote(first, second, crit, opp_df, crit_df, update_opponent_ratings, True, "opponents_df.csv"),
136
- inputs=[compare_output, compare_output, criteria_output, rankings, criteria_rankings],
137
- outputs=[compare_output, compare_output, compare_output, criteria_output, rankings])
138
- no_button.click(lambda first, second, crit, opp_df, crit_df: handle_vote(first, second, crit, opp_df, crit_df, update_opponent_ratings, False, "opponents_df.csv"),
139
- inputs=[compare_output, compare_output, criteria_output, rankings, criteria_rankings],
140
- outputs=[compare_output, compare_output, compare_output, criteria_output, rankings])
141
-
142
- new_vote.click(lambda opp_df, crit_df: get_vote_start_data(opp_df, crit_df),
143
- inputs=[rankings, criteria_rankings], outputs=[compare_output, compare_output, compare_output, criteria_output, rankings])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  app.launch(share=False)
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import re
4
+ import csv
5
 
6
+ def update_scores(winner, loser, k_factor=100):
7
+ score_difference = int(k_factor / (winner / loser))
8
+ winner += score_difference
9
+ loser -= score_difference
10
+ return winner, loser
11
 
12
+ def vote_startup_criteria(criteria_df):
13
+ if len(criteria_df) > 1:
14
+ sample = criteria_df.sample(n=2)
15
+ first_string = sample.iloc[0]["criteria"]
16
+ second_string = sample.iloc[1]["criteria"]
17
+ return f"Is '{first_string}' more important than '{second_string}'?", first_string, second_string, display_criteria_rankings(criteria_df)
18
+ else:
19
+ return "Add more criteria to start ranking!", "", "", display_criteria_rankings(criteria_df)
20
 
21
+ def vote_startup_opponents(opponents_df, criteria_df):
22
+ try:
23
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
24
+ except:
25
+ opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
26
+ if len(opponents_df) > 0:
27
+ if len(opponents_df) > 10:
28
+ slice_size = 4
29
+ slice = int(len(opponents_df) / slice_size)
30
+ sample = opponents_df[slice:(slice_size - 1) * slice].sample(frac=1).iloc[0]
31
+ opponent, descriptor = sample["opponent"], sample["descriptor"]
32
+ else:
33
+ sample = opponents_df.sample(frac=1).iloc[0]
34
+ opponent, descriptor = sample["opponent"], sample["descriptor"]
35
+ if len(opponents_df) > 1:
36
+ sample = opponents_df.sample(frac=1)
37
+ comparison_opponent = sample.iloc[0]
38
+ if comparison_opponent['opponent'] == opponent and comparison_opponent['descriptor'] == descriptor:
39
+ comparison_opponent = sample.iloc[1]
40
+ first_df = opponents_df[opponents_df["opponent"] == opponent][opponents_df["descriptor"] == descriptor]
41
+ first_string = first_df["opponent"].tolist()[0] + " - " + first_df["descriptor"].tolist()[0]
42
+ second_df = comparison_opponent
43
+ second_string = second_df["opponent"] + " - " + second_df["descriptor"]
44
+ criteria = criteria_df.sample(n=1)["criteria"].values[0]
45
+ return f"Which opponent better represents '{criteria}': '{descriptor} - {opponent}' or '{comparison_opponent['descriptor']} - {comparison_opponent['opponent']}'?", first_string, second_string, criteria, display_rankings(opponents_df, criteria_df)
46
+ else:
47
+ return "Add some opponents to start voting!", "", "", "", display_rankings(opponents_df, criteria_df)
48
 
49
+ def clean_string(string):
50
+ string = string.strip().replace(" ", " ").lower()
51
+ string = " ".join([x[0].upper() + x[1:] for x in string.split()])
52
+ return string
53
 
54
+ def add_and_compare(descriptor, opponent, opponents_df, criteria_df):
55
+ try:
56
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
57
+ except:
58
+ opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
59
+ if descriptor != "" and opponent != "":
60
+ descriptor = clean_string(descriptor)
61
+ opponent = clean_string(opponent)
62
+ new_opponent = pd.DataFrame({'descriptor': [descriptor], 'opponent': [opponent]})
63
+ for c in criteria_df["criteria"]:
64
+ new_opponent[f"{c}_score"] = 1000
65
+ new_opponent["overall_score"] = 1000
66
+ opponents_df = pd.concat([opponents_df, new_opponent], ignore_index=True)
67
+ opponents_df.to_csv("opponents_df.csv")
68
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
69
+ return "", "", display_rankings(opponents_df, criteria_df)
70
 
71
+ def update_ratings_pos(first_string, second_string, criteria, opponents_df, criteria_df):
72
+ try:
73
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
74
+ except:
75
+ opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
76
+ if len(opponents_df) == 0:
77
+ return "Add some opponents to start voting!", "", "", "", display_rankings(opponents_df, criteria_df)
78
+ if first_string != "":
79
+ opponents_df["combined"] = opponents_df["opponent"] + " - " + opponents_df["descriptor"]
80
+ loser = opponents_df[opponents_df["combined"] == second_string]
81
+ winner = opponents_df[opponents_df["combined"] == first_string]
82
+ winner_score, loser_score = update_scores(winner[f"{criteria}_score"].values[0], loser[f"{criteria}_score"].values[0])
83
+ opponents_df.at[winner.index[0], f"{criteria}_score"] = winner_score
84
+ opponents_df.at[loser.index[0], f"{criteria}_score"] = loser_score
85
+ opponents_df = calculate_overall_scores(opponents_df, criteria_df)
86
+ opponents_df.to_csv("opponents_df.csv")
87
+ return vote_startup_opponents(opponents_df, criteria_df)
88
 
89
+ def update_ratings_neg(first_string, second_string, criteria, opponents_df, criteria_df):
90
+ try:
91
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
92
+ except:
93
+ opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
94
+ if len(opponents_df) == 0:
95
+ return "Add some opponents to start voting!", "", "", "", display_rankings(opponents_df, criteria_df)
96
+ if first_string != "":
97
+ opponents_df["combined"] = opponents_df["opponent"] + " - " + opponents_df["descriptor"]
98
+ loser = opponents_df[opponents_df["combined"] == first_string]
99
+ winner = opponents_df[opponents_df["combined"] == second_string]
100
+ winner_score, loser_score = update_scores(winner[f"{criteria}_score"].values[0], loser[f"{criteria}_score"].values[0])
101
+ opponents_df.at[winner.index[0], f"{criteria}_score"] = winner_score
102
+ opponents_df.at[loser.index[0], f"{criteria}_score"] = loser_score
103
+ opponents_df = calculate_overall_scores(opponents_df, criteria_df)
104
+ opponents_df.to_csv("opponents_df.csv")
105
+ return vote_startup_opponents(opponents_df, criteria_df)
106
+
107
+ def display_rankings(opponents_df, criteria_df):
108
+ opponents_df = opponents_df.sort_values(by='overall_score', ascending=False)
109
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
110
+ opponents_df.to_csv("opponents_df.csv")
111
+ return opponents_df
112
 
113
+ def export_csv(opponents_df):
114
+ save_df = opponents_df
115
+ save_df.to_csv("opponents_df.csv")
116
+ return "opponents_df.csv"
117
 
118
+ def import_csv(file, opponents_df, criteria_df):
119
+ if file is not None:
120
+ new_df = pd.read_csv(file)
121
+ try:
122
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
123
+ except:
124
+ opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
125
+ new_df = new_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
126
+ opponents_df = pd.concat([opponents_df, new_df])
127
+ opponents_df = opponents_df.drop_duplicates(subset=['descriptor', 'opponent'])
128
  return opponents_df
129
 
130
+ def remove_opponent(descriptor, opponent, opponents_df):
131
+ descriptor = clean_string(descriptor)
132
+ opponent = clean_string(opponent)
133
+ opponents_df = opponents_df[~((opponents_df["descriptor"] == descriptor) & (opponents_df["opponent"] == opponent))]
134
+ return opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
135
+
136
+ def reset_rankings(opponents_df, criteria_df):
137
+ for c in criteria_df["criteria"]:
138
+ opponents_df[f"{c}_score"] = 1000
139
+ opponents_df["overall_score"] = 1000
140
+ opponents_df = opponents_df[["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]]
141
+ return display_rankings(opponents_df, criteria_df)
142
+
143
+ def clear_rankings(opponents_df, criteria_df):
144
+ opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
145
+ return display_rankings(opponents_df, criteria_df)
146
+
147
+ def add_criteria(criteria, criteria_df):
148
+ if criteria != "":
149
+ criteria = clean_string(criteria)
150
+ new_criteria = pd.DataFrame({'criteria': [criteria], 'score': [1000]})
151
+ criteria_df = pd.concat([criteria_df, new_criteria], ignore_index=True)
152
+ criteria_df.to_csv("criteria_df.csv")
153
+ criteria_df = criteria_df[["score", "criteria"]]
154
+ criteria_df = criteria_df.dropna()
155
+ return "", display_criteria_rankings(criteria_df)
156
+
157
+ def remove_criteria(criteria, criteria_df):
158
+ criteria = clean_string(criteria)
159
+ criteria_df = criteria_df[criteria_df["criteria"] != criteria]
160
+ return display_criteria_rankings(criteria_df)
161
+
162
+ def update_criteria_ratings_pos(first_string, second_string, criteria_df):
163
+ if len(criteria_df) == 0:
164
+ return "Add some criteria to start ranking!", "", "", display_criteria_rankings(criteria_df)
165
+ if first_string != "":
166
+ loser = criteria_df[criteria_df["criteria"] == second_string]
167
+ winner = criteria_df[criteria_df["criteria"] == first_string]
168
+ winner_score, loser_score = update_scores(winner['score'].values[0], loser['score'].values[0])
169
+ criteria_df.at[winner.index[0], 'score'] = winner_score
170
+ criteria_df.at[loser.index[0], 'score'] = loser_score
171
+ criteria_df = criteria_df.sort_values(by='score', ascending=False)
172
+ criteria_df.to_csv("criteria_df.csv")
173
+ return vote_startup_criteria(criteria_df)
174
+
175
+ def update_criteria_ratings_neg(first_string, second_string, criteria_df):
176
+ if len(criteria_df) == 0:
177
+ return "Add some criteria to start ranking!", "", "", display_criteria_rankings(criteria_df)
178
+ if first_string != "":
179
+ loser = criteria_df[criteria_df["criteria"] == first_string]
180
+ winner = criteria_df[criteria_df["criteria"] == second_string]
181
+ winner_score, loser_score = update_scores(winner['score'].values[0], loser['score'].values[0])
182
+ criteria_df.at[winner.index[0], 'score'] = winner_score
183
+ criteria_df.at[loser.index[0], 'score'] = loser_score
184
+ criteria_df = criteria_df.sort_values(by='score', ascending=False)
185
+ criteria_df.to_csv("criteria_df.csv")
186
+ return vote_startup_criteria(criteria_df)
187
+
188
+ def display_criteria_rankings(criteria_df):
189
+ criteria_df = criteria_df.sort_values(by='score', ascending=False)
190
+ criteria_df = criteria_df[["score", "criteria"]]
191
+ criteria_df.to_csv("criteria_df.csv")
192
+ return criteria_df
193
+
194
+ def calculate_overall_scores(opponents_df, criteria_df):
195
+ criteria_scores = criteria_df.set_index("criteria")["score"]
196
+ new_scores = []
197
+ for _, row in opponents_df.iterrows():
198
+ overall_score = 0
199
+ total_weight = 0
200
+ for c in criteria_df["criteria"]:
201
+ weight = criteria_scores[c]
202
+ score = row[f"{c}_score"]
203
+ overall_score += weight * score
204
+ total_weight += weight
205
+ # opponents_df.at[row.name, "overall_score"] = overall_score / total_weight
206
+ score = overall_score / total_weight
207
+ new_scores.append(score)
208
+ opponents_df["overall_score"] = new_scores
209
+ return opponents_df
210
 
211
  theme = gr.themes.Soft(primary_hue="red", secondary_hue="blue")
212
 
213
  with gr.Blocks(theme=theme) as app:
214
+ gr.Markdown(
215
+ """## Preference-based Elo Ranker
216
+ This tool helps you create **accurate rankings** of things based on your personal preferences.
217
+ It does this by asking you questions comparing a random pair of your inputs, and then using your
218
+ answers to calculate Elo scores for ranking.
219
+ """
220
+ )
221
  with gr.Tab("Criteria Ranking"):
222
+ gr.Markdown(
223
+ """### Rank Criteria
224
+ Add and rank the criteria that will be used to evaluate the opponents.
225
+ """
226
+ )
227
+ with gr.Row():
228
+ criteria_input = gr.Textbox(label="Criteria")
229
+ add_criteria_button = gr.Button("Add Criteria")
230
+ with gr.Row():
231
+ remove_criteria_input = gr.Textbox(label="Criteria")
232
+ remove_criteria_button = gr.Button("Remove Criteria")
233
  criteria_df = pd.DataFrame(columns=['score', 'criteria'])
234
  criteria_rankings = gr.DataFrame(value=criteria_df, interactive=False, headers=["Score", "Criteria"])
235
+ with gr.Row():
236
+ criteria_compare_output = gr.Textbox("Add some criteria to start ranking!", label="Comparison", interactive=False)
237
+ with gr.Row():
238
+ criteria_yes_button = gr.Button("Yes", variant="secondary")
239
+ criteria_no_button = gr.Button("No", variant="primary")
240
+ with gr.Row():
241
+ with gr.Column():
242
+ criteria_compare_index_1 = gr.Textbox(label="", interactive=False, visible=False)
243
+ with gr.Column():
244
+ criteria_compare_index_2 = gr.Textbox(label="", interactive=False, visible=False)
245
+ criteria_new_vote = gr.Button("New Vote")
246
+ add_criteria_button.click(add_criteria, inputs=[criteria_input, criteria_rankings], outputs=[criteria_input, criteria_rankings])
247
+ remove_criteria_button.click(remove_criteria, inputs=[remove_criteria_input, criteria_rankings], outputs=criteria_rankings)
248
+ criteria_yes_button.click(update_criteria_ratings_pos, inputs=[criteria_compare_index_1, criteria_compare_index_2, criteria_rankings], outputs=[criteria_compare_output, criteria_compare_index_1, criteria_compare_index_2, criteria_rankings])
249
+ criteria_no_button.click(update_criteria_ratings_neg, inputs=[criteria_compare_index_1, criteria_compare_index_2, criteria_rankings], outputs=[criteria_compare_output, criteria_compare_index_1, criteria_compare_index_2, criteria_rankings])
250
+ criteria_new_vote.click(vote_startup_criteria, inputs=[criteria_rankings], outputs=[criteria_compare_output, criteria_compare_index_1, criteria_compare_index_2, criteria_rankings])
 
251
 
252
  with gr.Tab("Opponent Ranking"):
253
+ with gr.Row():
254
+ previews_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
255
+ previews = gr.DataFrame(value=previews_df, interactive=False, visible=False)
256
+ with gr.Column():
257
+ gr.Markdown(
258
+ """### Vote to Rank
259
+ """
260
+ )
261
+ with gr.Row():
262
+ compare_output = gr.Textbox("Add some options to start voting!", label="Comparison", interactive=False)
263
+ with gr.Row():
264
+ yes_button = gr.Button("1", variant="secondary")
265
+ no_button = gr.Button("2", variant="primary")
266
+ with gr.Row():
267
+ criteria_output = gr.Textbox(label="Criteria", interactive=False)
268
+ new_vote = gr.Button("New Vote")
269
+ with gr.Row():
270
+ with gr.Column():
271
+ compare_index_1 = gr.Textbox(label="", interactive=False, visible=False)
272
+ with gr.Column():
273
+ compare_index_2 = gr.Textbox(label="", interactive=False, visible=False)
274
+ with gr.Column():
275
+ gr.Markdown(
276
+ """### Rankings
277
+ """
278
+ )
279
+ opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
280
+ rankings = gr.DataFrame(value=opponents_df, interactive=False, headers=["Descriptor", "Opponent"] + [f"{c} Score" for c in criteria_df["criteria"]] + ["Overall Score"])
281
+
282
+ gr.Markdown(
283
+ """### Add Opponents
284
+ """
285
+ )
286
+ with gr.Row():
287
+ descriptor_input = gr.Textbox(label="Descriptor")
288
+ opponent_input = gr.Textbox(label="Opponent")
289
+ add_button = gr.Button("Add Opponent")
290
+ add_button.click(add_and_compare, inputs=[descriptor_input, opponent_input, rankings, criteria_rankings], outputs=[descriptor_input, opponent_input, rankings])
291
+ gr.Markdown(
292
+ """### Remove Opponents
293
+ """
294
+ )
295
+ with gr.Row():
296
+ remove_descriptor_input = gr.Textbox(label="Descriptor")
297
+ remove_opponent_input = gr.Textbox(label="Opponent")
298
+ remove_button = gr.Button("Remove Opponent")
299
+ remove_button.click(remove_opponent, inputs=[remove_descriptor_input, remove_opponent_input, rankings], outputs=rankings)
300
+
301
+ gr.Markdown(
302
+ """### Import and Export Rankings
303
+ """
304
+ )
305
+ with gr.Row():
306
+ import_button = gr.File(label="Import CSV", file_count="single")
307
+ import_button.change(fn=import_csv, inputs=[import_button, rankings, criteria_rankings], outputs=[rankings])
308
+ with gr.Column():
309
+ export_link = gr.File(label="Download CSV", file_count="single")
310
+ export_button = gr.Button("Export as CSV")
311
+ export_button.click(fn=export_csv, inputs=[rankings], outputs=export_link)
312
+
313
+ gr.Markdown("### Reset Data")
314
+ with gr.Row():
315
+ reset_button = gr.Button("Reset Scores")
316
+ reset_button.click(reset_rankings, inputs=[rankings, criteria_rankings], outputs=rankings)
317
+ clear_button = gr.Button("Clear Table", variant="primary")
318
+ clear_button.click(clear_rankings, inputs=[rankings, criteria_rankings], outputs=rankings)
319
+
320
+ yes_button.click(update_ratings_pos, inputs=[compare_index_1, compare_index_2, criteria_output, rankings, criteria_rankings], outputs=[compare_output, compare_index_1, compare_index_2, criteria_output, rankings])
321
+ no_button.click(update_ratings_neg, inputs=[compare_index_1, compare_index_2, criteria_output, rankings, criteria_rankings], outputs=[compare_output, compare_index_1, compare_index_2, criteria_output, rankings])
322
+ new_vote.click(vote_startup_opponents, inputs=[rankings, criteria_rankings], outputs=[compare_output, compare_index_1, compare_index_2, criteria_output, rankings])
323
 
324
  app.launch(share=False)