Spaces:
Paused
Paused
Download webui.py from Porst/Hojo-tts-webui: direct link, hf CLI and curl.
- Browser
- Download file 25.9 kB
-
https://huggingface.co/spaces/Porst/Hojo-tts-webui/resolve/main/webui.py
- Command line
-
hf download hf://spaces/Porst/Hojo-tts-webui/webui.py
-
curl -L -o webui.py https://huggingface.co/spaces/Porst/Hojo-tts-webui/resolve/main/webui.py
25.9 kB
| #!/usr/bin/env python3 | |
| """ | |
| Gradio WebUI for Hojo-TTS-Light-40M. | |
| Three tabs: | |
| 1. 🎙️ 语音合成 / TTS — text, voice, speed -> audio + timing stats | |
| 2. 🔌 API 信息 — model id, endpoints, ready-to-copy curl example | |
| 3. ⚙️ 设置 · API 密钥 — billing mode, internal API hash, key CRUD + usage | |
| Every handler calls the service layer in ``app.py`` (the exact code the HTTP | |
| API routes use) in-process instead of going over the network, so the browser | |
| UI and the API can never disagree about billing, quota or error behaviour. | |
| The Flask app (OpenAI-compatible API + internal HMAC API + admin routes) is | |
| mounted into the Gradio server at ``/`` by :func:`mount_api`, so the service | |
| still runs on a single port and every documented endpoint URL is unchanged. | |
| """ | |
| from __future__ import annotations | |
| import inspect | |
| from typing import Callable | |
| import gradio as gr | |
| import app as svc # noqa: E402 — app.main() aliases __main__ to "app" so both share one instance | |
| MAX_CHARS = 20000 | |
| DEFAULT_TEXT = ( | |
| "你好,欢迎使用 Hojo TTS Light 40M 本地语音合成系统。" | |
| "本模型支持中英文混合朗读,无需联网,完全在本地运行。" | |
| ) | |
| BILLING_LABELS = { | |
| "none": "关闭计费 — 所有有效密钥无限调用", | |
| "per_call": "按次计费 — 每次成功合成扣减 1 次(quota 字段)", | |
| "per_token": "按 Token 计费 — 按输入文本 Token 数扣减(token_quota 字段)", | |
| } | |
| TOKEN_PRESETS = [("无限 (-1)", -1), ("1", 1), ("百 (100)", 100), | |
| ("千 (1000)", 1000), ("万 (10000)", 10000), ("亿 (1亿)", 100000000)] | |
| KEY_ACTIONS = [ | |
| ("✏️ 重命名", "rename"), | |
| ("🔢 改按次额度", "quota"), | |
| ("🔢 改 Token 额度", "token_quota"), | |
| ("♻️ 重置调用次数", "reset_used"), | |
| ("♻️ 重置 Token 用量", "reset_tokens"), | |
| ] | |
| CSS = """ | |
| .gradio-container { max-width: 1180px !important; } | |
| #hojo-keys td { font-size: 13px; } | |
| footer { display: none !important; } | |
| """ | |
| THEME = gr.themes.Soft( | |
| primary_hue=gr.themes.colors.teal, | |
| secondary_hue=gr.themes.colors.orange, | |
| neutral_hue=gr.themes.colors.slate, | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Small compatibility helpers (Gradio 5.x / 6.x) | |
| # --------------------------------------------------------------------------- | |
| def _supports(fn: Callable, name: str) -> bool: | |
| try: | |
| return name in inspect.signature(fn).parameters | |
| except (TypeError, ValueError): | |
| return False | |
| def _kwargs(fn: Callable, **wanted) -> dict: | |
| """Keep only the kwargs this Gradio version understands.""" | |
| try: | |
| params = inspect.signature(fn).parameters | |
| except (TypeError, ValueError): | |
| return wanted | |
| if any(p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values()): | |
| return wanted | |
| return {k: v for k, v in wanted.items() if k in params} | |
| def _textbox(**wanted) -> gr.Textbox: | |
| return gr.Textbox(**_kwargs(gr.Textbox, **wanted)) | |
| def _audio(**wanted) -> gr.Audio: | |
| return gr.Audio(**_kwargs(gr.Audio, **wanted)) | |
| def _button(label: str = "", **wanted) -> gr.Button: | |
| return gr.Button(label, **_kwargs(gr.Button, **wanted)) | |
| def _button_size(value: str) -> dict: | |
| return {"size": value} if _supports(gr.Button, "size") else {} | |
| # --------------------------------------------------------------------------- | |
| # Tab 1 — TTS | |
| # --------------------------------------------------------------------------- | |
| def _char_count(text) -> str: | |
| return f"{len(text or '')} / {MAX_CHARS}" | |
| def _synthesize(text, voice, speed, request: gr.Request = None): | |
| """Synthesize and return (audio path, status line, char counter).""" | |
| try: | |
| result = svc.synth_text(text, voice, float(speed or 1.0), max_chars=MAX_CHARS) | |
| path = svc.write_wav_file(result["wav"], result["sample_rate"]) | |
| except Exception as exc: # noqa: BLE001 — reported in the UI, never raised | |
| print(f"[WebUI] Synthesis failed: {exc}") | |
| return None, f"❌ **合成失败 / Synthesis failed:** {exc}", _char_count(text) | |
| status = ( | |
| f"✅ **合成完成** · 音色 `{result['voice']}` · 音频 {result['duration']:.1f}s · " | |
| f"耗时 {result['elapsed']:.1f}s · RTF {result['rtf']:.2f} · {result['chars']} 字" | |
| ) | |
| return path, status, f"{result['chars']} / {MAX_CHARS}" | |
| def _clear(): | |
| return "", "", None | |
| # --------------------------------------------------------------------------- | |
| # Tab 2 — API info | |
| # --------------------------------------------------------------------------- | |
| def _api_info(request: gr.Request = None) -> str: | |
| info = svc.status_summary() | |
| base = f"http://127.0.0.1:{_current_port(request)}" | |
| auth = ("✅ 已开启 — `/v1/*` 需 `Authorization: Bearer sk-...`" if info["auth_required"] | |
| else "⚠️ 未设置任何密钥 — `/v1/*` 暂不校验密钥") | |
| admin = ("🔐 已设置 — 设置页需输入管理密码" if info["admin_password_set"] | |
| else "🔓 未设置 — 仅本机 127.0.0.1 可管理密钥") | |
| rows = [ | |
| ("模型名称 Model", f"`{info['model_name']}`"), | |
| ("默认音色 Default voice", f"`{info['default_voice']}`"), | |
| ("采样率 Sample rate", f"{info['sample_rate']} Hz"), | |
| ("音色数量 Voices", f"{info['voices_count']}"), | |
| ("平台 Platform", f"{info['platform']} · {info['default_threads']} 线程"), | |
| ("站内 TTS", f"`POST {base}/api/tts`(需 HMAC 签名)"), | |
| ("OpenAI 兼容", f"`POST {base}/v1/audio/speech`"), | |
| ("模型列表 Models", f"`GET {base}/v1/models`"), | |
| ("API 认证 Auth", auth), | |
| ("管理密码 Admin", admin), | |
| ("计费模式 Billing", BILLING_LABELS.get(info["billing_mode"], info["billing_mode"])), | |
| ] | |
| table = "\n".join(f"| {k} | {v} |" for k, v in rows) | |
| curl = f"""# OpenAI 兼容接口(OpenAI SDK 直接可用) | |
| curl {base}/v1/audio/speech \\ | |
| -H "Authorization: Bearer sk-..." \\ | |
| -H "Content-Type: application/json" \\ | |
| -d '{{"model": "{info['model_name']}", "input": "你好,世界", "voice": "alloy"}}' \\ | |
| --output speech.wav | |
| # 站内接口(免密钥,但需哈希签名,签名算法见 README) | |
| curl {base}/api/tts -X POST \\ | |
| -H "Content-Type: application/json" \\ | |
| -H "X-Timestamp: <unix 秒>" \\ | |
| -H "X-Nonce: <随机串>" \\ | |
| -H "X-Hash: <HMAC-SHA256(哈希, "ts:nonce:sha256(body)")>" \\ | |
| -d '{{"text": "你好,世界", "voice": "hojo_zh_f_02"}}' \\ | |
| --output tts.wav""" | |
| return f"### API 信息\n\n| 项目 | 值 |\n| --- | --- |\n{table}\n\n### curl 调用示例\n\n```bash\n{curl}\n```" | |
| def _current_port(request: gr.Request = None) -> int: | |
| """Best-effort port of the running server, for the curl examples.""" | |
| port = None | |
| if request is not None: | |
| headers = getattr(request, "headers", {}) or {} | |
| raw = headers.get("x-forwarded-port") or headers.get("x-forwarded-host") or headers.get("host") | |
| if raw: | |
| tail = str(raw).rsplit(":", 1)[-1] | |
| if tail.isdigit(): | |
| port = int(tail) | |
| return port or 7860 | |
| # --------------------------------------------------------------------------- | |
| # Tab 3 — settings / API keys | |
| # --------------------------------------------------------------------------- | |
| def _admin_gate(password: str, request: gr.Request = None) -> str: | |
| """Human-readable result of the admin check (used by the verify button).""" | |
| ok, reason = svc.admin_authorized(password, request) | |
| if not ok: | |
| return f"❌ {reason}" | |
| return f"✅ **管理员验证通过** — {reason}" if reason else "✅ **管理员验证通过**" | |
| def _hash_panel(password: str, request: gr.Request = None): | |
| """Return (billing status, internal hash, signature hint).""" | |
| ok, reason = svc.admin_authorized(password, request) | |
| if not ok: | |
| return f"🔒 {reason}", "—", "🔒 验证管理身份后显示内部 API 哈希。" | |
| billing = f"✅ **当前计费模式** — {BILLING_LABELS.get(svc.get_billing_mode(), svc.get_billing_mode())}" | |
| hint = (f"签名头:`X-Timestamp` + `X-Nonce` + `X-Hash`" | |
| f"(HMAC-SHA256,TTL {svc.INTERNAL_SIG_TTL}s)。把这个哈希发给请求方," | |
| f"对方按 README 的算法签名即可调用 `POST /api/tts`。") | |
| return billing, svc.API_HASH, hint | |
| def _apply_billing(mode: str, password: str, request: gr.Request = None): | |
| ok, reason = svc.admin_authorized(password, request) | |
| if not ok: | |
| return f"❌ {reason}", *_hash_panel(password, request) | |
| try: | |
| applied = svc.set_billing_mode(mode) | |
| except ValueError as exc: | |
| return f"❌ {exc}", *_hash_panel(password, request) | |
| billing, digest, hint = _hash_panel(password, request) | |
| return f"✅ 已切换为:{BILLING_LABELS[applied]}", billing, digest, hint | |
| def _field_visibility(mode: str): | |
| """Only show the quota inputs that the selected billing mode actually uses.""" | |
| mode = mode or svc.get_billing_mode() | |
| return gr.update(visible=mode == "per_call"), gr.update(visible=mode == "per_token") | |
| def _random_key(password: str, request: gr.Request = None): | |
| ok, reason = svc.admin_authorized(password, request) | |
| if not ok: | |
| return f"❌ {reason}" | |
| return svc.generate_api_key() | |
| def _key_state(message: str = ""): | |
| """(rows, choices, selected, message) for the key table + selector.""" | |
| choices = svc.key_choices() | |
| return svc.keys_table_rows(), choices, (choices[0][1] if choices else None), message | |
| def _key_status(message: str): | |
| """Result tuple for handlers wired to (key_table, key_select, status).""" | |
| rows, choices, selected, message = _key_state(message) | |
| return rows, gr.update(choices=choices, value=selected), message | |
| def _key_added(key: str, message: str): | |
| """Result tuple for the add-key handler: (key_field, table, selector, status).""" | |
| rows, choices, selected, _ = _key_state(message) | |
| return key, rows, gr.update(choices=choices, value=selected), message | |
| def _add_key(name, quota, token_quota, key_value, password: str, request: gr.Request = None): | |
| ok, reason = svc.admin_authorized(password, request) | |
| if not ok: | |
| return _key_added(key_value, f"❌ {reason}") | |
| try: | |
| new_key = svc.create_key(name=name, quota=quota, token_quota=token_quota, key=key_value or "") | |
| except ValueError as exc: | |
| return _key_added(key_value, f"❌ {exc}") | |
| return _key_added(new_key, f"✅ 已添加密钥 `{new_key}` — 请立刻复制并保存。") | |
| def _run_key_action(action: str, key: str, name, quota, token_quota, | |
| confirmed: bool, password: str, request: gr.Request = None): | |
| ok, reason = svc.admin_authorized(password, request) | |
| if not ok: | |
| return _key_status(f"❌ {reason}") | |
| if not key: | |
| return _key_status("⚠️ 请先在上方下拉框中选择一个密钥。") | |
| try: | |
| if action == "rename": | |
| if not (name or "").strip(): | |
| return _key_status("⚠️ 「新名称」不能为空。") | |
| done = svc.modify_key(key, name=name) | |
| message = f"✅ 已重命名为 **{name.strip()}**" if done else "❌ 密钥不存在" | |
| elif action == "quota": | |
| done = svc.modify_key(key, quota=int(quota)) | |
| message = f"✅ 按次额度已设为 **{int(quota)}**(-1 = 无限)" if done else "❌ 密钥不存在" | |
| elif action == "token_quota": | |
| done = svc.modify_key(key, token_quota=int(token_quota)) | |
| message = f"✅ Token 额度已设为 **{int(token_quota)}**(-1 = 无限)" if done else "❌ 密钥不存在" | |
| elif action == "reset_used": | |
| done = svc.modify_key(key, reset_used=True) | |
| message = "✅ 调用次数用量已重置" if done else "❌ 密钥不存在" | |
| elif action == "reset_tokens": | |
| done = svc.modify_key(key, reset_tokens=True) | |
| message = "✅ Token 用量已重置" if done else "❌ 密钥不存在" | |
| elif action == "delete": | |
| if not confirmed: | |
| return _key_status("⚠️ 删除不可撤销,请先勾选「确认删除」再执行。") | |
| done = svc.remove_key(key) | |
| message = "✅ 密钥已删除" if done else "❌ 密钥不存在" | |
| else: | |
| return _key_status(f"⚠️ 未知操作:{action}") | |
| except (TypeError, ValueError) as exc: | |
| return _key_status(f"❌ 参数无效:{exc}") | |
| return _key_status(message) | |
| def _refresh_keys(password: str, request: gr.Request = None): | |
| ok, reason = svc.admin_authorized(password, request) | |
| return _key_status("🔄 已刷新密钥列表" if ok else f"🔒 {reason}") | |
| # --------------------------------------------------------------------------- | |
| # Load handler | |
| # --------------------------------------------------------------------------- | |
| def _on_load(request: gr.Request = None): | |
| """Fill in the status line, voices, billing mode, API info and key table.""" | |
| info = svc.status_summary() | |
| if info["loaded"]: | |
| status = (f"🟢 **模型已就绪** · `{info['model_name']}` · {info['voices_count']} 个音色 · " | |
| f"{info['sample_rate']} Hz · {info['platform']} · {info['default_threads']} 线程") | |
| elif info["load_error"]: | |
| status = (f"🔴 **模型加载失败** · {info['load_error']} — 请检查 `models/` 目录," | |
| f"或重新运行 `start.sh` / `start.bat` 下载模型。") | |
| else: | |
| status = "🟡 **模型加载中** — 首次合成会自动加载,请稍候…" | |
| try: | |
| voices = svc.voice_choices() | |
| default = next((v for _, v in voices if v == svc.DEFAULT_VOICE), voices[0][1] if voices else None) | |
| voice_update = gr.update(choices=voices, value=default) | |
| except Exception as exc: # model unavailable — show why instead of an empty dropdown | |
| voice_update = gr.update(choices=[], value=None) | |
| status += f"\n\n> ⚠️ 音色列表不可用:{exc}" | |
| rows, choices, selected, _ = _key_state() | |
| return ( | |
| status, | |
| voice_update, | |
| svc.get_billing_mode(), | |
| _api_info(request), | |
| rows, | |
| choices, | |
| selected, | |
| *_field_visibility(svc.get_billing_mode()), | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # UI | |
| # --------------------------------------------------------------------------- | |
| def build_ui() -> gr.Blocks: | |
| """Build the Gradio Blocks app. The Flask API is mounted on top of it.""" | |
| with gr.Blocks(**blocks_options(), | |
| title=f"Hojo TTS Light 40M · {svc.MODEL_NAME}") as demo: | |
| gr.Markdown( | |
| f"# Hojo TTS Light **40M**\n" | |
| f"<div style='opacity:.75;font-size:14px'>{svc.MODEL_NAME} · 轻量级中英文语音合成 · " | |
| f"ONNX Runtime · 本地运行,数据不上传</div>", | |
| elem_classes=["header-markdown"], | |
| ) | |
| status_bar = gr.Markdown("🟡 模型加载中…") | |
| with gr.Tabs(): | |
| # ---------------- Tab 1: TTS ---------------- | |
| with gr.Tab("🎙️ 语音合成", id="tts"): | |
| with gr.Row(): | |
| text_box = _textbox( | |
| label="输入文本 / Text", | |
| value=DEFAULT_TEXT, | |
| lines=8, | |
| max_lines=20, | |
| placeholder="在此输入要合成的文本,支持中英文混合…", | |
| max_length=MAX_CHARS, | |
| scale=3, | |
| show_copy_button=True, | |
| ) | |
| with gr.Column(scale=1, min_width=270): | |
| voice_box = gr.Dropdown(label="音色 / Voice", choices=[]) | |
| speed_box = gr.Slider(0.5, 2.0, value=1.0, step=0.05, label="语速 / Speed") | |
| synth_btn = _button("🔊 合成语音", variant="primary", **_button_size("lg")) | |
| clear_btn = _button("🧹 清空", **_button_size("lg")) | |
| char_count = gr.Markdown(f"**{_char_count(DEFAULT_TEXT)}**") | |
| tts_status = gr.Markdown("模型加载中…") | |
| audio_out = _audio(label="合成结果 / Result", type="filepath", format="wav") | |
| text_box.input(_char_count, inputs=text_box, outputs=char_count, show_progress="hidden") | |
| synth_btn.click(_synthesize, inputs=[text_box, voice_box, speed_box], | |
| outputs=[audio_out, tts_status, char_count], api_name="synthesize") | |
| clear_btn.click(_clear, inputs=None, outputs=[text_box, tts_status, audio_out]) | |
| # ---------------- Tab 2: API info ---------------- | |
| with gr.Tab("🔌 API 信息", id="api"): | |
| api_md = gr.Markdown("加载中…") | |
| # ---------------- Tab 3: settings / keys ---------------- | |
| with gr.Tab("⚙️ 设置 · API 密钥", id="settings"): | |
| gr.Markdown("### 管理员验证 / Admin") | |
| with gr.Row(): | |
| pwd_box = _textbox(label="管理密码 / Admin password", type="password", scale=3, | |
| placeholder="config.json 中的 admin_password(未设置则留空)") | |
| verify_btn = _button("🔓 验证", scale=1) | |
| admin_status = gr.Markdown("🔒 请先验证管理员身份 / Verify first.") | |
| with gr.Row(equal_height=True): | |
| billing_md = gr.Markdown("🔒 需要管理员权限", scale=1) | |
| hash_md = gr.Markdown("—", scale=1) | |
| hash_hint = gr.Markdown("🔒 验证管理身份后显示内部 API 哈希。") | |
| verify_btn.click(_hash_panel, inputs=pwd_box, outputs=[billing_md, hash_md, hash_hint]) | |
| verify_btn.click(_admin_gate, inputs=pwd_box, outputs=admin_status, api_name=False) | |
| gr.Markdown("### 计费模式 / Billing") | |
| billing_mode = gr.Radio( | |
| choices=[(label, mode) for mode, label in BILLING_LABELS.items()], | |
| value=svc.get_billing_mode(), | |
| label="三选一互斥,切换即时生效", | |
| scale=2, | |
| ) | |
| apply_billing_btn = _button("💾 应用计费模式", variant="primary", scale=1) | |
| apply_billing_btn.click(_apply_billing, inputs=[billing_mode, pwd_box], | |
| outputs=[admin_status, billing_md, hash_md, hash_hint], | |
| api_name="set_billing_mode") | |
| gr.Markdown("### 添加 API 密钥 / Add key") | |
| with gr.Row(): | |
| new_name = _textbox(label="密钥名称 / Name", placeholder="例如:我的项目密钥", scale=2) | |
| new_key_value = _textbox(label="密钥 / Key(留空自动生成)", placeholder="sk-…", scale=2, | |
| show_copy_button=True) | |
| with gr.Row(): | |
| per_call_field = gr.Number(label="按次额度 / Call quota(-1 = 无限)", value=-1, | |
| precision=0, scale=2) | |
| per_token_field = gr.Number(label="Token 额度 / Token quota(-1 = 无限)", value=-1, | |
| precision=0, scale=2) | |
| token_preset = gr.Radio(choices=TOKEN_PRESETS, value=-1, | |
| label="Token 快捷档位", scale=2) | |
| token_preset.change(lambda v: v, inputs=token_preset, outputs=per_token_field, | |
| show_progress="hidden") | |
| with gr.Row(): | |
| gen_key_btn = _button("🎲 随机生成", scale=1) | |
| add_key_btn = _button("➕ 添加密钥", variant="primary", scale=1) | |
| add_result = gr.Markdown() | |
| gen_key_btn.click(_random_key, inputs=pwd_box, outputs=[new_key_value, add_result]) | |
| gr.Markdown("### API 密钥列表 / Keys") | |
| with gr.Row(): | |
| key_select = gr.Dropdown(choices=[], label="选择密钥 / Select a key", scale=3) | |
| refresh_btn = _button("🔄 刷新", scale=1) | |
| key_table = gr.Dataframe( | |
| headers=svc.KEY_TABLE_HEADERS, | |
| datatype=["str"] * len(svc.KEY_TABLE_HEADERS), | |
| interactive=False, | |
| wrap=True, | |
| label="", | |
| elem_id="hojo-keys", | |
| ) | |
| with gr.Row(): | |
| action_name = _textbox(label="新名称 / New name", scale=2) | |
| action_quota = gr.Number(label="新按次额度", value=-1, precision=0, scale=1) | |
| action_token_quota = gr.Number(label="新 Token 额度", value=-1, precision=0, scale=1) | |
| with gr.Row(): | |
| action_pick = gr.Radio(choices=KEY_ACTIONS, value="rename", label="操作 / Action", scale=2) | |
| apply_action_btn = _button("⚡️ 执行", variant="primary", scale=1) | |
| with gr.Row(): | |
| confirm_delete = gr.Checkbox(label="⚠️ 我确认删除该密钥(删除不可撤销)", scale=2) | |
| delete_btn = _button("🗑️ 删除所选密钥", variant="stop", scale=1) | |
| action_status = gr.Markdown() | |
| refresh_btn.click(_refresh_keys, inputs=pwd_box, | |
| outputs=[key_table, key_select, action_status], api_name="refresh_keys") | |
| add_key_btn.click( | |
| _add_key, | |
| inputs=[new_name, per_call_field, per_token_field, new_key_value, pwd_box], | |
| outputs=[new_key_value, key_table, key_select, add_result], | |
| api_name="add_key", | |
| ) | |
| # NOTE: output arity must match the handler's return tuple exactly — | |
| # Gradio 6 collapses duplicated output components, so a repeated | |
| # component in `outputs` would be an arity mismatch. | |
| apply_action_btn.click( | |
| _run_key_action, | |
| inputs=[action_pick, key_select, action_name, action_quota, action_token_quota, | |
| confirm_delete, pwd_box], | |
| outputs=[key_table, key_select, action_status], | |
| api_name="key_action", | |
| ) | |
| delete_btn.click( | |
| _run_key_action, | |
| inputs=[gr.State("delete"), key_select, action_name, action_quota, | |
| action_token_quota, confirm_delete, pwd_box], | |
| outputs=[key_table, key_select, action_status], | |
| api_name="delete_key", | |
| ) | |
| billing_mode.change(_field_visibility, inputs=billing_mode, | |
| outputs=[per_call_field, per_token_field], show_progress="hidden") | |
| gr.Markdown("---") | |
| gr.Markdown("Hojo-TTS-Light-40M · Apache 2.0 · 本地运行,数据不上传") | |
| demo.load( | |
| _on_load, | |
| inputs=None, | |
| outputs=[status_bar, voice_box, billing_mode, api_md, key_table, key_select, action_status, | |
| per_call_field, per_token_field], | |
| show_progress="hidden", | |
| ) | |
| return demo | |
| def _wsgi_middleware(flask_app): | |
| """WSGI -> ASGI adapter (Starlette's, with a2wsgi as a fallback).""" | |
| import warnings | |
| with warnings.catch_warnings(): | |
| # Starlette marks its own WSGI shim deprecated in favour of a2wsgi, | |
| # which is not a Gradio dependency. The shim is stable and shipped | |
| # with every Starlette release Gradio pins, so keep using it quietly. | |
| warnings.simplefilter("ignore") | |
| try: | |
| from starlette.middleware.wsgi import WSGIMiddleware | |
| return WSGIMiddleware(flask_app) | |
| except ImportError: # pragma: no cover — Starlette dropped the shim | |
| from a2wsgi import WSGIMiddleware | |
| return WSGIMiddleware(flask_app) | |
| def _gradio_major() -> int: | |
| try: | |
| return int(gr.__version__.split(".")[0]) | |
| except (AttributeError, ValueError): # pragma: no cover | |
| return 0 | |
| def blocks_options() -> dict: | |
| """Theme/CSS kwargs for the ``Blocks`` constructor (Gradio 5.x).""" | |
| return {} if _gradio_major() >= 6 else _kwargs(gr.Blocks, theme=THEME, css=CSS) | |
| def launch_options() -> dict: | |
| """Theme/CSS kwargs for ``launch()`` (Gradio 6 moved them there).""" | |
| return _kwargs(gr.Blocks.launch, theme=THEME, css=CSS) if _gradio_major() >= 6 else {} | |
| def mount_api(demo: gr.Blocks, flask_app) -> None: | |
| """Mount the Flask API app into the running Gradio server at ``/``. | |
| Gradio owns the socket and serves the UI from ``/``; every Flask route | |
| (``/v1/audio/speech``, ``/api/tts``, ``/api/admin/*`` …) is dispatched by | |
| the same port, exactly as it was before the Gradio rewrite. | |
| The route has to be attached to the app Gradio builds inside ``launch()`` | |
| (it recreates the ASGI app there), so call this right after ``launch()`` | |
| returned with ``prevent_thread_lock=True``. | |
| """ | |
| app = demo.app | |
| middleware = _wsgi_middleware(flask_app) | |
| if hasattr(gr, "mount_wsgi_app"): # Gradio <= 5.x ships its own helper | |
| gr.mount_wsgi_app(app, flask_app, path="/", **_kwargs(gr.mount_wsgi_app, show_api=False)) | |
| return | |
| app.mount("/", middleware) | |
| router = getattr(app, "router", None) | |
| if router is not None and hasattr(router, "_mark_routes_changed"): | |
| router._mark_routes_changed() # Gradio 6 caches the route table | |