Spaces:
Running
on
Zero
Running
on
Zero
File size: 15,968 Bytes
3427608 243f5a6 3427608 b177a48 3427608 b6dc501 3427608 a4b32da 3427608 a4b32da 3427608 a4b32da 3427608 a4b32da 3427608 a4b32da 3427608 a4b32da 3427608 a4b32da 3427608 a4b32da 3427608 b177a48 3427608 b177a48 3427608 a4b32da b177a48 a4b32da 3427608 a4b32da 3427608 a4b32da 346927b 3427608 346927b 3427608 a4b32da b6dc501 3427608 243f5a6 3427608 346927b b6dc501 3427608 b177a48 243f5a6 3427608 b177a48 3427608 a4b32da b257e01 b6dc501 b257e01 b6dc501 3427608 a4b32da 3427608 b6dc501 3427608 b2a20c6 b6dc501 a4b32da b6dc501 b257e01 b6dc501 b257e01 3427608 b177a48 3427608 b177a48 3427608 a4b32da 3427608 b6dc501 3427608 b2a20c6 b6dc501 a4b32da b6dc501 b257e01 b6dc501 b257e01 3427608 a4b32da 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 243f5a6 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 243f5a6 3427608 a4b32da b6dc501 243f5a6 b6dc501 243f5a6 a4b32da 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 243f5a6 3427608 243f5a6 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 b177a48 3427608 243f5a6 a4b32da 243f5a6 7d71710 a4b32da 7d71710 |
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
import gradio as gr
from PIL import Image, ImageDraw, ImageFont, ImageOps
import os
from .constants import KSORT_IMAGE_DIR
from .constants import COLOR1, COLOR2, COLOR3, COLOR4
from .vote_utils import save_any_image
from .utils import disable_btn, enable_btn, invisible_btn
from .upload import create_remote_directory, upload_ssh_all, upload_ssh_data
import json
def reset_level(Top_btn):
if Top_btn == "Top 1":
level = 0
elif Top_btn == "Top 2":
level = 1
elif Top_btn == "Top 3":
level = 2
elif Top_btn == "Top 4":
level = 3
return level
def reset_rank(windows, rank, vote_level):
if windows == "Model A":
rank[0] = vote_level
elif windows == "Model B":
rank[1] = vote_level
elif windows == "Model C":
rank[2] = vote_level
elif windows == "Model D":
rank[3] = vote_level
return rank
def reset_btn_rank(windows, rank, btn, vote_level):
if windows == "Model A" and btn == "1":
rank[0] = 0
elif windows == "Model A" and btn == "2":
rank[0] = 1
elif windows == "Model A" and btn == "3":
rank[0] = 2
elif windows == "Model A" and btn == "4":
rank[0] = 3
elif windows == "Model B" and btn == "1":
rank[1] = 0
elif windows == "Model B" and btn == "2":
rank[1] = 1
elif windows == "Model B" and btn == "3":
rank[1] = 2
elif windows == "Model B" and btn == "4":
rank[1] = 3
elif windows == "Model C" and btn == "1":
rank[2] = 0
elif windows == "Model C" and btn == "2":
rank[2] = 1
elif windows == "Model C" and btn == "3":
rank[2] = 2
elif windows == "Model C" and btn == "4":
rank[2] = 3
elif windows == "Model D" and btn == "1":
rank[3] = 0
elif windows == "Model D" and btn == "2":
rank[3] = 1
elif windows == "Model D" and btn == "3":
rank[3] = 2
elif windows == "Model D" and btn == "4":
rank[3] = 3
if btn == "1":
vote_level = 0
elif btn == "2":
vote_level = 1
elif btn == "3":
vote_level = 2
elif btn == "4":
vote_level = 3
return (rank, vote_level)
def reset_vote_text(rank):
rank_str = ""
for i in range(len(rank)):
if rank[i] == None:
rank_str = rank_str + str(rank[i])
else:
rank_str = rank_str + str(rank[i]+1)
rank_str = rank_str + " "
return rank_str
def clear_rank(rank, vote_level):
for i in range(len(rank)):
rank[i] = None
vote_level = 0
return rank, vote_level
def revote_windows(generate_ig0, generate_ig1, generate_ig2, generate_ig3, rank, vote_level):
for i in range(len(rank)):
rank[i] = None
vote_level = 0
return generate_ig0, generate_ig1, generate_ig2, generate_ig3, rank, vote_level
def reset_submit(rank):
for i in range(len(rank)):
if rank[i] == None:
return disable_btn
return enable_btn
def reset_mode(mode):
if mode == "Best":
return (gr.update(visible=False, interactive=False),) * 5 + \
(gr.update(visible=True, interactive=True),) * 16 + \
(gr.update(visible=True, interactive=True),) * 3 + \
(gr.Textbox(value="Rank", visible=False, interactive=False),)
elif mode == "Rank":
return (gr.update(visible=True, interactive=True),) * 5 + \
(gr.update(visible=False, interactive=False),) * 16 + \
(gr.update(visible=True, interactive=False),) * 2 + \
(gr.update(visible=True, interactive=True),) + \
(gr.Textbox(value="Best", visible=False, interactive=False),)
else:
raise ValueError("Undefined mode")
def reset_chatbot(mode, generate_ig0, generate_ig1, generate_ig2, generate_ig3):
return generate_ig0, generate_ig1, generate_ig2, generate_ig3
def get_json_filename(conv_id):
output_dir = f'{KSORT_IMAGE_DIR}/{conv_id}/json/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_file = os.path.join(output_dir, "information.json")
# name = os.path.join(KSORT_IMAGE_DIR, f"{conv_id}/json/information.json")
print(output_file)
return output_file
def get_img_filename(conv_id, i):
output_dir = f'{KSORT_IMAGE_DIR}/{conv_id}/image/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_file = os.path.join(output_dir, f"{i}.jpg")
print(output_file)
return output_file
def vote_submit(states, textbox, rank, request: gr.Request):
conv_id = states[0].conv_id
for i in range(len(states)):
output_file = get_img_filename(conv_id, i)
save_any_image(states[i].output, output_file)
with open(get_json_filename(conv_id), "a") as fout:
data = {
"models_name": [x.model_name for x in states],
"img_rank": [x for x in rank],
"prompt": [textbox],
}
fout.write(json.dumps(data) + "\n")
def vote_ssh_submit(states, textbox, rank, user_name, user_institution):
conv_id = states[0].conv_id
output_dir = create_remote_directory(conv_id)
# upload_image(states, output_dir)
data = {
"models_name": [x.model_name for x in states],
"img_rank": [x for x in rank],
"prompt": [textbox],
"user_info": {"name": [user_name], "institution": [user_institution]},
}
output_file = os.path.join(output_dir, "result.json")
# upload_informance(data, output_file)
upload_ssh_all(states, output_dir, data, output_file)
from .update_skill import update_skill
update_skill(rank, [x.model_name for x in states])
def vote_video_ssh_submit(states, textbox, prompt_path, rank, user_name, user_institution):
conv_id = states[0].conv_id
output_dir = create_remote_directory(conv_id, video=True)
data = {
"models_name": [x.model_name for x in states],
"video_rank": [x for x in rank],
"prompt": [textbox],
"prompt_path": [prompt_path],
"video_path": [x.output for x in states],
"user_info": {"name": [user_name], "institution": [user_institution]},
}
output_file = os.path.join(output_dir, "result.json")
upload_ssh_data(data, output_file)
from .update_skill_video import update_skill_video
update_skill_video(rank, [x.model_name for x in states])
def submit_response_igm(
state0, state1, state2, state3, model_selector0, model_selector1, model_selector2, model_selector3, textbox, rank, user_name, user_institution, request: gr.Request
):
# vote_submit([state0, state1, state2, state3], textbox, rank, request)
vote_ssh_submit([state0, state1, state2, state3], textbox, rank, user_name, user_institution)
if model_selector0 == "":
return (disable_btn,) * 6 + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model C: {state2.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model D: {state3.model_name.split('_')[1]}", visible=True)
) + (disable_btn,)
else:
return (disable_btn,) * 6 + (
gr.Markdown(state0.model_name, visible=True),
gr.Markdown(state1.model_name, visible=True),
gr.Markdown(state2.model_name, visible=True),
gr.Markdown(state3.model_name, visible=True)
) + (disable_btn,)
def submit_response_vg(
state0, state1, state2, state3, model_selector0, model_selector1, model_selector2, model_selector3, textbox, prompt_path, rank, user_name, user_institution, request: gr.Request
):
vote_video_ssh_submit([state0, state1, state2, state3], textbox, prompt_path, rank, user_name, user_institution)
if model_selector0 == "":
return (disable_btn,) * 6 + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model C: {state2.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model D: {state3.model_name.split('_')[1]}", visible=True)
) + (disable_btn,)
else:
return (disable_btn,) * 6 + (
gr.Markdown(state0.model_name, visible=True),
gr.Markdown(state1.model_name, visible=True),
gr.Markdown(state2.model_name, visible=True),
gr.Markdown(state3.model_name, visible=True)
) + (disable_btn,)
def submit_response_rank_igm(
state0, state1, state2, state3, model_selector0, model_selector1, model_selector2, model_selector3, textbox, rank, right_vote_text, user_name, user_institution, request: gr.Request
):
print(rank)
if right_vote_text == "right":
# vote_submit([state0, state1, state2, state3], textbox, rank, request)
vote_ssh_submit([state0, state1, state2, state3], textbox, rank, user_name, user_institution)
if model_selector0 == "":
return (disable_btn,) * 16 + (disable_btn,) * 3 + ("wrong",) + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model C: {state2.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model D: {state3.model_name.split('_')[1]}", visible=True)
)
else:
return (disable_btn,) * 16 + (disable_btn,) * 3 + ("wrong",) + (
gr.Markdown(state0.model_name, visible=True),
gr.Markdown(state1.model_name, visible=True),
gr.Markdown(state2.model_name, visible=True),
gr.Markdown(state3.model_name, visible=True)
)
else:
return (enable_btn,) * 16 + (enable_btn,) * 3 + ("wrong",) + (gr.Markdown("", visible=False),) * 4
def submit_response_rank_vg(
state0, state1, state2, state3, model_selector0, model_selector1, model_selector2, model_selector3, textbox, prompt_path, rank, right_vote_text, user_name, user_institution, request: gr.Request
):
print(rank)
if right_vote_text == "right":
vote_video_ssh_submit([state0, state1, state2, state3], textbox, prompt_path, rank, user_name, user_institution)
if model_selector0 == "":
return (disable_btn,) * 16 + (disable_btn,) * 3 + ("wrong",) + (
gr.Markdown(f"### Model A: {state0.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model B: {state1.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model C: {state2.model_name.split('_')[1]}", visible=True),
gr.Markdown(f"### Model D: {state3.model_name.split('_')[1]}", visible=True)
)
else:
return (disable_btn,) * 16 + (disable_btn,) * 3 + ("wrong",) + (
gr.Markdown(state0.model_name, visible=True),
gr.Markdown(state1.model_name, visible=True),
gr.Markdown(state2.model_name, visible=True),
gr.Markdown(state3.model_name, visible=True)
)
else:
return (enable_btn,) * 16 + (enable_btn,) * 3 + ("wrong",) + (gr.Markdown("", visible=False),) * 4
def text_response_rank_igm(generate_ig0, generate_ig1, generate_ig2, generate_ig3, Top1_text, Top2_text, Top3_text, Top4_text, vote_textbox):
rank_list = [char for char in vote_textbox if char.isdigit()]
generate_ig = [generate_ig0, generate_ig1, generate_ig2, generate_ig3]
chatbot = []
rank = [None, None, None, None]
if len(rank_list) != 4:
return generate_ig + ["error rank"] + ["wrong"] + [rank]
for num in range(len(rank_list)):
if rank_list[num] in ['1', '2', '3', '4']:
base_image = Image.fromarray(generate_ig[num]).convert("RGBA")
base_image = base_image.resize((512, 512), Image.ANTIALIAS)
if rank_list[num] == '1':
border_color = COLOR1
elif rank_list[num] == '2':
border_color = COLOR2
elif rank_list[num] == '3':
border_color = COLOR3
elif rank_list[num] == '4':
border_color = COLOR4
border_size = 10 # Size of the border
base_image = ImageOps.expand(base_image, border=border_size, fill=border_color)
draw = ImageDraw.Draw(base_image)
font = ImageFont.truetype("./serve/Arial.ttf", 66)
text_position = (180, 25)
if rank_list[num] == '1':
text_color = COLOR1
draw.text(text_position, Top1_text, font=font, fill=text_color)
elif rank_list[num] == '2':
text_color = COLOR2
draw.text(text_position, Top2_text, font=font, fill=text_color)
elif rank_list[num] == '3':
text_color = COLOR3
draw.text(text_position, Top3_text, font=font, fill=text_color)
elif rank_list[num] == '4':
text_color = COLOR4
draw.text(text_position, Top4_text, font=font, fill=text_color)
base_image = base_image.convert("RGB")
chatbot.append(base_image.copy())
else:
return generate_ig + ["error rank"] + ["wrong"] + [rank]
rank_str = ""
for str_num in rank_list:
rank_str = rank_str + str_num
rank_str = rank_str + " "
rank = [int(x) for x in rank_list]
return chatbot + [rank_str] + ["right"] + [rank]
def text_response_rank_vg(vote_textbox):
rank_list = [char for char in vote_textbox if char.isdigit()]
rank = [None, None, None, None]
if len(rank_list) != 4:
return ["error rank"] + ["wrong"] + [rank]
for num in range(len(rank_list)):
if rank_list[num] in ['1', '2', '3', '4']:
continue
else:
return ["error rank"] + ["wrong"] + [rank]
rank_str = ""
for str_num in rank_list:
rank_str = rank_str + str_num
rank_str = rank_str + " "
rank = [int(x) for x in rank_list]
return [rank_str] + ["right"] + [rank]
def add_foreground(image, vote_level, Top1_text, Top2_text, Top3_text, Top4_text):
base_image = Image.fromarray(image).convert("RGBA")
base_image = base_image.resize((512, 512), Image.ANTIALIAS)
if vote_level == 0:
border_color = COLOR1
elif vote_level == 1:
border_color = COLOR2
elif vote_level == 2:
border_color = COLOR3
elif vote_level == 3:
border_color = COLOR4
border_size = 10 # Size of the border
base_image = ImageOps.expand(base_image, border=border_size, fill=border_color)
draw = ImageDraw.Draw(base_image)
font = ImageFont.truetype("./serve/Arial.ttf", 66)
text_position = (180, 25)
if vote_level == 0:
text_color = COLOR1
draw.text(text_position, Top1_text, font=font, fill=text_color)
elif vote_level == 1:
text_color = COLOR2
draw.text(text_position, Top2_text, font=font, fill=text_color)
elif vote_level == 2:
text_color = COLOR3
draw.text(text_position, Top3_text, font=font, fill=text_color)
elif vote_level == 3:
text_color = COLOR4
draw.text(text_position, Top4_text, font=font, fill=text_color)
base_image = base_image.convert("RGB")
return base_image
def add_green_border(image):
border_color = (0, 255, 0) # RGB for green
border_size = 10 # Size of the border
img_with_border = ImageOps.expand(image, border=border_size, fill=border_color)
return img_with_border
def check_textbox(textbox):
if textbox=="":
return False
else:
return True |