akhaliq HF Staff commited on
Commit
7a54ad9
·
1 Parent(s): e78ea69

Fix: read OAuth token from session via LocalContext (call_fn doesn't forward OAuthToken to bound fns)

Browse files
Files changed (1) hide show
  1. app.py +31 -5
app.py CHANGED
@@ -1,12 +1,38 @@
1
  import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
- from gradio.oauth import OAuthToken
 
5
  import tempfile
6
  import uuid
7
 
8
 
9
- def generate_prompt(concept: str, oauth_token: OAuthToken | None = None) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  """
11
  Expands a simple concept into a detailed image prompt using the NVIDIA Nemotron model.
12
  Uses the signed-in user's HF OAuth token for inference provider billing.
@@ -14,7 +40,7 @@ def generate_prompt(concept: str, oauth_token: OAuthToken | None = None) -> str:
14
  if not concept:
15
  return "a ginger cat wearing a tiny wizard hat reading a spellbook"
16
  try:
17
- token = oauth_token.token if oauth_token else None
18
  client = InferenceClient(
19
  provider="together",
20
  api_key=token,
@@ -49,7 +75,7 @@ def generate_prompt(concept: str, oauth_token: OAuthToken | None = None) -> str:
49
  return f"A detailed, high-quality, professional commercial product photograph of {concept}"
50
 
51
 
52
- def generate_z_image(prompt: str, oauth_token: OAuthToken | None = None) -> dict:
53
  """
54
  Generates an image from a prompt using the Tongyi-MAI/Z-Image-Turbo model.
55
  Uses the signed-in user's HF OAuth token for inference provider billing.
@@ -58,7 +84,7 @@ def generate_z_image(prompt: str, oauth_token: OAuthToken | None = None) -> dict
58
  if not prompt:
59
  prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"
60
  try:
61
- token = oauth_token.token if oauth_token else None
62
  client = InferenceClient(
63
  provider="auto",
64
  api_key=token,
 
1
  import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
+ from huggingface_hub import get_token as hf_get_token
5
+ from gradio.context import LocalContext
6
  import tempfile
7
  import uuid
8
 
9
 
10
+ def _get_user_token() -> str | None:
11
+ """
12
+ Get the logged-in user's HF OAuth token from the current request session.
13
+ On Spaces: returns the real OAuth access_token after HF login.
14
+ Locally: the OAuth flow mocks the token, so we fall back to hf_get_token().
15
+ """
16
+ try:
17
+ request = LocalContext.request.get(None)
18
+ if request is not None:
19
+ session = getattr(request, "session", {})
20
+ oauth_info = session.get("oauth_info", {})
21
+ if oauth_info:
22
+ token = oauth_info.get("access_token")
23
+ # Skip the local dev mock token — fall through to hf_get_token()
24
+ if token and token != "mock-oauth-token-for-local-dev":
25
+ return token
26
+ except Exception:
27
+ pass
28
+ # Fallback: use the locally saved HF token (hf auth login / HF_TOKEN env var)
29
+ try:
30
+ return hf_get_token()
31
+ except Exception:
32
+ return None
33
+
34
+
35
+ def generate_prompt(concept: str) -> str:
36
  """
37
  Expands a simple concept into a detailed image prompt using the NVIDIA Nemotron model.
38
  Uses the signed-in user's HF OAuth token for inference provider billing.
 
40
  if not concept:
41
  return "a ginger cat wearing a tiny wizard hat reading a spellbook"
42
  try:
43
+ token = _get_user_token()
44
  client = InferenceClient(
45
  provider="together",
46
  api_key=token,
 
75
  return f"A detailed, high-quality, professional commercial product photograph of {concept}"
76
 
77
 
78
+ def generate_z_image(prompt: str) -> dict:
79
  """
80
  Generates an image from a prompt using the Tongyi-MAI/Z-Image-Turbo model.
81
  Uses the signed-in user's HF OAuth token for inference provider billing.
 
84
  if not prompt:
85
  prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"
86
  try:
87
+ token = _get_user_token()
88
  client = InferenceClient(
89
  provider="auto",
90
  api_key=token,