Spaces:
Sleeping
Sleeping
Youngsun Lim
commited on
Commit
ยท
e7ac655
1
Parent(s):
f99f1f1
1014
Browse files
app.py
CHANGED
|
@@ -92,6 +92,97 @@ def _progress_html(done, total):
|
|
| 92 |
<div style="font-size:12px; margin-top:4px;">{done} / {total}</div>
|
| 93 |
"""
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# -------------------- Example videos (download to local cache) --------------------
|
| 96 |
EXAMPLES = {
|
| 97 |
"BodyWeightSquats": {
|
|
@@ -268,39 +359,38 @@ with gr.Blocks(fill_height=True, css=GLOBAL_CSS) as demo:
|
|
| 268 |
pid.change(_toggle_by_pid, inputs=pid, outputs=save_next)
|
| 269 |
|
| 270 |
# -------- ํ์ด์ง ์ ํ & ์ฒซ ๋ก๋ --------
|
| 271 |
-
#
|
| 272 |
-
#
|
| 273 |
-
# return (
|
| 274 |
-
# gr.update(visible=False), # page_intro off
|
| 275 |
-
# gr.update(visible=True), # page_eval on
|
| 276 |
-
# url, action, 5.0, "", 0, _progress_html(0, TOTAL_PER_PARTICIPANT)
|
| 277 |
-
# )
|
| 278 |
-
|
| 279 |
-
# start_btn.click(
|
| 280 |
-
# _start_and_load_first,
|
| 281 |
-
# inputs=[],
|
| 282 |
-
# outputs=[page_intro, page_eval, video, action_tb, score, status, done_state, progress]
|
| 283 |
-
# )
|
| 284 |
|
| 285 |
def _start_and_load_first():
|
| 286 |
-
|
| 287 |
-
order =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
first_idx = order[0]
|
| 289 |
v0 = V[first_idx]
|
| 290 |
-
url0
|
|
|
|
|
|
|
| 291 |
return (
|
| 292 |
gr.update(visible=False), # page_intro off
|
| 293 |
gr.update(visible=True), # page_eval on
|
| 294 |
url0, # video
|
| 295 |
action0, # action_tb
|
| 296 |
5.0, # score ์ด๊ธฐ๊ฐ
|
| 297 |
-
"",
|
| 298 |
0, # done_state
|
| 299 |
_progress_html(0, TOTAL_PER_PARTICIPANT),
|
| 300 |
-
order, # order_state
|
| 301 |
-
1 # ptr_state
|
| 302 |
)
|
| 303 |
|
|
|
|
| 304 |
start_btn.click(
|
| 305 |
_start_and_load_first,
|
| 306 |
inputs=[],
|
|
|
|
| 92 |
<div style="font-size:12px; margin-top:4px;">{done} / {total}</div>
|
| 93 |
"""
|
| 94 |
|
| 95 |
+
def _build_order_with_anchor(total:int, anchor_idx:int, repeats:int, pool_size:int, min_gap:int=1):
|
| 96 |
+
"""
|
| 97 |
+
total: TOTAL_PER_PARTICIPANT (e.g., 30)
|
| 98 |
+
anchor_idx: index of the anchor video in V (0 for first item)
|
| 99 |
+
repeats: how many times to show anchor (e.g., 5)
|
| 100 |
+
pool_size: len(V)
|
| 101 |
+
min_gap: ์ต์ ๊ฐ๊ฒฉ(์ธ์ ๊ธ์ง => 1)
|
| 102 |
+
|
| 103 |
+
return: list of indices (length=total)
|
| 104 |
+
"""
|
| 105 |
+
assert repeats <= total, "repeats must be <= total"
|
| 106 |
+
assert pool_size >= 1, "videos pool must be non-empty"
|
| 107 |
+
|
| 108 |
+
# 1) ๋ค๋ฅธ ๋น๋์ค 25๊ฐ(์ค๋ณต ์์ด) ๋ฝ๊ธฐ
|
| 109 |
+
others_needed = total - repeats
|
| 110 |
+
# anchor๋ฅผ ์ ์ธํ ํ๋ณด ์ธ๋ฑ์ค
|
| 111 |
+
candidates = list(range(1, pool_size)) if anchor_idx == 0 else [i for i in range(pool_size) if i != anchor_idx]
|
| 112 |
+
if len(candidates) < others_needed:
|
| 113 |
+
raise ValueError("Not enough unique non-anchor videos to fill the schedule without duplication.")
|
| 114 |
+
|
| 115 |
+
others = random.sample(candidates, k=others_needed)
|
| 116 |
+
|
| 117 |
+
# 2) ๊ธฐ๋ณธ ์ํ์ค(others)๋ฅผ ๋ฌด์์๋ก ์๊ธฐ
|
| 118 |
+
random.shuffle(others)
|
| 119 |
+
|
| 120 |
+
# 3) ์ต์ปค๋ฅผ min_gap๋ฅผ ๋ง์กฑํ๋๋ก ์ฝ์
ํ ์์น ์ ์
|
| 121 |
+
# 30๊ฐ๋ฅผ 5๊ตฌ๊ฐ์ผ๋ก ๋๋ , ๊ฐ ๊ตฌ๊ฐ ๋ด์์ ์ถฉ๋ ๋ ๋๊ฒ ๋ฐฐ์น
|
| 122 |
+
# (๊ฐ๋จํ๊ณ ์์ ์ ์ธ ๋ฐฉ์)
|
| 123 |
+
seq = others[:] # ๊ธธ์ด=25
|
| 124 |
+
anchor_positions = []
|
| 125 |
+
segment = total // repeats # 30//5 = 6
|
| 126 |
+
|
| 127 |
+
for k in range(repeats):
|
| 128 |
+
# ๊ฐ ๊ตฌ๊ฐ [k*segment, (k+1)*segment) ์์์ ํ๋ณด ์์น๋ฅผ ๊ณ ๋ฆ
|
| 129 |
+
lo = k * segment
|
| 130 |
+
hi = (k + 1) * segment if k < repeats - 1 else total # ๋ง์ง๋ง์ ๋๊น์ง
|
| 131 |
+
# ๊ฒฝ๊ณ ๋ด ์์ ์คํ์
์ ํ (์ฌ์ ๋ฅผ ๋๊ณ ์ถฉ๋์ ํผํจ)
|
| 132 |
+
candidate_pos = random.randrange(lo, hi)
|
| 133 |
+
|
| 134 |
+
# ์ธ์ ๊ธ์ง ๋ณด์ : ์ด๋ฏธ ๋ฐฐ์ ๋ anchor ์์น๋ค๊ณผ์ ๊ฑฐ๋ฆฌ๊ฐ min_gap ์ด์ ๋๋๋ก ์กฐ์
|
| 135 |
+
# ํ์ ์ ์ข์ฐ๋ก ๊ทผ์ ํ ๋น ์ฌ๋กฏ ํ์
|
| 136 |
+
def ok(pos):
|
| 137 |
+
return all(abs(pos - p) >= min_gap + 1 for p in anchor_positions) # ์ฐ์๊ธ์ง => ๊ฑฐ๋ฆฌ >= 2
|
| 138 |
+
|
| 139 |
+
# ๊ทผ๋ฐฉ ํ์ ํญ
|
| 140 |
+
found = None
|
| 141 |
+
for delta in range(0, segment): # ๊ตฌ๊ฐ ํฌ๊ธฐ ๋ด์์ ํ์
|
| 142 |
+
# ์ข/์ฐ ๋ฒ๊ฐ์๊ฐ๋ฉฐ ํ๋ณด ์๋
|
| 143 |
+
for sign in (+1, -1):
|
| 144 |
+
pos = candidate_pos + sign * delta
|
| 145 |
+
if 0 <= pos < total and ok(pos):
|
| 146 |
+
found = pos
|
| 147 |
+
break
|
| 148 |
+
if found is not None:
|
| 149 |
+
break
|
| 150 |
+
|
| 151 |
+
if found is None:
|
| 152 |
+
# ์ตํ: 0..total-1 ๋ฒ์์์ ์๋ฌด ๋ฐ๋ ์ถฉ๋ ์๋ ๊ณณ ์ฐพ๊ธฐ
|
| 153 |
+
for pos in range(total):
|
| 154 |
+
if ok(pos):
|
| 155 |
+
found = pos
|
| 156 |
+
break
|
| 157 |
+
if found is None:
|
| 158 |
+
raise RuntimeError("Failed to place anchor without adjacency. Try different strategy or loosen min_gap.")
|
| 159 |
+
|
| 160 |
+
anchor_positions.append(found)
|
| 161 |
+
|
| 162 |
+
# 4) others๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ธธ์ด total์ ๋น ์ํ์ค๋ฅผ ๋ง๋ค๊ณ anchor๋ฅผ ์ฃผ์
|
| 163 |
+
# ์ฐ์ ๋น ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ anchor ์์น๋ฅผ ์ฑ์ด ํ, ๋๋จธ์ง๋ฅผ others๋ก ์ฑ์
|
| 164 |
+
result = [None] * total
|
| 165 |
+
for pos in anchor_positions:
|
| 166 |
+
result[pos] = anchor_idx
|
| 167 |
+
|
| 168 |
+
# others ํฌ์ธํฐ
|
| 169 |
+
j = 0
|
| 170 |
+
for i in range(total):
|
| 171 |
+
if result[i] is None:
|
| 172 |
+
result[i] = others[j]
|
| 173 |
+
j += 1
|
| 174 |
+
|
| 175 |
+
# ์์ ์ฒดํฌ
|
| 176 |
+
assert len(result) == total
|
| 177 |
+
# ์ธ์ anchor ์๋์ง ํ์ธ
|
| 178 |
+
for i in range(1, total):
|
| 179 |
+
assert not (result[i] == anchor_idx and result[i-1] == anchor_idx), "Adjacent anchors found."
|
| 180 |
+
# anchor ๊ฐ์ ํ์ธ
|
| 181 |
+
assert sum(1 for x in result if x == anchor_idx) == repeats, "Anchor count mismatch."
|
| 182 |
+
|
| 183 |
+
return result
|
| 184 |
+
|
| 185 |
+
|
| 186 |
# -------------------- Example videos (download to local cache) --------------------
|
| 187 |
EXAMPLES = {
|
| 188 |
"BodyWeightSquats": {
|
|
|
|
| 359 |
pid.change(_toggle_by_pid, inputs=pid, outputs=save_next)
|
| 360 |
|
| 361 |
# -------- ํ์ด์ง ์ ํ & ์ฒซ ๋ก๋ --------
|
| 362 |
+
ANCHOR_IDX = 0 # videos.json์ ๋งจ ์ฒซ ๋ฒ์งธ ๋น๋์ค
|
| 363 |
+
ANCHOR_REPEATS = 5 # ์ต์ปค 5ํ ๋
ธ์ถ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
def _start_and_load_first():
|
| 366 |
+
total = TOTAL_PER_PARTICIPANT
|
| 367 |
+
order = _build_order_with_anchor(
|
| 368 |
+
total=total,
|
| 369 |
+
anchor_idx=ANCHOR_IDX,
|
| 370 |
+
repeats=ANCHOR_REPEATS,
|
| 371 |
+
pool_size=len(V),
|
| 372 |
+
min_gap=1 # ์ธ์ ๊ธ์ง
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
first_idx = order[0]
|
| 376 |
v0 = V[first_idx]
|
| 377 |
+
url0 = v0["url"]
|
| 378 |
+
action0 = _extract_action(v0)
|
| 379 |
+
|
| 380 |
return (
|
| 381 |
gr.update(visible=False), # page_intro off
|
| 382 |
gr.update(visible=True), # page_eval on
|
| 383 |
url0, # video
|
| 384 |
action0, # action_tb
|
| 385 |
5.0, # score ์ด๊ธฐ๊ฐ
|
| 386 |
+
gr.update(visible=False, value=""), # status ์ด๊ธฐํ (hidden)
|
| 387 |
0, # done_state
|
| 388 |
_progress_html(0, TOTAL_PER_PARTICIPANT),
|
| 389 |
+
order, # order_state
|
| 390 |
+
1 # ptr_state (๋ค์ ์์ดํ
๊ฐ๋ฆฌํด)
|
| 391 |
)
|
| 392 |
|
| 393 |
+
|
| 394 |
start_btn.click(
|
| 395 |
_start_and_load_first,
|
| 396 |
inputs=[],
|