celinah HF Staff commited on
Commit
b0a3aec
·
1 Parent(s): e77f197
Files changed (1) hide show
  1. app.py +54 -28
app.py CHANGED
@@ -43,47 +43,65 @@ def download_image_locally(image_url: str, local_path: str = "downloaded_image.p
43
  return local_path
44
 
45
 
46
- def login(oauth_token: gr.OAuthToken | None, fal_key_from_ui: str | None):
47
  """
48
- Login to Hugging Face and FAL.
49
 
50
  Args:
51
  oauth_token (gr.OAuthToken | None): The OAuth token from Hugging Face.
52
- fal_key_from_ui (str | None): The FAL key from the UI.
53
  """
54
- global TOKEN, FAL_KEY
55
 
56
  if oauth_token and oauth_token.token:
57
  print("Received OAuth token, logging in for Hugging Face...")
58
  TOKEN = oauth_token.token
 
59
  else:
60
  env_hf_token = os.environ.get("HF_TOKEN")
61
  if env_hf_token:
62
  TOKEN = env_hf_token
63
  print("Using environment variable HF_TOKEN for Hugging Face.")
 
64
  else:
65
- print("No Hugging Face OAuth token received and HF_TOKEN environment variable not set.")
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
 
 
 
 
 
 
 
67
  if fal_key_from_ui and fal_key_from_ui.strip():
68
  FAL_KEY = fal_key_from_ui.strip()
69
- elif os.environ.get("FAL_KEY"):
70
- if FAL_KEY == os.environ.get("FAL_KEY"):
71
- print("Using FAL_KEY from environment variable.")
 
 
 
 
 
 
72
  else:
73
- FAL_KEY = os.environ.get("FAL_KEY")
74
- print("Using FAL_KEY from environment variable (UI input was blank).")
75
- gr.Info("FAL_KEY has been set from environment variable.")
76
-
77
- else:
78
- print("FAL_KEY not provided in UI or environment.")
79
- FAL_KEY = None
80
-
81
- if not TOKEN:
82
- gr.Warning("Hugging Face token not set. Image generation via HF Inference Providers might fail.")
83
- else:
84
- gr.Info("Hugging Face token is configured.")
85
-
86
- if not FAL_KEY:
87
  gr.Warning("FAL_KEY not set. Video generation will not work.")
88
  else:
89
  gr.Info("FAL_KEY is configured.")
@@ -207,18 +225,26 @@ css = """
207
  with gr.Blocks(css=css) as demo:
208
  demo.load(login, inputs=None, outputs=None)
209
  with gr.Sidebar():
210
- gr.Markdown("# Inference Provider")
211
  gr.Markdown(
212
- "This Space showcases the black‑forest‑labs/FLUX.1‑dev model, served by the nebius API. Sign in with your Hugging Face account to use this API."
213
  )
214
- hf_login_button = gr.LoginButton("Sign in")
 
 
 
 
 
 
215
  fal_key_input = gr.Textbox(
216
- label="FAL_KEY",
217
  placeholder="Enter your FAL API Key here",
218
  type="password",
219
  value=FAL_KEY if FAL_KEY else "", # Pre-fill if loaded from env
220
  )
221
- hf_login_button.click(fn=login, inputs=[hf_login_button, fal_key_input], outputs=None)
 
 
222
  with gr.Column(elem_id="col-container"):
223
  gr.Markdown(
224
  """# FLUX.1 [schnell] with fal‑ai through HF Inference Providers ⚡\nLearn more about HF Inference Providers [here](https://huggingface.co/docs/inference-providers/index)"""
@@ -298,7 +324,7 @@ with gr.Blocks(css=css) as demo:
298
  outputs=[result, seed_number],
299
  ).then(
300
  lambda img_path, vid_accordion, vid_btn: { # Make video section interactive
301
- vid_accordion: gr.Accordion(open=True, interactive=True),
302
  vid_btn: gr.Button(interactive=True),
303
  },
304
  inputs=[result],
 
43
  return local_path
44
 
45
 
46
+ def login(oauth_token: gr.OAuthToken | None):
47
  """
48
+ Login to Hugging Face and check initial key statuses.
49
 
50
  Args:
51
  oauth_token (gr.OAuthToken | None): The OAuth token from Hugging Face.
 
52
  """
53
+ global TOKEN
54
 
55
  if oauth_token and oauth_token.token:
56
  print("Received OAuth token, logging in for Hugging Face...")
57
  TOKEN = oauth_token.token
58
+ gr.Info("Hugging Face token is configured from OAuth.")
59
  else:
60
  env_hf_token = os.environ.get("HF_TOKEN")
61
  if env_hf_token:
62
  TOKEN = env_hf_token
63
  print("Using environment variable HF_TOKEN for Hugging Face.")
64
+ gr.Info("Hugging Face token is configured from HF_TOKEN environment variable.")
65
  else:
66
+ if not TOKEN:
67
+ print("No Hugging Face OAuth token received and HF_TOKEN environment variable not set.")
68
+ gr.Warning("Hugging Face token not set. Image generation via HF Inference Providers might fail.")
69
+ else: # It was already set, and no new OAuth token, so it's still configured
70
+ gr.Info("Hugging Face token remains configured (no new OAuth token).")
71
+
72
+ if oauth_token is None:
73
+ if FAL_KEY:
74
+ print("FAL_KEY found in environment on app load.")
75
+ gr.Info("FAL_KEY is configured from environment variable.")
76
+ else:
77
+ print("FAL_KEY not found in environment on app load.")
78
+ gr.Warning("FAL_KEY not set. Video generation will not work until set.")
79
+
80
 
81
+ def set_fal_key(fal_key_from_ui: str | None):
82
+ """
83
+ Sets the FAL API key from the UI.
84
+ Args:
85
+ fal_key_from_ui (str | None): The FAL key from the UI textbox.
86
+ """
87
+ global FAL_KEY
88
  if fal_key_from_ui and fal_key_from_ui.strip():
89
  FAL_KEY = fal_key_from_ui.strip()
90
+ print("FAL_KEY has been set from UI input.")
91
+ gr.Info("FAL_KEY has been updated from UI input.")
92
+ elif not fal_key_from_ui and FAL_KEY:
93
+ print("FAL_KEY UI input cleared. Relying on previous value (if any) or environment variable.")
94
+ env_fal_key = os.environ.get("FAL_KEY")
95
+ if env_fal_key:
96
+ FAL_KEY = env_fal_key
97
+ print("FAL_KEY UI input empty, using key from environment variable.")
98
+ gr.Info("FAL_KEY is configured from environment variable (UI input was empty).")
99
  else:
100
+ FAL_KEY = None
101
+ print("FAL_KEY UI input empty and not found in environment. FAL_KEY is now unset.")
102
+ gr.Warning("FAL_KEY is not set. Video generation will not work.")
103
+ elif not FAL_KEY:
104
+ print("FAL_KEY not provided in UI and not in environment.")
 
 
 
 
 
 
 
 
 
105
  gr.Warning("FAL_KEY not set. Video generation will not work.")
106
  else:
107
  gr.Info("FAL_KEY is configured.")
 
225
  with gr.Blocks(css=css) as demo:
226
  demo.load(login, inputs=None, outputs=None)
227
  with gr.Sidebar():
228
+ gr.Markdown("# Authentication")
229
  gr.Markdown(
230
+ "Sign in with Hugging Face for image generation. Separately, set your FAL_KEY for video generation."
231
  )
232
+
233
+ gr.Markdown("### Hugging Face Login")
234
+ hf_login_button = gr.LoginButton("Sign in with Hugging Face")
235
+ # When hf_login_button is clicked, it provides an OAuthToken or None to the login function.
236
+ hf_login_button.click(fn=login, inputs=[hf_login_button], outputs=None)
237
+
238
+ gr.Markdown("### FAL API Key (for Video Generation)")
239
  fal_key_input = gr.Textbox(
240
+ label="FAL API Key",
241
  placeholder="Enter your FAL API Key here",
242
  type="password",
243
  value=FAL_KEY if FAL_KEY else "", # Pre-fill if loaded from env
244
  )
245
+ set_fal_key_button = gr.Button("Set FAL Key")
246
+ set_fal_key_button.click(fn=set_fal_key, inputs=[fal_key_input], outputs=None)
247
+
248
  with gr.Column(elem_id="col-container"):
249
  gr.Markdown(
250
  """# FLUX.1 [schnell] with fal‑ai through HF Inference Providers ⚡\nLearn more about HF Inference Providers [here](https://huggingface.co/docs/inference-providers/index)"""
 
324
  outputs=[result, seed_number],
325
  ).then(
326
  lambda img_path, vid_accordion, vid_btn: { # Make video section interactive
327
+ vid_accordion: gr.Accordion(open=True),
328
  vid_btn: gr.Button(interactive=True),
329
  },
330
  inputs=[result],