Spaces:
Runtime error
Runtime error
| import base64 | |
| import json | |
| import logging | |
| import os | |
| import re | |
| import sys | |
| import tempfile | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from threading import Lock | |
| from typing import Optional, Tuple | |
| import gradio as gr | |
| import numpy as np | |
| os.environ["TOKENIZERS_PARALLELISM"] = "false" | |
| os.environ.setdefault("OPENBLAS_NUM_THREADS", "4") | |
| os.environ.setdefault("OMP_NUM_THREADS", "4") | |
| os.environ.setdefault("MKL_NUM_THREADS", "4") | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(levelname)s - %(message)s", | |
| handlers=[logging.StreamHandler(sys.stdout)], | |
| ) | |
| logger = logging.getLogger(__name__) | |
| NANOVLLM_API_BASE = os.environ.get("NANOVLLM_API_BASE", "").rstrip("/") | |
| if not NANOVLLM_API_BASE: | |
| raise RuntimeError("NANOVLLM_API_BASE environment variable is required but not set.") | |
| DEFAULT_ASR_MODEL_REF = "FunAudioLLM/SenseVoiceSmall" | |
| MAX_REFERENCE_AUDIO_SECONDS = 50.0 | |
| _persistent_root = None | |
| _request_log_dir = None | |
| _active_generation_requests = 0 | |
| _active_generation_lock = Lock() | |
| def _get_int_env(name: str, default: int) -> int: | |
| value = os.environ.get(name, "").strip() | |
| if not value: | |
| return default | |
| return int(value) | |
| def _get_float_env(name: str, default: float) -> float: | |
| value = os.environ.get(name, "").strip() | |
| if not value: | |
| return default | |
| return float(value) | |
| def _get_bool_env(name: str, default: bool) -> bool: | |
| value = os.environ.get(name, "").strip().lower() | |
| if not value: | |
| return default | |
| if value in {"1", "true", "yes", "on"}: | |
| return True | |
| if value in {"0", "false", "no", "off"}: | |
| return False | |
| raise ValueError(f"Invalid boolean env: {name}={value!r}") | |
| # ---------- Request Logging ---------- | |
| def _configure_cache_dirs() -> None: | |
| global _persistent_root, _request_log_dir | |
| persistent_root = Path(os.environ.get("SPACE_PERSISTENT_ROOT", "/data")).expanduser() | |
| if not persistent_root.exists(): | |
| logger.info("Persistent storage not detected. Request logs disabled.") | |
| return | |
| logs_dir = Path( | |
| os.environ.get("REQUEST_LOG_DIR", str(persistent_root / "logs")) | |
| ).expanduser() | |
| logs_dir.mkdir(parents=True, exist_ok=True) | |
| _persistent_root = persistent_root | |
| _request_log_dir = logs_dir | |
| logger.info(f"Persistent storage detected at {persistent_root}") | |
| logger.info(f"Request logs will be written to daily files under {_request_log_dir}") | |
| _configure_cache_dirs() | |
| def _append_request_log(payload: dict) -> None: | |
| if _request_log_dir is None: | |
| return | |
| now = datetime.now(timezone.utc) | |
| record = {"timestamp": now.isoformat(), **payload} | |
| log_path = _request_log_dir / f"{now.date().isoformat()}.jsonl" | |
| with log_path.open("a", encoding="utf-8") as fp: | |
| fp.write(json.dumps(record, ensure_ascii=False) + "\n") | |
| def _begin_generation_request() -> None: | |
| global _active_generation_requests | |
| with _active_generation_lock: | |
| _active_generation_requests += 1 | |
| def _end_generation_request() -> None: | |
| global _active_generation_requests | |
| with _active_generation_lock: | |
| _active_generation_requests = max(0, _active_generation_requests - 1) | |
| def _get_active_generation_requests() -> int: | |
| with _active_generation_lock: | |
| return _active_generation_requests | |
| # ---------- Remote ASR & Denoise via HTTP API ---------- | |
| def _api_asr(audio_path: str) -> str: | |
| """Call POST /asr on the nanovllm server to transcribe audio.""" | |
| import requests | |
| path = Path(audio_path) | |
| wav_b64 = base64.b64encode(path.read_bytes()).decode("utf-8") | |
| wav_fmt = path.suffix.lstrip(".").lower() or "wav" | |
| resp = requests.post( | |
| f"{NANOVLLM_API_BASE}/asr", | |
| json={"wav_base64": wav_b64, "wav_format": wav_fmt}, | |
| timeout=60, | |
| ) | |
| resp.raise_for_status() | |
| return resp.json().get("text", "") | |
| def _api_denoise(audio_path: str) -> str: | |
| """Call POST /denoise on the nanovllm server, return path to denoised temp file.""" | |
| import requests | |
| path = Path(audio_path) | |
| wav_b64 = base64.b64encode(path.read_bytes()).decode("utf-8") | |
| wav_fmt = path.suffix.lstrip(".").lower() or "wav" | |
| resp = requests.post( | |
| f"{NANOVLLM_API_BASE}/denoise", | |
| json={"wav_base64": wav_b64, "wav_format": wav_fmt}, | |
| timeout=120, | |
| ) | |
| resp.raise_for_status() | |
| denoised_b64 = resp.json()["wav_base64"] | |
| denoised_bytes = base64.b64decode(denoised_b64) | |
| tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") | |
| tmp.write(denoised_bytes) | |
| tmp.close() | |
| return tmp.name | |
| # ---------- Text Normalization (CPU-only, from VoxCPM text_normalize.py) ---------- | |
| _chinese_char_pattern = re.compile(r"[\u4e00-\u9fff]+") | |
| _text_normalizer = None | |
| _text_normalizer_lock = Lock() | |
| def _contains_chinese(text: str) -> bool: | |
| return bool(_chinese_char_pattern.search(text)) | |
| def _replace_corner_mark(text: str) -> str: | |
| text = text.replace("\u00b2", "\u5e73\u65b9") | |
| text = text.replace("\u00b3", "\u7acb\u65b9") | |
| text = text.replace("\u221a", "\u6839\u53f7") | |
| text = text.replace("\u2248", "\u7ea6\u7b49\u4e8e") | |
| text = text.replace("<", "\u5c0f\u4e8e") | |
| return text | |
| def _remove_bracket(text: str) -> str: | |
| text = text.replace("\uff08", " ").replace("\uff09", " ") | |
| text = text.replace("\u3010", " ").replace("\u3011", " ") | |
| text = text.replace("\u2018", "").replace("\u2019", "") | |
| text = text.replace("\u2014\u2014", " ") | |
| return text | |
| def _spell_out_number(text: str, inflect_parser) -> str: | |
| new_text = [] | |
| st = None | |
| for i, c in enumerate(text): | |
| if not c.isdigit(): | |
| if st is not None: | |
| num_str = inflect_parser.number_to_words(text[st:i]) | |
| new_text.append(num_str) | |
| st = None | |
| new_text.append(c) | |
| else: | |
| if st is None: | |
| st = i | |
| if st is not None and st < len(text): | |
| num_str = inflect_parser.number_to_words(text[st:]) | |
| new_text.append(num_str) | |
| return "".join(new_text) | |
| def _replace_blank(text: str) -> str: | |
| out_str = [] | |
| for i, c in enumerate(text): | |
| if c == " ": | |
| if ( | |
| i + 1 < len(text) and text[i + 1].isascii() and text[i + 1] != " " | |
| and i - 1 >= 0 and text[i - 1].isascii() and text[i - 1] != " " | |
| ): | |
| out_str.append(c) | |
| else: | |
| out_str.append(c) | |
| return "".join(out_str) | |
| def _clean_markdown(md_text: str) -> str: | |
| import regex | |
| md_text = re.sub(r"```.*?```", "", md_text, flags=re.DOTALL) | |
| md_text = re.sub(r"`[^`]*`", "", md_text) | |
| md_text = re.sub(r"!\[[^\]]*\]\([^\)]+\)", "", md_text) | |
| md_text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", md_text) | |
| md_text = re.sub(r"^(\s*)-\s+", r"\1", md_text, flags=re.MULTILINE) | |
| md_text = re.sub(r"<[^>]+>", "", md_text) | |
| md_text = re.sub(r"^#{1,6}\s*", "", md_text, flags=re.MULTILINE) | |
| md_text = re.sub(r"\n\s*\n", "\n", md_text) | |
| md_text = md_text.strip() | |
| return md_text | |
| def _clean_text(text: str) -> str: | |
| import regex | |
| text = _clean_markdown(text) | |
| text = regex.compile(r"\p{Emoji_Presentation}|\p{Emoji}\uFE0F", flags=regex.UNICODE).sub("", text) | |
| text = text.replace("\n", " ").replace("\t", " ") | |
| text = text.replace("\u201c", '"').replace("\u201d", '"') | |
| return text | |
| def _get_text_normalizer(): | |
| global _text_normalizer | |
| if _text_normalizer is not None: | |
| return _text_normalizer | |
| with _text_normalizer_lock: | |
| if _text_normalizer is not None: | |
| return _text_normalizer | |
| from wetext import Normalizer | |
| import inflect | |
| _text_normalizer = { | |
| "zh_tn": Normalizer(lang="zh", operator="tn", remove_erhua=True), | |
| "en_tn": Normalizer(lang="en", operator="tn"), | |
| "inflect": inflect.engine(), | |
| } | |
| logger.info("TextNormalizer loaded.") | |
| return _text_normalizer | |
| def normalize_text(text: str) -> str: | |
| """Normalize text (numbers, dates, abbreviations) for TTS input.""" | |
| tn = _get_text_normalizer() | |
| lang = "zh" if _contains_chinese(text) else "en" | |
| text = _clean_text(text) | |
| if lang == "zh": | |
| text = text.replace("=", "\u7b49\u4e8e") | |
| if re.search(r"([\d$%^*_+\u2265\u2264\u2260\u00d7\u00f7?=])", text): | |
| text = re.sub(r"(?<=[a-zA-Z0-9])-(?=\d)", " - ", text) | |
| text = tn["zh_tn"].normalize(text) | |
| text = _replace_blank(text) | |
| text = _replace_corner_mark(text) | |
| text = _remove_bracket(text) | |
| else: | |
| text = tn["en_tn"].normalize(text) | |
| text = _spell_out_number(text, tn["inflect"]) | |
| return text | |
| def _safe_prompt_wav_recognition( | |
| use_prompt_text: bool, prompt_wav: Optional[str], request: Optional[gr.Request] = None | |
| ) -> str: | |
| if not use_prompt_text or prompt_wav is None or not prompt_wav.strip(): | |
| return "" | |
| try: | |
| return _api_asr(prompt_wav) | |
| except Exception as exc: | |
| logger.warning(f"ASR recognition failed: {exc}") | |
| raise gr.Error(_get_i18n_text("asr_failed_error", request)) from exc | |
| # ---------- Audio helpers ---------- | |
| def _get_audio_duration_seconds(audio_path: str) -> float: | |
| import soundfile as sf | |
| try: | |
| info = sf.info(audio_path) | |
| return float(info.frames) / float(info.samplerate) | |
| except Exception: | |
| pass | |
| import subprocess | |
| try: | |
| result = subprocess.run( | |
| ["ffprobe", "-v", "error", "-show_entries", "format=duration", | |
| "-of", "default=noprint_wrappers=1:nokey=1", audio_path], | |
| capture_output=True, text=True, timeout=10, | |
| ) | |
| if result.returncode == 0 and result.stdout.strip(): | |
| return float(result.stdout.strip()) | |
| except (FileNotFoundError, subprocess.TimeoutExpired, ValueError): | |
| pass | |
| raise RuntimeError(f"Cannot determine audio duration: {audio_path}") | |
| def _validate_reference_audio_duration( | |
| audio_path: str, request: Optional[gr.Request] = None | |
| ) -> None: | |
| duration_seconds = _get_audio_duration_seconds(audio_path) | |
| if duration_seconds > MAX_REFERENCE_AUDIO_SECONDS: | |
| raise gr.Error(_get_i18n_text("reference_audio_too_long_error", request)) | |
| # ---------- Nano-vLLM HTTP API Client ---------- | |
| def _api_generate(payload: dict) -> str: | |
| """Call POST /generate, receive streaming MP3, save to temp file and return path.""" | |
| import requests | |
| url = f"{NANOVLLM_API_BASE}/generate" | |
| logger.info(f"Calling {url} ...") | |
| resp = requests.post(url, json=payload, stream=True, timeout=300) | |
| resp.raise_for_status() | |
| tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
| try: | |
| for chunk in resp.iter_content(chunk_size=64 * 1024): | |
| tmp.write(chunk) | |
| tmp.close() | |
| return tmp.name | |
| except Exception: | |
| tmp.close() | |
| if os.path.exists(tmp.name): | |
| os.unlink(tmp.name) | |
| raise | |
| def _api_get_info() -> dict: | |
| import requests | |
| resp = requests.get(f"{NANOVLLM_API_BASE}/info", timeout=10) | |
| resp.raise_for_status() | |
| return resp.json() | |
| # ---------- Generation via HTTP API ---------- | |
| def generate_tts_audio( | |
| text_input: str, | |
| control_instruction: str = "", | |
| reference_wav_path_input: Optional[str] = None, | |
| use_prompt_text: bool = False, | |
| prompt_text_input: str = "", | |
| cfg_value_input: float = 2.0, | |
| do_normalize: bool = True, | |
| denoise: bool = True, | |
| request: Optional[gr.Request] = None, | |
| ) -> str: | |
| _begin_generation_request() | |
| request_payload = { | |
| "event": "tts_request", | |
| "ui_language": _resolve_ui_language(request), | |
| "text": (text_input or "").strip(), | |
| "control_instruction": (control_instruction or "").strip(), | |
| "use_prompt_text": bool(use_prompt_text), | |
| "prompt_text": (prompt_text_input or "").strip(), | |
| "cfg_value": float(cfg_value_input), | |
| "do_normalize": bool(do_normalize), | |
| "denoise": bool(denoise), | |
| "has_reference_audio": bool(reference_wav_path_input and reference_wav_path_input.strip()), | |
| } | |
| if request_payload["has_reference_audio"]: | |
| try: | |
| request_payload["reference_audio_duration_seconds"] = round( | |
| _get_audio_duration_seconds(reference_wav_path_input), 3 | |
| ) | |
| except Exception as exc: | |
| request_payload["reference_audio_duration_error"] = str(exc) | |
| try: | |
| text = (text_input or "").strip() | |
| if not text: | |
| raise ValueError("Please input text to synthesize.") | |
| control = (control_instruction or "").strip() | |
| final_text = f"({control}){text}" if control and not use_prompt_text else text | |
| if do_normalize: | |
| try: | |
| original = final_text | |
| final_text = normalize_text(final_text) | |
| if final_text != original: | |
| logger.info(f"Text normalized: '{original[:60]}' -> '{final_text[:60]}'") | |
| except Exception as exc: | |
| logger.warning(f"Text normalization failed, using original: {exc}") | |
| prompt_text_clean = (prompt_text_input or "").strip() | |
| if use_prompt_text and not reference_wav_path_input: | |
| raise ValueError("Ultimate Cloning Mode requires a reference audio clip.") | |
| if use_prompt_text and not prompt_text_clean: | |
| raise ValueError( | |
| "Ultimate Cloning Mode requires a transcript. " | |
| "Please wait for ASR or fill it in manually." | |
| ) | |
| if not use_prompt_text: | |
| prompt_text_clean = "" | |
| has_ref = reference_wav_path_input and reference_wav_path_input.strip() | |
| if has_ref: | |
| _validate_reference_audio_duration(reference_wav_path_input, request) | |
| denoised_tmp = None | |
| api_payload: dict = { | |
| "target_text": final_text, | |
| "cfg_value": float(cfg_value_input), | |
| } | |
| try: | |
| if has_ref: | |
| actual_ref_path = reference_wav_path_input | |
| if denoise: | |
| logger.info("Applying server-side denoise to reference audio ...") | |
| try: | |
| denoised_tmp = _api_denoise(reference_wav_path_input) | |
| actual_ref_path = denoised_tmp | |
| logger.info("Denoise completed.") | |
| except Exception as exc: | |
| logger.warning(f"Denoise failed, using original audio: {exc}") | |
| ref_path = Path(actual_ref_path) | |
| wav_b64 = base64.b64encode(ref_path.read_bytes()).decode("utf-8") | |
| wav_fmt = ref_path.suffix.lstrip(".").lower() or "wav" | |
| if use_prompt_text: | |
| logger.info("[Ultimate Cloning] reference audio + transcript") | |
| api_payload["prompt_wav_base64"] = wav_b64 | |
| api_payload["prompt_wav_format"] = wav_fmt | |
| api_payload["prompt_text"] = prompt_text_clean | |
| api_payload["ref_audio_wav_base64"] = wav_b64 | |
| api_payload["ref_audio_wav_format"] = wav_fmt | |
| else: | |
| logger.info("[Controllable Cloning] reference audio only") | |
| api_payload["ref_audio_wav_base64"] = wav_b64 | |
| api_payload["ref_audio_wav_format"] = wav_fmt | |
| else: | |
| logger.info(f"[Voice Design] control: {control[:50] if control else 'None'}") | |
| logger.info(f"Generating: '{final_text[:80]}...'") | |
| mp3_path = _api_generate(api_payload) | |
| finally: | |
| if denoised_tmp and os.path.exists(denoised_tmp): | |
| try: | |
| os.unlink(denoised_tmp) | |
| except OSError: | |
| pass | |
| try: | |
| _append_request_log({**request_payload, "status": "success"}) | |
| except Exception as exc: | |
| logger.warning(f"Failed to append request log: {exc}") | |
| return mp3_path | |
| except (ValueError, gr.Error) as exc: | |
| try: | |
| _append_request_log({**request_payload, "status": "rejected", "error": str(exc)}) | |
| except Exception: | |
| pass | |
| if isinstance(exc, gr.Error): | |
| raise | |
| raise gr.Error(str(exc)) from exc | |
| except Exception as exc: | |
| logger.exception("Generation failed") | |
| try: | |
| _append_request_log({**request_payload, "status": "error", "error": str(exc)}) | |
| except Exception: | |
| pass | |
| raise gr.Error(_get_i18n_text("backend_retry_error", request)) from exc | |
| finally: | |
| _end_generation_request() | |
| # ---------- Inline i18n (en + zh-CN) ---------- | |
| _USAGE_INSTRUCTIONS_EN = ( | |
| "**VoxCPM2 — Three Modes of Speech Generation:**\n\n" | |
| "🎨 **Voice Design** — Create a brand-new voice \n" | |
| "No reference audio required. Describe the desired voice characteristics " | |
| "(gender, age, tone, emotion, pace …) in **Control Instruction**, and VoxCPM2 " | |
| "will craft a unique voice from your description alone.\n\n" | |
| "🎛️ **Controllable Cloning** — Clone a voice with optional style guidance \n" | |
| "Upload a reference audio clip, then use **Control Instruction** to steer " | |
| "emotion, speaking pace, and overall style while preserving the original timbre.\n\n" | |
| "🎙️ **Ultimate Cloning** — Reproduce every vocal nuance through audio continuation \n" | |
| "Turn on **Ultimate Cloning Mode** and provide (or auto-transcribe) the reference audio's transcript. " | |
| "The model treats the reference clip as a spoken prefix and seamlessly **continues** from it, faithfully preserving every vocal detail." | |
| "Note: This mode will disable Control Instruction.\n\n" | |
| "### [A Voice Chef's Guide to VoxCPM2](https://voxcpm.readthedocs.io/en/latest/cookbook.html)" | |
| ) | |
| _EXAMPLES_FOOTER_EN = ( | |
| "---\n" | |
| "**💡 Voice Description Examples:** \n" | |
| "Try the following Control Instructions to explore different voices: \n\n" | |
| "**Example 1 — Gentle & Melancholic Girl** \n" | |
| '`Control Instruction`: *"A young girl with a soft, sweet voice. ' | |
| 'Speaks slowly with a melancholic, slightly tsundere tone."* \n' | |
| '`Target Text`: *"I never asked you to stay… It\'s not like I care or anything. ' | |
| 'But… why does it still hurt so much now that you\'re gone?"* \n\n' | |
| "**Example 2 — Laid-Back Surfer Dude** \n" | |
| '`Control Instruction`: *"Relaxed young male voice, slightly nasal, ' | |
| 'lazy drawl, very casual and chill."* \n' | |
| '`Target Text`: *"Dude, did you see that set? The waves out there are totally gnarly today. ' | |
| "Just catching barrels all morning — it's like, totally righteous, you know what I mean?\"*" | |
| ) | |
| _USAGE_INSTRUCTIONS_ZH = ( | |
| "**VoxCPM2 — 三种语音生成方式:**\n\n" | |
| "🎨 **声音设计(Voice Design)** \n" | |
| "无需参考音频。在 **Control Instruction** 中描述目标音色特征" | |
| "(性别、年龄、语气、情绪、语速等),VoxCPM2 即可为你从零创造独一无二的声音。\n\n" | |
| "🎛️ **可控克隆(Controllable Cloning)** \n" | |
| "上传参考音频,同时可选地使用 **Control Instruction** 来指定情绪、语速、风格等表达方式," | |
| "在保留原始音色的基础上灵活控制说话风格。\n\n" | |
| "🎙️ **极致克隆(Ultimate Cloning)** \n" | |
| "开启 **极致克隆模式** 并提供参考音频的文字内容(可自动识别)。" | |
| "模型会将参考音频视为已说出的前文,以**音频续写**的方式完整还原参考音频中的所有声音细节。" | |
| "注意:该模式与可控克隆模式互斥,将禁用Control Instruction。\n\n" | |
| "### [VoxCPM 2 最佳实践指南](https://voxcpm.readthedocs.io/en/latest/cookbook.html)\n\n" | |
| ) | |
| _EXAMPLES_FOOTER_ZH = ( | |
| "---\n" | |
| "**💡 声音描述示例(中英文均可):** \n\n" | |
| "**示例 1 — 深宫太后** \n" | |
| '`Control Instruction`: *"中老年女性,声音低沉阴冷,语速缓慢而有力,' | |
| '字字深思熟虑,带有深不可测的城府与威慑感。"* \n' | |
| '`Target Text`: *"哀家在这深宫待了四十年,什么风浪没见过?你以为瞒得过哀家?"* \n\n' | |
| "**示例 2 — 暴躁驾校教练** \n" | |
| '`Control Instruction`: *"暴躁的中年男声,语速快,充满无奈和愤怒"* \n' | |
| '`Target Text`: *"踩离合!踩刹车啊!你往哪儿开呢?前面是树你看不见吗?' | |
| '我教了你八百遍了,打死方向盘!你是不是想把车给我开到沟里去?"* \n\n' | |
| "---\n" | |
| "**🗣️ 方言生成指南:** \n" | |
| "要生成地道的方言语音,请在 **Target Text** 中直接使用方言词汇和句式," | |
| "并在 **Control Instruction** 中描述方言特征。 \n\n" | |
| "**示例 — 广东话** \n" | |
| '`Control Instruction`: *"粤语,中年男性,语气平淡"* \n' | |
| '✅ 正确(粤语表达):*"伙計,唔該一個A餐,凍奶茶少甜!"* \n' | |
| '❌ 错误(普通话原文):*"伙计,麻烦来一个A餐,冻奶茶少甜!"* \n\n' | |
| "**示例 — 河南话** \n" | |
| '`Control Instruction`: *"河南话,接地气的大叔"* \n' | |
| '✅ 正确(河南话表达):*"恁这是弄啥嘞?晌午吃啥饭?"* \n' | |
| '❌ 错误(普通话原文):*"你这是在干什么呢?中午吃什么饭?"* \n\n' | |
| "🤖 **小技巧:** 不知道方言怎么写?可以用豆包、DeepSeek、Kimi 等 AI 助手" | |
| "将普通话翻译为方言文本,再粘贴到 Target Text 中即可。 \n\n" | |
| ) | |
| _I18N_TRANSLATIONS = { | |
| "en": { | |
| "reference_audio_label": "🎤 Reference Audio (optional — upload for cloning)", | |
| "show_prompt_text_label": "🎙️ Ultimate Cloning Mode (transcript-guided cloning)", | |
| "show_prompt_text_info": "Auto-transcribes reference audio for every vocal nuance reproduced. Control Instruction will be disabled when active.", | |
| "prompt_text_label": "Transcript of Reference Audio (auto-filled via ASR, editable)", | |
| "prompt_text_placeholder": "The transcript of your reference audio will appear here …", | |
| "control_label": "🎛️ Control Instruction (optional — supports Chinese & English)", | |
| "control_placeholder": "e.g. A warm young woman / 年轻女性,温柔甜美 / Excited and fast-paced", | |
| "target_text_label": "✍️ Target Text — the content to speak", | |
| "generate_btn": "🔊 Generate Speech", | |
| "generated_audio_label": "Generated Audio", | |
| "advanced_settings_title": "⚙️ Advanced Settings", | |
| "ref_denoise_label": "Reference audio enhancement", | |
| "ref_denoise_info": "Apply ZipEnhancer denoising to the reference audio before cloning", | |
| "normalize_label": "Text normalization", | |
| "normalize_info": "Normalize numbers, dates, and abbreviations via wetext", | |
| "cfg_label": "CFG (guidance scale)", | |
| "cfg_info": "Higher → closer to the prompt / reference; lower → more creative variation", | |
| "reference_audio_too_long_error": "Reference audio is too long. Please upload audio no longer than 50 seconds.", | |
| "denoise_busy_error": "Too many reference-audio enhancement requests are running. Please try again in a moment.", | |
| "denoise_failed_error": "Reference audio enhancement failed. Please try disabling denoise or use a cleaner clip.", | |
| "backend_retry_error": "The backend is temporarily unstable. Please try again in a moment.", | |
| "asr_failed_error": "ASR failed. Please fill the transcript manually or try another reference audio.", | |
| "usage_instructions": _USAGE_INSTRUCTIONS_EN, | |
| "examples_footer": _EXAMPLES_FOOTER_EN, | |
| }, | |
| "zh-CN": { | |
| "reference_audio_label": "🎤 参考音频(可选 — 上传后用于克隆)", | |
| "show_prompt_text_label": "🎙️ 极致克隆模式(基于文本引导的极致克隆)", | |
| "show_prompt_text_info": "自动识别参考音频文本,完整还原音色、节奏、情感等全部声音细节。开启后 Control Instruction 将暂时禁用", | |
| "prompt_text_label": "参考音频内容文本(ASR 自动填充,可手动编辑)", | |
| "prompt_text_placeholder": "参考音频的文字内容将自动识别并显示在此处 …", | |
| "control_label": "🎛️ Control Instruction(可选 — 支持中英文描述)", | |
| "control_placeholder": "如:年轻女性,温柔甜美 / A warm young woman / 暴躁老哥,语速飞快", | |
| "target_text_label": "✍️ Target Text — 要合成的目标文本", | |
| "generate_btn": "🔊 开始生成", | |
| "generated_audio_label": "生成结果", | |
| "advanced_settings_title": "⚙️ 高级设置", | |
| "ref_denoise_label": "参考音频降噪增强", | |
| "ref_denoise_info": "克隆前使用 ZipEnhancer 对参考音频进行降噪处理", | |
| "normalize_label": "文本规范化", | |
| "normalize_info": "自动规范化数字、日期及缩写(基于 wetext)", | |
| "cfg_label": "CFG(引导强度)", | |
| "cfg_info": "数值越高 → 越贴合提示/参考音色;数值越低 → 生成风格更自由", | |
| "reference_audio_too_long_error": "参考音频太长了,请上传不超过 50 秒的音频。", | |
| "denoise_busy_error": "当前参考音频降噪请求过多,请稍后再试。", | |
| "denoise_failed_error": "参考音频降噪失败,请尝试关闭降噪或更换更干净的音频。", | |
| "backend_retry_error": "后端暂时不稳定,请稍后再试。", | |
| "asr_failed_error": "ASR 识别失败,请手动填写参考音频文本,或更换一段参考音频后重试。", | |
| "usage_instructions": _USAGE_INSTRUCTIONS_ZH, | |
| "examples_footer": _EXAMPLES_FOOTER_ZH, | |
| }, | |
| "zh-Hans": None, | |
| "zh": None, | |
| } | |
| _I18N_TRANSLATIONS["zh-Hans"] = _I18N_TRANSLATIONS["zh-CN"] | |
| _I18N_TRANSLATIONS["zh"] = _I18N_TRANSLATIONS["zh-CN"] | |
| for _d in _I18N_TRANSLATIONS.values(): | |
| if _d is not None: | |
| for _k, _v in _I18N_TRANSLATIONS["en"].items(): | |
| _d.setdefault(_k, _v) | |
| I18N = gr.I18n(**_I18N_TRANSLATIONS) | |
| def _resolve_ui_language(request: Optional[gr.Request] = None) -> str: | |
| if request is None: | |
| return "en" | |
| accept_language = str(request.headers.get("accept-language", "")).lower() | |
| if accept_language.startswith("zh"): | |
| return "zh-CN" | |
| return "en" | |
| def _get_i18n_text(key: str, request: Optional[gr.Request] = None) -> str: | |
| locale = _resolve_ui_language(request) | |
| return _I18N_TRANSLATIONS.get(locale, _I18N_TRANSLATIONS["en"]).get( | |
| key, _I18N_TRANSLATIONS["en"].get(key, key) | |
| ) | |
| # ---------- Theme & CSS ---------- | |
| DEFAULT_TARGET_TEXT = ( | |
| "VoxCPM2 is a creative multilingual TTS model from ModelBest, " | |
| "designed to generate highly realistic speech." | |
| ) | |
| _CUSTOM_CSS = """ | |
| .logo-container { | |
| text-align: center; | |
| margin: 0.5rem 0 1rem 0; | |
| } | |
| .logo-container img { | |
| height: 80px; | |
| width: auto; | |
| max-width: 200px; | |
| display: inline-block; | |
| } | |
| /* Toggle switch style */ | |
| .switch-toggle { | |
| padding: 8px 12px; | |
| border-radius: 8px; | |
| background: var(--block-background-fill); | |
| } | |
| .switch-toggle input[type="checkbox"] { | |
| appearance: none; | |
| -webkit-appearance: none; | |
| width: 44px; | |
| height: 24px; | |
| background: #ccc; | |
| border-radius: 12px; | |
| position: relative; | |
| cursor: pointer; | |
| transition: background 0.3s ease; | |
| flex-shrink: 0; | |
| } | |
| .switch-toggle input[type="checkbox"]::after { | |
| content: ""; | |
| position: absolute; | |
| top: 2px; | |
| left: 2px; | |
| width: 20px; | |
| height: 20px; | |
| background: white; | |
| border-radius: 50%; | |
| transition: transform 0.3s ease; | |
| box-shadow: 0 1px 3px rgba(0,0,0,0.2); | |
| } | |
| .switch-toggle input[type="checkbox"]:checked { | |
| background: var(--color-accent); | |
| } | |
| .switch-toggle input[type="checkbox"]:checked::after { | |
| transform: translateX(20px); | |
| } | |
| """ | |
| _APP_THEME = gr.themes.Soft( | |
| primary_hue="blue", | |
| secondary_hue="gray", | |
| neutral_hue="slate", | |
| font=[gr.themes.GoogleFont("Inter"), "Arial", "sans-serif"], | |
| ) | |
| # ---------- UI ---------- | |
| def create_demo_interface(): | |
| assets_dir = Path.cwd().absolute() / "assets" | |
| if assets_dir.exists(): | |
| gr.set_static_paths(paths=[assets_dir]) | |
| def _on_toggle_instant(checked): | |
| if checked: | |
| return ( | |
| gr.update(visible=True, value="", placeholder="Recognizing reference audio..."), | |
| gr.update(visible=False), | |
| ) | |
| return ( | |
| gr.update(visible=False), | |
| gr.update(visible=True, interactive=True), | |
| ) | |
| def _run_asr_if_needed(checked, audio_path, request: gr.Request = None): | |
| if not checked or not audio_path: | |
| return gr.update() | |
| logger.info("Running ASR on reference audio...") | |
| asr_text = _safe_prompt_wav_recognition(True, audio_path, request=request) | |
| logger.info(f"ASR result: {asr_text[:60]}...") | |
| return gr.update( | |
| value=asr_text, | |
| placeholder=_get_i18n_text("prompt_text_placeholder", request), | |
| ) | |
| with gr.Blocks() as interface: | |
| if (assets_dir / "voxcpm_logo.png").exists(): | |
| gr.HTML( | |
| '<div class="logo-container">' | |
| '<img src="/gradio_api/file=assets/voxcpm_logo.png" alt="VoxCPM Logo">' | |
| "</div>" | |
| ) | |
| gr.Markdown(I18N("usage_instructions")) | |
| with gr.Row(): | |
| with gr.Column(): | |
| reference_wav = gr.Audio( | |
| sources=["upload", "microphone"], | |
| type="filepath", | |
| label=I18N("reference_audio_label"), | |
| ) | |
| show_prompt_text = gr.Checkbox( | |
| value=False, | |
| label=I18N("show_prompt_text_label"), | |
| info=I18N("show_prompt_text_info"), | |
| elem_classes=["switch-toggle"], | |
| ) | |
| prompt_text = gr.Textbox( | |
| value="", | |
| label=I18N("prompt_text_label"), | |
| placeholder=I18N("prompt_text_placeholder"), | |
| lines=2, | |
| visible=False, | |
| ) | |
| control_instruction = gr.Textbox( | |
| value="", | |
| label=I18N("control_label"), | |
| placeholder=I18N("control_placeholder"), | |
| lines=2, | |
| ) | |
| text = gr.Textbox( | |
| value=DEFAULT_TARGET_TEXT, | |
| label=I18N("target_text_label"), | |
| lines=3, | |
| ) | |
| with gr.Accordion(I18N("advanced_settings_title"), open=False): | |
| DoDenoisePromptAudio = gr.Checkbox( | |
| value=False, | |
| label=I18N("ref_denoise_label"), | |
| elem_classes=["switch-toggle"], | |
| info=I18N("ref_denoise_info"), | |
| ) | |
| DoNormalizeText = gr.Checkbox( | |
| value=False, | |
| label=I18N("normalize_label"), | |
| elem_classes=["switch-toggle"], | |
| info=I18N("normalize_info"), | |
| ) | |
| cfg_value = gr.Slider( | |
| minimum=1.0, | |
| maximum=3.0, | |
| value=2.0, | |
| step=0.1, | |
| label=I18N("cfg_label"), | |
| info=I18N("cfg_info"), | |
| ) | |
| run_btn = gr.Button(I18N("generate_btn"), variant="primary", size="lg") | |
| with gr.Column(): | |
| audio_output = gr.Audio(label=I18N("generated_audio_label")) | |
| gr.Markdown(I18N("examples_footer")) | |
| show_prompt_text.change( | |
| fn=_on_toggle_instant, | |
| inputs=[show_prompt_text], | |
| outputs=[prompt_text, control_instruction], | |
| ).then( | |
| fn=_run_asr_if_needed, | |
| inputs=[show_prompt_text, reference_wav], | |
| outputs=[prompt_text], | |
| ) | |
| run_btn.click( | |
| fn=generate_tts_audio, | |
| inputs=[ | |
| text, | |
| control_instruction, | |
| reference_wav, | |
| show_prompt_text, | |
| prompt_text, | |
| cfg_value, | |
| DoNormalizeText, | |
| DoDenoisePromptAudio, | |
| ], | |
| outputs=[audio_output], | |
| show_progress=True, | |
| api_name="generate", | |
| ) | |
| return interface | |
| def run_demo( | |
| server_name: str = "0.0.0.0", server_port: int = 7860, show_error: bool = True | |
| ): | |
| interface = create_demo_interface() | |
| interface.queue( | |
| max_size=_get_int_env("GRADIO_QUEUE_MAX_SIZE", 10), | |
| default_concurrency_limit=_get_int_env("GRADIO_DEFAULT_CONCURRENCY_LIMIT", 4), | |
| ).launch( | |
| server_name=server_name, | |
| server_port=int(os.environ.get("PORT", server_port)), | |
| show_error=show_error, | |
| i18n=I18N, | |
| theme=_APP_THEME, | |
| css=_CUSTOM_CSS, | |
| ssr_mode=_get_bool_env("GRADIO_SSR_MODE", False), | |
| ) | |
| if __name__ == "__main__": | |
| run_demo() | |