Remove every field that can reconstruct a private composition from the leaderboard response
Browse filesThe masked formula hid the element counts but the composition object beside it carried them verbatim, and the measured energies would have allowed the same reconstruction. Both, along with the atom count, are now stripped after the division is assigned and before the response is returned.
app.py
CHANGED
|
@@ -1,432 +1,455 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""Open Materials Challenge โ ์ ์์ ์์ํ.
|
| 3 |
-
|
| 4 |
-
**์ด ์๋น์ค๊ฐ ํ์ง ์๋ ์ผ**: ์ฑ์ . ์คํ์ด์ค์๋ Materials Project ์๋ฃ๋ GPU ๋ ์๋ค.
|
| 5 |
-
์ ์ถ์ ๋ฐ์ ์์ฅ(๋น๊ณต๊ฐ ๋ฐ์ดํฐ์
)์ ์ ๊ณ , ์์ปค๊ฐ ์ฑ์ ํด ๋ฃ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค ๋ฟ์ด๋ค.
|
| 6 |
-
์ฌ๊ธฐ์ ๋ฌผ์ฑ์ ํ์ ํ๋ ์ฒํ๋ฉด ์ฐธ๊ฐ์๋ ๊ทผ๊ฑฐ ์๋ ๋ฐ๋ ค๋ฅผ ๋ฐ๊ฒ ๋๋ค.
|
| 7 |
-
|
| 8 |
-
์์ํ๋ TTL ๋น ํ ๋ฒ๋ง ๋ง๋ค์ด ๋ฏธ๋ฆฌ ์์ถํด ๋๊ณ , ์บ์๋ ๋จ์ผ ๊ฐฑ์ ์๋ก ๋๋ฆฐ๋ค.
|
| 9 |
-
์ฒซ ํ์ด์ง๋ async ๋ก ์๋นํ๊ณ , ํ๋ 500 ํ์ฉ ๋๋ ๊ทธ๋ฆฐ๋ค.
|
| 10 |
-
"""
|
| 11 |
-
import base64
|
| 12 |
-
import gzip
|
| 13 |
-
import hashlib
|
| 14 |
-
import hmac
|
| 15 |
-
import json
|
| 16 |
-
import os
|
| 17 |
-
import re
|
| 18 |
-
import secrets
|
| 19 |
-
import time
|
| 20 |
-
import urllib.parse
|
| 21 |
-
import urllib.request
|
| 22 |
-
import uuid
|
| 23 |
-
|
| 24 |
-
from fastapi import FastAPI, HTTPException, Request
|
| 25 |
-
from fastapi.middleware.gzip import GZipMiddleware
|
| 26 |
-
from fastapi.responses import (FileResponse, JSONResponse, RedirectResponse,
|
| 27 |
-
Response)
|
| 28 |
-
from pydantic import BaseModel
|
| 29 |
-
|
| 30 |
-
import gates
|
| 31 |
-
import seasons
|
| 32 |
-
import store
|
| 33 |
-
|
| 34 |
-
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 35 |
-
DATA = os.path.join(HERE, "data")
|
| 36 |
-
# ํ์ด์ง ํด๋ง ์ฃผ๊ธฐ(20์ด)๋ณด๋ค ํฌ๊ฒ ์ก๋๋ค.
|
| 37 |
-
CACHE = store.Cached(ttl=int(os.environ.get("OMC_CACHE_TTL", "60")))
|
| 38 |
-
SALT = os.environ.get("OMC_SALT", "omc-season1")
|
| 39 |
-
|
| 40 |
-
OAUTH_ID = os.environ.get("OAUTH_CLIENT_ID", "")
|
| 41 |
-
OAUTH_SECRET = os.environ.get("OAUTH_CLIENT_SECRET", "")
|
| 42 |
-
OAUTH_ISS = os.environ.get("OPENID_PROVIDER_URL", "https://huggingface.co")
|
| 43 |
-
SPACE_HOST = os.environ.get("SPACE_HOST", "")
|
| 44 |
-
COOKIE = "omc_session"
|
| 45 |
-
IN_FRAME = bool(SPACE_HOST)
|
| 46 |
-
COOKIE_KW = ({"samesite": "none", "secure": True} if IN_FRAME
|
| 47 |
-
else {"samesite": "lax", "secure": False})
|
| 48 |
-
SESSION_KEY = os.environ.get("OMC_SESSION_KEY") or secrets.token_hex(16)
|
| 49 |
-
|
| 50 |
-
DAILY_CAP = int(os.environ.get("OMC_DAILY_CAP", "30"))
|
| 51 |
-
CAP_TZ_OFFSET = 9 * 3600 # ์ฐธ๊ฐ์๊ฐ ๊ฐ์ ํ ํ๋ฃจ ๊ฒฝ๊ณ (KST)
|
| 52 |
-
|
| 53 |
-
app = FastAPI(title="Open Materials Challenge")
|
| 54 |
-
app.add_middleware(GZipMiddleware, minimum_size=1024)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
class Submission(BaseModel):
|
| 58 |
-
formula: str # ์กฐ์ฑ. ์: Li3YCl6, LiZr2(PO4)3
|
| 59 |
-
display_name: str # ์์ํ์ ํ์๋ ์ด๋ฆ
|
| 60 |
-
model_name: str = "" # ์ด๋ค ๋ชจ๋ธ์ด ์ ์ํ๋์ง. ์์ ๊ธฐ์ฌ
|
| 61 |
-
rationale: str = "" # ์ค๊ณ ๊ทผ๊ฑฐ (์ ํ)
|
| 62 |
-
structure: str = "" # CIF (์ ํ) - ๋ด๋ฉด ์ฑ์ ์ด ๋น ๋ฅด๊ณ ์ ํํด์ง๋ค
|
| 63 |
-
season: int = 0
|
| 64 |
-
# ์กฐ์ฑ์ ๊ณต๊ฐํ ์ง๋ ์ ์ถ์๊ฐ ์ ํ๋ค. ๋๋๋ฆด ์ ์๋ ์ชฝ์ด ๊ธฐ๋ณธ๊ฐ์ด์ด์ผ ํ๋ค -
|
| 65 |
-
# ํ ๋ฒ ๊ณต๊ฐ๋ ์กฐ์ฑ์ ๋๊ฐ์ ์ ์๊ณ , ๊ณต๊ฐ๋ ํนํ์ฑ์ ๊น๋๋ค.
|
| 66 |
-
visibility: str = "private"
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# ---------------------------------------------------------------- ์์ฅ ์ ๊ทผ
|
| 70 |
-
def _submissions(s):
|
| 71 |
-
return CACHE.get("ids:%d" % s["number"],
|
| 72 |
-
lambda: store.listdir(seasons.path(s, "submissions")))
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
def _board(s):
|
| 76 |
-
return CACHE.get("board:%d" % s["number"],
|
| 77 |
-
lambda: store.read(seasons.path(s, "leaderboard.json"),
|
| 78 |
-
default={}) or {})
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def public_id(key):
|
| 82 |
-
return "OMC-" + hashlib.sha256((SALT + key).encode()).hexdigest()[:10].upper()
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def mask(rec):
|
| 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 |
-
for
|
| 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 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Open Materials Challenge โ ์ ์์ ์์ํ.
|
| 3 |
+
|
| 4 |
+
**์ด ์๋น์ค๊ฐ ํ์ง ์๋ ์ผ**: ์ฑ์ . ์คํ์ด์ค์๋ Materials Project ์๋ฃ๋ GPU ๋ ์๋ค.
|
| 5 |
+
์ ์ถ์ ๋ฐ์ ์์ฅ(๋น๊ณต๊ฐ ๋ฐ์ดํฐ์
)์ ์ ๊ณ , ์์ปค๊ฐ ์ฑ์ ํด ๋ฃ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค ๋ฟ์ด๋ค.
|
| 6 |
+
์ฌ๊ธฐ์ ๋ฌผ์ฑ์ ํ์ ํ๋ ์ฒํ๋ฉด ์ฐธ๊ฐ์๋ ๊ทผ๊ฑฐ ์๋ ๋ฐ๋ ค๋ฅผ ๋ฐ๊ฒ ๋๋ค.
|
| 7 |
+
|
| 8 |
+
์์ํ๋ TTL ๋น ํ ๋ฒ๋ง ๋ง๋ค์ด ๋ฏธ๋ฆฌ ์์ถํด ๋๊ณ , ์บ์๋ ๋จ์ผ ๊ฐฑ์ ์๋ก ๋๋ฆฐ๋ค.
|
| 9 |
+
์ฒซ ํ์ด์ง๋ async ๋ก ์๋นํ๊ณ , ํ๋ 500 ํ์ฉ ๋๋ ๊ทธ๋ฆฐ๋ค.
|
| 10 |
+
"""
|
| 11 |
+
import base64
|
| 12 |
+
import gzip
|
| 13 |
+
import hashlib
|
| 14 |
+
import hmac
|
| 15 |
+
import json
|
| 16 |
+
import os
|
| 17 |
+
import re
|
| 18 |
+
import secrets
|
| 19 |
+
import time
|
| 20 |
+
import urllib.parse
|
| 21 |
+
import urllib.request
|
| 22 |
+
import uuid
|
| 23 |
+
|
| 24 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 25 |
+
from fastapi.middleware.gzip import GZipMiddleware
|
| 26 |
+
from fastapi.responses import (FileResponse, JSONResponse, RedirectResponse,
|
| 27 |
+
Response)
|
| 28 |
+
from pydantic import BaseModel
|
| 29 |
+
|
| 30 |
+
import gates
|
| 31 |
+
import seasons
|
| 32 |
+
import store
|
| 33 |
+
|
| 34 |
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 35 |
+
DATA = os.path.join(HERE, "data")
|
| 36 |
+
# ํ์ด์ง ํด๋ง ์ฃผ๊ธฐ(20์ด)๋ณด๋ค ํฌ๊ฒ ์ก๋๋ค.
|
| 37 |
+
CACHE = store.Cached(ttl=int(os.environ.get("OMC_CACHE_TTL", "60")))
|
| 38 |
+
SALT = os.environ.get("OMC_SALT", "omc-season1")
|
| 39 |
+
|
| 40 |
+
OAUTH_ID = os.environ.get("OAUTH_CLIENT_ID", "")
|
| 41 |
+
OAUTH_SECRET = os.environ.get("OAUTH_CLIENT_SECRET", "")
|
| 42 |
+
OAUTH_ISS = os.environ.get("OPENID_PROVIDER_URL", "https://huggingface.co")
|
| 43 |
+
SPACE_HOST = os.environ.get("SPACE_HOST", "")
|
| 44 |
+
COOKIE = "omc_session"
|
| 45 |
+
IN_FRAME = bool(SPACE_HOST)
|
| 46 |
+
COOKIE_KW = ({"samesite": "none", "secure": True} if IN_FRAME
|
| 47 |
+
else {"samesite": "lax", "secure": False})
|
| 48 |
+
SESSION_KEY = os.environ.get("OMC_SESSION_KEY") or secrets.token_hex(16)
|
| 49 |
+
|
| 50 |
+
DAILY_CAP = int(os.environ.get("OMC_DAILY_CAP", "30"))
|
| 51 |
+
CAP_TZ_OFFSET = 9 * 3600 # ์ฐธ๊ฐ์๊ฐ ๊ฐ์ ํ ํ๋ฃจ ๊ฒฝ๊ณ (KST)
|
| 52 |
+
|
| 53 |
+
app = FastAPI(title="Open Materials Challenge")
|
| 54 |
+
app.add_middleware(GZipMiddleware, minimum_size=1024)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class Submission(BaseModel):
|
| 58 |
+
formula: str # ์กฐ์ฑ. ์: Li3YCl6, LiZr2(PO4)3
|
| 59 |
+
display_name: str # ์์ํ์ ํ์๋ ์ด๋ฆ
|
| 60 |
+
model_name: str = "" # ์ด๋ค ๋ชจ๋ธ์ด ์ ์ํ๋์ง. ์์ ๊ธฐ์ฌ
|
| 61 |
+
rationale: str = "" # ์ค๊ณ ๊ทผ๊ฑฐ (์ ํ)
|
| 62 |
+
structure: str = "" # CIF (์ ํ) - ๋ด๋ฉด ์ฑ์ ์ด ๋น ๋ฅด๊ณ ์ ํํด์ง๋ค
|
| 63 |
+
season: int = 0
|
| 64 |
+
# ์กฐ์ฑ์ ๊ณต๊ฐํ ์ง๋ ์ ์ถ์๊ฐ ์ ํ๋ค. ๋๋๋ฆด ์ ์๋ ์ชฝ์ด ๊ธฐ๋ณธ๊ฐ์ด์ด์ผ ํ๋ค -
|
| 65 |
+
# ํ ๋ฒ ๊ณต๊ฐ๋ ์กฐ์ฑ์ ๋๊ฐ์ ์ ์๊ณ , ๊ณต๊ฐ๋ ํนํ์ฑ์ ๊น๋๋ค.
|
| 66 |
+
visibility: str = "private"
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# ---------------------------------------------------------------- ์์ฅ ์ ๊ทผ
|
| 70 |
+
def _submissions(s):
|
| 71 |
+
return CACHE.get("ids:%d" % s["number"],
|
| 72 |
+
lambda: store.listdir(seasons.path(s, "submissions")))
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def _board(s):
|
| 76 |
+
return CACHE.get("board:%d" % s["number"],
|
| 77 |
+
lambda: store.read(seasons.path(s, "leaderboard.json"),
|
| 78 |
+
default={}) or {})
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def public_id(key):
|
| 82 |
+
return "OMC-" + hashlib.sha256((SALT + key).encode()).hexdigest()[:10].upper()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def mask(rec):
|
| 86 |
+
"""๋น๊ณต๊ฐ ์ ์ถ์์ ์กฐ์ฑ์ ๊ฐ๋ฆฐ๋ค. ์์ ์ข
๋ฅ๊น์ง๋ ๋ณด์ฌ์ค๋ค - ๊ทธ๊ฒ๋ง์ผ๋ก๋
|
| 87 |
+
์กฐ์ฑ์ ๋๋๋ฆด ์ ์๊ณ , ์ด๋ค ํํ๊ณ๊ฐ ์์์ธ์ง๋ ๋ํ์ ๊ณต๊ฐ ์ง์์ด์ด์ผ ํ๋ค.
|
| 88 |
+
|
| 89 |
+
**์ฌ๊ธฐ์๋ ํ์์ฉ ๋ฌธ์์ด๋ง ๋ฐ๊พผ๋ค.** ์ค์ ์กฐ์ฑ์ ์๋ ๊ตฌ์ญ ๊ณ์ฐ์ ํ์ํ๋ฏ๋ก
|
| 90 |
+
ํ์ดํ๋ผ์ธ ์์์๋ ์ด๋ ค ๋๊ณ , ์๋ต์ผ๋ก ๋๊ฐ๊ธฐ ์ง์ ์ strip_masked() ๋ก ๊ฑท์ด๋ธ๋ค.
|
| 91 |
+
ํ์ชฝ๋ง ๊ฐ๋ฆฌ๊ณ ๋ค๋ฅธ ์ชฝ์ ๋จ๊ธฐ๋ฉด ๊ฐ๋ฆฐ ์๋ฏธ๊ฐ ์๋ค.
|
| 92 |
+
"""
|
| 93 |
+
out = dict(rec)
|
| 94 |
+
if rec.get("visibility") != "public":
|
| 95 |
+
comp = rec.get("composition") or {}
|
| 96 |
+
out["formula"] = "ยท".join(sorted(comp)) if comp else "๋น๊ณต๊ฐ"
|
| 97 |
+
out["masked"] = True
|
| 98 |
+
out.pop("structure", None)
|
| 99 |
+
return out
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# ๋น๊ณต๊ฐ ํญ๋ชฉ์์ ์๋ต ์ง์ ์ ์ง์์ผ ํ๋ ๊ฒ๋ค.
|
| 103 |
+
# composition ์ ํํ ์กฐ์ฑ๋น ๊ทธ ์์ฒด
|
| 104 |
+
# measured ์์ฑ์๋์ง ๋ฑ ๊ณ์ฐ๊ฐ. ์์ ์งํฉ๊ณผ ํจ๊ป ๋๋ฉด ํ๋ณด ์กฐ์ฑ์ ํ์ด
|
| 105 |
+
# ๋ง์ถฐ๋ณผ ์ ์์ด ์ฌ์ค์ ์กฐ์ฑ์ ์๋ ค์ฃผ๋ ๊ฒ๊ณผ ๊ฐ๋ค
|
| 106 |
+
# n_atoms ํํ์ ๋จ์ ์์ ์. ์กฐ์ฑ ํ๋ณด๋ฅผ ํฌ๊ฒ ์ขํ๋ค
|
| 107 |
+
_MASK_DROP = ("composition", "measured", "n_atoms", "structure", "rationale")
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def strip_masked(entries):
|
| 111 |
+
"""๋ง์คํน๋ ํญ๋ชฉ์์ ์กฐ์ฑ์ ๋๋๋ฆด ์ ์๋ ํ๋๋ฅผ ์ ๋ถ ์์ค๋ค."""
|
| 112 |
+
for e in entries:
|
| 113 |
+
if e.get("masked"):
|
| 114 |
+
for k in _MASK_DROP:
|
| 115 |
+
e.pop(k, None)
|
| 116 |
+
return entries
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# ---------------------------------------------------------------- ์ธ์
|
| 120 |
+
def _sign(payload):
|
| 121 |
+
raw = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
| 122 |
+
sig = hmac.new(SESSION_KEY.encode(), raw.encode(), hashlib.sha256).hexdigest()[:32]
|
| 123 |
+
return raw + "." + sig
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def _unsign(token):
|
| 127 |
+
try:
|
| 128 |
+
raw, sig = (token or "").rsplit(".", 1)
|
| 129 |
+
except ValueError:
|
| 130 |
+
return None
|
| 131 |
+
good = hmac.new(SESSION_KEY.encode(), raw.encode(), hashlib.sha256).hexdigest()[:32]
|
| 132 |
+
if not hmac.compare_digest(sig, good):
|
| 133 |
+
return None
|
| 134 |
+
pad = "=" * (-len(raw) % 4)
|
| 135 |
+
try:
|
| 136 |
+
return json.loads(base64.urlsafe_b64decode(raw + pad))
|
| 137 |
+
except Exception:
|
| 138 |
+
return None
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def current_user(request: Request):
|
| 142 |
+
return _unsign(request.cookies.get(COOKIE))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def _redirect_uri(request: Request):
|
| 146 |
+
if SPACE_HOST:
|
| 147 |
+
return "https://%s/auth/callback" % SPACE_HOST
|
| 148 |
+
return str(request.base_url).rstrip("/") + "/auth/callback"
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# ---------------------------------------------------------------- ํ๋ฉด
|
| 152 |
+
@app.get("/")
|
| 153 |
+
async def index():
|
| 154 |
+
# ์ ์ ํ์ด์ง๋ ์์ฒญ ์ค๋ ๋ํ์ ํ์ง ์๊ฒ async ๋ก ๋๋ค.
|
| 155 |
+
return FileResponse(os.path.join(HERE, "index.html"))
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
@app.get("/api/me")
|
| 159 |
+
def me(request: Request):
|
| 160 |
+
u = current_user(request)
|
| 161 |
+
return {"signed_in": bool(u), "user": u,
|
| 162 |
+
"oauth_configured": bool(OAUTH_ID and OAUTH_SECRET)}
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
@app.get("/login")
|
| 166 |
+
def login(request: Request):
|
| 167 |
+
if not (OAUTH_ID and OAUTH_SECRET):
|
| 168 |
+
raise HTTPException(503, "OAuth ๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 169 |
+
state = secrets.token_urlsafe(16)
|
| 170 |
+
q = urllib.parse.urlencode({
|
| 171 |
+
"client_id": OAUTH_ID, "redirect_uri": _redirect_uri(request),
|
| 172 |
+
"response_type": "code", "scope": "openid profile", "state": state})
|
| 173 |
+
r = RedirectResponse("%s/oauth/authorize?%s" % (OAUTH_ISS, q))
|
| 174 |
+
r.set_cookie("omc_state", state, max_age=600, httponly=True, **COOKIE_KW)
|
| 175 |
+
return r
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
@app.get("/auth/callback")
|
| 179 |
+
def callback(request: Request, code: str = "", state: str = ""):
|
| 180 |
+
if not code or state != request.cookies.get("omc_state"):
|
| 181 |
+
return RedirectResponse("/?auth=failed")
|
| 182 |
+
body = urllib.parse.urlencode({
|
| 183 |
+
"client_id": OAUTH_ID, "client_secret": OAUTH_SECRET,
|
| 184 |
+
"grant_type": "authorization_code", "code": code,
|
| 185 |
+
"redirect_uri": _redirect_uri(request)}).encode()
|
| 186 |
+
try:
|
| 187 |
+
req = urllib.request.Request("%s/oauth/token" % OAUTH_ISS, data=body)
|
| 188 |
+
with urllib.request.urlopen(req, timeout=30) as x:
|
| 189 |
+
tok = json.loads(x.read())
|
| 190 |
+
req = urllib.request.Request(
|
| 191 |
+
"%s/oauth/userinfo" % OAUTH_ISS,
|
| 192 |
+
headers={"Authorization": "Bearer " + tok["access_token"]})
|
| 193 |
+
with urllib.request.urlopen(req, timeout=30) as x:
|
| 194 |
+
info = json.loads(x.read())
|
| 195 |
+
except Exception:
|
| 196 |
+
return RedirectResponse("/?auth=failed")
|
| 197 |
+
u = {"sub": info.get("sub"), "name": info.get("preferred_username") or info.get("name"),
|
| 198 |
+
"picture": info.get("picture")}
|
| 199 |
+
r = RedirectResponse("/?auth=ok")
|
| 200 |
+
r.set_cookie(COOKIE, _sign(u), max_age=30 * 86400, httponly=True, **COOKIE_KW)
|
| 201 |
+
r.delete_cookie("omc_state")
|
| 202 |
+
return r
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
@app.get("/logout")
|
| 206 |
+
def logout():
|
| 207 |
+
r = RedirectResponse("/")
|
| 208 |
+
r.delete_cookie(COOKIE)
|
| 209 |
+
return r
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
@app.get("/api/seasons")
|
| 213 |
+
def season_list():
|
| 214 |
+
return {"seasons": seasons.all_public()}
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
@app.get("/api/season")
|
| 218 |
+
def season(request: Request):
|
| 219 |
+
return seasons.public(seasons.get(request.query_params.get("season")))
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
# ---------------------------------------------------------------- ์ ์ถ
|
| 223 |
+
@app.post("/api/submit")
|
| 224 |
+
def submit(s: Submission, request: Request):
|
| 225 |
+
season = seasons.get(s.season or None)
|
| 226 |
+
if not season.get("open"):
|
| 227 |
+
raise HTTPException(400, "์ด ์์ฆ์ ์ ์ถ์ ๋ฐ์ง ์์ต๋๋ค.")
|
| 228 |
+
user = current_user(request)
|
| 229 |
+
if (OAUTH_ID and OAUTH_SECRET) and not user:
|
| 230 |
+
raise HTTPException(401, "์ ์ถํ๋ ค๋ฉด Hugging Face ๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค.")
|
| 231 |
+
|
| 232 |
+
g = seasons.gate(season)
|
| 233 |
+
ok, why, info = gates.check(s.formula, cif=s.structure or None,
|
| 234 |
+
require_elements=g.get("require_elements", ("Li",)),
|
| 235 |
+
max_atoms=g.get("max_atoms", 60))
|
| 236 |
+
if not ok:
|
| 237 |
+
raise HTTPException(400, why)
|
| 238 |
+
|
| 239 |
+
comp = info["composition"]
|
| 240 |
+
# ์ด์ ๋ **๊ธฐ์ฝ ์กฐ์ฑ**์ผ๋ก ๋ง๋ ๋ค. Li3YCl6 ์ Li9Y3Cl18 ์ ๊ฐ์ ๋ฌผ์ง์ด๋ค.
|
| 241 |
+
# ์๋ณธ ์กฐ์ฑ์ผ๋ก ๋ง๋ค๋ฉด ๋ฐฐ์๋ง ๋ฐ๊ฟ ๊ฐ์ ๋ฌผ์ง์ ๋ช ๋ฒ์ด๊ณ ์ฌ๋ฆด ์ ์๋ค.
|
| 242 |
+
red = info.get("reduced") or comp
|
| 243 |
+
key = "-".join("%s%g" % (k, red[k]) for k in sorted(red))
|
| 244 |
+
cid = public_id(key)
|
| 245 |
+
|
| 246 |
+
ids = _submissions(season)
|
| 247 |
+
if cid in ids:
|
| 248 |
+
raise HTTPException(409, "์ด๋ฏธ ์ ์ถ๋ ์กฐ์ฑ์
๋๋ค (%s)." % cid)
|
| 249 |
+
|
| 250 |
+
if user:
|
| 251 |
+
today = int((time.time() + CAP_TZ_OFFSET) // 86400)
|
| 252 |
+
mine = CACHE.get("cap:%s:%d:%d" % (user.get("sub"), season["number"], today),
|
| 253 |
+
lambda: 0)
|
| 254 |
+
if mine >= DAILY_CAP:
|
| 255 |
+
raise HTTPException(429, "ํ๋ฃจ ์ ์ถ ์ํ(%d๊ฑด)์ ๋๋ฌํ์ต๋๋ค." % DAILY_CAP)
|
| 256 |
+
|
| 257 |
+
rec = {"candidate_id": cid, "formula": s.formula.strip(),
|
| 258 |
+
"composition": comp, "n_atoms": info["n_atoms"],
|
| 259 |
+
"display_name": (s.display_name or "").strip()[:60],
|
| 260 |
+
"model_name": (s.model_name or "").strip()[:80],
|
| 261 |
+
"rationale": (s.rationale or "").strip()[:1000],
|
| 262 |
+
"visibility": "public" if s.visibility == "public" else "private",
|
| 263 |
+
"has_structure": bool(s.structure),
|
| 264 |
+
"hf_user": (user or {}).get("name", ""),
|
| 265 |
+
"hf_sub": (user or {}).get("sub", ""),
|
| 266 |
+
"season": season["number"],
|
| 267 |
+
"submitted_at": int(time.time()),
|
| 268 |
+
"uuid": uuid.uuid4().hex}
|
| 269 |
+
store.write(seasons.path(season, "submissions/%s.json" % cid), rec,
|
| 270 |
+
summary="submit %s" % cid)
|
| 271 |
+
if s.structure:
|
| 272 |
+
# ๊ตฌ์กฐ๋ ๋ฐ๋ก ๋๋ค. ๋ชฉ๋ก์ ํ์ ๋ ๊ฐ์ด ๋๋ ค์ค๋ฉด ์์ํ๊ฐ ๋ฌด๊ฑฐ์์ง๋ค.
|
| 273 |
+
store.write(seasons.path(season, "structures/%s.json" % cid),
|
| 274 |
+
{"candidate_id": cid, "cif": s.structure[:400_000]},
|
| 275 |
+
summary="structure %s" % cid)
|
| 276 |
+
CACHE.drop("ids:%d" % season["number"])
|
| 277 |
+
|
| 278 |
+
scored = set((_board(season).get("entries") or {}).keys())
|
| 279 |
+
ahead = len([i for i in _submissions(season) if i not in scored])
|
| 280 |
+
return {"accepted": True, "candidate_id": cid, "queue_position": ahead,
|
| 281 |
+
"note": "์ฑ์ ์ ๋ณ๋ ๊ณ์ฐ ์์
์ผ๋ก ์ฒ๋ฆฌ๋๋ฉฐ ์๋ฃ๊น์ง ์๊ฐ์ด ๊ฑธ๋ฆฝ๋๋ค."}
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
# ---------------------------------------------------------------- ์์ํ
|
| 285 |
+
def _anchors(season):
|
| 286 |
+
p = os.path.join(DATA, season.get("anchors") or "")
|
| 287 |
+
if not os.path.exists(p):
|
| 288 |
+
return []
|
| 289 |
+
out = []
|
| 290 |
+
for a in json.load(open(p, encoding="utf-8")):
|
| 291 |
+
if not a.get("admitted"):
|
| 292 |
+
continue
|
| 293 |
+
out.append({"candidate_id": a["label"], "is_anchor": True,
|
| 294 |
+
"display_name": "๊ธฐ์ค๋ฌผ์ง", "model_name": "",
|
| 295 |
+
"formula": a.get("formula", ""),
|
| 296 |
+
"total": a.get("total"), "total_max": a.get("total_max"),
|
| 297 |
+
"tier": a.get("tier", 1),
|
| 298 |
+
"axes": {k: v["points"] for k, v in (a.get("axes") or {}).items()},
|
| 299 |
+
"detail": {k: v["detail"] for k, v in (a.get("axes") or {}).items()},
|
| 300 |
+
"note": a.get("note", "")})
|
| 301 |
+
return out
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
# ์์ด์จ ๊ณจ๊ฒฉ์ด ๋ฌผ์ง์ ์ฑ์ง์ ํฌ๊ฒ ๊ฐ๋ฅธ๋ค. ๊ฐ์ ๊ณ์ด๋ผ๋ฆฌ ๊ฒฌ์ฃผ์ด์ผ ํ๋ณด์ ์์น๊ฐ ์ฝํ๋ค.
|
| 305 |
+
# ๋ถ๋ฌธ์ ๋๋๋ **์ ์๋ ๊ทธ๋๋ก ๋๋ค** - ์ด๋ค ์ ์ถ๋ ๋ฌดํจ๊ฐ ๋์ง ์๋๋ค.
|
| 306 |
+
_ANION_ORDER = ["F", "Cl", "Br", "I", "S", "Se", "O", "N", "P", "H"]
|
| 307 |
+
DIVISIONS = {"F": "fluoride", "Cl": "halide", "Br": "halide", "I": "halide",
|
| 308 |
+
"S": "sulfide", "Se": "sulfide", "O": "oxide", "N": "nitride",
|
| 309 |
+
"P": "oxide", "H": "hydride"}
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def division_of(comp):
|
| 313 |
+
"""๊ณจ๊ฒฉ ์์ด์จ์ผ๋ก ๋ถ๋ฌธ์ ์ ํ๋ค.
|
| 314 |
+
|
| 315 |
+
์์ฌ ์์ผ๋ฉด **๊ฐ์ฅ ๋ง์ ๊ฒ**์ ๊ณ ๋ฅธ๋ค. Li6PS5Cl ์ S ๊ฐ 5, Cl ์ด 1 ์ด๋ฏ๋ก ํฉํ๋ฌผ์ด๊ณ ,
|
| 316 |
+
์ค์ ๋ก ์์ง๋ก๋ค์ดํธ๋ ํฉํ๋ฌผ๋ก ๋ถ๋ฅ๋๋ค. ๊ฐ์๊ฐ ๊ฐ์ผ๋ฉด ์ ์์์์ ์์ ๊ฒ์ ์ด๋ค.
|
| 317 |
+
"""
|
| 318 |
+
if not comp:
|
| 319 |
+
return "other"
|
| 320 |
+
best, bestn = None, -1
|
| 321 |
+
for el in _ANION_ORDER:
|
| 322 |
+
n = comp.get(el, 0)
|
| 323 |
+
if n > bestn:
|
| 324 |
+
best, bestn = el, n
|
| 325 |
+
if bestn <= 0:
|
| 326 |
+
return "other"
|
| 327 |
+
return DIVISIONS.get(best, "other")
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def _leaderboard_body(season):
|
| 331 |
+
rows = [mask(r) for r in (_board(season).get("entries") or {}).values()]
|
| 332 |
+
anchors = _anchors(season)
|
| 333 |
+
merged = rows + anchors
|
| 334 |
+
# ์ ์๊ฐ ์๋ ๊ฒ์ ๋งจ ๋ค๋ก. ๊ทธ ์์์๋ ๋ธ ์์๋ฅผ ์งํจ๋ค.
|
| 335 |
+
# ์ ์ถ์ด ์ฑ์์ง ๊ฒ๋ถํฐ, ๊ทธ ์์์ ์ด์ ์. ์ถ์ด ๋น ๊ฒ์ ๋ค๋ก - ์ด์ ์ด ์์ด๋
|
| 336 |
+
# ๋ง์ ์ด ๋ฌ๋ผ ๊ฐ์ ์ค์ ์ธ์ธ ์ ์๋ค.
|
| 337 |
+
merged.sort(key=lambda e: (e.get("tier", 1),
|
| 338 |
+
1 if (e.get("total") is None or e.get("held_axes")) else 0,
|
| 339 |
+
-(e.get("total") if e.get("total") is not None else 0),
|
| 340 |
+
e.get("submitted_at") or 0))
|
| 341 |
+
n = 0
|
| 342 |
+
div_n = {}
|
| 343 |
+
for e in merged:
|
| 344 |
+
comp = e.get("composition") or {}
|
| 345 |
+
e["division"] = division_of(comp)
|
| 346 |
+
# ๋ฑ์๋ **์ ์ถ์ด ์ฑ์์ง ๊ฒ**์๋ง ์ค๋ค. ์ถ์ด ํ๋๋ผ๋ ๋น๋ฉด ๋ง์ ์ด ๋ฌ๋ผ ์ด์ ์
|
| 347 |
+
# ๋๋ํ ์ธ์ธ ์ ์๋ค - ์ ์๋ ๋จ๊ธฐ๋ ์์์๋ ๋ฃ์ง ์๋๋ค.
|
| 348 |
+
unscored = e.get("total") is None or bool(e.get("held_axes"))
|
| 349 |
+
if e.get("is_anchor") or e.get("tier", 1) != 1 or unscored:
|
| 350 |
+
e["rank"] = None # ๊ธฐ์ค๋ฌผ์ง์ ์ฒ๋๋ฅผ ํ์ํ ๋ฟ ๋ฑ์๋ฅผ ๊ฐ์ง ์๋๋ค
|
| 351 |
+
e["div_rank"] = None
|
| 352 |
+
else:
|
| 353 |
+
n += 1
|
| 354 |
+
e["rank"] = n
|
| 355 |
+
# ๋ถ๋ฌธ ์์๋ ์ ์ฒด ์์์ ๋ฐ๋ก ๋งค๊ธด๋ค. ์ ์๋ ์๋์ง ์๋๋ค.
|
| 356 |
+
d = e["division"]
|
| 357 |
+
div_n[d] = div_n.get(d, 0) + 1
|
| 358 |
+
e["div_rank"] = div_n[d]
|
| 359 |
+
tally = {}
|
| 360 |
+
for e in merged:
|
| 361 |
+
if e.get("is_anchor"):
|
| 362 |
+
continue
|
| 363 |
+
tally[e["division"]] = tally.get(e["division"], 0) + 1
|
| 364 |
+
n_held = sum(1 for e in merged
|
| 365 |
+
if not e.get("is_anchor")
|
| 366 |
+
and (e.get("total") is None or e.get("held_axes")))
|
| 367 |
+
strip_masked(merged) # ๊ตฌ์ญ๊น์ง ๋งค๊ธด ๋ค, ์๋ต์ผ๋ก ๋๊ฐ๊ธฐ ์ง์ ์ ๊ฑท์ด๋ธ๋ค
|
| 368 |
+
return {"season": seasons.public(season), "entries": merged,
|
| 369 |
+
"divisions": tally,
|
| 370 |
+
"counts": {"scored": len(rows) - n_held, "held": n_held,
|
| 371 |
+
"anchors": len(anchors)}}
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def _leaderboard_bytes(season):
|
| 375 |
+
raw = json.dumps(_leaderboard_body(season), ensure_ascii=False).encode("utf-8")
|
| 376 |
+
return raw, gzip.compress(raw, 6)
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
@app.get("/api/leaderboard")
|
| 380 |
+
def leaderboard(request: Request):
|
| 381 |
+
"""์๋ต์ ์์ฆ์๋ง ์์กดํ๋ฏ๋ก TTL ๋น ํ ๋ฒ๋ง ๋ง๋ค๊ณ ํ ๋ฒ๋ง ์์ถํ๋ค."""
|
| 382 |
+
season = seasons.get(request.query_params.get("season"))
|
| 383 |
+
raw, gz = CACHE.get("lbresp:%d" % season["number"],
|
| 384 |
+
lambda: _leaderboard_bytes(season))
|
| 385 |
+
if "gzip" in (request.headers.get("accept-encoding") or ""):
|
| 386 |
+
return Response(content=gz, media_type="application/json",
|
| 387 |
+
headers={"Content-Encoding": "gzip", "Vary": "Accept-Encoding"})
|
| 388 |
+
return Response(content=raw, media_type="application/json",
|
| 389 |
+
headers={"Vary": "Accept-Encoding"})
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
_FILLER = {"ai", "model", "llm", "the", "v", "ver", "version", "latest", "preview",
|
| 393 |
+
"chat", "instruct", "it", "api", "official", "new"}
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
def canon_model(name):
|
| 397 |
+
"""ํ๊ธฐ๊ฐ ๊ฐ๋ฆฐ ๊ฐ์ ๋ชจ๋ธ์ ํ๋๋ก ๋ฌถ๋๋ค.
|
| 398 |
+
|
| 399 |
+
'Anthropic Claude Opus 5' ์ 'claude-opus-5' ๋ ๊ฐ์ ๊ฒ์ด๊ณ , ๋ฐ๋ก ์ธ๋ฉด ๋ ๋ค
|
| 400 |
+
์ค์ ๋ณด๋ค ์ ๊ฒ ์ฐ์ธ ๊ฒ์ฒ๋ผ ๋ณด์ธ๋ค. ๋ค๋ง ๋ณ์ข
์ ๊ตฌ๋ถํ๋ ๋ฑ๋ง(Pro/Flash ๋ฑ)์
|
| 401 |
+
์ง์ฐ์ง ์๋๋ค - ๊ทธ๊ฒ๊น์ง ๋ญ์น๋ฉด ๋ค๋ฅธ ๋ชจ๋ธ์ด ํ ์นธ์ ๋ค์ด๊ฐ๋ค.
|
| 402 |
+
"""
|
| 403 |
+
s = re.sub(r"[^a-z0-9]+", " ", (name or "").lower()).strip()
|
| 404 |
+
toks = [t for t in s.split() if t and t not in _FILLER]
|
| 405 |
+
return " ".join(toks) or (name or "").strip().lower()
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
@app.get("/api/models")
|
| 409 |
+
def models(request: Request):
|
| 410 |
+
"""๋ชจ๋ธ๋ณ ์ฑ์ : ์ด๋ ๋ชจ๋ธ์ด ๊ฐ์ฅ ์ข์ ํ๋ณด๋ฅผ ๋๋๊ฐ, ๊ทธ๋ฆฌ๊ณ ์ผ๋ง๋ ์ฐ์ด๋๊ฐ.
|
| 411 |
+
|
| 412 |
+
๋ ์๋ฅผ **๋ฐ๋ก** ๋ธ๋ค. ๋ง์ด ์ฐ์ธ๋ค๊ณ ์ํ๋ ๊ฒ์ด ์๋๋ค - ๋ค๋ค ์ง๋ ๋ชจ๋ธ์ ์ ์์
|
| 413 |
+
๋ฌด๊ดํ๊ฒ ์ ์ถ ์๊ฐ ์์ด๊ณ , ์ธ ๋ฒ ์ฐ์ธ ๋ชจ๋ธ์ด 1์ ํ๋ณด๋ฅผ ๊ฐ๊ณ ์์ ์ ์๋ค.
|
| 414 |
+
ํ๊ท ๋ง ๋ด๋ฉด ๋ ๋ค ๊ฐ๋ ค์ง๋ค. ๊ทธ๋์ ์ต๊ณ ยทํ๊ท ยท๊ฑด์๋ฅผ ๋๋ํ ๋๋ค.
|
| 415 |
+
๊ธฐ์ค๋ฌผ์ง์ ์ ์ธํ๋ค - ์ฐธ๊ฐ์์ ๋ชจ๋ธ์ด ๋ง๋ ๊ฒ์ด ์๋๋ค.
|
| 416 |
+
"""
|
| 417 |
+
season = seasons.get(request.query_params.get("season"))
|
| 418 |
+
agg = {}
|
| 419 |
+
for r in (_board(season).get("entries") or {}).values():
|
| 420 |
+
if r.get("total") is None:
|
| 421 |
+
continue
|
| 422 |
+
name = (r.get("model_name") or "").strip() or "๋ฏธ๊ธฐ์ฌ / unspecified"
|
| 423 |
+
key = canon_model(name)
|
| 424 |
+
a = agg.setdefault(key, {"n": 0, "best": None, "sum": 0.0,
|
| 425 |
+
"top_candidate": None, "spellings": {}})
|
| 426 |
+
a["spellings"][name] = a["spellings"].get(name, 0) + 1
|
| 427 |
+
a["n"] += 1
|
| 428 |
+
a["sum"] += r["total"]
|
| 429 |
+
if a["best"] is None or r["total"] > a["best"]:
|
| 430 |
+
a["best"] = r["total"]
|
| 431 |
+
a["top_candidate"] = r.get("candidate_id")
|
| 432 |
+
out = []
|
| 433 |
+
for k, a in agg.items():
|
| 434 |
+
# ์ฐธ๊ฐ์๋ค์ด ์ค์ ๋ก ๊ฐ์ฅ ๋ง์ด ์ด ํ๊ธฐ๋ฅผ ์ด๋ฆ์ผ๋ก ์ผ๋๋ค
|
| 435 |
+
label = max(a["spellings"].items(), key=lambda kv: kv[1])[0]
|
| 436 |
+
out.append({"model": label, "n": a["n"], "best": round(a["best"], 2),
|
| 437 |
+
"mean": round(a["sum"] / a["n"], 2),
|
| 438 |
+
"top_candidate": a["top_candidate"]})
|
| 439 |
+
out.sort(key=lambda x: (-x["best"], -x["n"]))
|
| 440 |
+
return {"models": out, "counts": {"models": len(out),
|
| 441 |
+
"entries": sum(m["n"] for m in out)}}
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
@app.get("/api/queue")
|
| 445 |
+
def queue(request: Request):
|
| 446 |
+
season = seasons.get(request.query_params.get("season"))
|
| 447 |
+
ids = _submissions(season)
|
| 448 |
+
scored = set((_board(season).get("entries") or {}).keys())
|
| 449 |
+
return {"queued": len([i for i in ids if i not in scored]),
|
| 450 |
+
"scored": len(scored), "total": len(ids)}
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
@app.get("/api/health")
|
| 454 |
+
def health():
|
| 455 |
+
return {"ok": True, "seasons": [s["number"] for s in seasons.SEASONS]}
|