From a1c24dbf2ecace4db8692b7ed434f1ebfc4de75a Mon Sep 17 00:00:00 2001 From: Lucain Date: Thu, 14 Mar 2024 15:37:51 +0100 Subject: [PATCH] Fix OAuth + fix OAuth documentation + undocument logout button (#7697) * Remove mentions to gr.LogoutButton in docs * Fix session obj + update example * add changeset --------- Co-authored-by: gradio-pr-bot --- .changeset/plain-guests-attend.md | 5 +++++ gradio/oauth.py | 2 -- gradio/route_utils.py | 5 +++++ guides/01_getting-started/03_sharing-your-app.md | 7 ++++--- 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 .changeset/plain-guests-attend.md diff --git a/.changeset/plain-guests-attend.md b/.changeset/plain-guests-attend.md new file mode 100644 index 0000000000..58cd4d3c47 --- /dev/null +++ b/.changeset/plain-guests-attend.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Fix OAuth + fix OAuth documentation + undocument logout button diff --git a/gradio/oauth.py b/gradio/oauth.py index 0c8072fb27..fb1479e86c 100644 --- a/gradio/oauth.py +++ b/gradio/oauth.py @@ -196,7 +196,6 @@ class OAuthProfile(typing.Dict): # inherit from Dict for backward compatibility with gr.Blocks() as demo: gr.LoginButton() - gr.LogoutButton() gr.Markdown().attach_load_event(hello, None) """ @@ -242,7 +241,6 @@ class OAuthToken: with gr.Blocks() as demo: gr.LoginButton() - gr.LogoutButton() gr.Markdown().attach_load_event(list_organizations, None) """ diff --git a/gradio/route_utils.py b/gradio/route_utils.py index 8df92fecca..d076e945d6 100644 --- a/gradio/route_utils.py +++ b/gradio/route_utils.py @@ -82,6 +82,11 @@ class Obj: return True return False + def get(self, item, default=None): + if item in self: + return self.__dict__[item] + return default + def keys(self): return self.__dict__.keys() diff --git a/guides/01_getting-started/03_sharing-your-app.md b/guides/01_getting-started/03_sharing-your-app.md index 9af3520982..e00fe71207 100644 --- a/guides/01_getting-started/03_sharing-your-app.md +++ b/guides/01_getting-started/03_sharing-your-app.md @@ -263,7 +263,7 @@ Note: Gradio's built-in authentication provides a straightforward and basic laye Gradio natively supports OAuth login via Hugging Face. In other words, you can easily add a _"Sign in with Hugging Face"_ button to your demo, which allows you to get a user's HF username as well as other information from their HF profile. Check out [this Space](https://huggingface.co/spaces/Wauplin/gradio-oauth-demo) for a live demo. To enable OAuth, you must set `hf_oauth: true` as a Space metadata in your README.md file. This will register your Space -as an OAuth application on Hugging Face. Next, you can use `gr.LoginButton` and `gr.LogoutButton` to add login and logout buttons to +as an OAuth application on Hugging Face. Next, you can use `gr.LoginButton` to add a login button to your Gradio app. Once a user is logged in with their HF account, you can retrieve their profile by adding a parameter of type `gr.OAuthProfile` to any Gradio function. The user profile will be automatically injected as a parameter value. If you want to perform actions on behalf of the user (e.g. list user's private repos, create repo, etc.), you can retrieve the user @@ -274,7 +274,7 @@ Here is a short example: ```py import gradio as gr - +from huggingface_hub import whoami def hello(profile: gr.OAuthProfile | None) -> str: if profile is None: @@ -289,11 +289,12 @@ def list_organizations(oauth_token: gr.OAuthToken | None) -> str: with gr.Blocks() as demo: gr.LoginButton() - gr.LogoutButton() m1 = gr.Markdown() m2 = gr.Markdown() demo.load(hello, inputs=None, outputs=m1) demo.load(list_organizations, inputs=None, outputs=m2) + +demo.launch() ``` When the user clicks on the login button, they get redirected in a new page to authorize your Space.