thbndi commited on
Commit
3ec3248
·
verified ·
1 Parent(s): b3b050b

Gated API access: accept explicit hf_token as 6th /generate input (gr.OAuthToken is None over gradio_client)

Browse files
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -15,12 +15,14 @@ Hugging Face and being on the allowlist (the ``ALLOWLIST`` Space variable, a
15
  comma-separated list of usernames). Access is requested from ``@thbndi``.
16
 
17
  Programmatic access (gradio_client / the MovetsaDataset loading script):
18
- - named API endpoint ``/generate`` with the 5 inputs below, in this order:
19
- window_size, overlap, normalize, include_baselines, include_familiarization
20
- - identity is injected by Gradio: in the browser via ``gr.OAuthProfile``;
21
- over the API via ``gr.OAuthToken`` (resolved with ``whoami``). Callers just
22
- pass ``hf_token=...`` to ``gradio_client.Client(...)``.
23
- - requires ``hf_oauth: true`` in the Space README front-matter.
 
 
24
  """
25
 
26
  import os
@@ -62,29 +64,42 @@ RAW_DIR = snapshot_download(RAW_REPO, repo_type="dataset", token=TOKEN,
62
  from MoveTSA.export_hf_dataset import build_windows # noqa: E402 (needs sys.path)
63
 
64
 
65
- def _resolve_username(profile, oauth_token):
66
- """Identity from the browser OAuth profile, or from an API token."""
 
 
 
 
 
 
 
67
  if profile is not None:
68
  return profile.username
69
- if oauth_token is not None and getattr(oauth_token, "token", None):
 
 
 
 
 
70
  try:
71
- return whoami(token=oauth_token.token).get("name")
72
  except Exception: # noqa: BLE001
73
  return None
74
  return None
75
 
76
 
77
  def generate(window_size, overlap, normalize, include_baselines,
78
- include_familiarization,
79
  profile: gr.OAuthProfile | None = None,
80
  oauth_token: gr.OAuthToken | None = None):
81
  """Run the pipeline with the chosen parameters and return a parquet file.
82
 
83
- ``profile`` (browser) and ``oauth_token`` (API) are injected by Gradio and
84
- are NOT part of the API signature the named endpoint only exposes the 5
85
- parameters above.
 
86
  """
87
- username = _resolve_username(profile, oauth_token)
88
  if username is None:
89
  return None, "Sign in with your Hugging Face account to generate."
90
  if username.lower() not in ALLOWLIST:
@@ -151,6 +166,9 @@ with gr.Blocks(title="MoveTSA dataset builder") as demo:
151
  value=True, label="include baselines (B1–B4)")
152
  include_familiarization = gr.Checkbox(
153
  value=True, label="include familiarization (F)")
 
 
 
154
  btn = gr.Button("Generate parquet", variant="primary")
155
  with gr.Column():
156
  status = gr.Markdown()
@@ -159,7 +177,7 @@ with gr.Blocks(title="MoveTSA dataset builder") as demo:
159
  btn.click(
160
  generate,
161
  inputs=[window_size, overlap, normalize, include_baselines,
162
- include_familiarization],
163
  outputs=[out_file, status],
164
  api_name="generate", # => client.predict(..., api_name="/generate")
165
  )
 
15
  comma-separated list of usernames). Access is requested from ``@thbndi``.
16
 
17
  Programmatic access (gradio_client / the MovetsaDataset loading script):
18
+ - named API endpoint ``/generate`` with 6 inputs, in this order:
19
+ window_size, overlap, normalize, include_baselines,
20
+ include_familiarization, hf_token
21
+ - identity: in the browser via ``gr.OAuthProfile`` (the LoginButton); over
22
+ the API via the explicit ``hf_token`` argument, resolved with ``whoami``.
23
+ ``gr.OAuthToken`` is NOT populated over the gradio_client API — it only
24
+ works through the browser OAuth login — so headless callers must pass their
25
+ token as the 6th argument.
26
  """
27
 
28
  import os
 
64
  from MoveTSA.export_hf_dataset import build_windows # noqa: E402 (needs sys.path)
65
 
66
 
67
+ def _resolve_username(profile, oauth_token, hf_token):
68
+ """Identity from the browser OAuth profile, or from an explicit HF token.
69
+
70
+ In the browser, Gradio injects ``profile`` after the OAuth login. Headless
71
+ callers (gradio_client / the MovetsaDataset loading script) cannot go
72
+ through the OAuth flow, so they pass their token explicitly and we resolve
73
+ the username with ``whoami``. ``oauth_token`` is kept as a fallback but is
74
+ ``None`` over the API.
75
+ """
76
  if profile is not None:
77
  return profile.username
78
+ token = None
79
+ if hf_token:
80
+ token = hf_token
81
+ elif oauth_token is not None and getattr(oauth_token, "token", None):
82
+ token = oauth_token.token
83
+ if token:
84
  try:
85
+ return whoami(token=token).get("name")
86
  except Exception: # noqa: BLE001
87
  return None
88
  return None
89
 
90
 
91
  def generate(window_size, overlap, normalize, include_baselines,
92
+ include_familiarization, hf_token="",
93
  profile: gr.OAuthProfile | None = None,
94
  oauth_token: gr.OAuthToken | None = None):
95
  """Run the pipeline with the chosen parameters and return a parquet file.
96
 
97
+ ``hf_token`` is the 6th API input: headless callers pass their HF token so
98
+ the Space can identify them (the browser leaves it empty and authenticates
99
+ via the LoginButton). ``profile``/``oauth_token`` are injected by Gradio and
100
+ are NOT part of the API signature.
101
  """
102
+ username = _resolve_username(profile, oauth_token, hf_token)
103
  if username is None:
104
  return None, "Sign in with your Hugging Face account to generate."
105
  if username.lower() not in ALLOWLIST:
 
166
  value=True, label="include baselines (B1–B4)")
167
  include_familiarization = gr.Checkbox(
168
  value=True, label="include familiarization (F)")
169
+ # 6th API input: headless callers pass their HF token here; the
170
+ # browser leaves it empty and authenticates via the LoginButton.
171
+ hf_token = gr.Textbox(value="", visible=False, label="hf_token")
172
  btn = gr.Button("Generate parquet", variant="primary")
173
  with gr.Column():
174
  status = gr.Markdown()
 
177
  btn.click(
178
  generate,
179
  inputs=[window_size, overlap, normalize, include_baselines,
180
+ include_familiarization, hf_token],
181
  outputs=[out_file, status],
182
  api_name="generate", # => client.predict(..., api_name="/generate")
183
  )