celinah HF Staff commited on
Commit
ab28053
·
1 Parent(s): d446c1c
Files changed (1) hide show
  1. app.py +18 -50
app.py CHANGED
@@ -8,29 +8,26 @@ import requests
8
  from dotenv import load_dotenv
9
  from huggingface_hub import InferenceClient
10
 
11
- FAL_KEY = None
12
-
13
-
14
  load_dotenv()
15
 
16
  MAX_SEED = np.iinfo(np.int32).max
17
  MAX_IMAGE_SIZE = 2048
18
  TOKEN = None
 
19
 
20
-
21
- def download_image_locally(image_url: str, local_path: str = "downloaded_image.png") -> str:
22
- """Download an image from a URL to a local path.
23
 
24
  Args:
25
- image_url (str): The URL of the image to download. Must be an http(s) URL.
26
- local_path (str, optional): The path (including filename) where the file should be saved. Defaults to "downloaded_image.png".
27
 
28
  Returns:
29
  str: The filesystem path of the saved file – suitable for returning to a **gr.File** output, or as an MCP tool response.
30
  """
31
  if local_path == "":
32
- local_path = "downloaded_image.png"
33
- response = requests.get(image_url, timeout=30)
34
  response.raise_for_status()
35
  # If the caller passed only a filename, save into a temporary directory to avoid permission issues
36
  if os.path.dirname(local_path) == "":
@@ -41,7 +38,7 @@ def download_image_locally(image_url: str, local_path: str = "downloaded_image.p
41
  return local_path
42
 
43
 
44
- def login(oauth_token: gr.OAuthToken | None):
45
  """
46
  Login to Hugging Face and check initial key statuses.
47
 
@@ -49,33 +46,15 @@ def login(oauth_token: gr.OAuthToken | None):
49
  oauth_token (gr.OAuthToken | None): The OAuth token from Hugging Face.
50
  """
51
  global TOKEN
52
-
53
  if oauth_token and oauth_token.token:
54
- print("Received OAuth token, logging in for Hugging Face...")
55
  TOKEN = oauth_token.token
56
- gr.Info("Hugging Face token is configured from OAuth.")
57
  else:
58
- env_hf_token = os.environ.get("HF_TOKEN")
59
- if env_hf_token:
60
- TOKEN = env_hf_token
61
- print("Using environment variable HF_TOKEN for Hugging Face.")
62
- gr.Info("Hugging Face token is configured from HF_TOKEN environment variable.")
63
- else:
64
- if not TOKEN:
65
- print("No Hugging Face OAuth token received and HF_TOKEN environment variable not set.")
66
- else:
67
- gr.Info("Hugging Face token remains configured (no new OAuth token).")
68
-
69
- if oauth_token is None:
70
- if FAL_KEY:
71
- print("FAL_KEY found in environment on app load.")
72
- gr.Info("FAL_KEY is configured from environment variable.")
73
- else:
74
- print("FAL_KEY not found in environment on app load.")
75
- gr.Warning("FAL_KEY not set. Video generation will not work until set.")
76
 
77
 
78
- def set_fal_key(fal_key_from_ui: str | None):
79
  """
80
  Sets the FAL API key from the UI.
81
  Args:
@@ -86,20 +65,9 @@ def set_fal_key(fal_key_from_ui: str | None):
86
  FAL_KEY = fal_key_from_ui.strip()
87
  os.environ["FAL_KEY"] = FAL_KEY
88
  print("FAL_KEY has been set from UI input.")
89
- gr.Info("FAL_KEY has been updated from UI input.")
90
- elif not fal_key_from_ui and FAL_KEY:
91
- print("FAL_KEY UI input cleared. Relying on previous value (if any) or environment variable.")
92
- env_fal_key = os.environ.get("FAL_KEY")
93
- if env_fal_key:
94
- FAL_KEY = env_fal_key
95
- gr.Info("FAL_KEY is configured from environment variable (UI input was empty).")
96
- else:
97
- FAL_KEY = None
98
- print("FAL_KEY UI input empty and not found in environment. FAL_KEY is now unset.")
99
- elif not FAL_KEY:
100
- print("FAL_KEY not provided in UI and not in environment.")
101
  else:
102
- gr.Info("FAL_KEY is configured.")
 
103
 
104
 
105
  def generate_image(prompt: str, seed: int = 42, width: int = 1024, height: int = 1024, num_inference_steps: int = 25):
@@ -219,8 +187,8 @@ css = """
219
  """
220
 
221
  with gr.Blocks(css=css) as demo:
222
- demo.load(login, inputs=None, outputs=None)
223
- demo.load(set_fal_key, inputs=None, outputs=None)
224
  with gr.Sidebar():
225
  gr.Markdown("# Authentication")
226
  gr.Markdown(
@@ -230,7 +198,7 @@ with gr.Blocks(css=css) as demo:
230
  gr.Markdown("### Hugging Face Login")
231
  hf_login_button = gr.LoginButton("Sign in with Hugging Face")
232
  # When hf_login_button is clicked, it provides an OAuthToken or None to the login function.
233
- hf_login_button.click(fn=login, inputs=[hf_login_button], outputs=None)
234
 
235
  gr.Markdown("### FAL Login (for Image to Video)")
236
  fal_key_input = gr.Textbox(
@@ -240,7 +208,7 @@ with gr.Blocks(css=css) as demo:
240
  value=os.environ.get("FAL_KEY", ""), # Pre-fill if loaded from env
241
  )
242
  set_fal_key_button = gr.Button("Set FAL Key")
243
- set_fal_key_button.click(fn=set_fal_key, inputs=[fal_key_input], outputs=None)
244
 
245
  with gr.Column(elem_id="col-container"):
246
  gr.Markdown(
 
8
  from dotenv import load_dotenv
9
  from huggingface_hub import InferenceClient
10
 
 
 
 
11
  load_dotenv()
12
 
13
  MAX_SEED = np.iinfo(np.int32).max
14
  MAX_IMAGE_SIZE = 2048
15
  TOKEN = None
16
+ FAL_KEY = None
17
 
18
+ def download_locally(url: str, local_path: str = "downloaded_file.png") -> str:
19
+ """Download an image or a video from a URL to a local path.
 
20
 
21
  Args:
22
+ url (str): The URL of the image to download. Must be an http(s) URL.
23
+ local_path (str, optional): The path (including filename) where the file should be saved. Defaults to "downloaded_file.png".
24
 
25
  Returns:
26
  str: The filesystem path of the saved file – suitable for returning to a **gr.File** output, or as an MCP tool response.
27
  """
28
  if local_path == "":
29
+ local_path = "downloaded_file.png"
30
+ response = requests.get(url, timeout=30)
31
  response.raise_for_status()
32
  # If the caller passed only a filename, save into a temporary directory to avoid permission issues
33
  if os.path.dirname(local_path) == "":
 
38
  return local_path
39
 
40
 
41
+ def login_hf(oauth_token: gr.OAuthToken | None):
42
  """
43
  Login to Hugging Face and check initial key statuses.
44
 
 
46
  oauth_token (gr.OAuthToken | None): The OAuth token from Hugging Face.
47
  """
48
  global TOKEN
 
49
  if oauth_token and oauth_token.token:
50
+ print("Received OAuth token, logging in...")
51
  TOKEN = oauth_token.token
 
52
  else:
53
+ print("No OAuth token provided, using environment variable HF_TOKEN.")
54
+ TOKEN = os.environ.get("HF_TOKEN")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
+ def login_fal(fal_key_from_ui: str | None):
58
  """
59
  Sets the FAL API key from the UI.
60
  Args:
 
65
  FAL_KEY = fal_key_from_ui.strip()
66
  os.environ["FAL_KEY"] = FAL_KEY
67
  print("FAL_KEY has been set from UI input.")
 
 
 
 
 
 
 
 
 
 
 
 
68
  else:
69
+ FAL_KEY = os.environ.get("FAL_KEY")
70
+ print("FAL_KEY is configured from environment variable.")
71
 
72
 
73
  def generate_image(prompt: str, seed: int = 42, width: int = 1024, height: int = 1024, num_inference_steps: int = 25):
 
187
  """
188
 
189
  with gr.Blocks(css=css) as demo:
190
+ demo.load(login_hf, inputs=None, outputs=None)
191
+ demo.load(login_fal, inputs=None, outputs=None)
192
  with gr.Sidebar():
193
  gr.Markdown("# Authentication")
194
  gr.Markdown(
 
198
  gr.Markdown("### Hugging Face Login")
199
  hf_login_button = gr.LoginButton("Sign in with Hugging Face")
200
  # When hf_login_button is clicked, it provides an OAuthToken or None to the login function.
201
+ hf_login_button.click(fn=login_hf, inputs=[hf_login_button], outputs=None)
202
 
203
  gr.Markdown("### FAL Login (for Image to Video)")
204
  fal_key_input = gr.Textbox(
 
208
  value=os.environ.get("FAL_KEY", ""), # Pre-fill if loaded from env
209
  )
210
  set_fal_key_button = gr.Button("Set FAL Key")
211
+ set_fal_key_button.click(fn=login_fal, inputs=[fal_key_input], outputs=None)
212
 
213
  with gr.Column(elem_id="col-container"):
214
  gr.Markdown(