"""Resolve the current HF user from Gradio's OAuthProfile. `gr.LoginButton()` populates `gr.OAuthProfile` for every callback that declares it as a parameter. We add a `DEBUG_USER` escape hatch for local development, gated on the SPACE_ID env var so it can never fire in production. """ import os def is_production(): """True when running inside the HF Space sandbox (vs local dev).""" return os.environ.get("SPACE_ID") is not None def resolve_user(profile): """Returns the HF username of the requesting user, or None if not logged in. `profile` is the `gr.OAuthProfile | None` Gradio passes to callbacks that declare it. In local dev, set DEBUG_USER=alice to pretend to be `alice`. """ if not is_production(): debug = os.environ.get("DEBUG_USER") if debug: return debug if profile is None: return None return profile.username