Spaces:
Running
Running
upload from gradio
Browse files- app.py +4 -30
- auth.py +0 -58
- patch_gradio.py +0 -31
- requirements.txt +2 -2
app.py
CHANGED
@@ -1,11 +1,6 @@
|
|
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:
|
@@ -29,41 +24,20 @@ def show_profile(request: gr.Request) -> str:
|
|
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 "
|
33 |
return "Please login first"
|
34 |
-
return TEMPLATE.format(**session["
|
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.location.assign('{url}');}}"
|
40 |
-
# return f"function() {{window.open('{url}', '_blank');}}"
|
41 |
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
with gr.Row():
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
logout_button = gr.Button("Logout")
|
49 |
-
logout_button.click(None, None, None, _js=js_open(f"{URL}/logout"))
|
50 |
|
51 |
profile_btn = gr.Button("Show profile")
|
52 |
output = gr.Markdown()
|
53 |
profile_btn.click(fn=show_profile, outputs=output)
|
54 |
|
55 |
-
|
56 |
-
old_create_app = gradio.networking.App.create_app
|
57 |
-
|
58 |
-
|
59 |
-
def patched_create_app(*args, **kwargs):
|
60 |
-
app = old_create_app(*args, **kwargs)
|
61 |
-
attach_oauth(app)
|
62 |
-
return app
|
63 |
-
|
64 |
-
|
65 |
-
gradio.networking.App.create_app = patched_create_app
|
66 |
-
|
67 |
print(URL)
|
68 |
|
69 |
demo.queue()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import gradio.utils
|
3 |
|
|
|
|
|
|
|
4 |
if gradio.utils.get_space() is not None:
|
5 |
URL, PORT = "https://wauplin-gradio-oauth-test.hf.space", 7860
|
6 |
else:
|
|
|
24 |
session = getattr(request, "session", None) or getattr(request.request, "session", None)
|
25 |
if session is None: # should never happen...
|
26 |
return "No session attached"
|
27 |
+
if "oauth_info" not in session:
|
28 |
return "Please login first"
|
29 |
+
return TEMPLATE.format(**session["oauth_info"]["userinfo"])
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
with gr.Blocks() as demo:
|
33 |
with gr.Row():
|
34 |
+
gr.LoginButton()
|
35 |
+
gr.LogoutButton()
|
|
|
|
|
|
|
36 |
|
37 |
profile_btn = gr.Button("Show profile")
|
38 |
output = gr.Markdown()
|
39 |
profile_btn.click(fn=show_profile, outputs=output)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
print(URL)
|
42 |
|
43 |
demo.queue()
|
auth.py
DELETED
@@ -1,58 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
from authlib.integrations.starlette_client import OAuth
|
4 |
-
from fastapi import FastAPI
|
5 |
-
from fastapi.requests import Request
|
6 |
-
from fastapi.responses import RedirectResponse
|
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")
|
13 |
-
OPENID_PROVIDER_URL = os.environ.get("OPENID_PROVIDER_URL")
|
14 |
-
|
15 |
-
for value in (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_SCOPES, OPENID_PROVIDER_URL):
|
16 |
-
if value is None:
|
17 |
-
raise ValueError("Missing environment variable")
|
18 |
-
|
19 |
-
USER_INFO_URL = OPENID_PROVIDER_URL + "/oauth/userinfo"
|
20 |
-
METADATA_URL = OPENID_PROVIDER_URL + "/.well-known/openid-configuration"
|
21 |
-
|
22 |
-
oauth = OAuth()
|
23 |
-
oauth.register(
|
24 |
-
name="huggingface",
|
25 |
-
client_id=OAUTH_CLIENT_ID,
|
26 |
-
client_secret=OAUTH_CLIENT_SECRET,
|
27 |
-
client_kwargs={"scope": OAUTH_SCOPES},
|
28 |
-
server_metadata_url=METADATA_URL,
|
29 |
-
)
|
30 |
-
|
31 |
-
# Hack to close the login/logout page once the user is logged in/out.
|
32 |
-
# TODO: can it be less hacky?
|
33 |
-
# CLOSE_WINDOW_HTML = HTMLResponse("<script>window.close();</script>")
|
34 |
-
|
35 |
-
|
36 |
-
async def oauth_login(request: Request):
|
37 |
-
redirect_uri = str(request.url_for("oauth_redirect_callback"))
|
38 |
-
if ".hf.space" in redirect_uri: # In Space, FastAPI redirect as http but we want https
|
39 |
-
redirect_uri = redirect_uri.replace("http://", "https://")
|
40 |
-
return await oauth.huggingface.authorize_redirect(request, redirect_uri)
|
41 |
-
|
42 |
-
|
43 |
-
async def oauth_logout(request: Request) -> RedirectResponse:
|
44 |
-
request.session.pop("user", None)
|
45 |
-
return RedirectResponse("/")
|
46 |
-
|
47 |
-
|
48 |
-
async def oauth_redirect_callback(request: Request) -> RedirectResponse:
|
49 |
-
token = await oauth.huggingface.authorize_access_token(request)
|
50 |
-
request.session["user"] = token["userinfo"] # TODO: we should store entire token
|
51 |
-
return RedirectResponse("/")
|
52 |
-
|
53 |
-
|
54 |
-
def attach_oauth(app: FastAPI) -> None:
|
55 |
-
app.add_middleware(SessionMiddleware, secret_key="session-secret-key") # TODO: make this is secret key
|
56 |
-
app.get("/login/huggingface")(oauth_login)
|
57 |
-
app.get("/login/callback")(oauth_redirect_callback)
|
58 |
-
app.get("/logout")(oauth_logout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
-
|
2 |
-
|
|
|
1 |
+
# Install from Gradio PR https://github.com/gradio-app/gradio/pull/4943
|
2 |
+
https://gradio-builds.s3.amazonaws.com/6d40cdc7c516434d498d6b1bf129cfaa1d5a7eb3/gradio-3.36.1-py3-none-any.whl
|