"""Entrypoint for the Distinct server on HuggingFace Spaces. This version is optimized for Spaces execution, where the Gradio app must be available at module level. """ from __future__ import annotations import os import sys from distinct_protocol.netpolicy import announce_bind, launch_kwargs from distinct_server.auth_routes import attach as attach_auth from distinct_server.identity import NotConfigured, refuse_mocked_oauth from distinct_server.presentation import CSS, HEAD from distinct_server.ui import build_app, distinct_theme # Build the app at module level for Spaces demo = build_app() # Apply presentation settings demo.css = CSS demo.theme = distinct_theme() demo.head = HEAD # For Spaces: the app is ready to be served as-is # HuggingFace will automatically launch this Gradio app if __name__ == "__main__": # Local development path try: refuse_mocked_oauth() except NotConfigured as reason: print(f"distinct: {reason}", file=sys.stderr) raise SystemExit(2) from None network = launch_kwargs() announce_bind(str(network["server_name"]), int(network["server_port"])) served = demo.queue(default_concurrency_limit=16, max_size=256) presentation = { "show_error": os.environ.get("DISTINCT_SHOW_ERRORS") == "1", "theme": distinct_theme(), "css": CSS, "head": HEAD, } import fastapi import gradio as gr import uvicorn api = fastapi.FastAPI(docs_url=None, redoc_url=None, openapi_url=None) if attach_auth(api): print( "distinct: sign-in at /auth/login, callback at /auth/callback.", file=sys.stderr, flush=True, ) mounted = gr.mount_gradio_app(api, served, path="/", **presentation) uvicorn.run( mounted, host=str(network["server_name"]), port=int(network["server_port"]), ) else: served.launch(**presentation, **network)