Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201import osimport sysimport tracebackfrom pathlib import Path
import gradio as grimport numpy as npimport spacesimport torchfrom huggingface_hub import snapshot_download
sys.path.append(str(Path(__file__).parent))
from fish_speech.models.text2semantic.inference import ( init_model, generate_long, load_codec_model, decode_to_audio, encode_audio)
device = "cuda" if torch.cuda.is_available() else "cpu"precision = torch.bfloat16
print("Downloading Fish Audio S2 Pro weights...")checkpoint_dir = snapshot_download(repo_id="fishaudio/s2-pro")
print("Loading LLAMA model...")llama_model, decode_one_token = init_model( checkpoint_path=checkpoint_dir, device=device, precision=precision, compile=False)
with torch.device(device): llama_model.setup_caches( max_batch_size=1, max_seq_len=llama_model.config.max_seq_len, dtype=next(llama_model.parameters()).dtype, )
print("Loading Codec model...")codec_checkpoint = os.path.join(checkpoint_dir, "codec.pth")codec_model = load_codec_model(codec_checkpoint, device=device, precision=precision)
print("All models loaded successfully!")
@spaces.GPU(duration=120)def tts_inference( text, ref_audio, ref_text, max_new_tokens, chunk_length, top_p, repetition_penalty, temperature): """ Main TTS Generation function decorated with @spaces.GPU to request GPU allocation only during execution. """ try: prompt_tokens_list = None if ref_audio is not None and ref_text: prompt_tokens_list = [encode_audio(ref_audio, codec_model, device).cpu()] generator = generate_long( model=llama_model, device=device, decode_one_token=decode_one_token, text=text, num_samples=1, max_new_tokens=max_new_tokens, top_p=top_p, top_k=30, temperature=temperature, repetition_penalty=repetition_penalty, compile=False, iterative_prompt=True, chunk_length=chunk_length, prompt_text=[ref_text] if ref_text else None, prompt_tokens=prompt_tokens_list, ) codes = [] for response in generator: if response.action == "sample": codes.append(response.codes) elif response.action == "next": break if not codes: raise gr.Error("No audio generated. Please check your text.") merged_codes = torch.cat(codes, dim=1) audio_waveform = decode_to_audio(merged_codes.to(device), codec_model) audio_np = audio_waveform.cpu().float().numpy() return (codec_model.sample_rate, audio_np)
except Exception as e: traceback.print_exc() raise gr.Error(f"Inference Error: {str(e)}")
custom_theme = gr.themes.Soft( primary_hue="blue", secondary_hue="indigo", font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],).set( block_title_text_weight="600", block_border_width="1px", block_shadow="0px 2px 4px rgba(0, 0, 0, 0.05)", button_shadow="0px 2px 4px rgba(0, 0, 0, 0.1)",)
with gr.Blocks(theme=custom_theme, title="Fish Audio S2 Pro") as app: gr.Markdown( """ <div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px 0;"> <h1 style="font-size: 2.5rem; font-weight: 800; color: #1E3A8A; margin-bottom: 10px;"> ๐ Fish Audio S2 Pro </h1> <p style="font-size: 1.1rem; color: #4B5563;"> State-of-the-Art Dual-Autoregressive Text-to-Speech. Supports 80+ languages, emotional inline control (e.g., <code>[laugh]</code>, <code>[whisper]</code>), and zero-shot voice cloning. </p> </div> """ ) with gr.Row(): with gr.Column(scale=5): gr.Markdown("### โ๏ธ Text Input") text_input = gr.Textbox( show_label=False, placeholder="Enter the text you want to synthesize here.\nTry adding tags like [laugh], [whisper], or [angry]!", lines=7 ) with gr.Accordion("๐๏ธ Voice Cloning (Optional Reference)", open=False): gr.Markdown("Upload a 5-10 second clear audio clip and type its exact transcription to clone the voice.") ref_audio = gr.Audio(label="Reference Audio", type="filepath") ref_text = gr.Textbox(label="Reference Text", placeholder="Transcription of the reference audio...") with gr.Accordion("โ๏ธ Advanced Settings", open=False): with gr.Row(): max_new_tokens = gr.Slider(0, 2048, 1024, step=8, label="Max New Tokens (0 = unlimited)") chunk_length = gr.Slider(100, 400, 200, step=8, label="Chunk Length") with gr.Row(): top_p = gr.Slider(0.1, 1.0, 0.7, step=0.01, label="Top-P") repetition_penalty = gr.Slider(0.9, 2.0, 1.2, step=0.01, label="Repetition Penalty") temperature = gr.Slider(0.1, 1.0, 0.7, step=0.01, label="Temperature") generate_btn = gr.Button("๐ Generate Speech", variant="primary", size="lg") with gr.Column(scale=4): gr.Markdown("### ๐ง Output") audio_output = gr.Audio(label="Generated Audio", type="numpy", interactive=False, autoplay=True) gr.Markdown( """ <div style="background-color: #EFF6FF; padding: 15px; border-radius: 8px; margin-top: 20px;"> <h4 style="margin-top: 0; color: #1D4ED8;">๐ก Pro Tips</h4> <ul style="margin-bottom: 0; color: #1E3A8A; font-size: 0.95rem;"> <li>You don't need phonemes, the model understands raw text seamlessly.</li> <li>Try wrapping specific words in brackets for inline emotional control.</li> <li>For cloning, the closer the transcript matches the audio, the better the result.</li> </ul> </div> """ ) gr.Markdown("### ๐ Examples") gr.Examples( examples=[ ["Hello world! This is a test of the Fish Audio S2 Pro model.", None, "", 1024, 200, 0.7, 1.2, 0.7], ["I can't believe it! [laugh] This is absolutely amazing!", None, "", 1024, 200, 0.7, 1.2, 0.7], ["[whisper in small voice] I have a secret to tell you... promise you won't tell anyone?", None, "", 1024, 200, 0.7, 1.2, 0.7] ], inputs=[text_input, ref_audio, ref_text, max_new_tokens, chunk_length, top_p, repetition_penalty, temperature], outputs=[audio_output], fn=tts_inference, cache_examples=False, ) generate_btn.click( fn=tts_inference, inputs=[text_input, ref_audio, ref_text, max_new_tokens, chunk_length, top_p, repetition_penalty, temperature], outputs=[audio_output] )
if __name__ == "__main__": app.launch()app.pyAdded+201