Upload 3 files
Browse filesFiles changed (3)
- README.md+6-6
- app.py+389-118
- requirements.txt+12-7
123456789101112---title: OmniVoiceemoji: πcolorFrom: greencolorTo: indigosdk: gradiosdk_version: 6.10.0app_file: app.pypinned: falselicense: apache-2.0short_description: High-quality voice cloning TTS for 600+ languages---123456789101112---title: Qwen3-TTS Demoemoji: ποΈcolorFrom: bluecolorTo: purplesdk: gradiosdk_version: 5.33.0app_file: app.pypinned: falselicense: apache-2.0suggested_hardware: zero-a10g---
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118#!/usr/bin/env python3"""HuggingFace Space entry point for OmniVoice demo."""import loggingimport osfrom typing import Any, Dictlogging.basicConfig(level=logging.WARNING,format="%(asctime)s %(name)s %(levelname)s: %(message)s",)logging.getLogger("omnivoice").setLevel(logging.DEBUG)import numpy as npimport spacesimport torchfrom omnivoice import OmniVoice, OmniVoiceGenerationConfigfrom omnivoice.cli.demo import build_demo# ---------------------------------------------------------------------------# Model loading# ---------------------------------------------------------------------------CHECKPOINT = os.environ.get("OMNIVOICE_MODEL", "k2-fsa/OmniVoice")print(f"Loading model from {CHECKPOINT} to cuda ...")model = OmniVoice.from_pretrained(CHECKPOINT,device_map="cuda",dtype=torch.float16,load_asr=True,)sampling_rate = model.sampling_rateprint("Model loaded successfully!")# ---------------------------------------------------------------------------# Generation logic# ---------------------------------------------------------------------------def _gen_core(text,language,ref_audio,instruct,num_step,guidance_scale,denoise,speed,duration,preprocess_prompt,postprocess_output,mode,ref_text=None,):if not text or not text.strip():return None, "Please enter the text to synthesize."gen_config = OmniVoiceGenerationConfig(num_step=int(num_step or 32),guidance_scale=float(guidance_scale) if guidance_scale is not None else 2.0,denoise=bool(denoise) if denoise is not None else True,preprocess_prompt=bool(preprocess_prompt),postprocess_output=bool(postprocess_output),)lang = language if (language and language != "Auto") else Nonekw: Dict[str, Any] = dict(text=text.strip(), language=lang, generation_config=gen_config)if speed is not None and float(speed) != 1.0:kw["speed"] = float(speed)if duration is not None and float(duration) > 0:kw["duration"] = float(duration)if mode == "clone":if not ref_audio:return None, "Please upload a reference audio."kw["voice_clone_prompt"] = model.create_voice_clone_prompt(ref_audio=ref_audio,ref_text=ref_text,)if mode == "design":if instruct and instruct.strip():kw["instruct"] = instruct.strip()try:audio = model.generate(**kw)except Exception as e:return None, f"Error: {type(e).__name__}: {e}"waveform = audio[0].squeeze(0).numpy()waveform = (waveform * 32767).astype(np.int16)return (sampling_rate, waveform), "Done."# ---------------------------------------------------------------------------# ZeroGPU wrapper# ---------------------------------------------------------------------------@spaces.GPU(duration=60)def generate_fn(*args, **kwargs):return _gen_core(*args, **kwargs)# ---------------------------------------------------------------------------# Build and launch demo# ---------------------------------------------------------------------------demo = build_demo(model, CHECKPOINT, generate_fn=generate_fn)if __name__ == "__main__":demo.queue().launch(allowed_paths=["/home/omnivoice/samples"])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389# coding=utf-8# Qwen3-TTS Gradio Demo for HuggingFace Spaces with Zero GPU# Supports: Voice Design, Voice Clone (Base), TTS (CustomVoice)#import subprocess#subprocess.run('pip install flash-attn==2.7.4.post1', shell=True)import osimport spacesimport gradio as grimport numpy as npimport torchfrom huggingface_hub import snapshot_download, loginfrom qwen_tts import Qwen3TTSModel# HF_TOKEN = os.environ.get('HF_TOKEN')# login(token=HF_TOKEN)# Model size optionsMODEL_SIZES = ["0.6B", "1.7B"]# Speaker and language choices for CustomVoice modelSPEAKERS = ["Aiden", "Dylan", "Eric", "Ono_anna", "Ryan", "Serena", "Sohee", "Uncle_fu", "Vivian"]LANGUAGES = ["Auto", "Chinese", "English", "Japanese", "Korean", "French", "German", "Spanish", "Portuguese", "Russian"]def get_model_path(model_type: str, model_size: str) -> str:"""Get model path based on type and size."""return snapshot_download(f"Qwen/Qwen3-TTS-12Hz-{model_size}-{model_type}")# ============================================================================# GLOBAL MODEL LOADING - Load all models at startup# ============================================================================print("Loading all models to CUDA...")# Voice Design model (1.7B only)print("Loading VoiceDesign 1.7B model...")voice_design_model = Qwen3TTSModel.from_pretrained(get_model_path("VoiceDesign", "1.7B"),device_map="cuda",dtype=torch.bfloat16,# token=HF_TOKEN,attn_implementation="kernels-community/flash-attn3",)# Base (Voice Clone) models - both sizesprint("Loading Base 0.6B model...")base_model_0_6b = Qwen3TTSModel.from_pretrained(get_model_path("Base", "0.6B"),device_map="cuda",dtype=torch.bfloat16,# token=HF_TOKEN,attn_implementation="kernels-community/flash-attn3",)print("Loading Base 1.7B model...")base_model_1_7b = Qwen3TTSModel.from_pretrained(get_model_path("Base", "1.7B"),device_map="cuda",dtype=torch.bfloat16,# token=HF_TOKEN,attn_implementation="kernels-community/flash-attn3",)# CustomVoice models - both sizesprint("Loading CustomVoice 0.6B model...")custom_voice_model_0_6b = Qwen3TTSModel.from_pretrained(get_model_path("CustomVoice", "0.6B"),device_map="cuda",dtype=torch.bfloat16,# token=HF_TOKEN,attn_implementation="kernels-community/flash-attn3",)print("Loading CustomVoice 1.7B model...")custom_voice_model_1_7b = Qwen3TTSModel.from_pretrained(get_model_path("CustomVoice", "1.7B"),device_map="cuda",dtype=torch.bfloat16,# token=HF_TOKEN,attn_implementation="kernels-community/flash-attn3",)print("All models loaded successfully!")# Model lookup dictionaries for easy accessBASE_MODELS = {"0.6B": base_model_0_6b,"1.7B": base_model_1_7b,}CUSTOM_VOICE_MODELS = {"0.6B": custom_voice_model_0_6b,"1.7B": custom_voice_model_1_7b,}# ============================================================================def _normalize_audio(wav, eps=1e-12, clip=True):"""Normalize audio to float32 in [-1, 1] range."""x = np.asarray(wav)if np.issubdtype(x.dtype, np.integer):info = np.iinfo(x.dtype)if info.min < 0:y = x.astype(np.float32) / max(abs(info.min), info.max)else:mid = (info.max + 1) / 2.0y = (x.astype(np.float32) - mid) / midelif np.issubdtype(x.dtype, np.floating):y = x.astype(np.float32)m = np.max(np.abs(y)) if y.size else 0.0if m > 1.0 + 1e-6:y = y / (m + eps)else:raise TypeError(f"Unsupported dtype: {x.dtype}")if clip:y = np.clip(y, -1.0, 1.0)if y.ndim > 1:y = np.mean(y, axis=-1).astype(np.float32)return ydef _audio_to_tuple(audio):"""Convert Gradio audio input to (wav, sr) tuple."""if audio is None:return Noneif isinstance(audio, tuple) and len(audio) == 2 and isinstance(audio[0], int):sr, wav = audiowav = _normalize_audio(wav)return wav, int(sr)if isinstance(audio, dict) and "sampling_rate" in audio and "data" in audio:sr = int(audio["sampling_rate"])wav = _normalize_audio(audio["data"])return wav, srreturn None@spaces.GPU(duration=60)def generate_voice_design(text, language, voice_description, progress=gr.Progress(track_tqdm=True)):"""Generate speech using Voice Design model (1.7B only)."""if not text or not text.strip():return None, "Error: Text is required."if not voice_description or not voice_description.strip():return None, "Error: Voice description is required."try:wavs, sr = voice_design_model.generate_voice_design(text=text.strip(),language=language,instruct=voice_description.strip(),non_streaming_mode=True,max_new_tokens=2048,)return (sr, wavs[0]), "Voice design generation completed successfully!"except Exception as e:return None, f"Error: {type(e).__name__}: {e}"@spaces.GPU(duration=60)def generate_voice_clone(ref_audio, ref_text, target_text, language, use_xvector_only, model_size, progress=gr.Progress(track_tqdm=True)):"""Generate speech using Base (Voice Clone) model."""if not target_text or not target_text.strip():return None, "Error: Target text is required."audio_tuple = _audio_to_tuple(ref_audio)if audio_tuple is None:return None, "Error: Reference audio is required."if not use_xvector_only and (not ref_text or not ref_text.strip()):return None, "Error: Reference text is required when 'Use x-vector only' is not enabled."try:tts = BASE_MODELS[model_size]wavs, sr = tts.generate_voice_clone(text=target_text.strip(),language=language,ref_audio=audio_tuple,ref_text=ref_text.strip() if ref_text else None,x_vector_only_mode=use_xvector_only,max_new_tokens=2048,)return (sr, wavs[0]), "Voice clone generation completed successfully!"except Exception as e:return None, f"Error: {type(e).__name__}: {e}"@spaces.GPU(duration=60)def generate_custom_voice(text, language, speaker, instruct, model_size, progress=gr.Progress(track_tqdm=True)):"""Generate speech using CustomVoice model."""if not text or not text.strip():return None, "Error: Text is required."if not speaker:return None, "Error: Speaker is required."try:tts = CUSTOM_VOICE_MODELS[model_size]wavs, sr = tts.generate_custom_voice(text=text.strip(),language=language,speaker=speaker.lower().replace(" ", "_"),instruct=instruct.strip() if instruct else None,non_streaming_mode=True,max_new_tokens=2048,)return (sr, wavs[0]), "Generation completed successfully!"except Exception as e:return None, f"Error: {type(e).__name__}: {e}"# Build Gradio UIdef build_ui():theme = gr.themes.Soft(font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"],)css = """.gradio-container {max-width: none !important;}.tab-content {padding: 20px;}"""with gr.Blocks(theme=theme, css=css, title="Qwen3-TTS Demo") as demo:gr.Markdown("""# Qwen3-TTS DemoA unified Text-to-Speech demo featuring three powerful modes:- **Voice Design**: Create custom voices using natural language descriptions- **Voice Clone (Base)**: Clone any voice from a reference audio- **TTS (CustomVoice)**: Generate speech with predefined speakers and optional style instructionsBuilt with [Qwen3-TTS](https://github.com/QwenLM/Qwen3-TTS) by Alibaba Qwen Team.""")with gr.Tabs():# Tab 1: Voice Design (Default, 1.7B only)with gr.Tab("Voice Design"):gr.Markdown("### Create Custom Voice with Natural Language")with gr.Row():with gr.Column(scale=2):design_text = gr.Textbox(label="Text to Synthesize",lines=4,placeholder="Enter the text you want to convert to speech...",value="It's in the top drawer... wait, it's empty? No way, that's impossible! I'm sure I put it there!")design_language = gr.Dropdown(label="Language",choices=LANGUAGES,value="Auto",interactive=True,)design_instruct = gr.Textbox(label="Voice Description",lines=3,placeholder="Describe the voice characteristics you want...",value="Speak in an incredulous tone, but with a hint of panic beginning to creep into your voice.")design_btn = gr.Button("Generate with Custom Voice", variant="primary")with gr.Column(scale=2):design_audio_out = gr.Audio(label="Generated Audio", type="numpy")design_status = gr.Textbox(label="Status", lines=2, interactive=False)design_btn.click(generate_voice_design,inputs=[design_text, design_language, design_instruct],outputs=[design_audio_out, design_status],)# Tab 2: Voice Clone (Base)with gr.Tab("Voice Clone (Base)"):gr.Markdown("### Clone Voice from Reference Audio")with gr.Row():with gr.Column(scale=2):clone_ref_audio = gr.Audio(label="Reference Audio (Upload a voice sample to clone)",type="numpy",)clone_ref_text = gr.Textbox(label="Reference Text (Transcript of the reference audio)",lines=2,placeholder="Enter the exact text spoken in the reference audio...",)clone_xvector = gr.Checkbox(label="Use x-vector only (No reference text needed, but lower quality)",value=False,)with gr.Column(scale=2):clone_target_text = gr.Textbox(label="Target Text (Text to synthesize with cloned voice)",lines=4,placeholder="Enter the text you want the cloned voice to speak...",)with gr.Row():clone_language = gr.Dropdown(label="Language",choices=LANGUAGES,value="Auto",interactive=True,)clone_model_size = gr.Dropdown(label="Model Size",choices=MODEL_SIZES,value="1.7B",interactive=True,)clone_btn = gr.Button("Clone & Generate", variant="primary")with gr.Row():clone_audio_out = gr.Audio(label="Generated Audio", type="numpy")clone_status = gr.Textbox(label="Status", lines=2, interactive=False)clone_btn.click(generate_voice_clone,inputs=[clone_ref_audio, clone_ref_text, clone_target_text, clone_language, clone_xvector, clone_model_size],outputs=[clone_audio_out, clone_status],)# Tab 3: TTS (CustomVoice)with gr.Tab("TTS (CustomVoice)"):gr.Markdown("### Text-to-Speech with Predefined Speakers")with gr.Row():with gr.Column(scale=2):tts_text = gr.Textbox(label="Text to Synthesize",lines=4,placeholder="Enter the text you want to convert to speech...",value="Hello! Welcome to Text-to-Speech system. This is a demo of our TTS capabilities.")with gr.Row():tts_language = gr.Dropdown(label="Language",choices=LANGUAGES,value="English",interactive=True,)tts_speaker = gr.Dropdown(label="Speaker",choices=SPEAKERS,value="Ryan",interactive=True,)with gr.Row():tts_instruct = gr.Textbox(label="Style Instruction (Optional)",lines=2,placeholder="e.g., Speak in a cheerful and energetic tone",)tts_model_size = gr.Dropdown(label="Model Size",choices=MODEL_SIZES,value="1.7B",interactive=True,)tts_btn = gr.Button("Generate Speech", variant="primary")with gr.Column(scale=2):tts_audio_out = gr.Audio(label="Generated Audio", type="numpy")tts_status = gr.Textbox(label="Status", lines=2, interactive=False)tts_btn.click(generate_custom_voice,inputs=[tts_text, tts_language, tts_speaker, tts_instruct, tts_model_size],outputs=[tts_audio_out, tts_status],)gr.Markdown("""---**Note**: This demo uses HuggingFace Spaces Zero GPU. Each generation has a time limit.For longer texts, please split them into smaller segments.""")return demoif __name__ == "__main__":demo = build_ui()demo.launch()
12345678910--extra-index-url https://download.pytorch.org/whl/cu128torch==2.8.0torchaudio==2.8.0transformers==5.3acceleratepydubsoundfilenumpygradiohf_transfer123456789101112131415# Qwen3-TTS Dependencies for HuggingFace Spacestorch==2.8.0transformers==4.57.3accelerate==1.12.0einopsgradiolibrosatorchaudiosoundfilesoxonnxruntimespacestorchnumpykernels
README.md+6-6
app.py+389-118
requirements.txt+12-7