Spaces:
Sleeping
Sleeping
Fix session cookies for embedded HF Space login
Browse files
app.py
CHANGED
|
@@ -21,16 +21,29 @@ from werkzeug.utils import secure_filename
|
|
| 21 |
|
| 22 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 23 |
from pipeline import SecondLifePipeline
|
| 24 |
-
import database as db
|
| 25 |
-
|
| 26 |
-
app = Flask(__name__)
|
| 27 |
-
app.secret_key = os.environ.get("SECRET_KEY", secrets.token_hex(32))
|
| 28 |
-
app.config["JSON_SORT_KEYS"] = False
|
| 29 |
-
app.config["MAX_CONTENT_LENGTH"] = 10 * 1024 * 1024 # 10 MB upload limit
|
| 30 |
-
app.config["TEMPLATES_AUTO_RELOAD"] = True
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# ---------------------------------------------------------------------------
|
| 36 |
# Pipeline boot (background thread — server comes up immediately)
|
|
|
|
| 21 |
|
| 22 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 23 |
from pipeline import SecondLifePipeline
|
| 24 |
+
import database as db
|
| 25 |
+
|
| 26 |
+
app = Flask(__name__)
|
| 27 |
+
app.secret_key = os.environ.get("SECRET_KEY", secrets.token_hex(32))
|
| 28 |
+
app.config["JSON_SORT_KEYS"] = False
|
| 29 |
+
app.config["MAX_CONTENT_LENGTH"] = 10 * 1024 * 1024 # 10 MB upload limit
|
| 30 |
+
app.config["TEMPLATES_AUTO_RELOAD"] = True
|
| 31 |
+
|
| 32 |
+
_RUNNING_ON_HF_SPACE = bool(os.environ.get("SPACE_ID"))
|
| 33 |
+
if _RUNNING_ON_HF_SPACE:
|
| 34 |
+
# Hugging Face renders Spaces under huggingface.co as an embedded app, so
|
| 35 |
+
# the session cookie must be explicitly marked for cross-site use.
|
| 36 |
+
app.config["SESSION_COOKIE_SAMESITE"] = "None"
|
| 37 |
+
app.config["SESSION_COOKIE_SECURE"] = True
|
| 38 |
+
app.config["SESSION_COOKIE_PARTITIONED"] = True
|
| 39 |
+
if not os.environ.get("SECRET_KEY"):
|
| 40 |
+
print(
|
| 41 |
+
"[app] WARNING: SECRET_KEY is not set. Sessions will reset when the Space restarts.",
|
| 42 |
+
file=sys.stderr,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
UPLOAD_DIR = Path(__file__).parent / "uploads" / "patient_docs"
|
| 46 |
+
ALLOWED_EXTENSIONS = {".pdf", ".docx", ".doc", ".txt", ".png", ".jpg", ".jpeg"}
|
| 47 |
|
| 48 |
# ---------------------------------------------------------------------------
|
| 49 |
# Pipeline boot (background thread — server comes up immediately)
|