Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import csv
|
4 |
+
|
5 |
+
def update_scores(winner_score, loser_score, k_factor=100):
|
6 |
+
score_difference = k_factor / (winner_score / loser_score)
|
7 |
+
return winner_score + score_difference, loser_score - score_difference
|
8 |
+
|
9 |
+
def prepare_dataframe(opponents_df, criteria_df, additional_columns=None):
|
10 |
+
columns = ["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"]
|
11 |
+
if additional_columns:
|
12 |
+
columns += additional_columns
|
13 |
+
return opponents_df[columns] if not opponents_df.empty else pd.DataFrame(columns=columns)
|
14 |
+
|
15 |
+
def clean_string(s):
|
16 |
+
return " ".join(word.capitalize() for word in s.strip().replace(" ", " ").lower().split())
|
17 |
+
|
18 |
+
def display_dataframe(df, sort_column, file_name):
|
19 |
+
df = df.sort_values(by=sort_column, ascending=False)
|
20 |
+
df.to_csv(file_name, index=False)
|
21 |
+
return df
|
22 |
+
|
23 |
+
def update_criteria_ratings(first, second, criteria_df, is_positive):
|
24 |
+
winner, loser = (first, second) if is_positive else (second, first)
|
25 |
+
winner_score, loser_score = update_scores(
|
26 |
+
criteria_df.at[winner, 'score'], criteria_df.at[loser, 'score']
|
27 |
+
)
|
28 |
+
criteria_df.at[winner, 'score'], criteria_df.at[loser, 'score'] = winner_score, loser_score
|
29 |
+
return criteria_df
|
30 |
+
|
31 |
+
def update_opponent_ratings(first, second, criterion, opponents_df, criteria_df, is_positive):
|
32 |
+
winner, loser = (first, second) if is_positive else (second, first)
|
33 |
+
winner_score, loser_score = update_scores(
|
34 |
+
opponents_df.at[winner, f"{criterion}_score"], opponents_df.at[loser, f"{criterion}_score"]
|
35 |
+
)
|
36 |
+
opponents_df.at[winner, f"{criterion}_score"], opponents_df.at[loser, f"{criterion}_score"] = winner_score, loser_score
|
37 |
+
return calculate_overall_scores(opponents_df, criteria_df)
|
38 |
+
|
39 |
+
def calculate_overall_scores(opponents_df, criteria_df):
|
40 |
+
criteria_scores = criteria_df.set_index("criteria")["score"]
|
41 |
+
criteria_list = criteria_df["criteria"]
|
42 |
+
|
43 |
+
def compute_score(row):
|
44 |
+
total_score = sum(row[f"{c}_score"] * criteria_scores[c] for c in criteria_list)
|
45 |
+
total_weight = sum(criteria_scores[c] for c in criteria_list)
|
46 |
+
return total_score / total_weight if total_weight else 0
|
47 |
+
|
48 |
+
opponents_df["overall_score"] = opponents_df.apply(compute_score, axis=1)
|
49 |
+
return opponents_df
|
50 |
+
|
51 |
+
def handle_vote(first, second, criteria, opponents_df, criteria_df, is_symbolic_func, data_file):
|
52 |
+
data_frame = is_symbolic_func(first, second, criteria, opponents_df, criteria_df)
|
53 |
+
data_frame = display_dataframe(data_frame, 'overall_score', data_file)
|
54 |
+
return get_vote_start_data(opponents_df, criteria_df)
|
55 |
+
|
56 |
+
def get_vote_start_data(opponents_df, criteria_df):
|
57 |
+
if len(opponents_df) > 1 and len(criteria_df) > 0:
|
58 |
+
sample = opponents_df.sample(n=2)
|
59 |
+
first_string = sample.iloc[0]["descriptor"] + " - " + sample.iloc[0]["opponent"]
|
60 |
+
second_string = sample.iloc[1]["descriptor"] + " - " + sample.iloc[1]["opponent"]
|
61 |
+
criterion = criteria_df.sample(n=1)["criteria"].values[0]
|
62 |
+
return f"Which better reflects '{criterion}': '{first_string}' or '{second_string}'?", first_string, second_string, criterion
|
63 |
+
return "Add more options and criteria to start voting!", "", "", ""
|
64 |
+
|
65 |
+
def handle_criteria_vote(first, second, criteria_df, is_positive):
|
66 |
+
criteria_df = update_criteria_ratings(first, second, criteria_df, is_positive)
|
67 |
+
criteria_df = display_dataframe(criteria_df, 'score', 'criteria_df.csv')
|
68 |
+
return get_criteria_vote_start(criteria_df)
|
69 |
+
|
70 |
+
def get_criteria_vote_start(criteria_df):
|
71 |
+
if len(criteria_df) > 1:
|
72 |
+
sample = criteria_df.sample(n=2)
|
73 |
+
first_string, second_string = sample.iloc[0]["criteria"], sample.iloc[1]["criteria"]
|
74 |
+
return f"Is '{first_string}' more important than '{second_string}'?", first_string, second_string
|
75 |
+
return "Add more criteria to start ranking!", "", ""
|
76 |
+
|
77 |
+
theme = gr.themes.Soft(primary_hue="red", secondary_hue="blue")
|
78 |
+
|
79 |
+
with gr.Blocks(theme=theme) as app:
|
80 |
+
gr.Markdown("""## Preference-based Elo Ranker""")
|
81 |
+
|
82 |
+
with gr.Tab("Criteria Ranking"):
|
83 |
+
gr.Markdown("### Rank Criteria")
|
84 |
+
criteria_input = gr.Textbox(label="Criteria")
|
85 |
+
add_criteria_button = gr.Button("Add Criteria")
|
86 |
+
remove_criteria_button = gr.Button("Remove Criteria")
|
87 |
+
|
88 |
+
criteria_df = pd.DataFrame(columns=['score', 'criteria'])
|
89 |
+
criteria_rankings = gr.DataFrame(value=criteria_df, interactive=False, headers=["Score", "Criteria"])
|
90 |
+
criteria_compare_output = gr.Textbox("Add some criteria to start ranking!", label="Comparison", interactive=False)
|
91 |
+
|
92 |
+
criteria_yes_button = gr.Button("Yes", variant="secondary")
|
93 |
+
criteria_no_button = gr.Button("No", variant="primary")
|
94 |
+
criteria_new_vote = gr.Button("New Vote")
|
95 |
+
|
96 |
+
add_criteria_button.click(lambda _: add_criteria(criteria_input, criteria_rankings),
|
97 |
+
inputs=[criteria_input, criteria_rankings], outputs=[criteria_input, criteria_rankings])
|
98 |
+
remove_criteria_button.click(lambda _: remove_criteria(clean_string(remove_criteria_input.value), criteria_rankings),
|
99 |
+
inputs=[remove_criteria_input, criteria_rankings], outputs=criteria_rankings)
|
100 |
+
|
101 |
+
criteria_yes_button.click(lambda first, second: handle_criteria_vote(first, second, criteria_rankings, True),
|
102 |
+
inputs=[criteria_input, criteria_input], outputs=[criteria_compare_output, criteria_input, criteria_input])
|
103 |
+
criteria_no_button.click(lambda first, second: handle_criteria_vote(first, second, criteria_rankings, False),
|
104 |
+
inputs=[criteria_input, criteria_input], outputs=[criteria_compare_output, criteria_input, criteria_input])
|
105 |
+
criteria_new_vote.click(lambda data_frame: get_criteria_vote_start(data_frame),
|
106 |
+
inputs=[criteria_rankings], outputs=[criteria_compare_output, criteria_input, criteria_input])
|
107 |
+
|
108 |
+
with gr.Tab("Opponent Ranking"):
|
109 |
+
opponents_df = pd.DataFrame(columns=["descriptor", "opponent"] + [f"{c}_score" for c in criteria_df["criteria"]] + ["overall_score"])
|
110 |
+
rankings = gr.DataFrame(value=opponents_df, interactive=False,
|
111 |
+
headers=["Descriptor", "Opponent"] + [f"{c} Score" for c in criteria_df["criteria"]] + ["Overall Score"])
|
112 |
+
|
113 |
+
compare_output = gr.Textbox("Add some options to start voting!", label="Comparison", interactive=False)
|
114 |
+
yes_button = gr.Button("1", variant="secondary")
|
115 |
+
no_button = gr.Button("2", variant="primary")
|
116 |
+
criteria_output = gr.Textbox(label="Criteria", interactive=False)
|
117 |
+
|
118 |
+
new_vote = gr.Button("New Vote")
|
119 |
+
descriptor_input = gr.Textbox(label="Descriptor")
|
120 |
+
opponent_input = gr.Textbox(label="Opponent")
|
121 |
+
add_button = gr.Button("Add Opponent")
|
122 |
+
|
123 |
+
add_button.click(lambda: add_and_compare(clean_string(descriptor_input.value), clean_string(opponent_input.value), rankings, criteria_rankings),
|
124 |
+
inputs=[descriptor_input, opponent_input, rankings, criteria_rankings], outputs=[descriptor_input, opponent_input, rankings])
|
125 |
+
|
126 |
+
remove_descriptor_input = gr.Textbox(label="Descriptor")
|
127 |
+
remove_opponent_input = gr.Textbox(label="Opponent")
|
128 |
+
remove_button = gr.Button("Remove Opponent")
|
129 |
+
|
130 |
+
remove_button.click(lambda _, __: remove_opponent(remove_descriptor_input, remove_opponent_input, rankings),
|
131 |
+
inputs=[remove_descriptor_input, remove_opponent_input, rankings], outputs=rankings)
|
132 |
+
|
133 |
+
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"),
|
134 |
+
inputs=[compare_output, compare_output, criteria_output, rankings, criteria_rankings],
|
135 |
+
outputs=[compare_output, compare_output, compare_output, criteria_output, rankings])
|
136 |
+
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"),
|
137 |
+
inputs=[compare_output, compare_output, criteria_output, rankings, criteria_rankings],
|
138 |
+
outputs=[compare_output, compare_output, compare_output, criteria_output, rankings])
|
139 |
+
|
140 |
+
new_vote.click(lambda opp_df, crit_df: get_vote_start_data(opp_df, crit_df),
|
141 |
+
inputs=[rankings, criteria_rankings], outputs=[compare_output, compare_output, compare_output, criteria_output, rankings])
|
142 |
+
|
143 |
+
app.launch(share=False)
|