yujiepan commited on
Commit
0d68c58
Β·
1 Parent(s): 2c3c98c

Add HuggingFace login support for private/gated models

Browse files
Files changed (1) hide show
  1. app.py +75 -1
app.py CHANGED
@@ -10,10 +10,45 @@ import sys
10
 
11
  import gradio as gr
12
  import torch
13
- from huggingface_hub import hf_hub_download, scan_cache_dir
14
  from safetensors import safe_open
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def get_param(model_id: str, param_key: str, log_buffer: io.StringIO, progress: gr.Progress):
18
  """
19
  Download and return a specific parameter tensor from a Hugging Face model.
@@ -345,6 +380,27 @@ custom_css = """
345
 
346
  with gr.Blocks(title="Hugging Face Model Weight Inspector") as demo:
347
  gr.Markdown("# πŸ” Hugging Face Model Weight Inspector")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
 
349
  with gr.Row():
350
  with gr.Column(scale=1):
@@ -393,6 +449,24 @@ with gr.Blocks(title="Hugging Face Model Weight Inspector") as demo:
393
  label="Status", interactive=False, lines=6)
394
 
395
  # Event handlers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
  list_keys_btn.click(
397
  fn=list_keys,
398
  inputs=[model_id_input],
 
10
 
11
  import gradio as gr
12
  import torch
13
+ from huggingface_hub import hf_hub_download, scan_cache_dir, login, whoami
14
  from safetensors import safe_open
15
 
16
 
17
+ def hf_login(token: str):
18
+ """Login to Hugging Face with provided token."""
19
+ if not token:
20
+ return "❌ Please provide a token", "Not logged in"
21
+
22
+ try:
23
+ login(token=token, add_to_git_credential=False)
24
+ user_info = whoami()
25
+ username = user_info.get('name', 'Unknown')
26
+ return f"βœ… Successfully logged in as: {username}", f"βœ… Logged in as {username}"
27
+ except Exception as e:
28
+ return f"❌ Login failed: {str(e)}", "❌ Not logged in"
29
+
30
+
31
+ def hf_logout():
32
+ """Logout from Hugging Face."""
33
+ try:
34
+ # Clear token by logging in with empty token
35
+ from huggingface_hub import logout
36
+ logout()
37
+ return "βœ… Successfully logged out", "Not logged in"
38
+ except Exception as e:
39
+ return f"❌ Logout failed: {str(e)}", "Status unknown"
40
+
41
+
42
+ def check_hf_status():
43
+ """Check current HF login status."""
44
+ try:
45
+ user_info = whoami()
46
+ username = user_info.get('name', 'Unknown')
47
+ return f"βœ… Currently logged in as: {username}", f"βœ… Logged in as {username}"
48
+ except Exception:
49
+ return "ℹ️ Not logged in", "Not logged in"
50
+
51
+
52
  def get_param(model_id: str, param_key: str, log_buffer: io.StringIO, progress: gr.Progress):
53
  """
54
  Download and return a specific parameter tensor from a Hugging Face model.
 
380
 
381
  with gr.Blocks(title="Hugging Face Model Weight Inspector") as demo:
382
  gr.Markdown("# πŸ” Hugging Face Model Weight Inspector")
383
+
384
+ # HF Login section
385
+ with gr.Accordion("πŸ” Hugging Face Login (for private/gated models)", open=False):
386
+ with gr.Row():
387
+ with gr.Column(scale=3):
388
+ hf_token_input = gr.Textbox(
389
+ label="HF Token",
390
+ placeholder="hf_...",
391
+ type="password",
392
+ )
393
+ with gr.Column(scale=2):
394
+ hf_status = gr.Textbox(
395
+ label="Status",
396
+ value="Not logged in",
397
+ interactive=False,
398
+ )
399
+ with gr.Row():
400
+ login_btn = gr.Button("πŸ”‘ Login", variant="primary", scale=1)
401
+ logout_btn = gr.Button("πŸšͺ Logout", variant="secondary", scale=1)
402
+ check_status_btn = gr.Button("ℹ️ Check Status", variant="secondary", scale=1)
403
+ login_output = gr.Textbox(label="Login Status", interactive=False, lines=2)
404
 
405
  with gr.Row():
406
  with gr.Column(scale=1):
 
449
  label="Status", interactive=False, lines=6)
450
 
451
  # Event handlers
452
+ login_btn.click(
453
+ fn=hf_login,
454
+ inputs=[hf_token_input],
455
+ outputs=[login_output, hf_status],
456
+ )
457
+
458
+ logout_btn.click(
459
+ fn=hf_logout,
460
+ inputs=[],
461
+ outputs=[login_output, hf_status],
462
+ )
463
+
464
+ check_status_btn.click(
465
+ fn=check_hf_status,
466
+ inputs=[],
467
+ outputs=[login_output, hf_status],
468
+ )
469
+
470
  list_keys_btn.click(
471
  fn=list_keys,
472
  inputs=[model_id_input],