Spaces:
Runtime error
Runtime error
finally?
Browse files- .gitattributes +0 -3
- app.py +30 -4
- auth.py +60 -0
- gradio-3.37.1689678355-py3-none-any.whl +0 -3
- patch_gradio.py +31 -0
- requirements.txt +2 -1
.gitattributes
CHANGED
|
@@ -33,6 +33,3 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
-
|
| 37 |
-
# whl
|
| 38 |
-
*.whl filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
|
|
|
|
|
| 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,20 +29,41 @@ def show_profile(request: gr.Request) -> str:
|
|
| 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 "
|
| 28 |
return "Please login first"
|
| 29 |
-
return TEMPLATE.format(**session["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
with gr.Row():
|
| 34 |
-
gr.
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
|
|
| 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 |
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.location.assign('{url}');}}"
|
| 40 |
+
# return f"function() {{window.open('{url}', '_blank');}}"
|
| 41 |
|
| 42 |
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
with gr.Row():
|
| 45 |
+
login_button = gr.Button("Login")
|
| 46 |
+
login_button.click(None, None, None, _js=js_open(f"{URL}/login/huggingface"))
|
| 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()
|
auth.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import hashlib
|
| 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 |
+
print("this one")
|
| 50 |
+
print(request.session)
|
| 51 |
+
token = await oauth.huggingface.authorize_access_token(request)
|
| 52 |
+
request.session["user"] = token["userinfo"] # TODO: we should store entire token
|
| 53 |
+
return RedirectResponse("/")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def attach_oauth(app: FastAPI) -> None:
|
| 57 |
+
app.add_middleware(SessionMiddleware, secret_key=hashlib.sha256(OAUTH_CLIENT_SECRET.encode()).hexdigest())
|
| 58 |
+
app.get("/login/huggingface")(oauth_login)
|
| 59 |
+
app.get("/login/callback")(oauth_redirect_callback)
|
| 60 |
+
app.get("/logout")(oauth_logout)
|
gradio-3.37.1689678355-py3-none-any.whl
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:3b4cfad28418833f7fee6dec1a31ede548f30c886bb3254559f15d4662c66b4b
|
| 3 |
-
size 2819959
|
|
|
|
|
|
|
|
|
|
|
|
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
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
authlib==1.2.1
|
| 2 |
+
itsdangerous==2.1.2
|