File size: 904 Bytes
e63c592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
import os

from app.core.logging import get_logger

logger = get_logger(__name__)


def get_port(default: int = 7860) -> int:
    """Return the port the application should bind to.

    - Uses the PORT environment variable when set (e.g. on Hugging Face Spaces).
    - Falls back to the provided default (7860 for Spaces compatibility).
    - Logs a message indicating the chosen port and whether we appear to be
      running inside a Hugging Face Spaces environment.
    """
    raw = os.getenv("PORT")
    try:
        port = int(raw) if raw else default
    except (TypeError, ValueError):
        port = default

    # Heuristic to detect HF Spaces: SPACE_ID or SPACE_REPO_ID are usually set.
    hf_spaces_mode = bool(os.getenv("SPACE_ID") or os.getenv("SPACE_REPO_ID"))

    logger.info(
        "Starting on port=%d hf_spaces_mode=%s",
        port,
        hf_spaces_mode,
    )

    return port