import gradio as gr import gradio.blocks from auth import attach_oauth TEMPLATE = """ ### Name: {name} ### Username: {preferred_username} ### Profile: {profile} ### Website: {website} ![Profile Picture]({picture}) You can manage your connected applications in your [settings](https://huggingface.co/settings/connected-applications). """ def show_profile(request: gr.Request) -> str: if "user" not in request.request.session: return "Please login first" return TEMPLATE.format(**request.request.session["user"]) with gr.Blocks() as demo: # Taken from https://cmgdo.com/external-link-in-gradio-button/ login_button = gr.Button("Login") login_button.click( None, None, None, _js="function() {window.open('https://wauplin-gradio-oauth-test.hf.space/login/huggingface');}" ) # Taken from https://cmgdo.com/external-link-in-gradio-button/ logout_button = gr.Button("Logout", variant="secondary") logout_button.click(None, None, None, _js="function() {window.open('https://wauplin-gradio-oauth-test.hf.space/logout');}") profile_btn = gr.Button("Show profile") output = gr.Markdown() profile_btn.click(fn=show_profile, outputs=output) old_start_server = gradio.blocks.networking.start_server def patched_start_server(*args, **kwargs): server_name, server_port, local_url, app, server = old_start_server(*args, **kwargs) attach_oauth(app) return server_name, server_port, local_url, app, server gradio.blocks.networking.start_server = patched_start_server demo.launch()