Spaces:
Running
Turn Flask debug OFF by default and pre-warm models without blocking startup
Browse filesNothing sets FLASK_DEBUG in the Dockerfile, startup.sh or the Space config, and
both reads defaulted it to "true" — so the deployed Space served the Werkzeug
interactive debugger on the public internet, and CORS took the localhost-only
dev branch instead of the intended production one.
The default is now off (opt in locally with FLASK_DEBUG=true), which also means
_prewarm_models() no longer gets skipped, so the first swap doesn't pay for
loading buffalo_l + inswapper + GFPGAN on the request path. It runs in a daemon
thread so the port still binds immediately — startup.sh backgrounds the model
downloads for exactly that reason, since HF kills a container that doesn't
answer on its port within ~60s.
Verified locally: "Debug mode: off", port bound while the pre-warm ran, and
/api/locations served 200 during it.
15 unmodified lines161718192021214 unmodified lines236237238239240241242654 unmodified lines89789889990090190290390490515 unmodified linesimport ioimport reimport base64import tracebackimport mimetypes214 unmodified linesapp = Flask(__name__, static_folder="static")app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024 # 50 MB_debug_mode = os.environ.get("FLASK_DEBUG", "true").lower() == "true"CORS(app, origins=["http://localhost:5173", "http://127.0.0.1:5173"] if _debug_mode else "*")# Nothing is written to disk — uploads are decoded in memory and the result is654 unmodified linesif __name__ == "__main__":port = int(os.environ.get("PORT", 5000))debug = os.environ.get("FLASK_DEBUG", "true").lower() == "true"print(f"Starting Face Swap Web App on port {port}...")if not debug:_prewarm_models()app.run(debug=debug, use_reloader=False, host="0.0.0.0", port=port)15 unmodified lines16171819202122214 unmodified lines237238239240241242243654 unmodified lines89889990090190290390490590690790890991091191215 unmodified linesimport ioimport reimport base64import threadingimport tracebackimport mimetypes214 unmodified linesapp = Flask(__name__, static_folder="static")app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024 # 50 MB_debug_mode = os.environ.get("FLASK_DEBUG", "false").lower() == "true"CORS(app, origins=["http://localhost:5173", "http://127.0.0.1:5173"] if _debug_mode else "*")# Nothing is written to disk — uploads are decoded in memory and the result is654 unmodified linesif __name__ == "__main__":port = int(os.environ.get("PORT", 5000))# Defaults to OFF: nothing sets FLASK_DEBUG on the deployed Space, and a# true default there serves the Werkzeug debugger on the public internet.# Opt in locally with FLASK_DEBUG=true.debug = _debug_modeprint(f"Starting Face Swap Web App on port {port}...")if not debug:# In a daemon thread so the port binds immediately — startup.sh# backgrounds the model downloads for the same reason (HF Spaces kills# a container that doesn't answer on its port within ~60s).threading.Thread(target=_prewarm_models, daemon=True).start()app.run(debug=debug, use_reloader=False, host="0.0.0.0", port=port)