import gradio as gr import gradio.blocks import gradio.networking import gradio.utils from auth import attach_oauth if gradio.utils.get_space() is not None: URL, PORT = "https://wauplin-gradio-oauth-test.hf.space", 7860 else: URL, PORT = "http://localhost:5173", 5173 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: fastapi_request = request.request if fastapi_request is None or "user" not in request.request.session: return "Please login first" return TEMPLATE.format(**request.request.session["user"]) def js_open(url: str) -> str: # Taken from https://cmgdo.com/external-link-in-gradio-button/ return f"function() {{window.open('{url}', '_blank');}}" with gr.Blocks() as demo: login_button = gr.Button("Login") login_button.click(None, None, None, _js=js_open(f"{URL}/login/huggingface")) logout_button = gr.Button("Logout") logout_button.click(None, None, None, _js=js_open(f"{URL}/logout")) profile_btn = gr.Button("Show profile") output = gr.Markdown() profile_btn.click(fn=show_profile, outputs=output) old_create_app = gradio.networking.App.create_app def patched_create_app(*args, **kwargs): app = old_create_app(*args, **kwargs) attach_oauth(app) return app gradio.networking.App.create_app = patched_create_app print(URL) demo.launch(server_port=PORT)