Spaces:
Running
Running
fix app when queued
Browse files- app.py +10 -9
- auth.py +1 -0
- patch_gradio.py +31 -0
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import gradio.blocks
|
3 |
-
import gradio.networking
|
4 |
import gradio.utils
|
5 |
|
6 |
from auth import attach_oauth
|
7 |
|
|
|
8 |
if gradio.utils.get_space() is not None:
|
9 |
URL, PORT = "https://wauplin-gradio-oauth-test.hf.space", 7860
|
10 |
else:
|
@@ -23,14 +23,14 @@ You can manage your connected applications in your [settings](https://huggingfac
|
|
23 |
|
24 |
|
25 |
def show_profile(request: gr.Request) -> str:
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
if
|
32 |
return "Please login first"
|
33 |
-
return TEMPLATE.format(**
|
34 |
|
35 |
|
36 |
def js_open(url: str) -> str:
|
@@ -64,4 +64,5 @@ gradio.networking.App.create_app = patched_create_app
|
|
64 |
|
65 |
print(URL)
|
66 |
|
|
|
67 |
demo.launch(server_port=PORT)
|
|
|
1 |
+
|
2 |
import gradio as gr
|
|
|
|
|
3 |
import gradio.utils
|
4 |
|
5 |
from auth import attach_oauth
|
6 |
|
7 |
+
|
8 |
if gradio.utils.get_space() is not None:
|
9 |
URL, PORT = "https://wauplin-gradio-oauth-test.hf.space", 7860
|
10 |
else:
|
|
|
23 |
|
24 |
|
25 |
def show_profile(request: gr.Request) -> str:
|
26 |
+
# request.session in case of websockets (see `def get_request_params`)
|
27 |
+
# request.request.session in case of direct call
|
28 |
+
session = getattr(request, "session", None) or getattr(request.request, "session", None)
|
29 |
+
if session is None: # should never happen...
|
30 |
+
return "No session attached"
|
31 |
+
if "user" not in session:
|
32 |
return "Please login first"
|
33 |
+
return TEMPLATE.format(**session["user"])
|
34 |
|
35 |
|
36 |
def js_open(url: str) -> str:
|
|
|
64 |
|
65 |
print(URL)
|
66 |
|
67 |
+
demo.queue()
|
68 |
demo.launch(server_port=PORT)
|
auth.py
CHANGED
@@ -6,6 +6,7 @@ from fastapi.requests import Request
|
|
6 |
from fastapi.responses import HTMLResponse
|
7 |
from starlette.middleware.sessions import SessionMiddleware
|
8 |
|
|
|
9 |
OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
|
10 |
OAUTH_CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET")
|
11 |
OAUTH_SCOPES = os.environ.get("OAUTH_SCOPES")
|
|
|
6 |
from fastapi.responses import HTMLResponse
|
7 |
from starlette.middleware.sessions import SessionMiddleware
|
8 |
|
9 |
+
|
10 |
OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
|
11 |
OAUTH_CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET")
|
12 |
OAUTH_SCOPES = os.environ.get("OAUTH_SCOPES")
|
patch_gradio.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|