Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OAuth + fix OAuth documentation + undocument logout button #7697

Merged
merged 3 commits into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plain-guests-attend.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Fix OAuth + fix OAuth documentation + undocument logout button
2 changes: 0 additions & 2 deletions gradio/oauth.py
Expand Up @@ -196,7 +196,6 @@ def hello(profile: Optional[gr.OAuthProfile]) -> str:

with gr.Blocks() as demo:
gr.LoginButton()
gr.LogoutButton()
gr.Markdown().attach_load_event(hello, None)
"""

Expand Down Expand Up @@ -242,7 +241,6 @@ def list_organizations(oauth_token: Optional[gr.OAuthToken]) -> str:

with gr.Blocks() as demo:
gr.LoginButton()
gr.LogoutButton()
gr.Markdown().attach_load_event(list_organizations, None)
"""

Expand Down
5 changes: 5 additions & 0 deletions gradio/route_utils.py
Expand Up @@ -82,6 +82,11 @@ def __contains__(self, item) -> bool:
return True
return False

def get(self, item, default=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to understand if we need this? Where do we call .get() on the request object? Note that we already have a __getitem__ which should provide a basic way to let this class be used as a dictionary

Copy link
Contributor Author

@Wauplin Wauplin Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if item in self:
return self.__dict__[item]
return default

def keys(self):
return self.__dict__.keys()

Expand Down
7 changes: 4 additions & 3 deletions guides/01_getting-started/03_sharing-your-app.md
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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.
Expand Down