"""Entry point for a Hugging Face Space with the Gradio SDK (free, unlike Docker). A Gradio Space runs `python app.py` and expects a server on port 7860: this serves the demo API (server.py) there, with a minimal Gradio page mounted at /gradio. On the free tier a Gradio Space gets ZeroGPU hardware. The GPU is deliberately not used: it is metered per visitor (a couple of minutes a day) and queued, which does not suit a score recomputed on every keystroke, and Laya answers in a fraction of a second on CPU anyway. So inference is pinned to the CPU. """ import os # ZeroGPU refuses to start a Space that declares no @spaces.GPU function, so one is declared but # never called. `spaces` must be imported before anything that imports torch (it patches CUDA # initialisation); outside Hugging Face the package is absent or has no effect. try: import spaces @spaces.GPU def _zerogpu_placeholder(): """Never called: only satisfies ZeroGPU's startup check.""" except Exception as error: # absent locally; any other failure shows in the Space logs print(f"spaces unavailable ({type(error).__name__}: {error})", flush=True) # One model on CPU keeps memory and latency reasonable; override in the Space settings. os.environ.setdefault("LAYA_BACKEND", "torch") os.environ.setdefault("LAYA_MODELS", "typed") os.environ.setdefault("LAYA_DEVICE", "cpu") # About 1 s per request on the Space's CPU: wait for a pause in typing before calling the model. os.environ.setdefault("LAYA_DEBOUNCE", "700") import gradio as gr # noqa: E402 import uvicorn # noqa: E402 from server import MODELS, app # noqa: E402 with gr.Blocks(title="Laya Demo API") as info: gr.Markdown( "## Laya Demo API\n" f"Modèles chargés : {', '.join(MODELS.values())}.\n\n" "La démo est sur `/`, l'état de l'API sur `/health`." ) # Spaces set GRADIO_SSR_MODE, which starts a Node server on port 7860 and leaves uvicorn unable # to bind it ("address already in use"); this page needs no server-side rendering. app = gr.mount_gradio_app(app, info, path="/gradio", ssr_mode=False) def report_zerogpu_startup(): """Send ZeroGPU the startup report that `spaces` normally sends from `gr.Blocks.launch()`. This app is served by uvicorn, so `launch()` never runs and ZeroGPU stops the Space ("No @spaces.GPU function detected during startup"). `spaces.zero.startup` only exists on ZeroGPU hardware; elsewhere this does nothing. """ try: from spaces import zero except Exception: return if hasattr(zero, "startup"): zero.startup() if __name__ == "__main__": report_zerogpu_startup() uvicorn.run(app, host="0.0.0.0", port=7860)