Spaces:
Sleeping
Sleeping
Commit ·
465d818
1
Parent(s): e1e6e90
app.py: add long-lived Cache-Control for large web/ assets via Starlette middleware
Browse files23 unmodified lines24252627282915 unmodified lines454647484950938 unmodified lines98999099199299399499599699723 unmodified linesimport gradio as grfrom lilyscript.generator import StreamingLilyletGeneratorfrom lilyscript.postprocess import postprocessfrom lilyscript.mask_monitor import MaskMonitor, load_blacklist15 unmodified linesOUTPUT_DIR = os.path.join(HERE, 'outputs')WEB_DIR = os.path.join(HERE, 'web') # vendored browser libs + score player (gitignored bundles)EXAMPLE_PREFIX = '\U0001F4C4 ' # 📄 examplesOUTPUT_PREFIX = '✨ ' # ✨ session outputs938 unmodified linesif __name__ == '__main__':demo = build_ui()demo.queue().launch(theme=gr.themes.Soft(),css=CUSTOM_CSS,head=build_head(),allowed_paths=[WEB_DIR],)23 unmodified lines242526272829303115 unmodified lines4748495051525354555657585960616263646566676869707172737475767778798081938 unmodified lines102010211022102310241025102610271028102910301031103223 unmodified linesimport gradio as grfrom starlette.middleware.base import BaseHTTPMiddlewarefrom lilyscript.generator import StreamingLilyletGeneratorfrom lilyscript.postprocess import postprocessfrom lilyscript.mask_monitor import MaskMonitor, load_blacklist15 unmodified linesOUTPUT_DIR = os.path.join(HERE, 'outputs')WEB_DIR = os.path.join(HERE, 'web') # vendored browser libs + score player (gitignored bundles)# Gradio serves our vendored web/ assets (soundfont gm.sf3 ~38 MB, verovio.bundle.js ~8 MB,# libfluidsynth ~2 MB, …) via `/gradio_api/file=<abs-path>` with NO Cache-Control header, so the# browser re-validates (or refetches) them on every load. These are large, static, and effectively# immutable per deploy — bundles carry a `?v=<mtime>` cache-buster (see _file_url), and the soundfont# rarely changes. This middleware adds a long-lived Cache-Control to responses for web/ assets so# returning visitors reuse them from the browser cache instead of re-downloading. It runs inside our# own app.py, so it applies on Hugging Face and ModelScope Spaces (where we are the server), unlike a# static-host CDN we couldn't configure._WEB_ASSET_MARKER = '/web/'_CACHEABLE_EXTS = ('.sf3', '.sf2', '.js', '.wasm', '.css', '.mp3', '.ogg')class AssetCacheHeaderMiddleware(BaseHTTPMiddleware):async def dispatch (self, request, call_next):response = await call_next(request)# Only touch Gradio's static-file route for our vendored web/ assets, and only if a# Cache-Control wasn't already set. request.url.path holds the decoded `/gradio_api/file=...`.path = request.url.pathif ('/file=' in pathand _WEB_ASSET_MARKER in pathand path.endswith(_CACHEABLE_EXTS)and 'cache-control' not in {k.lower() for k in response.headers.keys()}):# 30 days. Safe: bundle URLs change via ?v=<mtime> on redeploy; soundfont is stable.response.headers['Cache-Control'] = 'public, max-age=2592000'return responseEXAMPLE_PREFIX = '\U0001F4C4 ' # 📄 examplesOUTPUT_PREFIX = '✨ ' # ✨ session outputs938 unmodified linesif __name__ == '__main__':demo = build_ui()from starlette.middleware import Middlewaredemo.queue().launch(theme=gr.themes.Soft(),css=CUSTOM_CSS,head=build_head(),allowed_paths=[WEB_DIR],# Inject a long-lived Cache-Control on our large static web/ assets (see# AssetCacheHeaderMiddleware). app_kwargs flows into the FastAPI/Starlette constructor.app_kwargs={'middleware': [Middleware(AssetCacheHeaderMiddleware)]},)
app.py+35