File size: 905 Bytes
1bf5b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""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