Add more demos to deploy scripts (#9127)

* add demos

* tweak demo
This commit is contained in:
Abubakar Abid 2024-08-15 13:49:41 -07:00 committed by GitHub
parent 30b5d6f2b7
commit 8a3e1e289e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 24 deletions

View File

@ -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}

View File

@ -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()

View File

@ -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.

View File

@ -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",