diff --git a/demo/login_with_huggingface/run.ipynb b/demo/login_with_huggingface/run.ipynb new file mode 100644 index 0000000000..dbb9058f2a --- /dev/null +++ b/demo/login_with_huggingface/run.ipynb @@ -0,0 +1 @@ +{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: login_with_huggingface"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["from __future__ import annotations\n", "\n", "import gradio as gr\n", "from huggingface_hub import whoami\n", "\n", "def hello(profile: gr.OAuthProfile | None) -> str:\n", " if profile is None:\n", " return \"I don't know you.\"\n", " return f\"Hello {profile.name}\"\n", "\n", "def list_organizations(oauth_token: gr.OAuthToken | None) -> str:\n", " if oauth_token is None:\n", " return \"Please deploy this on Spaces and log in to list organizations.\"\n", " org_names = [org[\"name\"] for org in whoami(oauth_token.token)[\"orgs\"]]\n", " return f\"You belong to {', '.join(org_names)}.\"\n", "\n", "with gr.Blocks() as demo:\n", " gr.LoginButton()\n", " m1 = gr.Markdown()\n", " m2 = gr.Markdown()\n", " demo.load(hello, inputs=None, outputs=m1)\n", " demo.load(list_organizations, inputs=None, outputs=m2)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file diff --git a/demo/login_with_huggingface/run.py b/demo/login_with_huggingface/run.py new file mode 100644 index 0000000000..980dd2babd --- /dev/null +++ b/demo/login_with_huggingface/run.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +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 list_organizations(oauth_token: gr.OAuthToken | None) -> str: + if oauth_token is None: + return "Please deploy this on Spaces and log in to list organizations." + org_names = [org["name"] for org in whoami(oauth_token.token)["orgs"]] + return f"You belong to {', '.join(org_names)}." + +with gr.Blocks() as demo: + gr.LoginButton() + m1 = gr.Markdown() + m2 = gr.Markdown() + demo.load(hello, inputs=None, outputs=m1) + demo.load(list_organizations, inputs=None, outputs=m2) + +if __name__ == "__main__": + demo.launch() diff --git a/guides/04_additional-features/07_sharing-your-app.md b/guides/04_additional-features/07_sharing-your-app.md index 351ad0a997..877de5e53f 100644 --- a/guides/04_additional-features/07_sharing-your-app.md +++ b/guides/04_additional-features/07_sharing-your-app.md @@ -275,30 +275,7 @@ token by adding a parameter of type `gr.OAuthToken`. You must define which scope 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: - return "I don't know you." - return f"Hello {profile.name}" - -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(oauth_token.token)["orgs"]] - return f"You belong to {', '.join(org_names)}." - -with gr.Blocks() as demo: - gr.LoginButton() - m1 = gr.Markdown() - m2 = gr.Markdown() - demo.load(hello, inputs=None, outputs=m1) - demo.load(list_organizations, inputs=None, outputs=m2) - -demo.launch() -``` +$code_login_with_huggingface When the user clicks on the login button, they get redirected in a new page to authorize your Space. diff --git a/scripts/copy_demos.py b/scripts/copy_demos.py index 2de1d5df3e..c57f5f787b 100644 --- a/scripts/copy_demos.py +++ b/scripts/copy_demos.py @@ -23,12 +23,14 @@ def copy_all_demos(source_dir: str, dest_dir: str): "fake_gan", "fake_diffusion_with_gif", "file_explorer_component_events", + "gradio_pdf_demo", "image_mod_default_image", "image_editor_events", "image_segmentation", "interface_random_slider", "kitchen_sink", "kitchen_sink_random", + "login_with_huggingface", "matrix_transpose", "mini_leaderboard", "model3D",