Use user OAuth token for inference calls instead of billing to Space owner
Browse files
app.py
CHANGED
|
@@ -1,50 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
-
from
|
| 5 |
-
from gradio.context import LocalContext
|
| 6 |
-
import contextvars
|
| 7 |
import tempfile
|
| 8 |
import uuid
|
| 9 |
|
| 10 |
-
workflow_token = contextvars.ContextVar("workflow_token", default=None)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
def get_hf_token() -> str | None:
|
| 14 |
-
"""
|
| 15 |
-
Retrieves the HF API token from either the workflow context,
|
| 16 |
-
the user's Gradio OAuth session, or falls back to the system environment.
|
| 17 |
-
"""
|
| 18 |
-
w_token = workflow_token.get()
|
| 19 |
-
if w_token:
|
| 20 |
-
return w_token
|
| 21 |
-
|
| 22 |
-
request = LocalContext.request.get(None)
|
| 23 |
-
if request is not None:
|
| 24 |
-
session = getattr(request, "session", {})
|
| 25 |
-
oauth_info = session.get("oauth_info", {})
|
| 26 |
-
if oauth_info:
|
| 27 |
-
token = oauth_info.get("access_token")
|
| 28 |
-
if token and token != "mock-oauth-token-for-local-dev":
|
| 29 |
-
return token
|
| 30 |
-
try:
|
| 31 |
-
return hf_get_token()
|
| 32 |
-
except Exception:
|
| 33 |
-
return None
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def generate_prompt(concept: str) -> str:
|
| 37 |
"""
|
| 38 |
Expands a simple concept into a detailed image prompt using the NVIDIA Nemotron model.
|
|
|
|
| 39 |
"""
|
| 40 |
if not concept:
|
| 41 |
return "a ginger cat wearing a tiny wizard hat reading a spellbook"
|
| 42 |
try:
|
| 43 |
-
token =
|
| 44 |
client = InferenceClient(
|
| 45 |
provider="together",
|
| 46 |
api_key=token,
|
| 47 |
-
bill_to="huggingface",
|
| 48 |
)
|
| 49 |
system_instruction = (
|
| 50 |
"You are an expert prompt engineer for text-to-image models. "
|
|
@@ -76,19 +49,19 @@ def generate_prompt(concept: str) -> str:
|
|
| 76 |
return f"A detailed, high-quality, professional commercial product photograph of {concept}"
|
| 77 |
|
| 78 |
|
| 79 |
-
def generate_z_image(prompt: str) -> dict:
|
| 80 |
"""
|
| 81 |
Generates an image from a prompt using the Tongyi-MAI/Z-Image-Turbo model.
|
|
|
|
| 82 |
Returns a dictionary structure compatible with Gradio's image viewer.
|
| 83 |
"""
|
| 84 |
if not prompt:
|
| 85 |
prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"
|
| 86 |
try:
|
| 87 |
-
token =
|
| 88 |
client = InferenceClient(
|
| 89 |
provider="auto",
|
| 90 |
api_key=token,
|
| 91 |
-
bill_to="huggingface",
|
| 92 |
)
|
| 93 |
image = client.text_to_image(
|
| 94 |
prompt,
|
|
|
|
| 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.
|
| 13 |
"""
|
| 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,
|
|
|
|
| 21 |
)
|
| 22 |
system_instruction = (
|
| 23 |
"You are an expert prompt engineer for text-to-image models. "
|
|
|
|
| 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.
|
| 56 |
Returns a dictionary structure compatible with Gradio's image viewer.
|
| 57 |
"""
|
| 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,
|
|
|
|
| 65 |
)
|
| 66 |
image = client.text_to_image(
|
| 67 |
prompt,
|