Spaces:
Sleeping
Sleeping
Add top-level direct login fallback for HF iframe
Browse files- hf_start.py +91 -0
hf_start.py
CHANGED
|
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import sys
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def running_on_hugging_face() -> bool:
|
|
@@ -25,13 +26,103 @@ def configure_iframe_cookies(app) -> None:
|
|
| 25 |
)
|
| 26 |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def main() -> int:
|
| 29 |
port = os.environ.get("PORT", "7860")
|
| 30 |
config = os.environ.get("POTATO_CONFIG", "config.yaml")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
from potato.flask_server import create_app
|
| 33 |
|
| 34 |
app = create_app(config)
|
|
|
|
| 35 |
if running_on_hugging_face():
|
| 36 |
configure_iframe_cookies(app)
|
| 37 |
print("Enabled Hugging Face iframe-compatible session cookies.", flush=True)
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
|
| 10 |
|
| 11 |
def running_on_hugging_face() -> bool:
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
|
| 29 |
+
def patch_login_template_for_iframe() -> None:
|
| 30 |
+
"""Patch Potato's login form so HF iframe login navigates top-level."""
|
| 31 |
+
import potato
|
| 32 |
+
|
| 33 |
+
template_path = Path(potato.__file__).resolve().parent / "templates" / "home.html"
|
| 34 |
+
template = template_path.read_text(encoding="utf-8")
|
| 35 |
+
marker = "<!-- HF_DIRECT_LOGIN_PATCH -->"
|
| 36 |
+
if marker in template:
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
patch = f"""
|
| 40 |
+
{marker}
|
| 41 |
+
<script>
|
| 42 |
+
(function() {{
|
| 43 |
+
function isInIframe() {{
|
| 44 |
+
try {{
|
| 45 |
+
return window.self !== window.top;
|
| 46 |
+
}} catch (error) {{
|
| 47 |
+
return true;
|
| 48 |
+
}}
|
| 49 |
+
}}
|
| 50 |
+
|
| 51 |
+
document.addEventListener('DOMContentLoaded', function() {{
|
| 52 |
+
if (!isInIframe()) return;
|
| 53 |
+
var form = document.querySelector('form[action="/auth"]');
|
| 54 |
+
var usernameInput = document.querySelector('#login-email');
|
| 55 |
+
if (!form || !usernameInput) return;
|
| 56 |
+
|
| 57 |
+
form.setAttribute('method', 'GET');
|
| 58 |
+
form.setAttribute('action', '/hf-direct-login');
|
| 59 |
+
form.setAttribute('target', '_top');
|
| 60 |
+
usernameInput.setAttribute('name', 'username');
|
| 61 |
+
|
| 62 |
+
var note = document.createElement('div');
|
| 63 |
+
note.className = 'potato-alert';
|
| 64 |
+
note.style.marginBottom = '0.75rem';
|
| 65 |
+
note.style.fontSize = '0.875rem';
|
| 66 |
+
note.textContent = 'Login will open the app directly so your browser can keep the session.';
|
| 67 |
+
form.parentNode.insertBefore(note, form);
|
| 68 |
+
}});
|
| 69 |
+
}}());
|
| 70 |
+
</script>
|
| 71 |
+
"""
|
| 72 |
+
template = template.replace("</body>", patch + "\n</body>")
|
| 73 |
+
template_path.write_text(template, encoding="utf-8")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def register_direct_login_route(app) -> None:
|
| 77 |
+
"""Register a cookie-friendly top-level login route for HF Spaces."""
|
| 78 |
+
from flask import redirect, request, session, url_for
|
| 79 |
+
from potato.authentication import UserAuthenticator
|
| 80 |
+
from potato.flask_server import (
|
| 81 |
+
UserPhase,
|
| 82 |
+
get_item_state_manager,
|
| 83 |
+
get_user_state_manager,
|
| 84 |
+
init_user_state,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
def hf_direct_login():
|
| 88 |
+
username = (request.args.get("username") or "").strip()
|
| 89 |
+
if not username:
|
| 90 |
+
return redirect(url_for("home"))
|
| 91 |
+
|
| 92 |
+
authenticator = UserAuthenticator.get_instance()
|
| 93 |
+
if not authenticator.is_valid_username(username):
|
| 94 |
+
authenticator.add_user(username, None)
|
| 95 |
+
|
| 96 |
+
session.clear()
|
| 97 |
+
session["username"] = username
|
| 98 |
+
session.permanent = True
|
| 99 |
+
|
| 100 |
+
user_state_manager = get_user_state_manager()
|
| 101 |
+
if not user_state_manager.has_user(username):
|
| 102 |
+
init_user_state(username)
|
| 103 |
+
|
| 104 |
+
user_state = user_state_manager.get_user_state(username)
|
| 105 |
+
if user_state and user_state.get_phase() == UserPhase.LOGIN:
|
| 106 |
+
user_state_manager.advance_phase(username)
|
| 107 |
+
if user_state and not user_state.has_assignments():
|
| 108 |
+
get_item_state_manager().assign_instances_to_user(user_state)
|
| 109 |
+
|
| 110 |
+
return redirect(url_for("annotate"))
|
| 111 |
+
|
| 112 |
+
app.add_url_rule("/hf-direct-login", "hf_direct_login", hf_direct_login, methods=["GET"])
|
| 113 |
+
|
| 114 |
+
|
| 115 |
def main() -> int:
|
| 116 |
port = os.environ.get("PORT", "7860")
|
| 117 |
config = os.environ.get("POTATO_CONFIG", "config.yaml")
|
| 118 |
|
| 119 |
+
if running_on_hugging_face():
|
| 120 |
+
patch_login_template_for_iframe()
|
| 121 |
+
|
| 122 |
from potato.flask_server import create_app
|
| 123 |
|
| 124 |
app = create_app(config)
|
| 125 |
+
register_direct_login_route(app)
|
| 126 |
if running_on_hugging_face():
|
| 127 |
configure_iframe_cookies(app)
|
| 128 |
print("Enabled Hugging Face iframe-compatible session cookies.", flush=True)
|