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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpers to handle OAuth in a FastAPI app #2684

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Nov 29, 2024

This PR imports the code to handle OAuth in gradio to huggingface_hub. Goal is to make it available for non-Gradio apps (say autotrain-advanced for instance @abhishekkrthakur).

I also added tests to make sure the workflow still works correctly. Thanks @coyotte508 for huggingface/huggingface.js#1050 and advices :)

Missing parts:

  • docstrings and documentation

I have flagged this feature as "experimental" to be able to introduce breaking changes in the future.
Note that for now, the integration only works for Spaces integrations but the same codebase could be tweaked to handle any kind of websites. That's lower priority though (the JS integration is much better for that).


Demo: https://huggingface.co/spaces/Wauplin/fastapi-oauth

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Contributor

@hanouticelina hanouticelina left a comment

Choose a reason for hiding this comment

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

thanks a lot @Wauplin for the detailed code comments and the test suite ❤️ i guess this will be ready to merge after adding the documentation and fixing the test. also, let's open a PR in Gradio to remove the duplicated code after the release!

The org's profile picture URL. OpenID Connect field.
is_enterprise (`bool`):
Whether the org is an enterprise org. Hugging Face field.
can_pay (`Optional[bool]`, *optional*):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
can_pay (`Optional[bool]`, *optional*):
can_pay (`bool`, *optional*):

same for the other optional attributes in this dataclass and OAuthUserInfo dataclass

from starlette.middleware.sessions import SessionMiddleware
except ImportError as e:
raise ImportError(
"Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit)

Suggested change
"Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
"Cannot initialize OAuth due to a missing library. Please run `pip install huggingface_hub[oauth]` or add "

Comment on lines +65 to +67
# Little hack for the tests to work
# On staging, the redirect_url can be only "http://localhost:3000". Here I'm simply mocking the URL to work and
# remove any query parameters from the URL. In the test after the Hub call, we will replace back the URL with the
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines +161 to +162
def test_get_mocked_oauth_info():
oauth_info = _get_mocked_oauth_info()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def test_get_mocked_oauth_info():
oauth_info = _get_mocked_oauth_info()
def test_get_mocked_oauth_info(monkeypatch):
monkeypatch.setenv("HF_TOKEN", TOKEN)
oauth_info = _get_mocked_oauth_info()

This should fix the failing test. Alternatively, one can patch the get_token() function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants