stillerman commited on
Commit
e0359a1
Β·
1 Parent(s): bbd8771

check token permissions

Browse files
Files changed (1) hide show
  1. app.py +89 -1
app.py CHANGED
@@ -291,9 +291,70 @@ def generate_images(
291
  except json.JSONDecodeError:
292
  return "❌ Error: Invalid response from server", ""
293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  def deploy_for_user(token_id: str, token_secret: str) -> Tuple[str, str, str]:
295
  """
296
- Deploy a Modal endpoint for a user using their API key. Unless the user has already done this, its the first thing you must do in order to train a LoRA.
297
 
298
  This function deploys a Modal script using the user's Modal API token ID and secret,
299
  allowing them to set up their own training endpoint. You can create them at https://modal.com/settings/tokens
@@ -421,6 +482,33 @@ with gr.Blocks(title="FluxFoundry LoRA Training", theme=gr.themes.Soft()) as app
421
 
422
  gr.Markdown("---")
423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  # Training Section
425
  gr.Markdown("## 🎯 Start Training")
426
  gr.Markdown("After deploying your endpoint above, use it to train LoRA models.")
 
291
  except json.JSONDecodeError:
292
  return "❌ Error: Invalid response from server", ""
293
 
294
+ def check_model_access(hf_token: str) -> str:
295
+ """
296
+ Check if the user has access to the gated FLUX.1-dev model.
297
+
298
+ This function verifies that the user's HuggingFace token has access to the
299
+ gated FLUX.1-dev model required for LoRA training. This has to be done before we can deploy the endpoint.
300
+
301
+ Parameters:
302
+ - hf_token (str, required): HuggingFace access token, format: "hf_xxxxxxxxxxxx"
303
+
304
+ Returns:
305
+ - str: Status message indicating access status and next steps
306
+
307
+ Example usage:
308
+ status = check_model_access("hf_abcdef123456")
309
+ """
310
+
311
+ if not hf_token or not hf_token.strip():
312
+ return "❌ Error: HuggingFace token is required"
313
+
314
+ try:
315
+ # Try to import huggingface_hub - if not available, give instructions
316
+ try:
317
+ from huggingface_hub import HfApi
318
+ except ImportError:
319
+ return "❌ Error: huggingface_hub not installed. Please run: pip install huggingface_hub"
320
+
321
+ # Initialize HF API with token
322
+ api = HfApi(token=hf_token.strip())
323
+ model_name = "black-forest-labs/FLUX.1-dev"
324
+
325
+ try:
326
+ # Attempt to get repository info - this will fail if no access
327
+ repo_info = api.repo_info(repo_id=model_name, repo_type="model")
328
+
329
+ message = "βœ… **Access Confirmed!**\n\n"
330
+ message += f"You have access to the **{model_name}** model.\n"
331
+ message += "You can proceed with LoRA training."
332
+ return message
333
+
334
+ except Exception as access_error:
335
+ # Check if it's specifically an access/permission error
336
+ error_str = str(access_error).lower()
337
+ if any(keyword in error_str for keyword in ["gated", "access", "permission", "401", "403", "repo_not_found"]):
338
+ message = "πŸ”’ **Access Required**\n\n"
339
+ message += f"You don't have access to the gated **{model_name}** model.\n\n"
340
+ message += "**To gain access:**\n"
341
+ message += f"1. Visit: https://huggingface.co/{model_name}\n"
342
+ message += "2. Click **'Agree and access repository'**\n"
343
+ message += "3. Accept the license terms\n"
344
+ message += "4. Wait a few minutes for access to be granted\n"
345
+ message += "5. Come back and check access again\n\n"
346
+ message += "**Note:** You need to be logged into HuggingFace with the same account used to create your access token."
347
+ return message
348
+ else:
349
+ # Some other error occurred
350
+ return f"❌ **Error checking access**\n\nError details: {str(access_error)}"
351
+
352
+ except Exception as e:
353
+ return f"❌ **Unexpected error**\n\nFailed to check model access: {str(e)}"
354
+
355
  def deploy_for_user(token_id: str, token_secret: str) -> Tuple[str, str, str]:
356
  """
357
+ Deploy a Modal endpoint for a user using their API key. Unless the user has already done this, this must be done before they can train a LoRA.
358
 
359
  This function deploys a Modal script using the user's Modal API token ID and secret,
360
  allowing them to set up their own training endpoint. You can create them at https://modal.com/settings/tokens
 
482
 
483
  gr.Markdown("---")
484
 
485
+ # Model Access Check Section
486
+ gr.Markdown("## πŸ”’ Check Model Access")
487
+ gr.Markdown("""
488
+ Before training, verify that your HuggingFace token has access to the gated FLUX.1-dev model.
489
+ """)
490
+
491
+ with gr.Row():
492
+ with gr.Column():
493
+ hf_token_check = gr.Textbox(
494
+ label="HuggingFace Token",
495
+ placeholder="hf_...",
496
+ type="password",
497
+ info="Your HuggingFace access token"
498
+ )
499
+ with gr.Column():
500
+ check_access_btn = gr.Button("πŸ” Check Access", variant="secondary", size="lg")
501
+
502
+ access_status = gr.Markdown(label="Access Status")
503
+
504
+ check_access_btn.click(
505
+ fn=check_model_access,
506
+ inputs=[hf_token_check],
507
+ outputs=[access_status]
508
+ )
509
+
510
+ gr.Markdown("---")
511
+
512
  # Training Section
513
  gr.Markdown("## 🎯 Start Training")
514
  gr.Markdown("After deploying your endpoint above, use it to train LoRA models.")