Spaces:
Runtime error
Runtime error
push refactored app
Browse files
README.md
CHANGED
|
@@ -6,9 +6,9 @@ colorTo: pink
|
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.36.1
|
| 8 |
python_version: 3.10.6
|
| 9 |
-
app_file:
|
| 10 |
hf_oauth: true
|
| 11 |
-
hf_oauth_redirect_path: /
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.36.1
|
| 8 |
python_version: 3.10.6
|
| 9 |
+
app_file: app.py
|
| 10 |
hf_oauth: true
|
| 11 |
+
hf_oauth_redirect_path: /login/callback
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
TEMPLATE = """
|
|
@@ -15,13 +16,26 @@ You can manage your connected applications in your [settings](https://huggingfac
|
|
| 15 |
|
| 16 |
|
| 17 |
def show_profile(request: gr.Request) -> str:
|
|
|
|
|
|
|
| 18 |
return TEMPLATE.format(**request.request.session["user"])
|
| 19 |
|
| 20 |
|
| 21 |
with gr.Blocks() as demo:
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
output = gr.Markdown()
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from auth import attach_oauth
|
| 4 |
|
| 5 |
|
| 6 |
TEMPLATE = """
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
def show_profile(request: gr.Request) -> str:
|
| 19 |
+
if "user" not in request.request.session:
|
| 20 |
+
return "Please login first"
|
| 21 |
return TEMPLATE.format(**request.request.session["user"])
|
| 22 |
|
| 23 |
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
+
# Taken from https://cmgdo.com/external-link-in-gradio-button/
|
| 26 |
+
login_button = gr.Button("Login")
|
| 27 |
+
login_button.click(
|
| 28 |
+
None, None, None, _js="function() {location.replace('https://wauplin-gradio-oauth-test.hf.space/login/huggingface');}"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Taken from https://cmgdo.com/external-link-in-gradio-button/
|
| 32 |
+
logout_button = gr.Button("Logout", variant="secondary")
|
| 33 |
+
logout_button.click(None, None, None, _js="function() {location.replace('https://wauplin-gradio-oauth-test.hf.space/logout');}")
|
| 34 |
+
|
| 35 |
+
profile_btn = gr.Button("Show profile")
|
| 36 |
output = gr.Markdown()
|
| 37 |
+
profile_btn.click(fn=show_profile, outputs=output)
|
| 38 |
|
| 39 |
+
demo.launch(prevent_thread_lock=True, server_port=5173)
|
| 40 |
+
attach_oauth(demo.server_app)
|
| 41 |
+
demo.block_thread()
|
auth.py
CHANGED
|
@@ -30,18 +30,16 @@ oauth.register(
|
|
| 30 |
)
|
| 31 |
|
| 32 |
|
| 33 |
-
async def landing(request: Request):
|
| 34 |
-
if request.session.get("user"):
|
| 35 |
-
return RedirectResponse("/gradio")
|
| 36 |
-
else:
|
| 37 |
-
return RedirectResponse(request.url_for("oauth_login"))
|
| 38 |
-
|
| 39 |
-
|
| 40 |
async def oauth_login(request: Request):
|
| 41 |
redirect_uri = request.url_for("oauth_redirect_callback")
|
| 42 |
return await oauth.huggingface.authorize_redirect(request, redirect_uri)
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
async def oauth_redirect_callback(request: Request):
|
| 46 |
token = await oauth.huggingface.authorize_access_token(request)
|
| 47 |
|
|
@@ -49,21 +47,12 @@ async def oauth_redirect_callback(request: Request):
|
|
| 49 |
resp = await client.get(USER_INFO_URL, headers={"Authorization": f"Bearer {token['access_token']}"})
|
| 50 |
user_info = resp.json()
|
| 51 |
|
| 52 |
-
request.session["user"] = user_info
|
| 53 |
return RedirectResponse(request.url_for("landing"))
|
| 54 |
|
| 55 |
|
| 56 |
-
|
| 57 |
-
if request.url.path.startswith("/gradio") and not request.session.get("user"): # protected route but not authenticated
|
| 58 |
-
return RedirectResponse("/")
|
| 59 |
-
return await call_next(request)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def get_app() -> FastAPI:
|
| 63 |
-
app = FastAPI()
|
| 64 |
-
app.middleware("http")(check_oauth)
|
| 65 |
app.add_middleware(SessionMiddleware, secret_key="session-secret-key") # TODO: make this is secret key
|
| 66 |
-
app.get("/")(
|
| 67 |
-
app.get("/
|
| 68 |
-
app.get("/
|
| 69 |
-
return app
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
async def oauth_login(request: Request):
|
| 34 |
redirect_uri = request.url_for("oauth_redirect_callback")
|
| 35 |
return await oauth.huggingface.authorize_redirect(request, redirect_uri)
|
| 36 |
|
| 37 |
|
| 38 |
+
async def oauth_logout(request: Request):
|
| 39 |
+
request.session.pop("user", None)
|
| 40 |
+
return RedirectResponse("/")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
async def oauth_redirect_callback(request: Request):
|
| 44 |
token = await oauth.huggingface.authorize_access_token(request)
|
| 45 |
|
|
|
|
| 47 |
resp = await client.get(USER_INFO_URL, headers={"Authorization": f"Bearer {token['access_token']}"})
|
| 48 |
user_info = resp.json()
|
| 49 |
|
| 50 |
+
request.session["user"] = user_info # TODO: we should store token instead
|
| 51 |
return RedirectResponse(request.url_for("landing"))
|
| 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)
|
|
|
start.py
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
import subprocess
|
| 2 |
-
|
| 3 |
-
subprocess.run("uvicorn app:app --host 0.0.0.0 --port 7860 --forwarded-allow-ips '*'", shell=True)
|
|
|
|
|
|
|
|
|
|
|
|