Pass workflow OAuth token to InferenceClient via ContextVar
Browse files3 unmodified lines45678910111213141515 unmodified lines31323334353637387 unmodified lines46474849505118 unmodified lines7071727374757648 unmodified lines1251261271281291303 unmodified linesfrom huggingface_hub import InferenceClientfrom huggingface_hub import get_token as hf_get_tokenfrom gradio.context import LocalContextdef get_hf_token() -> str | None:"""Retrieves the HF API token from either the user's Gradio OAuth sessionor falls back to the system environment/CLI cached token."""request = LocalContext.request.get(None)if request is not None:session = getattr(request, "session", {})15 unmodified linesif not concept:return "a ginger cat wearing a tiny wizard hat reading a spellbook"try:token = get_hf_token()client = InferenceClient("nvidia/NVIDIA-Nemotron-3-Ultra-550B-A55B-NVFP4", token=token)system_instruction = ("You are an expert prompt engineer for text-to-image models. ""Your task is to take a simple concept and expand it into a detailed, "7 unmodified lines{"role": "user", "content": f"Concept: {concept}"}]response = client.chat_completion(messages=messages,temperature=0.7,max_tokens=25618 unmodified linesif not prompt:prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"try:token = get_hf_token()client = InferenceClient(provider="auto",api_key=token,48 unmodified linesbound = self._bounddef call_fn(data, _token=None) -> str:fn_name = data[0] if data else ""try:args_json = data[1] if len(data) > 1 else "[]"3 unmodified lines456789101112131415161718192021222315 unmodified lines3940414243444546474849507 unmodified lines5859606162636418 unmodified lines8384858687888948 unmodified lines1381391401411421431441451461473 unmodified linesfrom huggingface_hub import InferenceClientfrom huggingface_hub import get_token as hf_get_tokenfrom gradio.context import LocalContextimport contextvarsworkflow_token = contextvars.ContextVar("workflow_token", default=None)def get_hf_token() -> str | None:"""Retrieves the HF API token from either the workflow context,the user's Gradio OAuth session, or falls back to the system environment."""w_token = workflow_token.get()if w_token:return w_tokenrequest = LocalContext.request.get(None)if request is not None:session = getattr(request, "session", {})15 unmodified linesif not concept:return "a ginger cat wearing a tiny wizard hat reading a spellbook"try:token = get_hf_token() or os.environ.get("HF_TOKEN") or os.environ.get("HF_API_TOKEN")client = InferenceClient(provider="together",api_key=token,bill_to="huggingface",)system_instruction = ("You are an expert prompt engineer for text-to-image models. ""Your task is to take a simple concept and expand it into a detailed, "7 unmodified lines{"role": "user", "content": f"Concept: {concept}"}]response = client.chat_completion(model="nvidia/NVIDIA-Nemotron-3-Ultra-550B-A55B-NVFP4",messages=messages,temperature=0.7,max_tokens=25618 unmodified linesif not prompt:prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"try:token = get_hf_token() or os.environ.get("HF_TOKEN") or os.environ.get("HF_API_TOKEN")client = InferenceClient(provider="auto",api_key=token,48 unmodified linesbound = self._bounddef call_fn(data, _token=None) -> str:if _token:t_str = _token.token if hasattr(_token, "token") else _tokenif t_str:workflow_token.set(str(t_str))fn_name = data[0] if data else ""try:args_json = data[1] if len(data) > 1 else "[]"
app.py+22-5