File size: 1,442 Bytes
9e37410
 
2ce527c
bfb1433
9e37410
a1fd37a
13dc13f
a1fd37a
13dc13f
05d6cb0
5c13a8e
05d6cb0
 
 
 
 
 
 
 
 
 
5c13a8e
05d6cb0
 
30ec18a
 
 
5c13a8e
ef9edfd
05d6cb0
30ec18a
 
 
5c13a8e
 
9108b3d
05d6cb0
d13e562
 
 
ef9edfd
d13e562
4376153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()