import gradio as gr import os #from huggingface_hub import HfApi, hf_hub_url, whoami import json # Get authorized users from environment variable/secret authorized_users_str = os.environ.get("AUTHORIZED_USER_IDS", "") AUTHORIZED_USER_IDS = set(authorized_users_str.split(',') if authorized_users_str and authorized_users_str.strip() else []) # Function to check authorization def check_authorization(profile: gr.OAuthProfile | None): if profile is None: return False, "Not logged in. Please Login for full access.", False is_authorized = profile.username in AUTHORIZED_USER_IDS if is_authorized: message = f"Welcome {profile.username}! You are authorized." else: message = f"Sorry {profile.username}, you are not authorized." return is_authorized, message # Initialize UI components with gr.Blocks() as demo: # Create a State component to store authentication status auth_state = gr.State(False) login_button = gr.LoginButton() # Message component to show auth status auth_message = gr.Textbox(label="Authorization Status", interactive=False) status_message = gr.Textbox(label="Status", interactive=False) demo.load(check_authorization, inputs=None, outputs=[auth_state,status_message]) def test_func(auth_state): return auth_state login_button.click(fn=test_func,inputs=auth_state,outputs=auth_message) demo.launch()