Spaces:
Sleeping
Sleeping
ping98k
commited on
Commit
·
a01815f
1
Parent(s):
1d9f28c
Add ETA progress logging to UI
Browse files
main.py
CHANGED
|
@@ -5,6 +5,29 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
| 5 |
from tqdm import tqdm
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from tournament_utils import generate_players, prompt_score, prompt_pairwise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
NUM_TOP_PICKS_DEFAULT = int(os.getenv("NUM_TOP_PICKS", 3))
|
| 10 |
POOL_SIZE_DEFAULT = int(os.getenv("POOL_SIZE", 5))
|
|
@@ -135,13 +158,11 @@ def run_tournament(
|
|
| 135 |
|
| 136 |
yield from log("Histogram generating")
|
| 137 |
with ThreadPoolExecutor(max_workers=max_workers) as ex:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
)
|
| 144 |
-
}
|
| 145 |
hist_fig = plt.figure()
|
| 146 |
plt.hist(list(scores.values()), bins=10)
|
| 147 |
yield from log("Histogram generated")
|
|
@@ -171,11 +192,13 @@ def run_tournament(
|
|
| 171 |
def tournament_round(pairs, executor):
|
| 172 |
futures = {executor.submit(play, a, b): (a, b) for a, b in pairs}
|
| 173 |
results = []
|
| 174 |
-
|
|
|
|
| 175 |
a, b = futures[fut]
|
| 176 |
winner = fut.result()
|
| 177 |
loser = b if winner == a else a
|
| 178 |
results.append((winner, loser))
|
|
|
|
| 179 |
return results
|
| 180 |
|
| 181 |
def tournament(players, executor):
|
|
@@ -184,7 +207,7 @@ def run_tournament(
|
|
| 184 |
while len(current) > 1:
|
| 185 |
leftover = current[-1] if len(current) % 2 == 1 else None
|
| 186 |
pairs = [(current[i], current[i + 1]) for i in range(0, len(current) - 1, 2)]
|
| 187 |
-
round_results = tournament_round(pairs, executor)
|
| 188 |
for w, l in round_results:
|
| 189 |
lost_to[l] = w
|
| 190 |
current = [w for w, _ in round_results]
|
|
@@ -203,21 +226,24 @@ def run_tournament(
|
|
| 203 |
for j in range(i + 1, len(candidates))
|
| 204 |
]
|
| 205 |
futures = {executor.submit(play, a, b): (a, b) for a, b in pairs}
|
| 206 |
-
|
|
|
|
| 207 |
wins[fut.result()] += 1
|
|
|
|
| 208 |
return sorted(candidates, key=lambda p: wins[p], reverse=True)
|
| 209 |
|
| 210 |
def get_top(players, executor):
|
| 211 |
-
champion, lost_to = tournament(players, executor)
|
| 212 |
runner_up = lost_to.get(champion)
|
| 213 |
finalists = [champion] + ([runner_up] if runner_up else [])
|
| 214 |
semifinalists = [p for p, o in lost_to.items() if o in finalists and p not in finalists]
|
| 215 |
candidates = list(set(finalists + semifinalists + get_candidates(champion, lost_to)))
|
| 216 |
-
|
|
|
|
| 217 |
|
| 218 |
yield from log("Pairwise generating")
|
| 219 |
with ThreadPoolExecutor(max_workers=max_workers) as ex:
|
| 220 |
-
top_k = get_top(top_players, ex)
|
| 221 |
for i, txt in enumerate(pairwise_outputs, 1):
|
| 222 |
yield from log_completion(f"Pairwise completion {i}: ", txt)
|
| 223 |
else:
|
|
|
|
| 5 |
from tqdm import tqdm
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from tournament_utils import generate_players, prompt_score, prompt_pairwise
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SimpleProgress:
|
| 12 |
+
"""Minimal progress helper to compute ETA."""
|
| 13 |
+
|
| 14 |
+
def __init__(self, total: int, prefix: str = "Progress"):
|
| 15 |
+
self.total = total
|
| 16 |
+
self.prefix = prefix
|
| 17 |
+
self.start = time.time()
|
| 18 |
+
self.count = 0
|
| 19 |
+
|
| 20 |
+
def step(self) -> str:
|
| 21 |
+
self.count += 1
|
| 22 |
+
elapsed = time.time() - self.start
|
| 23 |
+
remaining = (elapsed / self.count) * (self.total - self.count) if self.count else 0
|
| 24 |
+
h, rem = divmod(int(remaining), 3600)
|
| 25 |
+
m, s = divmod(rem, 60)
|
| 26 |
+
if h:
|
| 27 |
+
eta = f"{h:d}:{m:02d}:{s:02d}"
|
| 28 |
+
else:
|
| 29 |
+
eta = f"{m:02d}:{s:02d}"
|
| 30 |
+
return f"{self.prefix} {self.count}/{self.total} - ETA {eta}"
|
| 31 |
|
| 32 |
NUM_TOP_PICKS_DEFAULT = int(os.getenv("NUM_TOP_PICKS", 3))
|
| 33 |
POOL_SIZE_DEFAULT = int(os.getenv("POOL_SIZE", 5))
|
|
|
|
| 158 |
|
| 159 |
yield from log("Histogram generating")
|
| 160 |
with ThreadPoolExecutor(max_workers=max_workers) as ex:
|
| 161 |
+
prog = SimpleProgress(len(all_players), "Scoring")
|
| 162 |
+
scores = {}
|
| 163 |
+
for p, s in zip(all_players, ex.map(score, all_players)):
|
| 164 |
+
scores[p] = s
|
| 165 |
+
yield from log(prog.step())
|
|
|
|
|
|
|
| 166 |
hist_fig = plt.figure()
|
| 167 |
plt.hist(list(scores.values()), bins=10)
|
| 168 |
yield from log("Histogram generated")
|
|
|
|
| 192 |
def tournament_round(pairs, executor):
|
| 193 |
futures = {executor.submit(play, a, b): (a, b) for a, b in pairs}
|
| 194 |
results = []
|
| 195 |
+
prog = SimpleProgress(len(futures), "Pairwise round")
|
| 196 |
+
for fut in as_completed(futures):
|
| 197 |
a, b = futures[fut]
|
| 198 |
winner = fut.result()
|
| 199 |
loser = b if winner == a else a
|
| 200 |
results.append((winner, loser))
|
| 201 |
+
yield from log(prog.step())
|
| 202 |
return results
|
| 203 |
|
| 204 |
def tournament(players, executor):
|
|
|
|
| 207 |
while len(current) > 1:
|
| 208 |
leftover = current[-1] if len(current) % 2 == 1 else None
|
| 209 |
pairs = [(current[i], current[i + 1]) for i in range(0, len(current) - 1, 2)]
|
| 210 |
+
round_results = yield from tournament_round(pairs, executor)
|
| 211 |
for w, l in round_results:
|
| 212 |
lost_to[l] = w
|
| 213 |
current = [w for w, _ in round_results]
|
|
|
|
| 226 |
for j in range(i + 1, len(candidates))
|
| 227 |
]
|
| 228 |
futures = {executor.submit(play, a, b): (a, b) for a, b in pairs}
|
| 229 |
+
prog = SimpleProgress(len(futures), "Playoff")
|
| 230 |
+
for fut in as_completed(futures):
|
| 231 |
wins[fut.result()] += 1
|
| 232 |
+
yield from log(prog.step())
|
| 233 |
return sorted(candidates, key=lambda p: wins[p], reverse=True)
|
| 234 |
|
| 235 |
def get_top(players, executor):
|
| 236 |
+
champion, lost_to = yield from tournament(players, executor)
|
| 237 |
runner_up = lost_to.get(champion)
|
| 238 |
finalists = [champion] + ([runner_up] if runner_up else [])
|
| 239 |
semifinalists = [p for p, o in lost_to.items() if o in finalists and p not in finalists]
|
| 240 |
candidates = list(set(finalists + semifinalists + get_candidates(champion, lost_to)))
|
| 241 |
+
result = yield from playoff(candidates, executor)
|
| 242 |
+
return result[:num_top_picks]
|
| 243 |
|
| 244 |
yield from log("Pairwise generating")
|
| 245 |
with ThreadPoolExecutor(max_workers=max_workers) as ex:
|
| 246 |
+
top_k = yield from get_top(top_players, ex)
|
| 247 |
for i, txt in enumerate(pairwise_outputs, 1):
|
| 248 |
yield from log_completion(f"Pairwise completion {i}: ", txt)
|
| 249 |
else:
|