badaoui HF Staff commited on
Commit
3a95276
Β·
verified Β·
1 Parent(s): ef7b1e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -20
app.py CHANGED
@@ -1,13 +1,11 @@
1
  import gradio as gr
2
- from huggingface_hub import HfApi
3
  from huggingface_hub.utils import HfHubHTTPError, RepositoryNotFoundError
4
 
5
- # Initialize the Hugging Face Hub API client
6
- api = HfApi()
7
-
8
  def check_model_access(model_id: str, oauth_token: gr.OAuthToken | None):
9
  """
10
- Checks if the logged-in user has access to a given model on the Hub.
 
11
 
12
  Args:
13
  model_id: The ID of the model to check (e.g., "meta-llama/Llama-2-7b-chat-hf").
@@ -25,18 +23,17 @@ def check_model_access(model_id: str, oauth_token: gr.OAuthToken | None):
25
  return "### <span style='color: orange;'>Input Missing 🟑</span>\nPlease enter a model ID to check."
26
 
27
  try:
28
- # 3. The core of the test: try to get model info using the user's token
29
- # This single API call is enough to verify read access.
30
- model_info = api.model_info(repo_id=model_id, token=oauth_token.token)
31
 
32
  # 4. If the call succeeds, format a success message
33
  return f"""
34
  ### <span style='color: green;'>Access Granted βœ…</span>
35
- You have access to **{model_id}**.
36
 
37
- - **Author:** {model_info.author}
38
- - **Last Modified:** {model_info.last_modified}
39
- - **Gated:** {'Yes' if model_info.gated else 'No'}
40
  """
41
 
42
  except RepositoryNotFoundError:
@@ -45,24 +42,23 @@ def check_model_access(model_id: str, oauth_token: gr.OAuthToken | None):
45
 
46
  except HfHubHTTPError as e:
47
  # 6. Handle HTTP errors, which typically indicate permission issues for gated models
48
- if e.response.status_code == 401 or e.response.status_code == 403:
49
  return f"""
50
  ### <span style='color: red;'>Access Denied πŸ”΄</span>
51
- You do not have access to **{model_id}**.
52
 
53
  - Please ensure you have accepted the terms and conditions on the model's page.
54
- - This might be a private model you don't have permission for.
55
  - **Status Code:** {e.response.status_code}
56
  """
57
  else:
58
  return f"### <span style='color: red;'>An Error Occurred πŸ”΄</span>\n**Details:** {str(e)}"
59
 
60
- # --- Gradio Interface ---
61
  with gr.Blocks(css="h1 { text-align: center; }") as demo:
62
  gr.Markdown("# Gated Model Access Tester")
63
- gr.Markdown("Log in with your Hugging Face account and enter a model ID to see if you have access.")
64
 
65
- # The LoginButton is the key to enabling OAuth
66
  gr.LoginButton()
67
 
68
  with gr.Row():
@@ -75,8 +71,6 @@ with gr.Blocks(css="h1 { text-align: center; }") as demo:
75
 
76
  result_display = gr.Markdown("### Result will be displayed here.")
77
 
78
- # When the button is clicked, call the check_model_access function.
79
- # Gradio automatically finds the oauth_token from the session and passes it.
80
  check_button.click(
81
  fn=check_model_access,
82
  inputs=[model_id_input],
 
1
  import gradio as gr
2
+ from transformers import AutoConfig
3
  from huggingface_hub.utils import HfHubHTTPError, RepositoryNotFoundError
4
 
 
 
 
5
  def check_model_access(model_id: str, oauth_token: gr.OAuthToken | None):
6
  """
7
+ Checks if the logged-in user can load the configuration for a given model on the Hub.
8
+ Loading the config proves file-level access to the repository.
9
 
10
  Args:
11
  model_id: The ID of the model to check (e.g., "meta-llama/Llama-2-7b-chat-hf").
 
23
  return "### <span style='color: orange;'>Input Missing 🟑</span>\nPlease enter a model ID to check."
24
 
25
  try:
26
+ # 3. The core test: try to load the model's config using the user's token.
27
+ # This will fail if the user doesn't have access to the gated repo's files.
28
+ config = AutoConfig.from_pretrained(pretrained_model_name_or_path=model_id, token=oauth_token.token)
29
 
30
  # 4. If the call succeeds, format a success message
31
  return f"""
32
  ### <span style='color: green;'>Access Granted βœ…</span>
33
+ Successfully loaded the configuration for **{model_id}**.
34
 
35
+ - **Model Type:** `{config.model_type}`
36
+ - **Architecture:** `{config.architectures[0] if config.architectures else 'N/A'}`
 
37
  """
38
 
39
  except RepositoryNotFoundError:
 
42
 
43
  except HfHubHTTPError as e:
44
  # 6. Handle HTTP errors, which typically indicate permission issues for gated models
45
+ if e.response.status_code in [401, 403]:
46
  return f"""
47
  ### <span style='color: red;'>Access Denied πŸ”΄</span>
48
+ You do not have permission to download files from **{model_id}**.
49
 
50
  - Please ensure you have accepted the terms and conditions on the model's page.
51
+ - This might be a private model you don't have access to.
52
  - **Status Code:** {e.response.status_code}
53
  """
54
  else:
55
  return f"### <span style='color: red;'>An Error Occurred πŸ”΄</span>\n**Details:** {str(e)}"
56
 
57
+ # --- Gradio Interface (No changes needed here) ---
58
  with gr.Blocks(css="h1 { text-align: center; }") as demo:
59
  gr.Markdown("# Gated Model Access Tester")
60
+ gr.Markdown("Log in with your Hugging Face account and enter a model ID. This will attempt to load the model's `config.json` file to verify access.")
61
 
 
62
  gr.LoginButton()
63
 
64
  with gr.Row():
 
71
 
72
  result_display = gr.Markdown("### Result will be displayed here.")
73
 
 
 
74
  check_button.click(
75
  fn=check_model_access,
76
  inputs=[model_id_input],