File size: 1,866 Bytes
0d86759
 
ccec886
91bbd67
4de0319
0d86759
 
 
91bbd67
 
 
 
ccec886
 
 
 
 
 
 
 
 
 
 
 
 
 
1af2fc7
 
 
 
 
0d86759
4de0319
0d86759
 
 
 
 
3014630
91bbd67
 
 
61dd0e0
0d86759
 
 
 
 
4de0319
 
ccec886
4de0319
ccec886
0d86759
 
 
 
 
 
 
 
 
 
 
 
91bbd67
3110228
1af2fc7
91bbd67
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import patch_gradio

import gradio as gr
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:
    # request.session in case of websockets (see `def get_request_params`)
    # request.request.session in case of direct call
    session = getattr(request, "session", None) or getattr(request.request, "session", None)
    if session is None:  # should never happen...
        return "No session attached"
    if "user" not in session:
        return "Please login first"
    return TEMPLATE.format(**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:
    with gr.Row():
        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.queue()
demo.launch(server_port=PORT)