Spaces:
Running
Running
arcade: app
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
-
"""κ΄λ¬Έ
|
| 3 |
|
| 4 |
π΄ μ μ μ Space λμ€ν¬κ° μλλΌ **λ°μ΄ν°μ
repo** μ μ΄λ€. Space λ μ¬μμνλ©΄ λμ€ν¬κ° λ μκ°λ€.
|
| 5 |
"""
|
|
@@ -12,7 +12,8 @@ DS = os.environ.get("RECORD_DATASET", "FINAL-Bench/gate-tetris-record")
|
|
| 12 |
TOK = os.environ.get("HF_TOKEN")
|
| 13 |
api = HfApi(token=TOK)
|
| 14 |
LOCK = threading.Lock()
|
| 15 |
-
BLANK = {"matches": 0, "wins": {"plain": 0, "jev": 0, "ztc": 0},
|
|
|
|
| 16 |
|
| 17 |
app = FastAPI()
|
| 18 |
HTML = open(os.path.join(os.path.dirname(__file__), "index.html"), encoding="utf-8").read()
|
|
@@ -24,13 +25,15 @@ def load():
|
|
| 24 |
force_download=True)
|
| 25 |
with open(p, encoding="utf-8") as f:
|
| 26 |
t = json.load(f)
|
| 27 |
-
for k, v in BLANK.items():
|
| 28 |
-
t.setdefault(k, v if not isinstance(v, dict) else dict(v))
|
| 29 |
-
for k in ("plain", "jev", "ztc"):
|
| 30 |
-
t["wins"].setdefault(k, 0)
|
| 31 |
-
return t
|
| 32 |
except Exception:
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
def save(t):
|
|
@@ -56,23 +59,25 @@ async def post_record(req: Request):
|
|
| 56 |
win = body.get("winner")
|
| 57 |
if win not in ("plain", "jev", "ztc"):
|
| 58 |
return JSONResponse({"error": "bad winner"}, status_code=400)
|
| 59 |
-
|
|
|
|
| 60 |
mode = "human" if body.get("mode") == "human" else "auto"
|
| 61 |
-
|
| 62 |
-
zc = float(body.get("ztcCost") or 0)
|
| 63 |
with LOCK:
|
| 64 |
t = load()
|
| 65 |
-
t["matches"] =
|
| 66 |
-
t["wins"][win] =
|
| 67 |
-
t["
|
| 68 |
row = {"mode": mode, "winner": win,
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
-
|
|
|
|
| 72 |
try:
|
| 73 |
save(t)
|
| 74 |
except Exception as e:
|
| 75 |
-
|
|
|
|
| 76 |
return JSONResponse(t)
|
| 77 |
|
| 78 |
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
+
"""νλ κ΄λ¬Έ μμΌμ΄λ Space β μ μ νμ΄μ§ + λμ μ μ (νλΌμ΄λΉ λ°μ΄ν°μ
)
|
| 3 |
|
| 4 |
π΄ μ μ μ Space λμ€ν¬κ° μλλΌ **λ°μ΄ν°μ
repo** μ μ΄λ€. Space λ μ¬μμνλ©΄ λμ€ν¬κ° λ μκ°λ€.
|
| 5 |
"""
|
|
|
|
| 12 |
TOK = os.environ.get("HF_TOKEN")
|
| 13 |
api = HfApi(token=TOK)
|
| 14 |
LOCK = threading.Lock()
|
| 15 |
+
BLANK = {"matches": 0, "wins": {"plain": 0, "jev": 0, "ztc": 0},
|
| 16 |
+
"blocked": 0, "recent": []}
|
| 17 |
|
| 18 |
app = FastAPI()
|
| 19 |
HTML = open(os.path.join(os.path.dirname(__file__), "index.html"), encoding="utf-8").read()
|
|
|
|
| 25 |
force_download=True)
|
| 26 |
with open(p, encoding="utf-8") as f:
|
| 27 |
t = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
except Exception:
|
| 29 |
+
t = {}
|
| 30 |
+
out = json.loads(json.dumps(BLANK))
|
| 31 |
+
out["matches"] = int(t.get("matches", 0) or 0)
|
| 32 |
+
for k in ("plain", "jev", "ztc"):
|
| 33 |
+
out["wins"][k] = int((t.get("wins") or {}).get(k, 0) or 0)
|
| 34 |
+
out["blocked"] = int(t.get("blocked", 0) or 0)
|
| 35 |
+
out["recent"] = list(t.get("recent") or [])
|
| 36 |
+
return out
|
| 37 |
|
| 38 |
|
| 39 |
def save(t):
|
|
|
|
| 59 |
win = body.get("winner")
|
| 60 |
if win not in ("plain", "jev", "ztc"):
|
| 61 |
return JSONResponse({"error": "bad winner"}, status_code=400)
|
| 62 |
+
sc = body.get("score") or {}
|
| 63 |
+
ac = body.get("acted") or {}
|
| 64 |
mode = "human" if body.get("mode") == "human" else "auto"
|
| 65 |
+
blocked = int(body.get("blocked") or 0)
|
|
|
|
| 66 |
with LOCK:
|
| 67 |
t = load()
|
| 68 |
+
t["matches"] += 1
|
| 69 |
+
t["wins"][win] += 1
|
| 70 |
+
t["blocked"] += max(0, blocked)
|
| 71 |
row = {"mode": mode, "winner": win,
|
| 72 |
+
"score": {k: int(sc.get(k, 0) or 0) for k in ("plain", "jev", "ztc")},
|
| 73 |
+
"acted": {k: int(ac.get(k, 0) or 0) for k in ("plain", "jev", "ztc")},
|
| 74 |
+
"blocked": max(0, blocked)}
|
| 75 |
+
t["recent"] = ([row] + t["recent"])[:50]
|
| 76 |
try:
|
| 77 |
save(t)
|
| 78 |
except Exception as e:
|
| 79 |
+
out = dict(t); out["error"] = "save failed: %s" % e
|
| 80 |
+
return JSONResponse(out, status_code=200)
|
| 81 |
return JSONResponse(t)
|
| 82 |
|
| 83 |
|