Wauplin HF staff commited on
Commit
1248eba
1 Parent(s): c28891c
Files changed (3) hide show
  1. app.py +10 -62
  2. app2.py +42 -0
  3. patch_gradio.py +0 -31
app.py CHANGED
@@ -1,69 +1,17 @@
1
- import patch_gradio
2
-
3
  import gradio as gr
4
- import gradio.utils
5
-
6
- from auth import attach_oauth
7
-
8
-
9
- if gradio.utils.get_space() is not None:
10
- URL, PORT = "https://wauplin-gradio-oauth-test.hf.space", 7860
11
- else:
12
- URL, PORT = "http://localhost:5173", 5173
13
-
14
- TEMPLATE = """
15
- ### Name: {name}
16
- ### Username: {preferred_username}
17
- ### Profile: {profile}
18
- ### Website: {website}
19
-
20
- ![Profile Picture]({picture})
21
-
22
- You can manage your connected applications in your [settings](https://huggingface.co/settings/connected-applications).
23
- """
24
-
25
-
26
- def show_profile(request: gr.Request) -> str:
27
- # request.session in case of websockets (see `def get_request_params`)
28
- # request.request.session in case of direct call
29
- session = getattr(request, "session", None) or getattr(request.request, "session", None)
30
- if session is None: # should never happen...
31
- return "No session attached"
32
- if "user" not in session:
33
- return "Please login first"
34
- return TEMPLATE.format(**session["user"])
35
 
36
 
37
- def js_open(url: str) -> str:
38
- # Taken from https://cmgdo.com/external-link-in-gradio-button/
39
- return f"function() {{window.open('{url}', '_blank');}}"
 
40
 
41
 
42
  with gr.Blocks() as demo:
43
- with gr.Row():
44
- login_button = gr.Button("Login")
45
- login_button.click(None, None, None, _js=js_open(f"{URL}/login/huggingface"))
46
-
47
- logout_button = gr.Button("Logout")
48
- logout_button.click(None, None, None, _js=js_open(f"{URL}/logout"))
49
-
50
- profile_btn = gr.Button("Show profile")
51
- output = gr.Markdown()
52
- profile_btn.click(fn=show_profile, outputs=output)
53
-
54
-
55
- old_create_app = gradio.networking.App.create_app
56
-
57
-
58
- def patched_create_app(*args, **kwargs):
59
- app = old_create_app(*args, **kwargs)
60
- attach_oauth(app)
61
- return app
62
-
63
-
64
- gradio.networking.App.create_app = patched_create_app
65
-
66
- print(URL)
67
 
68
- demo.queue()
69
- demo.launch(server_port=PORT)
 
 
 
1
  import gradio as gr
2
+ from typing import Optional
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
 
5
+ def hello(profile: Optional[gr.OAuthProfile]) -> str:
6
+ if profile is None:
7
+ return "I don't know you."
8
+ return f"Hello {profile.name}"
9
 
10
 
11
  with gr.Blocks() as demo:
12
+ gr.LoginButton()
13
+ gr.LogoutButton()
14
+ gr.Markdown().attach_load_event(hello, None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ print("http://localhost:5173")
17
+ demo.launch(server_port=5173)
app2.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import gradio as gr
4
+ import gradio.utils
5
+
6
+
7
+ if gradio.utils.get_space() is not None:
8
+ URL, PORT = "https://wauplin-gradio-oauth-test.hf.space", 7860
9
+ else:
10
+ URL, PORT = "http://localhost:5173", 5173
11
+
12
+ TEMPLATE = """
13
+ ### Name: {name}
14
+ ### Username: {preferred_username}
15
+ ### Profile: {profile}
16
+ ### Website: {website}
17
+
18
+ ![Profile Picture]({picture})
19
+
20
+ You can manage your connected applications in your [settings](https://huggingface.co/settings/connected-applications).
21
+ """
22
+
23
+
24
+ def show_profile(profile: gr.OAuthProfile) -> str:
25
+ # request.session in case of websockets (see `def get_request_params`)
26
+ # request.request.session in case of direct call
27
+ return TEMPLATE.format(**profile)
28
+
29
+
30
+ with gr.Blocks() as demo:
31
+ with gr.Row():
32
+ gr.LoginButton()
33
+ gr.LogoutButton()
34
+
35
+ profile_btn = gr.Button("Show profile")
36
+ output = gr.Markdown()
37
+ profile_btn.click(fn=show_profile, outputs=output)
38
+
39
+ print(URL)
40
+
41
+ demo.queue()
42
+ demo.launch(server_port=PORT)
patch_gradio.py DELETED
@@ -1,31 +0,0 @@
1
- import gradio.networking
2
- import gradio.queueing
3
-
4
- from auth import attach_oauth
5
-
6
-
7
- # 1. Patch => attach auth before starting app
8
-
9
- old_create_app = gradio.networking.App.create_app
10
-
11
-
12
- def patched_create_app(*args, **kwargs):
13
- app = old_create_app(*args, **kwargs)
14
- attach_oauth(app)
15
- return app
16
-
17
-
18
- gradio.networking.App.create_app = patched_create_app
19
-
20
- # 2. Patch => forward session info when using websockets (i.e. when queue is enabled which is the default on Spaces)
21
-
22
- old_get_request_params = gradio.queueing.Queue.get_request_params
23
-
24
-
25
- def new_get_request_params(self, websocket):
26
- params = old_get_request_params(self, websocket)
27
- params["session"] = websocket.session # Forward session info to the internal request
28
- return params
29
-
30
-
31
- gradio.queueing.Queue.get_request_params = new_get_request_params