nexora / app.py
ChengyiX's picture
Serve Nexora routes after Gradio startup
b12c4d7 verified
Raw History Blame
1.4 kB
import os
from pathlib import Path
import sys
import tarfile
import time
import gradio as gr
import spaces
ROOT = Path(__file__).resolve().parent
ARCHIVE = ROOT / "nexora-bundle.tar.gz"
if ARCHIVE.is_file() and not (ROOT / "static" / "index.html").is_file():
with tarfile.open(ARCHIVE, "r:gz") as bundle:
bundle.extractall(ROOT)
os.environ.setdefault("STATIC_DIR", str(ROOT / "static"))
os.environ.setdefault("PANOR_CATALOG_PATH", str(ROOT / "deploy" / "panor-catalog.json"))
sys.path.insert(0, str(ROOT))
from backend.app.main import app as api_app
@spaces.GPU
def zero_gpu_runtime_marker():
return None
with gr.Blocks() as zero_gpu_app:
trigger = gr.Button(visible=False)
trigger.click(zero_gpu_runtime_marker, outputs=None)
def attach_nexora_routes() -> None:
fallback_routes = [
route for route in api_app.router.routes if getattr(route, "path", None) == "/{path:path}"
]
api_routes = [route for route in api_app.router.routes if route not in fallback_routes]
zero_gpu_app.app.router.routes = [
*api_routes,
*zero_gpu_app.app.router.routes,
*fallback_routes,
]
if __name__ == "__main__":
zero_gpu_app.launch(
server_name="0.0.0.0",
server_port=int(os.environ.get("PORT", "7860")),
prevent_thread_lock=True,
)
attach_nexora_routes()
while True:
time.sleep(3600)