igerasimov's picture
Add a minimal ZeroGPU compatibility hook for Hugging Face Spaces
3be5901
Raw
History Blame Contribute Delete
1.47 kB
"""Hugging Face Spaces launcher for the GCMD classifier MVP."""
import os
import sys
from pathlib import Path
SRC_PATH = Path(__file__).resolve().parent / "src"
if SRC_PATH.exists() and str(SRC_PATH) not in sys.path:
sys.path.insert(0, str(SRC_PATH))
try:
import spaces
except ImportError:
class _SpacesFallback:
@staticmethod
def GPU(function=None):
if function is None:
return lambda inner_function: inner_function
return function
spaces = _SpacesFallback()
from gcmd_classifier.ui.gradio_app import GRADIO_CSS, create_demo # noqa: E402
@spaces.GPU
def _zerogpu_startup_probe() -> str:
"""No-op ZeroGPU startup probe for Hugging Face Spaces detection."""
return "ok"
demo = create_demo()
def gradio_server_name() -> str:
"""Return the configured Gradio bind host for local and Spaces runtime."""
return os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
def gradio_server_port() -> int:
"""Return the configured Gradio bind port for local and Spaces runtime."""
return int(os.environ.get("GRADIO_SERVER_PORT", "7860"))
def launch() -> None:
"""Launch the Gradio demo with Hugging Face compatible server settings."""
demo.queue().launch(
css=GRADIO_CSS,
server_name=gradio_server_name(),
server_port=gradio_server_port(),
share=False,
prevent_thread_lock=False,
)
if __name__ == "__main__":
launch()