File size: 1,546 Bytes
080ab33
5692a3b
 
 
 
 
080ab33
23d6cab
 
 
 
 
 
 
 
 
5692a3b
 
 
 
 
080ab33
5692a3b
 
 
 
23d6cab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b733c3a
5692a3b
 
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
44
45
46
import gradio as gr
from huggingface_hub import whoami
def hello(profile: gr.OAuthProfile | None) -> str:
    if profile is None:
        return "I don't know you."
    return f"Hello {profile.name}"

def is_user_in_organization(oauth_token: gr.OAuthToken | None, required_org: str) -> bool:
    if oauth_token is None:
        return False

    user_info = whoami(token=oauth_token.token)
    user_orgs = [org["name"] for org in user_info["orgs"]]

    return required_org in user_orgs

def list_organizations(oauth_token: gr.OAuthToken | None) -> str:
    if oauth_token is None:
        return "Please log in to list organizations."
    org_names = [org["name"] for org in whoami(token=oauth_token.token)["orgs"]]
    return f"You belong to {', '.join(org_names)}."

with gr.Blocks() as demo:
    gr.Markdown(
        "# Gradio OAuth Space\n\nThis Space is a demo for the new **Sign in with Hugging Face** feature."
    )
    with gr.Row():
        gr.LoginButton()
        gr.LogoutButton()

    with gr.Row():
        m1 = gr.Markdown()

    required_org = "YourOrganizationName"

    def update_interface(profile: gr.OAuthProfile | None):
        if profile and is_user_in_organization(profile.token, required_org):
            m1.update(f"Hello {profile.name}, welcome to the app.")
            # Load other components or functions relevant to authenticated users
        else:
            m1.update("Access denied. You do not belong to the required organization.")


    demo.load(update_interface, inputs=None, outputs=m1)

demo.launch()