Spaces:
Running
Running
Workflow1111 — Automatic1111-style diffusion studio on gr.Workflow
Browse files
README.md
CHANGED
|
@@ -348,18 +348,26 @@ endpoint list with parameter names and types.
|
|
| 348 |
### Bring your own token (API and MCP callers)
|
| 349 |
|
| 350 |
The Space holds **no** `HF_TOKEN` secret. In the browser, the Sign-in button
|
| 351 |
-
supplies your token through OAuth; API and MCP callers
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
```python
|
| 360 |
from gradio_client import Client
|
| 361 |
|
| 362 |
-
client = Client("https://ysharma-workflow1111.hf.space",
|
|
|
|
| 363 |
prompt = client.predict("an orange cat with a yellow hat", api_name="/generated_prompt")
|
| 364 |
```
|
| 365 |
|
|
|
|
| 348 |
### Bring your own token (API and MCP callers)
|
| 349 |
|
| 350 |
The Space holds **no** `HF_TOKEN` secret. In the browser, the Sign-in button
|
| 351 |
+
supplies your token through OAuth; API and MCP callers send it on the request
|
| 352 |
+
as an `X-HF-Token` header instead. Two hooks make that work:
|
| 353 |
+
|
| 354 |
+
* every node that talks to Inference Providers declares a `request: gr.Request`
|
| 355 |
+
parameter, and `_hf_token()` in `nodes.py` resolves the token in this order:
|
| 356 |
+
OAuth session → `X-HF-Token` header → `Authorization: Bearer` header →
|
| 357 |
+
server `HF_TOKEN` → local `hf auth login`;
|
| 358 |
+
* `app.py` extends gradio's own `_resolve_token` (used for `model` and `space`
|
| 359 |
+
nodes, which gradio runs itself) with the same header fallback.
|
| 360 |
+
|
| 361 |
+
Gradio's MCP server forwards the caller's headers verbatim, so the same header
|
| 362 |
+
works for tools. Use `X-HF-Token`, not `Authorization: Bearer`: the Spaces
|
| 363 |
+
proxy consumes the `Authorization` header before it reaches the app (it does
|
| 364 |
+
work when running locally).
|
| 365 |
|
| 366 |
```python
|
| 367 |
from gradio_client import Client
|
| 368 |
|
| 369 |
+
client = Client("https://ysharma-workflow1111.hf.space",
|
| 370 |
+
headers={"X-HF-Token": "hf_..."})
|
| 371 |
prompt = client.predict("an orange cat with a yellow hat", api_name="/generated_prompt")
|
| 372 |
```
|
| 373 |
|
app.py
CHANGED
|
@@ -79,6 +79,21 @@ import gradio as gr
|
|
| 79 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 80 |
import nodes # noqa: E402
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 83 |
WORKFLOW = os.path.join(HERE, "workflow.json")
|
| 84 |
|
|
|
|
| 79 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 80 |
import nodes # noqa: E402
|
| 81 |
|
| 82 |
+
# API/MCP callers authenticate per request with an `X-HF-Token` header (see
|
| 83 |
+
# `nodes._hf_token`). The four `model`/`space` nodes are run by gradio itself,
|
| 84 |
+
# whose `gradio.workflow._resolve_token` only knows OAuth sessions and the local
|
| 85 |
+
# write-token — so extend it here to fall back to the caller's header token.
|
| 86 |
+
import gradio.workflow as _gw # noqa: E402
|
| 87 |
+
|
| 88 |
+
_orig_resolve_token = _gw._resolve_token
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def _resolve_token_with_header(data, idx, token, request=None):
|
| 92 |
+
return _orig_resolve_token(data, idx, token, request) or nodes._caller_hf_token(request)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
_gw._resolve_token = _resolve_token_with_header
|
| 96 |
+
|
| 97 |
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 98 |
WORKFLOW = os.path.join(HERE, "workflow.json")
|
| 99 |
|
nodes.py
CHANGED
|
@@ -631,9 +631,10 @@ def _caller_hf_token(request):
|
|
| 631 |
the caller's headers verbatim — so a client can authenticate per call
|
| 632 |
without the Space holding a token of its own. Two spellings are accepted:
|
| 633 |
|
| 634 |
-
* ``X-HF-Token: hf_...`` (custom header;
|
| 635 |
* ``Authorization: Bearer hf_...`` (what ``gradio_client`` sends for
|
| 636 |
-
``Client(...,
|
|
|
|
| 637 |
"""
|
| 638 |
headers = getattr(request, "headers", None)
|
| 639 |
if not headers:
|
|
|
|
| 631 |
the caller's headers verbatim — so a client can authenticate per call
|
| 632 |
without the Space holding a token of its own. Two spellings are accepted:
|
| 633 |
|
| 634 |
+
* ``X-HF-Token: hf_...`` (custom header; works everywhere)
|
| 635 |
* ``Authorization: Bearer hf_...`` (what ``gradio_client`` sends for
|
| 636 |
+
``Client(..., token=...)``; fine locally, but the Spaces proxy
|
| 637 |
+
consumes this header, so on a Space use ``X-HF-Token``)
|
| 638 |
"""
|
| 639 |
headers = getattr(request, "headers", None)
|
| 640 |
if not headers:
|