File size: 1,305 Bytes
ccec886
4de0319
 
ccec886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4de0319
 
ccec886
 
 
 
4de0319
 
 
 
 
 
 
 
 
 
 
ccec886
4de0319
ccec886
4de0319
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import gradio as gr

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() {location.replace('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() {location.replace('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)

demo.launch(prevent_thread_lock=True, server_port=5173)
attach_oauth(demo.server_app)
demo.block_thread()