File size: 2,132 Bytes
f265752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Quiet the container logs down to signal.

The Space logs were dominated by three sources of noise that bury the lines
that actually matter (phase timings, the READY banner, real errors):

  * `Exception ignored in: <function BaseEventLoop.__del__>` with
    `ValueError: Invalid file descriptor: -1`, twice per boot. This is the
    garbage collector finalising an event loop that is already closed, during
    Gradio's SSR startup. Python prints it via the unraisable hook and then
    discards it -- it is unreachable by try/except and affects nothing.
  * `StarletteDeprecationWarning: 'HTTP_422_UNPROCESSABLE_ENTITY' is
    deprecated`, emitted by Gradio on essentially every queue join.
  * A MONAI `FutureWarning` about `Orientationd(labels=...)` on every run.

Each is suppressed narrowly, by exact type and message, so an unrelated
ValueError or a different deprecation still surfaces.
"""
from __future__ import annotations

import os
import sys
import warnings

_original_unraisablehook = sys.unraisablehook


def _quiet_unraisable(unraisable):
    """Drop only the closed-event-loop finaliser noise; forward everything else."""
    exc = unraisable.exc_value
    if (
        isinstance(exc, ValueError)
        and "Invalid file descriptor" in str(exc)
        and getattr(unraisable.object, "__qualname__", "").endswith("BaseEventLoop.__del__")
    ):
        return
    _original_unraisablehook(unraisable)


def configure() -> None:
    """Install the log filters. Safe to call once, early, before Gradio imports."""
    sys.unraisablehook = _quiet_unraisable

    warnings.filterwarnings(
        "ignore",
        message=r".*HTTP_422_UNPROCESSABLE_ENTITY.*",
    )
    warnings.filterwarnings(
        "ignore",
        category=FutureWarning,
        module=r"monai\..*",
        message=r".*Orientationd.*",
    )

    # hf_hub progress bars produce dozens of carriage-return lines per file and
    # are redundant now that prefetch reports a duration per model. Only set the
    # default: an explicit value in the Space environment still wins.
    os.environ.setdefault("HF_HUB_DISABLE_PROGRESS_BARS", "1")