Spaces:
Sleeping
Sleeping
File size: 6,037 Bytes
bdeb650 4187ca4 bdeb650 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
import gradio as gr
import random
import json
import os
initial_data = [
["dalle_desc_25", 1000, ""],
["dalle_desc_50", 1000, ""],
["dalle_desc_100", 1000, ""],
["dalle_desc_150", 1000, ""],
["dalle_desc_250", 1000, ""],
["desc_25_threshold_250", 1000, ""],
["desc_25_threshold_500", 1000, ""],
["desc_25_threshold_1000", 1000, ""],
["desc_250_threshold_250", 1000, ""],
["desc_250_threshold_500", 1000, ""],
["desc_250_threshold_1000", 1000, ""],
["jpeg_scale_2", 1000, ""],
["jpeg_scale_4", 1000, ""],
["jpeg_scale_8", 1000, ""],
["jpeg_scale_16", 1000, ""],
["jpeg_scale_32", 1000, ""],
["sa30_desc_50", 1000, ""],
["sa30_desc_100", 1000, ""],
["sa30_desc_150", 1000, ""],
["sa30_desc_250", 1000, ""],
["sd30_desc_25", 1000, ""],
["sd35_desc_25", 1000, ""],
["sd35_desc_50", 1000, ""],
["sd35_desc_100", 1000, ""],
["sd35_desc_150", 1000, ""],
["sd35_desc_250", 1000, ""],
]
# File to store leaderboard data
LEADERBOARD_FILE = "leaderboard_data.json"
def load_leaderboard_data():
if os.path.exists(LEADERBOARD_FILE):
with open(LEADERBOARD_FILE, 'r') as f:
return json.load(f)
else:
# Initial leaderboard data if file doesn't exist
save_leaderboard_data(initial_data)
return initial_data
def save_leaderboard_data(data):
with open(LEADERBOARD_FILE, 'w') as f:
json.dump(data, f)
# Initialize global variables
objs = ['axe', 'barrel', 'bed', 'bottle', 'canon', 'car', 'chair', 'chair2', 'chair3', 'chair4']
leaderboard_data = load_leaderboard_data()
images = [f"3d/{e[0]}/OBJ.png" for e in initial_data]
current_images = [0, 0]
def get_new_images():
global current_images
random.seed()
idx1, idx2 = random.sample(range(len(images)), 2)
current_images = [idx1, idx2]
obj = random.choice(objs)
new_images = [i.replace('OBJ', obj) for i in images]
original = f"3d/original/{obj}.png"
return {
"original": original,
"image1": new_images[idx1],
"image2": new_images[idx2],
"label1": "Left",
"label2": "Right"
}
def update_elo(winner, loser, k=32):
global leaderboard_data
winner_elo = leaderboard_data[winner][1]
loser_elo = leaderboard_data[loser][1]
expected_winner = 1 / (1 + 10 ** ((loser_elo - winner_elo) / 400))
expected_loser = 1 / (1 + 10 ** ((winner_elo - loser_elo) / 400))
leaderboard_data[winner][1] += round(k * (1 - expected_winner))
leaderboard_data[loser][1] += round(k * (0 - expected_loser))
save_leaderboard_data(leaderboard_data)
return leaderboard_data
def vote_and_randomize(choice):
global current_images
if choice == "left":
updated_data = update_elo(current_images[0], current_images[1])
winner_name = leaderboard_data[current_images[0]][0]
else:
updated_data = update_elo(current_images[1], current_images[0])
winner_name = leaderboard_data[current_images[1]][0]
new_state = get_new_images()
return (
f"Thanks for voting for {winner_name}!",
new_state["original"],
new_state["image1"],
new_state["image2"],
new_state["label1"],
new_state["label2"],
updated_data
)
def start_voting():
# Get initial random images
initial_state = get_new_images()
return (
gr.update(visible=False), # Hide start button
gr.update(visible=True), # Show voting container
initial_state["original"],
initial_state["image1"],
initial_state["image2"],
initial_state["label1"],
initial_state["label2"]
)
with gr.Blocks(css="""
#main-image {
margin: auto; /* Center the image */
display: block;
}
""") as demo:
with gr.Tabs():
# Tab 1: Voting
with gr.Tab("Voting"):
gr.Markdown("### Vote for your favorite option!")
# Start button (centered)
with gr.Column(elem_id="start-container"):
start_btn = gr.Button("Start!", scale=0.5)
# Voting interface (initially hidden)
with gr.Column(visible=False) as voting_container:
# Image Comparison Grid
# justify in the center
with gr.Row(equal_height=True):
main_image = gr.Image(value=None, label="Original", interactive=False, show_download_button=True, elem_id="main-image", scale=0.25)
with gr.Row():
left_image = gr.Image(value=None, label="Left Option", interactive=False, show_download_button=False)
right_image = gr.Image(value=None, label="Right Option", interactive=False, show_download_button=False)
with gr.Row():
vote_1 = gr.Button(value="")
vote_2 = gr.Button(value="")
output = gr.Textbox(label="Vote Result", interactive=False)
# Tab 2: Leaderboard
with gr.Tab("Leaderboard"):
gr.Markdown("### Leaderboard")
leaderboard_table = gr.DataFrame(
headers=["Name", "Elo", "Description"],
value=load_leaderboard_data(),
interactive=False
)
# Handle start button click
start_btn.click(
fn=start_voting,
outputs=[
start_btn,
voting_container,
main_image,
left_image,
right_image,
vote_1,
vote_2
]
)
# Handle voting buttons
vote_1.click(
fn=lambda: vote_and_randomize("left"),
outputs=[output, main_image, left_image, right_image, vote_1, vote_2, leaderboard_table]
)
vote_2.click(
fn=lambda: vote_and_randomize("right"),
outputs=[output, main_image, left_image, right_image, vote_1, vote_2, leaderboard_table]
)
if __name__ == "__main__":
demo.launch() |