mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-21 01:01:05 +08:00
parent
30b5d6f2b7
commit
8a3e1e289e
1
demo/login_with_huggingface/run.ipynb
Normal file
1
demo/login_with_huggingface/run.ipynb
Normal 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}
|
25
demo/login_with_huggingface/run.py
Normal file
25
demo/login_with_huggingface/run.py
Normal 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()
|
@ -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.
|
||||
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user