CoolFace
Apppublic

build-small-hackathon/elysium

sourceHugging Faceupdated 3mo agoView on Hugging Face
1likes
app.py53 linesDownload Raw Back to root
1"""Elysium — gradio.Server entrypoint (matches small-talk pattern).
2
3We use `gr.Server` (a FastAPI host with Gradio's backend) so our custom
4frontend (Canvas2D bioluminescent hypergraph) takes priority over any default
5Gradio UI. All inference happens via /api routes registered by backend.server.
6"""
7import os
8import pathlib
9import gradio as gr
10from fastapi.responses import FileResponse
11from fastapi.staticfiles import StaticFiles
12
13from backend.server import attach
14
15DIST = pathlib.Path(__file__).parent / "frontend" / "dist"
16
17app = gr.Server()
18attach(app)  # registers /api/* before static mounts
19
20
21class CachedStaticFiles(StaticFiles):
22    """Hashed bundle assets — cache hard."""
23    def file_response(self, *args, **kwargs):
24        r = super().file_response(*args, **kwargs)
25        r.headers["Cache-Control"] = "public, max-age=31536000, immutable"
26        return r
27
28
29# Mount static asset folders (won't collide with /api or Gradio internals)
30for sub in ("assets", "audio"):
31    d = DIST / sub
32    if d.is_dir():
33        cls = CachedStaticFiles if sub == "assets" else StaticFiles
34        app.mount(f"/{sub}", cls(directory=str(d)), name=sub)
35
36
37_NO_CACHE = {"Cache-Control": "no-cache, must-revalidate"}
38
39
40@app.get("/")
41async def index():
42    return FileResponse(DIST / "index.html", headers=_NO_CACHE)
43
44
45@app.get("/favicon.ico")
46async def favicon():
47    return FileResponse(DIST / "index.html", headers=_NO_CACHE)
48
49
50if __name__ == "__main__":
51    port = int(os.environ.get("GRADIO_SERVER_PORT", os.environ.get("PORT", 7860)))
52    app.launch(server_name="0.0.0.0", server_port=port)
53