Spaces:
Runtime error
Runtime error
| 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.LogoutButton() | |
| gr.Markdown().attach_load_event(hello, None) | |
| demo.launch() | |