Optimize tablet load: defer health check, lighten service worker, drop Google Fonts.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-12 14:49:58 +08:00
parent f0bb40c605
commit 7e65349878
4 changed files with 338 additions and 285 deletions
+44 -13
View File
@@ -197,9 +197,14 @@ PWA_HEAD = """
var deferredPrompt = null;
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
function registerSW() {
navigator.serviceWorker.register("/sw.js", { scope: "/" }).catch(function () {});
});
}
if ("requestIdleCallback" in window) {
requestIdleCallback(registerSW, { timeout: 5000 });
} else {
setTimeout(registerSW, 3000);
}
}
function isStandalone() {
@@ -711,24 +716,37 @@ def _status_html(title: str, message: str, level: str = "warn") -> str:
)
def ui_check_ollama_html() -> str:
ok, msg = check_ollama_health()
def ui_check_ollama_html(force: bool = False) -> str:
ok, msg = check_ollama_health(force=force)
return _status_html("Ollama 节点", msg, "ok" if ok else "err")
def ui_initial_load() -> tuple[str, str]:
"""首屏立即返回,不发起网络请求,避免平板白屏等待。"""
return (
_status_html("Ollama 节点", "后台检测中,请稍候…", "warn"),
ui_speaker_status_html(),
)
def ui_refresh_status_html(force: bool = False) -> tuple[str, str]:
"""刷新 Ollama + 音色状态(供 Timer / 按钮调用)。"""
return ui_check_ollama_html(force=force), ui_speaker_status_html()
def ui_speaker_status_html() -> str:
ok, msg = speaker_is_ready()
return _status_html("音色状态", msg, "ok" if ok else "warn")
def build_theme() -> gr.themes.Base:
"""高对比度暗色主题Gradio 6.0 需在 launch() 传入)"""
"""高对比度暗色主题;使用系统字体,避免平板拉取 Google Fonts 卡顿"""
return gr.themes.Base(
primary_hue="blue",
secondary_hue="blue",
neutral_hue="slate",
font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"],
font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "Consolas", "monospace"],
font=["system-ui", "-apple-system", "Segoe UI", "Roboto", "sans-serif"],
font_mono=["Consolas", "Monaco", "Courier New", "monospace"],
).set(
body_background_fill="#0f1419",
body_background_fill_dark="#0f1419",
@@ -780,7 +798,25 @@ def build_app() -> gr.Blocks:
refresh_btn = gr.Button("🔄 刷新状态", variant="secondary", scale=0, min_width=120)
refresh_btn.click(
fn=lambda: (ui_check_ollama_html(), ui_speaker_status_html()),
fn=lambda: ui_refresh_status_html(force=True),
outputs=[ollama_status, speaker_status],
)
# 首屏秒开:仅本地检测音色,Ollama 延后到 Timer
demo.load(
fn=ui_initial_load,
outputs=[ollama_status, speaker_status],
)
# 1 秒后后台检测 Ollama;之后每 30s 刷新(30s 内走缓存)
status_timer = gr.Timer(value=1, active=True)
status_timer.tick(
fn=lambda: ui_refresh_status_html(force=False),
outputs=[ollama_status, speaker_status],
)
status_timer_slow = gr.Timer(value=30, active=True)
status_timer_slow.tick(
fn=lambda: ui_refresh_status_html(force=True),
outputs=[ollama_status, speaker_status],
)
@@ -886,11 +922,6 @@ def build_app() -> gr.Blocks:
[pipe_raw, pipe_polished, pipe_output, pipeline_log],
)
demo.load(
fn=lambda: (ui_check_ollama_html(), ui_speaker_status_html()),
outputs=[ollama_status, speaker_status],
)
return demo