File size: 1,561 Bytes
ccec886
3110228
4de0319
 
ccec886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4de0319
 
ccec886
 
 
 
4de0319
 
 
bde0655
4de0319
 
 
 
bde0655
4de0319
 
ccec886
4de0319
ccec886
3110228
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
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()