Spaces:
Running on Zero
Running on Zero
Automatically handle long text in both voice modes
Browse filesFiles changed (5)
19 unmodified lines20212223242526272819 unmodified lines## 模式- **內建語者**:預設為內建語者 A;Speaker B 使用模型庫 `checkpoints/speaker_b_embedding.pt`,固定 seed 56789 與 projector scale 1.125。- **參考音色**:錄音直接送入模型原生的 raw reference-audio conditioning,不經 ECAPA centroid;固定 seed 1337,每個文字分段仍只生成一次。- **長文**:以最多 80 個字元快速分段,各分段單次生成後加入短靜音串接。TTS model snapshot 固定為`6f7cab914a1e27c56b504ec663c0144dc25cc0a3`,預設 CFG 為 3.0,NFE 為 10。19 unmodified lines2021222324252627282919 unmodified lines## 模式- **內建語者**:預設為內建語者 A;Speaker B 使用模型庫 `checkpoints/speaker_b_embedding.pt`,固定 seed 56789 與 projector scale 1.125。長文會自動以最多 80 個字元分段。- **參考音色**:錄音直接送入模型原生的 raw reference-audio conditioning,不經 ECAPA centroid;固定 seed 1337。長文同樣自動分段。兩種模式的每個文字分段都只生成一次,分段間加入短靜音串接;不需要切換獨立長文模式。TTS model snapshot 固定為`6f7cab914a1e27c56b504ec663c0144dc25cc0a3`,預設 CFG 為 3.0,NFE 為 10。
23 unmodified lines242526272829303132333435363738394041147 unmodified lines1891901911921931941953593 unmodified lines378937903791379237933794379519 unmodified lines381538163817381838193820382116 unmodified lines383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388363 unmodified lines394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397123 unmodified linesINTERACTIVE_GPU_LEASE_SECONDS = 60LONGFORM_GPU_LEASE_SECONDS = 120try:import spacesinteractive_gpu = spaces.GPU(duration=INTERACTIVE_GPU_LEASE_SECONDS)longform_gpu = spaces.GPU(duration=LONGFORM_GPU_LEASE_SECONDS)USING_ZERO_GPU = Trueexcept ImportError:def interactive_gpu(function):return functionlongform_gpu = interactive_gpuUSING_ZERO_GPU = Falseimport gradio as gr147 unmodified linesSHORT_TEXT_CFG_MIN = 3.0SHORT_TEXT_CFG_UNITS = 6NETWORK_TEXT_CFG_MIN = 3.0CHUNK_CHARS = 80ONSET_CLAUSE_SEARCH_CHARS = 0MIN_CHUNK_CHARS = 12QUALITY_MEDIUM_TEXT_MAX_UNITS = 483593 unmodified lines) from error@interactive_gpu@timed_latency_requestdef tts_speaker(text: str,19 unmodified lines)@interactive_gpu@timed_latency_requestdef tts_reference(text: str,16 unmodified lines))@longform_gpu@timed_latency_requestdef tts_longform(text: str,speaker: str = DEFAULT_SPEAKER,cfg: float = DEFAULT_CFG,steps: int = DEFAULT_STEPS,speed: float = 1.0,):return pcm16_audio_output(*_synthesize(text,SPEAKERS.get(speaker, DEFAULT_CENTROID),cfg=cfg,steps=steps,speed=speed,request_seed=SPEAKER_GENERATION_SEED,speaker_projector_scale=(SPEAKER_B_PROJECTOR_SCALEif speaker == SPEAKER_B_LABELelse 1.0),))EXAMPLE_TEXTS = ["今天天氣真好,我們一起去散步吧。","我要吃蚵仔煎,然後去丟垃圾。","這學期的成績包括研究報告和期末考。","這是 AI TTS code switching 測試,混合中英文也沒問題。","注音符號測試:ㄅ、ㄆ、ㄇ、ㄈ。",]LONGFORM_EXAMPLES = ["今天的會議會先整理目前進度,再確認下一階段的工作。遇到需要討論的項目時,""請先記下問題,等報告結束後再一起處理。最後,我們會確認負責人和預計完成時間。","歡迎收聽今天的內容。第一段會介紹背景,第二段整理實際案例,最後一段則說明後續安排。""如果中途聽到英文術語,不必擔心,我們會用中文補充它的意思。",]63 unmodified linesreference_output,)with gr.Tab("長文"):with gr.Row():with gr.Column():longform_speaker = gr.Dropdown(list(SPEAKERS), value=DEFAULT_SPEAKER, label="語者")longform_text = gr.Textbox(label="長文(自動分段)",lines=8,max_lines=12,)longform_button = gr.Button("合成完整長文", variant="primary")with gr.Column():longform_output = gr.Audio(label="合成結果", type="numpy")gr.Examples(LONGFORM_EXAMPLES, inputs=longform_text, label="長文範例")longform_button.click(tts_longform,[longform_text, longform_speaker, cfg_input, steps_input, speed_input],longform_output,)gr.Markdown("合成語音僅供研究與評估展示;正式使用前請人工檢視。 ""[模型](https://huggingface.co/OpenFormosa/BlueMagpie-TTS) · "23 unmodified lines242526272829303132333435363738394041424344454647484950515253545556575859606162147 unmodified lines2102112122132142152163593 unmodified lines381038113812381338143815381619 unmodified lines383638373838383938403841384216 unmodified lines3859386038613862386338643865386638673868386938703871387263 unmodified lines39363937393839393940394123 unmodified linesINTERACTIVE_GPU_LEASE_SECONDS = 60LONGFORM_GPU_LEASE_SECONDS = 120AUTO_LONG_TEXT_CHARS = 80def _gpu_lease_seconds(text: object) -> int:"""Reserve the long-form lease only when automatic chunking needs it."""return (LONGFORM_GPU_LEASE_SECONDSif len(str(text)) > AUTO_LONG_TEXT_CHARSelse INTERACTIVE_GPU_LEASE_SECONDS)def _speaker_gpu_duration(text, speaker, cfg, steps, speed) -> int:del speaker, cfg, steps, speedreturn _gpu_lease_seconds(text)def _reference_gpu_duration(text, reference_wav, cfg, steps, speed) -> int:del reference_wav, cfg, steps, speedreturn _gpu_lease_seconds(text)try:import spacesspeaker_gpu = spaces.GPU(duration=_speaker_gpu_duration)reference_gpu = spaces.GPU(duration=_reference_gpu_duration)USING_ZERO_GPU = Trueexcept ImportError:def speaker_gpu(function):return functionreference_gpu = speaker_gpuUSING_ZERO_GPU = Falseimport gradio as gr147 unmodified linesSHORT_TEXT_CFG_MIN = 3.0SHORT_TEXT_CFG_UNITS = 6NETWORK_TEXT_CFG_MIN = 3.0CHUNK_CHARS = AUTO_LONG_TEXT_CHARSONSET_CLAUSE_SEARCH_CHARS = 0MIN_CHUNK_CHARS = 12QUALITY_MEDIUM_TEXT_MAX_UNITS = 483593 unmodified lines) from error@speaker_gpu@timed_latency_requestdef tts_speaker(text: str,19 unmodified lines)@reference_gpu@timed_latency_requestdef tts_reference(text: str,16 unmodified lines))EXAMPLE_TEXTS = ["今天天氣真好,我們一起去散步吧。","我要吃蚵仔煎,然後去丟垃圾。","這學期的成績包括研究報告和期末考。","這是 AI TTS code switching 測試,混合中英文也沒問題。","注音符號測試:ㄅ、ㄆ、ㄇ、ㄈ。","今天的會議會先整理目前進度,再確認下一階段的工作。遇到需要討論的項目時,""請先記下問題,等報告結束後再一起處理。最後,我們會確認負責人和預計完成時間。",]63 unmodified linesreference_output,)gr.Markdown("合成語音僅供研究與評估展示;正式使用前請人工檢視。 ""[模型](https://huggingface.co/OpenFormosa/BlueMagpie-TTS) · "
87 unmodified lines8889909192939434 unmodified lines1291301311321331341359 unmodified lines14514614714814915015115215315487 unmodified lines"_generate_fast_chunk","_assemble_fast_audio","tts_speaker","tts_longform",):called = {node.func.id34 unmodified linessource = APP_PATH.read_text(encoding="utf-8")load_speakers = ast.get_source_segment(source, _function_node("_load_speakers"))speaker_source = ast.get_source_segment(source, _function_node("tts_speaker"))longform_source = ast.get_source_segment(source, _function_node("tts_longform"))assert load_speakers is not Noneassert "default_index = 0" in load_speakers9 unmodified linesassert "request_seed=SPEAKER_GENERATION_SEED" in speaker_sourceassert "speaker_projector_scale=(" in speaker_sourceassert "if speaker == SPEAKER_B_LABEL" in speaker_sourceassert longform_source is not Noneassert "request_seed=SPEAKER_GENERATION_SEED" in longform_sourceassert "speaker_projector_scale=(" in longform_sourceassert "if speaker == SPEAKER_B_LABEL" in longform_sourcedef test_builtin_speaker_b_uses_the_pinned_checkpoint_embedding(tmp_path):87 unmodified lines8889909192939434 unmodified lines1291301311321331349 unmodified lines14414514614714814915015115215315415515615715815916016116216316416516687 unmodified lines"_generate_fast_chunk","_assemble_fast_audio","tts_speaker","tts_reference",):called = {node.func.id34 unmodified linessource = APP_PATH.read_text(encoding="utf-8")load_speakers = ast.get_source_segment(source, _function_node("_load_speakers"))speaker_source = ast.get_source_segment(source, _function_node("tts_speaker"))assert load_speakers is not Noneassert "default_index = 0" in load_speakers9 unmodified linesassert "request_seed=SPEAKER_GENERATION_SEED" in speaker_sourceassert "speaker_projector_scale=(" in speaker_sourceassert "if speaker == SPEAKER_B_LABEL" in speaker_sourcedef test_speaker_and_reference_modes_automatically_reserve_long_text_lease():source = APP_PATH.read_text(encoding="utf-8")lease = _isolated_function("_gpu_lease_seconds",{"AUTO_LONG_TEXT_CHARS": 80,"INTERACTIVE_GPU_LEASE_SECONDS": 60,"LONGFORM_GPU_LEASE_SECONDS": 120,},)assert lease("甲" * 80) == 60assert lease("甲" * 81) == 120assert "def tts_longform(" not in sourceassert 'with gr.Tab("長文")' not in sourcedef test_builtin_speaker_b_uses_the_pinned_checkpoint_embedding(tmp_path):
203 unmodified lines204205206207208209210211212203 unmodified linesassert _decorator_stage(echo_functions["evaluate_echo_smearing"]) == "echo"expected_gpu_decorators = {"tts_speaker": "interactive_gpu","tts_reference": "interactive_gpu","tts_longform": "longform_gpu",}for name, gpu_decorator in expected_gpu_decorators.items():decorators = {203 unmodified lines204205206207208209210211203 unmodified linesassert _decorator_stage(echo_functions["evaluate_echo_smearing"]) == "echo"expected_gpu_decorators = {"tts_speaker": "speaker_gpu","tts_reference": "reference_gpu",}for name, gpu_decorator in expected_gpu_decorators.items():decorators = {
997 unmodified lines99899910001001100210031004150 unmodified lines1155115611571158115911601161259 unmodified lines1421142214231424142514261427142814291430143114321433143414351436143714381439144014411442997 unmodified linesassert synthesize.args.kw_defaults[-1].value is Noneassert "request_seed = resolve_request_seed(request_seed, secrets.randbelow)" in sourceassert "run_coverage_adaptive_cascade(\n chunks,\n request_seed," in sourcefor wrapper_name in ("tts_speaker", "tts_reference", "tts_longform"):wrapper = functions[wrapper_name]arguments = wrapper.args.args + wrapper.args.kwonlyargsassert "request_seed" not in [argument.arg for argument in arguments]150 unmodified linesassert "return pcm16_verification_waveform(waveform)" in assemble_sourceassert "pcm16_audio_output" not in synthesize_sourceassert "return SR, waveform" in synthesize_sourcefor wrapper_name in ("tts_speaker", "tts_reference", "tts_longform"):wrapper_source = ast.get_source_segment(source, functions[wrapper_name])assert wrapper_source is not Noneassert "return pcm16_audio_output(" in wrapper_source259 unmodified lines)def test_space_uses_shorter_interactive_zero_gpu_lease():source = (ROOT / "app.py").read_text(encoding="utf-8")assert "INTERACTIVE_GPU_LEASE_SECONDS = 60" in sourceassert "LONGFORM_GPU_LEASE_SECONDS = 120" in sourceassert ("interactive_gpu = spaces.GPU(""duration=INTERACTIVE_GPU_LEASE_SECONDS)"in source)assert ("longform_gpu = spaces.GPU(duration=LONGFORM_GPU_LEASE_SECONDS)"in source)assert source.count("@interactive_gpu") == 2assert source.count("@longform_gpu") == 1def test_network_fragment_relaxation_is_range_bound_and_local_only():997 unmodified lines99899910001001100210031004150 unmodified lines1155115611571158115911601161259 unmodified lines14211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443997 unmodified linesassert synthesize.args.kw_defaults[-1].value is Noneassert "request_seed = resolve_request_seed(request_seed, secrets.randbelow)" in sourceassert "run_coverage_adaptive_cascade(\n chunks,\n request_seed," in sourcefor wrapper_name in ("tts_speaker", "tts_reference"):wrapper = functions[wrapper_name]arguments = wrapper.args.args + wrapper.args.kwonlyargsassert "request_seed" not in [argument.arg for argument in arguments]150 unmodified linesassert "return pcm16_verification_waveform(waveform)" in assemble_sourceassert "pcm16_audio_output" not in synthesize_sourceassert "return SR, waveform" in synthesize_sourcefor wrapper_name in ("tts_speaker", "tts_reference"):wrapper_source = ast.get_source_segment(source, functions[wrapper_name])assert wrapper_source is not Noneassert "return pcm16_audio_output(" in wrapper_source259 unmodified lines)def test_space_automatically_selects_zero_gpu_lease_from_text_length():source = (ROOT / "app.py").read_text(encoding="utf-8")assert "INTERACTIVE_GPU_LEASE_SECONDS = 60" in sourceassert "LONGFORM_GPU_LEASE_SECONDS = 120" in sourceassert "AUTO_LONG_TEXT_CHARS = 80" in sourceassert ("speaker_gpu = spaces.GPU(duration=_speaker_gpu_duration)"in source)assert ("reference_gpu = spaces.GPU(duration=_reference_gpu_duration)"in source)assert source.count("@speaker_gpu") == 1assert source.count("@reference_gpu") == 1assert "def tts_longform(" not in sourcedef test_network_fragment_relaxation_is_range_bound_and_local_only():
README.md+4-3
app.py+28-58
tests/test_latency_first_mode.py+18-6
tests/test_latency_timing.py+2-3
tests/test_release_pins.py+9-8