badaoui HF Staff commited on
Commit
b81e0a9
Β·
verified Β·
1 Parent(s): 7f1f39f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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").
14
+ oauth_token: The user's OAuth token, automatically injected by Gradio.
15
+
16
+ Returns:
17
+ A string with the result of the access check, formatted for Markdown.
18
+ """
19
+ # 1. Check if the user is logged in
20
+ if oauth_token is None:
21
+ return "### <span style='color: red;'>Authentication Error πŸ”΄</span>\nPlease log in using the button above to check model access."
22
+
23
+ # 2. Check if a model ID was provided
24
+ if not model_id:
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:
43
+ # 5. Handle the case where the repository does not exist
44
+ return f"### <span style='color: red;'>Not Found πŸ”΄</span>\nThe repository **{model_id}** does not exist."
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():
69
+ model_id_input = gr.Textbox(
70
+ label="Model ID",
71
+ placeholder="e.g., meta-llama/Llama-2-7b-chat-hf",
72
+ scale=3,
73
+ )
74
+ check_button = gr.Button("Check Access", variant="primary", scale=1)
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],
83
+ outputs=[result_display]
84
+ )
85
+
86
+ demo.launch()