Spaces:
Sleeping
Sleeping
backend
Browse files- .gitignore +2 -1
- app.py +118 -73
.gitignore
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
.DS_Store
|
| 2 |
.vscode
|
| 3 |
__pycache__
|
| 4 |
-
leaderboard_data.json
|
|
|
|
|
|
| 1 |
.DS_Store
|
| 2 |
.vscode
|
| 3 |
__pycache__
|
| 4 |
+
leaderboard_data.json
|
| 5 |
+
.env
|
app.py
CHANGED
|
@@ -1,106 +1,116 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
-
import json
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 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 |
-
if os.path.exists(LEADERBOARD_FILE):
|
| 40 |
-
with open(LEADERBOARD_FILE, 'r') as f:
|
| 41 |
-
return json.load(f)
|
| 42 |
-
else:
|
| 43 |
-
# Initial leaderboard data if file doesn't exist
|
| 44 |
-
save_leaderboard_data(initial_data)
|
| 45 |
-
return initial_data
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
#
|
| 52 |
objs = ['axe', 'barrel', 'bed', 'bottle', 'canon', 'car', 'chair', 'chair2', 'chair3', 'chair4']
|
| 53 |
-
leaderboard_data = load_leaderboard_data()
|
| 54 |
-
images = [f"3d/{e[0]}/OBJ.png" for e in initial_data]
|
| 55 |
-
current_images = [0, 0]
|
| 56 |
|
| 57 |
def get_new_images():
|
| 58 |
-
global current_images
|
| 59 |
random.seed()
|
| 60 |
idx1, idx2 = random.sample(range(len(images)), 2)
|
| 61 |
current_images = [idx1, idx2]
|
| 62 |
obj = random.choice(objs)
|
| 63 |
-
|
|
|
|
| 64 |
original = f"3d/original/{obj}.png"
|
| 65 |
return {
|
| 66 |
"original": original,
|
| 67 |
"image1": new_images[idx1],
|
| 68 |
"image2": new_images[idx2],
|
| 69 |
"label1": "Left",
|
| 70 |
-
"label2": "Right"
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
-
def update_elo(winner, loser, k=32):
|
| 74 |
-
global leaderboard_data
|
| 75 |
-
winner_elo = leaderboard_data[winner][1]
|
| 76 |
-
loser_elo = leaderboard_data[loser][1]
|
| 77 |
-
expected_winner = 1 / (1 + 10 ** ((loser_elo - winner_elo) / 400))
|
| 78 |
-
expected_loser = 1 / (1 + 10 ** ((winner_elo - loser_elo) / 400))
|
| 79 |
-
leaderboard_data[winner][1] += round(k * (1 - expected_winner))
|
| 80 |
-
leaderboard_data[loser][1] += round(k * (0 - expected_loser))
|
| 81 |
-
save_leaderboard_data(leaderboard_data)
|
| 82 |
-
return leaderboard_data
|
| 83 |
-
|
| 84 |
def vote_and_randomize(choice):
|
| 85 |
-
global current_images
|
| 86 |
-
|
| 87 |
if choice == "left":
|
| 88 |
-
|
| 89 |
-
|
| 90 |
else:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
new_state = get_new_images()
|
| 95 |
-
|
|
|
|
| 96 |
return (
|
| 97 |
-
|
| 98 |
new_state["original"],
|
| 99 |
-
new_state["image1"],
|
| 100 |
new_state["image2"],
|
| 101 |
new_state["label1"],
|
| 102 |
new_state["label2"],
|
| 103 |
-
|
| 104 |
)
|
| 105 |
|
| 106 |
def start_voting():
|
|
@@ -117,13 +127,34 @@ def start_voting():
|
|
| 117 |
initial_state["label2"]
|
| 118 |
)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
with gr.Blocks(css="""
|
| 121 |
#main-image {
|
| 122 |
margin: auto; /* Center the image */
|
| 123 |
display: block;
|
| 124 |
}
|
| 125 |
""") as demo:
|
| 126 |
-
with gr.Tabs():
|
| 127 |
# Tab 1: Voting
|
| 128 |
with gr.Tab("Voting"):
|
| 129 |
gr.Markdown("### Vote for your favorite option!")
|
|
@@ -150,13 +181,15 @@ with gr.Blocks(css="""
|
|
| 150 |
output = gr.Textbox(label="Vote Result", interactive=False)
|
| 151 |
|
| 152 |
# Tab 2: Leaderboard
|
| 153 |
-
with gr.Tab("Leaderboard"):
|
| 154 |
gr.Markdown("### Leaderboard")
|
| 155 |
leaderboard_table = gr.DataFrame(
|
| 156 |
headers=["Name", "Elo", "Description"],
|
| 157 |
-
value=
|
| 158 |
interactive=False
|
| 159 |
)
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Handle start button click
|
| 162 |
start_btn.click(
|
|
@@ -182,5 +215,17 @@ with gr.Blocks(css="""
|
|
| 182 |
outputs=[output, main_image, left_image, right_image, vote_1, vote_2, leaderboard_table]
|
| 183 |
)
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
if __name__ == "__main__":
|
| 186 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
|
|
|
| 3 |
import os
|
| 4 |
+
import requests # added import for API calls
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
load_dotenv()
|
| 7 |
|
| 8 |
+
# Define model names for randomization (extracted from your previous initial_data)
|
| 9 |
+
model_names = [
|
| 10 |
+
"dalle_desc_25",
|
| 11 |
+
"dalle_desc_50",
|
| 12 |
+
"dalle_desc_100",
|
| 13 |
+
"dalle_desc_150",
|
| 14 |
+
"dalle_desc_250",
|
| 15 |
+
"desc_25_threshold_250",
|
| 16 |
+
"desc_25_threshold_500",
|
| 17 |
+
"desc_25_threshold_1000",
|
| 18 |
+
"desc_250_threshold_250",
|
| 19 |
+
"desc_250_threshold_500",
|
| 20 |
+
"desc_250_threshold_1000",
|
| 21 |
+
"jpeg_scale_2",
|
| 22 |
+
"jpeg_scale_4",
|
| 23 |
+
"jpeg_scale_8",
|
| 24 |
+
"jpeg_scale_16",
|
| 25 |
+
"jpeg_scale_32",
|
| 26 |
+
"sa30_desc_50",
|
| 27 |
+
"sa30_desc_100",
|
| 28 |
+
"sa30_desc_150",
|
| 29 |
+
"sa30_desc_250",
|
| 30 |
+
"sd30_desc_25",
|
| 31 |
+
"sd35_desc_25",
|
| 32 |
+
"sd35_desc_50",
|
| 33 |
+
"sd35_desc_100",
|
| 34 |
+
"sd35_desc_150",
|
| 35 |
+
"sd35_desc_250"
|
| 36 |
]
|
| 37 |
|
| 38 |
+
# Global variables for the image template and current state:
|
| 39 |
+
images = [f"3d/{model}/OBJ.png" for model in model_names]
|
| 40 |
+
current_images = [0, 0]
|
| 41 |
+
current_obj = None # will store the object used in the current voting round
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Set API host and access key from environment variables
|
| 44 |
+
BACK_HOST = os.getenv("BACK_HOST")
|
| 45 |
+
ACCESS_KEY = os.getenv("ACCESS_KEY")
|
| 46 |
|
| 47 |
+
# List of objects to choose from (kept as-is)
|
| 48 |
objs = ['axe', 'barrel', 'bed', 'bottle', 'canon', 'car', 'chair', 'chair2', 'chair3', 'chair4']
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def get_new_images():
|
| 51 |
+
global current_images, current_obj
|
| 52 |
random.seed()
|
| 53 |
idx1, idx2 = random.sample(range(len(images)), 2)
|
| 54 |
current_images = [idx1, idx2]
|
| 55 |
obj = random.choice(objs)
|
| 56 |
+
current_obj = obj # store the object for the current round
|
| 57 |
+
new_images = [img.replace('OBJ', obj) for img in images]
|
| 58 |
original = f"3d/original/{obj}.png"
|
| 59 |
return {
|
| 60 |
"original": original,
|
| 61 |
"image1": new_images[idx1],
|
| 62 |
"image2": new_images[idx2],
|
| 63 |
"label1": "Left",
|
| 64 |
+
"label2": "Right",
|
| 65 |
+
"obj": obj # return the object in case it is needed
|
| 66 |
}
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def vote_and_randomize(choice):
|
| 69 |
+
global current_images, current_obj
|
|
|
|
| 70 |
if choice == "left":
|
| 71 |
+
winner_index = current_images[0]
|
| 72 |
+
loser_index = current_images[1]
|
| 73 |
else:
|
| 74 |
+
winner_index = current_images[1]
|
| 75 |
+
loser_index = current_images[0]
|
| 76 |
|
| 77 |
+
winner_model = model_names[winner_index]
|
| 78 |
+
loser_model = model_names[loser_index]
|
| 79 |
+
# Use the current object generated during the image randomization
|
| 80 |
+
obj = current_obj
|
| 81 |
+
|
| 82 |
+
# Prepare payload for voting
|
| 83 |
+
payload = {
|
| 84 |
+
"winner": winner_model,
|
| 85 |
+
"loser": loser_model,
|
| 86 |
+
"object": obj
|
| 87 |
+
}
|
| 88 |
+
url = f"{BACK_HOST}/vote"
|
| 89 |
+
headers = {
|
| 90 |
+
"Authorization": f"Bearer {ACCESS_KEY}",
|
| 91 |
+
"Content-Type": "application/json"
|
| 92 |
+
}
|
| 93 |
+
try:
|
| 94 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 95 |
+
resp_json = response.json()
|
| 96 |
+
if resp_json.get("message") == "Vote recorded successfully":
|
| 97 |
+
message = f"Thanks for voting for {winner_model}!"
|
| 98 |
+
else:
|
| 99 |
+
message = "Error recording vote. Please try again."
|
| 100 |
+
except Exception as e:
|
| 101 |
+
message = "Error recording vote. Please try again."
|
| 102 |
+
|
| 103 |
new_state = get_new_images()
|
| 104 |
+
updated_leaderboard = get_leaderboard_data() # refresh leaderboard from API
|
| 105 |
+
|
| 106 |
return (
|
| 107 |
+
message,
|
| 108 |
new_state["original"],
|
| 109 |
+
new_state["image1"],
|
| 110 |
new_state["image2"],
|
| 111 |
new_state["label1"],
|
| 112 |
new_state["label2"],
|
| 113 |
+
updated_leaderboard
|
| 114 |
)
|
| 115 |
|
| 116 |
def start_voting():
|
|
|
|
| 127 |
initial_state["label2"]
|
| 128 |
)
|
| 129 |
|
| 130 |
+
def get_leaderboard_data():
|
| 131 |
+
"""Fetch leaderboard data from the API and transform it for display."""
|
| 132 |
+
headers = {
|
| 133 |
+
"Authorization": f"Bearer {ACCESS_KEY}"
|
| 134 |
+
}
|
| 135 |
+
try:
|
| 136 |
+
response = requests.get(f"{BACK_HOST}/get", headers=headers)
|
| 137 |
+
if response.status_code == 200:
|
| 138 |
+
data = response.json()
|
| 139 |
+
# Transform the dictionary into a list of rows for the DataFrame
|
| 140 |
+
leaderboard_list = [[name, elo, ""] for name, elo in data.items()]
|
| 141 |
+
return leaderboard_list
|
| 142 |
+
else:
|
| 143 |
+
return []
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return []
|
| 146 |
+
|
| 147 |
+
def refresh_leaderboard():
|
| 148 |
+
"""Refresh leaderboard data."""
|
| 149 |
+
return get_leaderboard_data()
|
| 150 |
+
|
| 151 |
with gr.Blocks(css="""
|
| 152 |
#main-image {
|
| 153 |
margin: auto; /* Center the image */
|
| 154 |
display: block;
|
| 155 |
}
|
| 156 |
""") as demo:
|
| 157 |
+
with gr.Tabs() as tabs: # Remove elem_id, we don't need it anymore
|
| 158 |
# Tab 1: Voting
|
| 159 |
with gr.Tab("Voting"):
|
| 160 |
gr.Markdown("### Vote for your favorite option!")
|
|
|
|
| 181 |
output = gr.Textbox(label="Vote Result", interactive=False)
|
| 182 |
|
| 183 |
# Tab 2: Leaderboard
|
| 184 |
+
with gr.Tab("Leaderboard") as leaderboard_tab:
|
| 185 |
gr.Markdown("### Leaderboard")
|
| 186 |
leaderboard_table = gr.DataFrame(
|
| 187 |
headers=["Name", "Elo", "Description"],
|
| 188 |
+
value=get_leaderboard_data(),
|
| 189 |
interactive=False
|
| 190 |
)
|
| 191 |
+
# Add a refresh button
|
| 192 |
+
refresh_btn = gr.Button("Refresh Leaderboard")
|
| 193 |
|
| 194 |
# Handle start button click
|
| 195 |
start_btn.click(
|
|
|
|
| 215 |
outputs=[output, main_image, left_image, right_image, vote_1, vote_2, leaderboard_table]
|
| 216 |
)
|
| 217 |
|
| 218 |
+
# Replace the tabs.change with a refresh button click handler
|
| 219 |
+
refresh_btn.click(
|
| 220 |
+
fn=refresh_leaderboard,
|
| 221 |
+
outputs=leaderboard_table
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
# Also refresh when the leaderboard tab is selected
|
| 225 |
+
leaderboard_tab.select(
|
| 226 |
+
fn=refresh_leaderboard,
|
| 227 |
+
outputs=leaderboard_table
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
if __name__ == "__main__":
|
| 231 |
demo.launch()
|