{"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 huggingface_hub "]}, {"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}