File size: 1,539 Bytes
c0683ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from typing import Optional, List
import requests


def list_org_users() -> List[str]:
    return [
        user["user"]  # e.g. "julien-c"
        for user in requests.get("https://huggingface.co/api/organizations/OAuthTesters/members").json()
    ]


def hello(profile: Optional[gr.OAuthProfile]) -> str:
    if profile is None:
        return "## You must be logged in to access this section."
    if profile["preferred_username"] not in list_org_users():
        return (
            "## You must be a member of the OAuthTesters organization to access this section.\n\nGo to"
            " https://huggingface.co/organizations/OAuthTesters to join."
        )
    return (
        "## Yay! You are part of the team and can see this very secret section!\n\n<img"
        " src='https://huggingface.co/spaces/Wauplin/gradio-oauth-test/resolve/main/happy.gif' height='700'>"
    )


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.\n\nIt shows"
        " how you can use it to restrict access only to members of an organization. First it requires users to log in"
        " and then checks if the user meets the requirements (been part of"
        " [OAuthTesters](https://huggingface.co/OAuthTesters) in this case)\n\nSee"
        " https://github.com/gradio-app/gradio/pull/4943 for technical details."
    )
    with gr.Row():
        gr.LoginButton()
    gr.Markdown().attach_load_event(hello, None)

demo.launch()