Skip to content

Commit 91b6ff2

Browse files
authored
Add user level to user stats (LAION-AI#2083)
The backend part of LAION-AI#1957
1 parent 3270bef commit 91b6ff2

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

.devcontainer/backend-dev/post_create_command.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash
22

3+
4+
# allow git usage
5+
git config --global --add safe.directory "*"
6+
37
# ensure pre-commit is installed
48
pre-commit install
59

backend/oasst_backend/user_stats_repository.py

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional
33
from uuid import UUID
44

5+
import numpy as np
56
import sqlalchemy as sa
67
from loguru import logger
78
from oasst_backend.config import settings
@@ -38,6 +39,15 @@
3839
from sqlmodel import Session, delete, func, text
3940

4041

42+
def get_thresholds(baseline: int = 3, alpha: float = 1.1521, max_level: int = 100) -> np.ndarray:
43+
level = np.round(np.cumsum(np.arange(1, max_level) * alpha + baseline))
44+
return np.array([0] + level.astype(int).tolist())
45+
46+
47+
# lookup table, never changes
48+
THRESHOLDS = get_thresholds()
49+
50+
4151
def _create_user_score(r, highlighted_user_id: UUID | None) -> UserScore:
4252
if r["UserStats"]:
4353
d = r["UserStats"].dict()
@@ -55,6 +65,7 @@ def _create_user_score(r, highlighted_user_id: UUID | None) -> UserScore:
5565
d[k] = r[k]
5666
if highlighted_user_id:
5767
d["highlighted"] = r["user_id"] == highlighted_user_id
68+
d["level"] = (THRESHOLDS <= d["leader_score"]).sum()
5869
return UserScore(**d)
5970

6071

oasst-shared/oasst_shared/schemas/protocol.py

+1
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ class UserScore(BaseModel):
460460
display_name: str
461461

462462
leader_score: int = 0
463+
level: int = 0 # between 0 and 100
463464

464465
base_date: Optional[datetime]
465466
modified_date: Optional[datetime]

0 commit comments

Comments
 (0)