gheinrich commited on
Commit
8d7976d
·
1 Parent(s): 978f0a8

Add startup Hub access check (whoami + model_info)

Browse files

Logs which account the HF token maps to and whether it can read MODEL_ID,
so private-repo 404s are diagnosable directly from the Space logs.

Files changed (1) hide show
  1. app.py +39 -0
app.py CHANGED
@@ -97,6 +97,45 @@ IMAGE_GUARD_MODEL_ID = os.getenv("IMAGE_GUARD_MODEL_ID", DEFAULT_IMAGE_GUARD_MOD
97
  IMAGE_GUARD_THRESHOLD = float(
98
  os.getenv("IMAGE_GUARD_THRESHOLD", str(DEFAULT_IMAGE_GUARD_THRESHOLD))
99
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  IMAGE_GUARD_OFFLOAD_T2I = os.getenv("IMAGE_GUARD_OFFLOAD_T2I", "0") == "1"
101
  # Opt-out: guard runs by default; set ENABLE_IMAGE_GUARD=0 or uncheck the UI box to disable.
102
  DEFAULT_ENABLE_IMAGE_GUARD = os.getenv("ENABLE_IMAGE_GUARD", "1") == "1"
 
97
  IMAGE_GUARD_THRESHOLD = float(
98
  os.getenv("IMAGE_GUARD_THRESHOLD", str(DEFAULT_IMAGE_GUARD_THRESHOLD))
99
  )
100
+
101
+
102
+ def _log_hub_access() -> None:
103
+ """Log which account the token maps to and whether it can see MODEL_ID."""
104
+ if not HF_TOKEN:
105
+ return
106
+ try:
107
+ from huggingface_hub import HfApi, whoami
108
+
109
+ try:
110
+ me = whoami(token=HF_TOKEN)
111
+ orgs = [o.get("name") for o in me.get("orgs", [])]
112
+ print(
113
+ f"HF token belongs to '{me.get('name')}' (orgs: {orgs or 'none'}).",
114
+ flush=True,
115
+ )
116
+ except Exception as exc: # noqa: BLE001
117
+ print(f"WARNING: token whoami failed: {exc}", flush=True)
118
+ return
119
+
120
+ try:
121
+ info = HfApi().model_info(MODEL_ID, token=HF_TOKEN)
122
+ print(
123
+ f"Access OK: '{MODEL_ID}' is reachable (private={info.private}).",
124
+ flush=True,
125
+ )
126
+ except Exception as exc: # noqa: BLE001
127
+ print(
128
+ f"ERROR: token cannot access '{MODEL_ID}': "
129
+ f"{type(exc).__name__}. The token's owner must have read access to "
130
+ "this private repo (a 'pull access' scope is not enough if the "
131
+ "account/org lacks access).",
132
+ flush=True,
133
+ )
134
+ except Exception as exc: # noqa: BLE001
135
+ print(f"WARNING: hub access check skipped: {exc}", flush=True)
136
+
137
+
138
+ _log_hub_access()
139
  IMAGE_GUARD_OFFLOAD_T2I = os.getenv("IMAGE_GUARD_OFFLOAD_T2I", "0") == "1"
140
  # Opt-out: guard runs by default; set ENABLE_IMAGE_GUARD=0 or uncheck the UI box to disable.
141
  DEFAULT_ENABLE_IMAGE_GUARD = os.getenv("ENABLE_IMAGE_GUARD", "1") == "1"