Spaces:
Running on Zero
Running on Zero
File size: 7,674 Bytes
8e83a5f ebc294a 8e83a5f 6b85415 8e83a5f ebc294a 8e83a5f ebc294a 8e83a5f ebc294a 8e83a5f 6b85415 8e83a5f 6b85415 8e83a5f 6b85415 8e83a5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 | """
Shared helpers for every feature: UI constants and token / login handling.
Each feature (tab) receives `session_token` (a gr.State) from `app.py` and calls
`resolve_token()` to pick a token in this order: session -> OAuth -> environment.
"""
from __future__ import annotations
import os
import re
from dataclasses import dataclass
from functools import lru_cache
from typing import Any, Optional
import gradio as gr
from huggingface_hub import login, logout, whoami
from core import hf_login
# ---------------------------------------------------------------------------
# Constants / UI configuration
# ---------------------------------------------------------------------------
#: Valid split-name regex of the `datasets` library (datasets.splits._split_re).
SPLIT_RE = re.compile(r"^\w+(\.\w+)*$")
CSS = """
.gradio-container {max-width: 1180px !important;}
.run-btn {min-height: 52px; font-size: 1.05rem; font-weight: 600;}
.log-box textarea {font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .82rem;}
"""
GRADIO_MAJOR = int(str(getattr(gr, "__version__", "5")).split(".")[0])
#: A job's status markdown starts with this when it failed (see `features/jobs.py`).
FAILED_PREFIX = "### β"
#: Gradio 6 renamed `show_copy_button` -> `buttons`; stay compatible with Gradio 5 and 6.
COPY_BUTTON_KWARGS = {"buttons": ["copy"]} if GRADIO_MAJOR >= 6 else {"show_copy_button": True}
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _clean(value: Optional[str]) -> str:
"""Normalize a string input."""
return value.strip() if isinstance(value, str) else ""
def _as_token(value) -> Optional[str]:
"""Accept both `str` and `gr.OAuthToken` -> return the token string."""
if value is None:
return None
if isinstance(value, str):
return value.strip() or None
token = getattr(value, "token", None) # gr.OAuthToken
return str(token).strip() if token else None
def resolve_token(*candidates) -> Optional[str]:
"""Pick a token by priority: session -> OAuth -> Space environment variables."""
for candidate in list(candidates) + [
os.environ.get("HF_TOKEN"),
os.environ.get("HUGGING_FACE_HUB_TOKEN"),
]:
token = _as_token(candidate)
if token:
return token
return None
def token_user(token: Optional[str]) -> Optional[str]:
"""Return the username if the token is valid, otherwise None."""
if not token:
return None
try:
info = whoami(token=token)
except Exception: # noqa: BLE001
return None
if isinstance(info, dict):
return info.get("name") or info.get("user") or "unknown"
return "unknown"
@lru_cache(maxsize=256)
def cached_user(token: Optional[str]) -> Optional[str]:
"""`token_user` cached per token β for handlers that run often (e.g. the Jobs tab timer)."""
return token_user(token)
def profile_username(profile) -> Optional[str]:
"""Get the username from a `gr.OAuthProfile` (dict-like)."""
if not profile:
return None
for key in ("preferred_username", "username", "name", "sub"):
try:
value = profile.get(key) if hasattr(profile, "get") else getattr(profile, key, None)
except Exception: # noqa: BLE001
value = None
if value:
return str(value)
return None
@dataclass
class AppContext:
"""Components shared by every tab, passed to each feature's `build(ctx)`."""
#: Token of this browser session (gr.State); resolve it with `resolve_token`.
session_token: Any
#: gr.State set to a job id whenever a tab submits a job (the Jobs tab follows it).
submitted_job: Any
#: The gr.Tabs container, so a handler can switch tabs.
tabs: Any = None
def login_badge() -> str:
return "β οΈ **Not signed in.** Use the OAuth button above, or paste an Access Token."
# ---------------------------------------------------------------------------
# Sign in / sign out
# ---------------------------------------------------------------------------
def on_page_load(
profile: Optional[gr.OAuthProfile],
oauth_token: Optional[gr.OAuthToken],
session_token: Optional[str],
request: gr.Request,
):
"""Runs on page load; also finishes the OAuth flow when this page is its redirect."""
token, error = hf_login.handle_callback(request)
if error:
return session_token, f"β **Sign-in with Hugging Face failed:** {error}"
if token:
user = token_user(token)
return token, (
f"β
**Signed in:** `{user}` (Hugging Face OAuth). The page where you clicked "
"*Sign in* is signed in as well β you can close this tab or keep using it."
)
token = resolve_token(session_token, oauth_token)
user = token_user(token)
if user:
scope = getattr(oauth_token, "scope", "") or ""
extra = f" _(OAuth scope: `{scope}`)_" if scope else ""
return session_token, f"β
**Signed in:** `{user}`{extra}"
name = profile_username(profile)
if name:
return session_token, (
f"π€ OAuth account: `{name}` β but no access token with write permission was received. "
"Grant the `write-repos` / `manage-repos` scopes, or paste an Access Token."
)
return session_token, login_badge()
def start_oauth_login(nonce: Optional[str]):
"""After the sign-in button opened the Hugging Face tab: start polling for the token."""
if not nonce:
return gr.skip(), gr.skip()
if not hf_login.ENABLED:
return (
"β Sign-in with Hugging Face only works on a Space with `hf_oauth: true` β "
"paste an Access Token instead.",
gr.Timer(active=False),
)
hf_login.start(nonce)
return (
"β³ Waiting for the Hugging Face sign-in in the new tab (allow pop-ups if no tab opened)β¦",
gr.Timer(active=True),
)
def poll_oauth_login(nonce: Optional[str], session_token: Optional[str]):
"""Timer tick: claim the token parked by the OAuth redirect tab, if it arrived."""
stop = gr.Timer(active=False)
if not nonce:
return gr.skip(), gr.skip(), gr.skip(), stop
token = hf_login.claim(nonce)
if token:
user = token_user(token)
return token, f"β
**Signed in:** `{user}` (Hugging Face OAuth)", "", stop
if hf_login.expired(nonce):
return gr.skip(), "β Sign-in with Hugging Face timed out β click the button again.", "", stop
return gr.skip(), gr.skip(), gr.skip(), gr.skip()
def do_token_login(raw_token: str, session_token: Optional[str]):
"""Sign in with a manually pasted Access Token."""
token = _clean(raw_token) or _as_token(session_token)
if not token:
return session_token, "β No token entered."
user = token_user(token)
if not user:
return (
session_token,
"β Invalid or expired token. Create a new one at "
"https://huggingface.co/settings/tokens (needs **Write** permission).",
)
try: # best-effort: lets underlying libraries reuse it when no token is passed
login(token=token, add_to_git_credential=False)
except Exception: # noqa: BLE001
pass
return token, f"β
**Signed in:** `{user}`"
def do_logout(session_token: Optional[str]):
if session_token:
try:
logout(token=session_token)
except Exception: # noqa: BLE001
pass
return None, "π Token removed from this session."
|