Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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
|
|
|
|
| 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
|
| 29 |
-
# This
|
| 30 |
-
|
| 31 |
|
| 32 |
# 4. If the call succeeds, format a success message
|
| 33 |
return f"""
|
| 34 |
### <span style='color: green;'>Access Granted β
</span>
|
| 35 |
-
|
| 36 |
|
| 37 |
-
- **
|
| 38 |
-
- **
|
| 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
|
| 49 |
return f"""
|
| 50 |
### <span style='color: red;'>Access Denied π΄</span>
|
| 51 |
-
You do not have
|
| 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
|
| 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
|
| 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],
|