GradioTest / app.py
Nikolay Banar
update app
b733c3a
raw
history blame
1.55 kB
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()