Spaces:
Sleeping
Sleeping
feat(profile): resolve public full names and stop fetch on invalid usernames
Browse files
app.py
CHANGED
|
@@ -70,135 +70,149 @@ def set_cache(key, value):
|
|
| 70 |
|
| 71 |
|
| 72 |
# ============================================================
|
| 73 |
-
# Public
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def fetch_public_profile(username: str):
|
| 76 |
username = (username or "").strip()
|
| 77 |
if not username:
|
| 78 |
-
return
|
| 79 |
-
"exists": False,
|
| 80 |
-
"username": "",
|
| 81 |
-
"display_name": "",
|
| 82 |
-
"fullname": None,
|
| 83 |
-
"avatar_url": None,
|
| 84 |
-
"error": "No username provided.",
|
| 85 |
-
}
|
| 86 |
|
| 87 |
cache_key = ("public_profile", username.lower())
|
| 88 |
cached = get_cache(cache_key)
|
| 89 |
-
if cached
|
| 90 |
return cached
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
-
response =
|
| 94 |
-
f"https://huggingface.co/api/
|
| 95 |
-
headers={
|
| 96 |
-
"User-Agent": "hf-contributions-app/1.0",
|
| 97 |
-
"Accept": "application/json",
|
| 98 |
-
"Cache-Control": "no-cache",
|
| 99 |
-
},
|
| 100 |
timeout=15,
|
| 101 |
)
|
| 102 |
|
| 103 |
if response.status_code == 404:
|
| 104 |
-
result =
|
| 105 |
-
"exists": False,
|
| 106 |
-
"username": username,
|
| 107 |
-
"display_name": f"@{username}",
|
| 108 |
-
"fullname": None,
|
| 109 |
-
"avatar_url": None,
|
| 110 |
-
"error": f"User or org '{username}' was not found.",
|
| 111 |
-
}
|
| 112 |
set_cache(cache_key, result)
|
| 113 |
return result
|
| 114 |
|
| 115 |
response.raise_for_status()
|
| 116 |
-
payload = response.json()
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
or payload.get("name")
|
|
|
|
|
|
|
|
|
|
| 121 |
or payload.get("username")
|
| 122 |
-
or payload.get("
|
| 123 |
or username
|
| 124 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
fullname = None
|
| 131 |
-
|
| 132 |
-
avatar_url = payload.get("avatarUrl") or payload.get("avatar_url")
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
else:
|
| 137 |
-
display_name = f"@{resolved_username}"
|
| 138 |
|
| 139 |
result = {
|
| 140 |
"exists": True,
|
| 141 |
-
"username":
|
| 142 |
-
"display_name": display_name,
|
| 143 |
"fullname": fullname,
|
| 144 |
-
"
|
|
|
|
| 145 |
"error": None,
|
| 146 |
}
|
| 147 |
set_cache(cache_key, result)
|
| 148 |
return result
|
| 149 |
|
| 150 |
except Exception as e:
|
|
|
|
|
|
|
| 151 |
result = {
|
| 152 |
"exists": False,
|
| 153 |
"username": username,
|
| 154 |
-
"display_name": f"@{username}",
|
| 155 |
"fullname": None,
|
|
|
|
| 156 |
"avatar_url": None,
|
| 157 |
-
"error": f"
|
| 158 |
}
|
| 159 |
set_cache(cache_key, result)
|
| 160 |
return result
|
| 161 |
|
| 162 |
|
| 163 |
-
# ============================================================
|
| 164 |
-
# Public cache only
|
| 165 |
-
# ============================================================
|
| 166 |
-
@lru_cache(maxsize=1000)
|
| 167 |
-
def cached_list_commits_public(repo_id: str, repo_type: str):
|
| 168 |
-
return list(HfApi().list_repo_commits(repo_id=repo_id, repo_type=repo_type))
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
@lru_cache(maxsize=200)
|
| 172 |
-
def cached_list_items_public(username: str, kind: str):
|
| 173 |
-
api = HfApi()
|
| 174 |
-
if kind == "model":
|
| 175 |
-
return list(api.list_models(author=username))
|
| 176 |
-
if kind == "dataset":
|
| 177 |
-
return list(api.list_datasets(author=username))
|
| 178 |
-
if kind == "space":
|
| 179 |
-
return list(api.list_spaces(author=username))
|
| 180 |
-
return []
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
def list_items(username: str, kind: str, token: str | None = None):
|
| 184 |
-
if token:
|
| 185 |
-
api = get_api(token)
|
| 186 |
-
if kind == "model":
|
| 187 |
-
return list(api.list_models(author=username))
|
| 188 |
-
if kind == "dataset":
|
| 189 |
-
return list(api.list_datasets(author=username))
|
| 190 |
-
if kind == "space":
|
| 191 |
-
return list(api.list_spaces(author=username))
|
| 192 |
-
return []
|
| 193 |
-
return cached_list_items_public(username, kind)
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
def list_commits(repo_id: str, repo_type: str, token: str | None = None):
|
| 197 |
-
if token:
|
| 198 |
-
return list(get_api(token).list_repo_commits(repo_id=repo_id, repo_type=repo_type))
|
| 199 |
-
return cached_list_commits_public(repo_id, repo_type)
|
| 200 |
-
|
| 201 |
-
|
| 202 |
# ============================================================
|
| 203 |
# Repo activity fetch
|
| 204 |
# ============================================================
|
|
@@ -720,7 +734,7 @@ def month_positions_for_year(year: int, grid_start: pd.Timestamp):
|
|
| 720 |
# ============================================================
|
| 721 |
# Name helpers
|
| 722 |
# ============================================================
|
| 723 |
-
def resolve_display_name(username: str, profile: gr.OAuthProfile | None, public_profile: dict | None):
|
| 724 |
username = (username or "").strip()
|
| 725 |
if not username:
|
| 726 |
return ""
|
|
@@ -737,6 +751,10 @@ def resolve_display_name(username: str, profile: gr.OAuthProfile | None, public_
|
|
| 737 |
if isinstance(display_name, str) and display_name.strip():
|
| 738 |
return display_name
|
| 739 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 740 |
return f"@{username}"
|
| 741 |
|
| 742 |
|
|
@@ -806,6 +824,14 @@ def render_idle_context_banner(
|
|
| 806 |
return render_status_badge("Not logged in · Enter a username to get started")
|
| 807 |
|
| 808 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 809 |
def render_context_banner(app_state: dict | None, profile: gr.OAuthProfile | None):
|
| 810 |
if not app_state or not app_state.get("username"):
|
| 811 |
return render_idle_context_banner("", profile)
|
|
@@ -898,6 +924,14 @@ def render_community_idle_banner(
|
|
| 898 |
return render_status_badge(" · ".join(parts))
|
| 899 |
|
| 900 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 901 |
def render_community_fetching_banner(
|
| 902 |
username,
|
| 903 |
selected_year,
|
|
@@ -1142,6 +1176,25 @@ def render_discussion_combined_chart(community_state: dict | None, mode: str, us
|
|
| 1142 |
# ============================================================
|
| 1143 |
# App state + render layer
|
| 1144 |
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1145 |
def build_app_state(
|
| 1146 |
username: str,
|
| 1147 |
selected_year: int,
|
|
@@ -1154,6 +1207,17 @@ def build_app_state(
|
|
| 1154 |
viewer_username = getattr(profile, "username", None) if profile is not None else None
|
| 1155 |
token = oauth_token.token if oauth_token is not None else None
|
| 1156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1157 |
is_own_profile = bool(
|
| 1158 |
viewer_username
|
| 1159 |
and username
|
|
@@ -1163,38 +1227,15 @@ def build_app_state(
|
|
| 1163 |
can_toggle_visibility = is_own_profile
|
| 1164 |
token_for_fetch = token if is_own_profile else None
|
| 1165 |
|
| 1166 |
-
empty_state = {
|
| 1167 |
-
"username": "",
|
| 1168 |
-
"selected_year": selected_year,
|
| 1169 |
-
"viewer_username": viewer_username,
|
| 1170 |
-
"is_own_profile": is_own_profile,
|
| 1171 |
-
"can_toggle_visibility": False,
|
| 1172 |
-
"default_visibility": "public",
|
| 1173 |
-
"kind_repo_data": {
|
| 1174 |
-
"model": {"total_items": 0, "repos": [], "errors": []},
|
| 1175 |
-
"dataset": {"total_items": 0, "repos": [], "errors": []},
|
| 1176 |
-
"space": {"total_items": 0, "repos": [], "errors": []},
|
| 1177 |
-
},
|
| 1178 |
-
"public_profile": None,
|
| 1179 |
-
"username_exists": False,
|
| 1180 |
-
"validation_error": "No username provided.",
|
| 1181 |
-
}
|
| 1182 |
-
|
| 1183 |
-
if not username:
|
| 1184 |
-
return empty_state
|
| 1185 |
-
|
| 1186 |
-
public_profile = fetch_public_profile(username)
|
| 1187 |
-
if not public_profile.get("exists"):
|
| 1188 |
-
empty_state["username"] = username
|
| 1189 |
-
empty_state["public_profile"] = public_profile
|
| 1190 |
-
empty_state["validation_error"] = public_profile.get("error") or f"User or org '{username}' was not found."
|
| 1191 |
-
return empty_state
|
| 1192 |
-
|
| 1193 |
scope = "repo_private" if is_own_profile else "repo_public"
|
| 1194 |
cache_key = (username.lower(), selected_year, scope)
|
| 1195 |
cached = get_cache(cache_key)
|
| 1196 |
if cached:
|
| 1197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1198 |
|
| 1199 |
kind_repo_data = {}
|
| 1200 |
for kind in ["model", "dataset", "space"]:
|
|
@@ -1214,7 +1255,7 @@ def build_app_state(
|
|
| 1214 |
"default_visibility": "all" if can_toggle_visibility else "public",
|
| 1215 |
"kind_repo_data": kind_repo_data,
|
| 1216 |
"public_profile": public_profile,
|
| 1217 |
-
"
|
| 1218 |
"validation_error": None,
|
| 1219 |
}
|
| 1220 |
|
|
@@ -1222,6 +1263,30 @@ def build_app_state(
|
|
| 1222 |
return result
|
| 1223 |
|
| 1224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1225 |
def render_app_state(
|
| 1226 |
app_state: dict | None,
|
| 1227 |
visibility_mode: str | None,
|
|
@@ -1247,31 +1312,12 @@ def render_app_state(
|
|
| 1247 |
"No username provided.",
|
| 1248 |
)
|
| 1249 |
|
| 1250 |
-
if not app_state.get("
|
| 1251 |
-
|
| 1252 |
-
empty_metrics = render_metric_cards(
|
| 1253 |
-
0,
|
| 1254 |
-
{"model": 0, "dataset": 0, "space": 0},
|
| 1255 |
-
{"model": 0, "dataset": 0, "space": 0},
|
| 1256 |
-
selected_year,
|
| 1257 |
-
)
|
| 1258 |
-
error_text = app_state.get("validation_error") or "Username not found."
|
| 1259 |
-
context_html = render_status_badge(html.escape(error_text))
|
| 1260 |
-
return (
|
| 1261 |
-
"## User not found",
|
| 1262 |
-
context_html,
|
| 1263 |
-
empty_metrics,
|
| 1264 |
-
render_empty_chart("All activity"),
|
| 1265 |
-
render_empty_chart("Models"),
|
| 1266 |
-
render_empty_chart("Datasets"),
|
| 1267 |
-
render_empty_chart("Spaces"),
|
| 1268 |
-
error_text,
|
| 1269 |
-
)
|
| 1270 |
|
| 1271 |
username = app_state["username"]
|
| 1272 |
selected_year = int(app_state["selected_year"])
|
| 1273 |
can_toggle_visibility = bool(app_state["can_toggle_visibility"])
|
| 1274 |
-
public_profile = app_state.get("public_profile")
|
| 1275 |
|
| 1276 |
effective_visibility = "all" if (visibility_mode == "all" and can_toggle_visibility) else "public"
|
| 1277 |
include_private = effective_visibility == "all"
|
|
@@ -1299,7 +1345,7 @@ def render_app_state(
|
|
| 1299 |
space_df = pd.DataFrame(kind_views["space"]["activity_dates"], columns=["date"])
|
| 1300 |
|
| 1301 |
context_html = render_context_banner(app_state, profile)
|
| 1302 |
-
display_name = resolve_display_name(username, profile, public_profile)
|
| 1303 |
|
| 1304 |
metrics_html = render_metric_cards(
|
| 1305 |
total_activity=total_activity,
|
|
@@ -1415,6 +1461,30 @@ def fetch_and_render_repo_only(
|
|
| 1415 |
|
| 1416 |
app_state = build_app_state(username, selected_year, profile, oauth_token)
|
| 1417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1418 |
requested_visibility = current_visibility_mode if current_visibility_mode in {"all", "public"} else app_state["default_visibility"]
|
| 1419 |
final_visibility = requested_visibility if app_state["can_toggle_visibility"] else "public"
|
| 1420 |
|
|
@@ -1462,19 +1532,6 @@ def maybe_reset_community_section(
|
|
| 1462 |
pr_mode = pr_mode or "both"
|
| 1463 |
discussion_mode = discussion_mode or "both"
|
| 1464 |
|
| 1465 |
-
public_profile = fetch_public_profile(username) if username else None
|
| 1466 |
-
if username and public_profile and not public_profile.get("exists"):
|
| 1467 |
-
state = empty_community_state(selected_year)
|
| 1468 |
-
return (
|
| 1469 |
-
state,
|
| 1470 |
-
render_status_badge(html.escape(public_profile.get("error") or "Username not found.")),
|
| 1471 |
-
gr.update(value="both", visible="hidden"),
|
| 1472 |
-
render_empty_chart("Pull Requests"),
|
| 1473 |
-
gr.update(value="both", visible="hidden"),
|
| 1474 |
-
render_empty_chart("Community Discussions"),
|
| 1475 |
-
public_profile.get("error") or "Username not found.",
|
| 1476 |
-
)
|
| 1477 |
-
|
| 1478 |
if not current_community_state:
|
| 1479 |
state = empty_community_state(selected_year)
|
| 1480 |
return (
|
|
@@ -1529,11 +1586,6 @@ def maybe_reset_community_section(
|
|
| 1529 |
|
| 1530 |
|
| 1531 |
def prepare_community_fetch_ui(username, selected_year, profile: gr.OAuthProfile | None):
|
| 1532 |
-
username = (username or "").strip()
|
| 1533 |
-
if username:
|
| 1534 |
-
public_profile = fetch_public_profile(username)
|
| 1535 |
-
if not public_profile.get("exists"):
|
| 1536 |
-
return render_status_badge(html.escape(public_profile.get("error") or "Username not found."))
|
| 1537 |
return render_community_fetching_banner(username, selected_year, profile)
|
| 1538 |
|
| 1539 |
|
|
@@ -1550,16 +1602,18 @@ def fetch_and_render_community(
|
|
| 1550 |
pr_mode = pr_mode or "both"
|
| 1551 |
discussion_mode = discussion_mode or "both"
|
| 1552 |
|
| 1553 |
-
public_profile = fetch_public_profile(username)
|
| 1554 |
-
if
|
| 1555 |
-
|
| 1556 |
-
|
|
|
|
|
|
|
| 1557 |
return (
|
| 1558 |
-
|
| 1559 |
-
|
| 1560 |
render_empty_chart("Pull Requests"),
|
| 1561 |
render_empty_chart("Community Discussions"),
|
| 1562 |
-
|
| 1563 |
gr.update(value="both", visible="hidden"),
|
| 1564 |
gr.update(value="both", visible="hidden"),
|
| 1565 |
)
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
# ============================================================
|
| 73 |
+
# Public cache only
|
| 74 |
+
# ============================================================
|
| 75 |
+
@lru_cache(maxsize=1000)
|
| 76 |
+
def cached_list_commits_public(repo_id: str, repo_type: str):
|
| 77 |
+
return list(HfApi().list_repo_commits(repo_id=repo_id, repo_type=repo_type))
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@lru_cache(maxsize=200)
|
| 81 |
+
def cached_list_items_public(username: str, kind: str):
|
| 82 |
+
api = HfApi()
|
| 83 |
+
if kind == "model":
|
| 84 |
+
return list(api.list_models(author=username))
|
| 85 |
+
if kind == "dataset":
|
| 86 |
+
return list(api.list_datasets(author=username))
|
| 87 |
+
if kind == "space":
|
| 88 |
+
return list(api.list_spaces(author=username))
|
| 89 |
+
return []
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def list_items(username: str, kind: str, token: str | None = None):
|
| 93 |
+
if token:
|
| 94 |
+
api = get_api(token)
|
| 95 |
+
if kind == "model":
|
| 96 |
+
return list(api.list_models(author=username))
|
| 97 |
+
if kind == "dataset":
|
| 98 |
+
return list(api.list_datasets(author=username))
|
| 99 |
+
if kind == "space":
|
| 100 |
+
return list(api.list_spaces(author=username))
|
| 101 |
+
return []
|
| 102 |
+
return cached_list_items_public(username, kind)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def list_commits(repo_id: str, repo_type: str, token: str | None = None):
|
| 106 |
+
if token:
|
| 107 |
+
return list(get_api(token).list_repo_commits(repo_id=repo_id, repo_type=repo_type))
|
| 108 |
+
return cached_list_commits_public(repo_id, repo_type)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
# ============================================================
|
| 112 |
+
# Public profile lookup / validation
|
| 113 |
+
# ============================================================
|
| 114 |
+
def empty_public_profile(username: str):
|
| 115 |
+
username = (username or "").strip()
|
| 116 |
+
return {
|
| 117 |
+
"exists": False,
|
| 118 |
+
"username": username,
|
| 119 |
+
"fullname": None,
|
| 120 |
+
"display_name": f"@{username}" if username else "",
|
| 121 |
+
"avatar_url": None,
|
| 122 |
+
"error": "Username not found." if username else "No username provided.",
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
|
| 126 |
def fetch_public_profile(username: str):
|
| 127 |
username = (username or "").strip()
|
| 128 |
if not username:
|
| 129 |
+
return empty_public_profile(username)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
cache_key = ("public_profile", username.lower())
|
| 132 |
cached = get_cache(cache_key)
|
| 133 |
+
if cached:
|
| 134 |
return cached
|
| 135 |
|
| 136 |
+
session = requests.Session()
|
| 137 |
+
session.headers.update(
|
| 138 |
+
{
|
| 139 |
+
"User-Agent": "hf-contributions-app/1.0",
|
| 140 |
+
"Accept": "application/json",
|
| 141 |
+
"Cache-Control": "no-cache",
|
| 142 |
+
}
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
try:
|
| 146 |
+
response = session.get(
|
| 147 |
+
f"https://huggingface.co/api/profiles/{username}",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
timeout=15,
|
| 149 |
)
|
| 150 |
|
| 151 |
if response.status_code == 404:
|
| 152 |
+
result = empty_public_profile(username)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
set_cache(cache_key, result)
|
| 154 |
return result
|
| 155 |
|
| 156 |
response.raise_for_status()
|
| 157 |
+
payload = response.json()
|
| 158 |
|
| 159 |
+
if not isinstance(payload, dict):
|
| 160 |
+
result = empty_public_profile(username)
|
| 161 |
+
set_cache(cache_key, result)
|
| 162 |
+
return result
|
| 163 |
+
|
| 164 |
+
# Be permissive with payload shape
|
| 165 |
+
fullname = (
|
| 166 |
+
payload.get("fullname")
|
| 167 |
+
or payload.get("fullName")
|
| 168 |
or payload.get("name")
|
| 169 |
+
)
|
| 170 |
+
handle = (
|
| 171 |
+
payload.get("user")
|
| 172 |
or payload.get("username")
|
| 173 |
+
or payload.get("preferred_username")
|
| 174 |
or username
|
| 175 |
)
|
| 176 |
+
avatar_url = (
|
| 177 |
+
payload.get("avatarUrl")
|
| 178 |
+
or payload.get("avatar_url")
|
| 179 |
+
or payload.get("picture")
|
| 180 |
+
)
|
| 181 |
|
| 182 |
+
if not isinstance(handle, str) or not handle.strip():
|
| 183 |
+
result = empty_public_profile(username)
|
| 184 |
+
set_cache(cache_key, result)
|
| 185 |
+
return result
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
handle = handle.strip()
|
| 188 |
+
fullname = fullname.strip() if isinstance(fullname, str) and fullname.strip() else None
|
|
|
|
|
|
|
| 189 |
|
| 190 |
result = {
|
| 191 |
"exists": True,
|
| 192 |
+
"username": handle,
|
|
|
|
| 193 |
"fullname": fullname,
|
| 194 |
+
"display_name": f"{fullname} (@{handle})" if fullname else f"@{handle}",
|
| 195 |
+
"avatar_url": avatar_url if isinstance(avatar_url, str) and avatar_url.strip() else None,
|
| 196 |
"error": None,
|
| 197 |
}
|
| 198 |
set_cache(cache_key, result)
|
| 199 |
return result
|
| 200 |
|
| 201 |
except Exception as e:
|
| 202 |
+
# If profile lookup fails for transient reasons, keep a conservative fallback:
|
| 203 |
+
# treat as not found so we stop the fetch cleanly as requested.
|
| 204 |
result = {
|
| 205 |
"exists": False,
|
| 206 |
"username": username,
|
|
|
|
| 207 |
"fullname": None,
|
| 208 |
+
"display_name": f"@{username}",
|
| 209 |
"avatar_url": None,
|
| 210 |
+
"error": f"Unable to validate username: {str(e)}",
|
| 211 |
}
|
| 212 |
set_cache(cache_key, result)
|
| 213 |
return result
|
| 214 |
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
# ============================================================
|
| 217 |
# Repo activity fetch
|
| 218 |
# ============================================================
|
|
|
|
| 734 |
# ============================================================
|
| 735 |
# Name helpers
|
| 736 |
# ============================================================
|
| 737 |
+
def resolve_display_name(username: str, profile: gr.OAuthProfile | None, public_profile: dict | None = None):
|
| 738 |
username = (username or "").strip()
|
| 739 |
if not username:
|
| 740 |
return ""
|
|
|
|
| 751 |
if isinstance(display_name, str) and display_name.strip():
|
| 752 |
return display_name
|
| 753 |
|
| 754 |
+
fullname = public_profile.get("fullname")
|
| 755 |
+
if isinstance(fullname, str) and fullname.strip():
|
| 756 |
+
return f"{fullname.strip()} (@{username})"
|
| 757 |
+
|
| 758 |
return f"@{username}"
|
| 759 |
|
| 760 |
|
|
|
|
| 824 |
return render_status_badge("Not logged in · Enter a username to get started")
|
| 825 |
|
| 826 |
|
| 827 |
+
def render_invalid_username_banner(username: str, error_text: str | None = None):
|
| 828 |
+
username = (username or "").strip()
|
| 829 |
+
base = f"Username <b>{html.escape(username)}</b> not found" if username else "Username not found"
|
| 830 |
+
if error_text and error_text != "Username not found.":
|
| 831 |
+
return render_status_badge(f"{base} · {html.escape(error_text)}")
|
| 832 |
+
return render_status_badge(base)
|
| 833 |
+
|
| 834 |
+
|
| 835 |
def render_context_banner(app_state: dict | None, profile: gr.OAuthProfile | None):
|
| 836 |
if not app_state or not app_state.get("username"):
|
| 837 |
return render_idle_context_banner("", profile)
|
|
|
|
| 924 |
return render_status_badge(" · ".join(parts))
|
| 925 |
|
| 926 |
|
| 927 |
+
def render_community_invalid_banner(username: str, error_text: str | None = None):
|
| 928 |
+
username = (username or "").strip()
|
| 929 |
+
base = f"Cannot fetch community activity: username <b>{html.escape(username)}</b> not found" if username else "Cannot fetch community activity: username not found"
|
| 930 |
+
if error_text and error_text != "Username not found.":
|
| 931 |
+
return render_status_badge(f"{base} · {html.escape(error_text)}")
|
| 932 |
+
return render_status_badge(base)
|
| 933 |
+
|
| 934 |
+
|
| 935 |
def render_community_fetching_banner(
|
| 936 |
username,
|
| 937 |
selected_year,
|
|
|
|
| 1176 |
# ============================================================
|
| 1177 |
# App state + render layer
|
| 1178 |
# ============================================================
|
| 1179 |
+
def build_empty_app_state(username: str, selected_year: int, viewer_username: str | None = None):
|
| 1180 |
+
return {
|
| 1181 |
+
"username": username,
|
| 1182 |
+
"selected_year": int(selected_year),
|
| 1183 |
+
"viewer_username": viewer_username,
|
| 1184 |
+
"is_own_profile": False,
|
| 1185 |
+
"can_toggle_visibility": False,
|
| 1186 |
+
"default_visibility": "public",
|
| 1187 |
+
"kind_repo_data": {
|
| 1188 |
+
"model": {"total_items": 0, "repos": [], "errors": []},
|
| 1189 |
+
"dataset": {"total_items": 0, "repos": [], "errors": []},
|
| 1190 |
+
"space": {"total_items": 0, "repos": [], "errors": []},
|
| 1191 |
+
},
|
| 1192 |
+
"public_profile": empty_public_profile(username),
|
| 1193 |
+
"is_valid_username": False,
|
| 1194 |
+
"validation_error": "Username not found." if username else "No username provided.",
|
| 1195 |
+
}
|
| 1196 |
+
|
| 1197 |
+
|
| 1198 |
def build_app_state(
|
| 1199 |
username: str,
|
| 1200 |
selected_year: int,
|
|
|
|
| 1207 |
viewer_username = getattr(profile, "username", None) if profile is not None else None
|
| 1208 |
token = oauth_token.token if oauth_token is not None else None
|
| 1209 |
|
| 1210 |
+
if not username:
|
| 1211 |
+
return build_empty_app_state(username, selected_year, viewer_username)
|
| 1212 |
+
|
| 1213 |
+
public_profile = fetch_public_profile(username)
|
| 1214 |
+
if not public_profile.get("exists"):
|
| 1215 |
+
return {
|
| 1216 |
+
**build_empty_app_state(username, selected_year, viewer_username),
|
| 1217 |
+
"public_profile": public_profile,
|
| 1218 |
+
"validation_error": public_profile.get("error") or "Username not found.",
|
| 1219 |
+
}
|
| 1220 |
+
|
| 1221 |
is_own_profile = bool(
|
| 1222 |
viewer_username
|
| 1223 |
and username
|
|
|
|
| 1227 |
can_toggle_visibility = is_own_profile
|
| 1228 |
token_for_fetch = token if is_own_profile else None
|
| 1229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1230 |
scope = "repo_private" if is_own_profile else "repo_public"
|
| 1231 |
cache_key = (username.lower(), selected_year, scope)
|
| 1232 |
cached = get_cache(cache_key)
|
| 1233 |
if cached:
|
| 1234 |
+
cached_with_profile = dict(cached)
|
| 1235 |
+
cached_with_profile["public_profile"] = public_profile
|
| 1236 |
+
cached_with_profile["is_valid_username"] = True
|
| 1237 |
+
cached_with_profile["validation_error"] = None
|
| 1238 |
+
return cached_with_profile
|
| 1239 |
|
| 1240 |
kind_repo_data = {}
|
| 1241 |
for kind in ["model", "dataset", "space"]:
|
|
|
|
| 1255 |
"default_visibility": "all" if can_toggle_visibility else "public",
|
| 1256 |
"kind_repo_data": kind_repo_data,
|
| 1257 |
"public_profile": public_profile,
|
| 1258 |
+
"is_valid_username": True,
|
| 1259 |
"validation_error": None,
|
| 1260 |
}
|
| 1261 |
|
|
|
|
| 1263 |
return result
|
| 1264 |
|
| 1265 |
|
| 1266 |
+
def render_invalid_app_state(app_state: dict | None, profile: gr.OAuthProfile | None):
|
| 1267 |
+
username = (app_state or {}).get("username", "")
|
| 1268 |
+
selected_year = int((app_state or {}).get("selected_year", datetime.now().year))
|
| 1269 |
+
error_text = (app_state or {}).get("validation_error") or "Username not found."
|
| 1270 |
+
|
| 1271 |
+
empty_metrics = render_metric_cards(
|
| 1272 |
+
0,
|
| 1273 |
+
{"model": 0, "dataset": 0, "space": 0},
|
| 1274 |
+
{"model": 0, "dataset": 0, "space": 0},
|
| 1275 |
+
selected_year,
|
| 1276 |
+
)
|
| 1277 |
+
|
| 1278 |
+
return (
|
| 1279 |
+
"## Ready",
|
| 1280 |
+
render_invalid_username_banner(username, error_text),
|
| 1281 |
+
empty_metrics,
|
| 1282 |
+
render_empty_chart("All activity"),
|
| 1283 |
+
render_empty_chart("Models"),
|
| 1284 |
+
render_empty_chart("Datasets"),
|
| 1285 |
+
render_empty_chart("Spaces"),
|
| 1286 |
+
error_text,
|
| 1287 |
+
)
|
| 1288 |
+
|
| 1289 |
+
|
| 1290 |
def render_app_state(
|
| 1291 |
app_state: dict | None,
|
| 1292 |
visibility_mode: str | None,
|
|
|
|
| 1312 |
"No username provided.",
|
| 1313 |
)
|
| 1314 |
|
| 1315 |
+
if not app_state.get("is_valid_username", True):
|
| 1316 |
+
return render_invalid_app_state(app_state, profile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1317 |
|
| 1318 |
username = app_state["username"]
|
| 1319 |
selected_year = int(app_state["selected_year"])
|
| 1320 |
can_toggle_visibility = bool(app_state["can_toggle_visibility"])
|
|
|
|
| 1321 |
|
| 1322 |
effective_visibility = "all" if (visibility_mode == "all" and can_toggle_visibility) else "public"
|
| 1323 |
include_private = effective_visibility == "all"
|
|
|
|
| 1345 |
space_df = pd.DataFrame(kind_views["space"]["activity_dates"], columns=["date"])
|
| 1346 |
|
| 1347 |
context_html = render_context_banner(app_state, profile)
|
| 1348 |
+
display_name = resolve_display_name(username, profile, app_state.get("public_profile"))
|
| 1349 |
|
| 1350 |
metrics_html = render_metric_cards(
|
| 1351 |
total_activity=total_activity,
|
|
|
|
| 1461 |
|
| 1462 |
app_state = build_app_state(username, selected_year, profile, oauth_token)
|
| 1463 |
|
| 1464 |
+
if not app_state.get("is_valid_username", True):
|
| 1465 |
+
title_md, context_box, metrics_box, all_chart, model_chart, dataset_chart, space_chart, logs_box = render_invalid_app_state(
|
| 1466 |
+
app_state,
|
| 1467 |
+
profile,
|
| 1468 |
+
)
|
| 1469 |
+
visibility_update = gr.update(
|
| 1470 |
+
choices=[("All repos", "all"), ("Public only", "public")],
|
| 1471 |
+
value="public",
|
| 1472 |
+
visible="hidden",
|
| 1473 |
+
interactive=False,
|
| 1474 |
+
)
|
| 1475 |
+
return (
|
| 1476 |
+
app_state,
|
| 1477 |
+
title_md,
|
| 1478 |
+
context_box,
|
| 1479 |
+
visibility_update,
|
| 1480 |
+
metrics_box,
|
| 1481 |
+
all_chart,
|
| 1482 |
+
model_chart,
|
| 1483 |
+
dataset_chart,
|
| 1484 |
+
space_chart,
|
| 1485 |
+
logs_box,
|
| 1486 |
+
)
|
| 1487 |
+
|
| 1488 |
requested_visibility = current_visibility_mode if current_visibility_mode in {"all", "public"} else app_state["default_visibility"]
|
| 1489 |
final_visibility = requested_visibility if app_state["can_toggle_visibility"] else "public"
|
| 1490 |
|
|
|
|
| 1532 |
pr_mode = pr_mode or "both"
|
| 1533 |
discussion_mode = discussion_mode or "both"
|
| 1534 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1535 |
if not current_community_state:
|
| 1536 |
state = empty_community_state(selected_year)
|
| 1537 |
return (
|
|
|
|
| 1586 |
|
| 1587 |
|
| 1588 |
def prepare_community_fetch_ui(username, selected_year, profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1589 |
return render_community_fetching_banner(username, selected_year, profile)
|
| 1590 |
|
| 1591 |
|
|
|
|
| 1602 |
pr_mode = pr_mode or "both"
|
| 1603 |
discussion_mode = discussion_mode or "both"
|
| 1604 |
|
| 1605 |
+
public_profile = fetch_public_profile(username)
|
| 1606 |
+
if not public_profile.get("exists"):
|
| 1607 |
+
empty_state = empty_community_state(selected_year)
|
| 1608 |
+
empty_state["username"] = username
|
| 1609 |
+
empty_state["selected_year"] = selected_year
|
| 1610 |
+
empty_state["logs"] = public_profile.get("error") or "Username not found."
|
| 1611 |
return (
|
| 1612 |
+
empty_state,
|
| 1613 |
+
render_community_invalid_banner(username, public_profile.get("error")),
|
| 1614 |
render_empty_chart("Pull Requests"),
|
| 1615 |
render_empty_chart("Community Discussions"),
|
| 1616 |
+
empty_state["logs"],
|
| 1617 |
gr.update(value="both", visible="hidden"),
|
| 1618 |
gr.update(value="both", visible="hidden"),
|
| 1619 |
)
|