Spaces:
Running
on
Zero
Running
on
Zero
File size: 20,676 Bytes
eb07486 61f0f43 eb07486 61f0f43 eb07486 61f0f43 73e47b2 eb07486 73e47b2 61f0f43 eb07486 61f0f43 eb07486 61f0f43 eb07486 61f0f43 73e47b2 61f0f43 eb07486 61f0f43 eb07486 61f0f43 73e47b2 61f0f43 73e47b2 61f0f43 73e47b2 61f0f43 eb07486 73e47b2 61f0f43 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
import gradio as gr
import sys
import os
import torch
import torchaudio
import torchaudio.transforms as T
import numpy as np
import tempfile
import librosa
from pathlib import Path
print("=" * 60)
print("ποΈ Fun-CosyVoice3 TTS Initialization")
print("=" * 60)
# Step 1: Setup directories
print("\nπ Step 1: Setting up directories...")
WORK_DIR = Path.cwd()
COSYVOICE_DIR = WORK_DIR / "CosyVoice"
MODEL_DIR = COSYVOICE_DIR / "pretrained_models" / "Fun-CosyVoice3-0.5B"
print(f"Working directory: {WORK_DIR}")
print(f"CosyVoice directory: {COSYVOICE_DIR}")
print(f"Model directory: {MODEL_DIR}")
# Step 2: Clone CosyVoice if needed
if not COSYVOICE_DIR.exists():
print("\nπ₯ Step 2: Cloning CosyVoice repository...")
import subprocess
try:
subprocess.run([
'git', 'clone', '--recursive',
'https://github.com/FunAudioLLM/CosyVoice.git',
str(COSYVOICE_DIR)
], check=True)
print("β
Repository cloned successfully")
except Exception as e:
print(f"β Failed to clone repository: {e}")
raise
else:
print("\nβ
Step 2: CosyVoice repository already exists")
# Step 3: Download models
if not MODEL_DIR.exists():
print("\nπ₯ Step 3: Downloading models (this may take a few minutes)...")
from huggingface_hub import snapshot_download
try:
print("Downloading Fun-CosyVoice3-0.5B-2512...")
snapshot_download(
'FunAudioLLM/Fun-CosyVoice3-0.5B-2512',
local_dir=str(MODEL_DIR),
local_dir_use_symlinks=False
)
print("β
Model downloaded successfully")
except Exception as e:
print(f"β Failed to download model: {e}")
raise
else:
print("\nβ
Step 3: Models already exist")
# Step 4: Download ttsfrd (optional)
TTSFRD_DIR = COSYVOICE_DIR / "pretrained_models" / "CosyVoice-ttsfrd"
if not TTSFRD_DIR.exists():
print("\nπ₯ Step 4: Downloading ttsfrd...")
from huggingface_hub import snapshot_download
try:
snapshot_download(
'FunAudioLLM/CosyVoice-ttsfrd',
local_dir=str(TTSFRD_DIR),
local_dir_use_symlinks=False
)
print("β
ttsfrd downloaded successfully")
except Exception as e:
print(f"β οΈ Failed to download ttsfrd (will use WeText): {e}")
else:
print("\nβ
Step 4: ttsfrd already exists")
# Step 5: Add to Python path
print("\nπ§ Step 5: Configuring Python path...")
sys.path.insert(0, str(COSYVOICE_DIR))
sys.path.insert(0, str(COSYVOICE_DIR / "third_party" / "Matcha-TTS"))
print(f"Added to path: {COSYVOICE_DIR}")
print(f"Added to path: {COSYVOICE_DIR / 'third_party' / 'Matcha-TTS'}")
# Step 6: Import CosyVoice
print("\nπ¦ Step 6: Importing CosyVoice...")
try:
from cosyvoice.cli.cosyvoice import AutoModel as CosyVoiceAutoModel
from cosyvoice.utils.file_utils import load_wav
from cosyvoice.utils.common import set_all_random_seed
print("β
CosyVoice imported successfully")
except Exception as e:
print(f"β Failed to import CosyVoice: {e}")
raise
print("\n" + "=" * 60)
print("β
Initialization completed successfully!")
print("=" * 60 + "\n")
# Global variables
cosyvoice = None
target_sr = 24000
prompt_sr = 16000
max_val = 0.8
top_db = 60
hop_length = 220
win_length = 440
def load_model():
"""Load the CosyVoice model"""
global cosyvoice
if cosyvoice is None:
print("π Loading CosyVoice model...")
try:
cosyvoice = CosyVoiceAutoModel(
model_dir=str(MODEL_DIR),
load_trt=False,
fp16=False
)
print("β
Model loaded successfully!")
except Exception as e:
print(f"β Error loading model: {e}")
import traceback
traceback.print_exc()
raise gr.Error(f"Failed to load model: {e}")
return cosyvoice
def postprocess(wav_path):
"""Post-process audio - trim silence and normalize (from official code)"""
try:
speech = load_wav(wav_path, target_sr=target_sr, min_sr=16000)
# Trim silence from beginning and end
speech, _ = librosa.effects.trim(
speech, top_db=top_db,
frame_length=win_length,
hop_length=hop_length
)
# Normalize if too loud
if speech.abs().max() > max_val:
speech = speech / speech.abs().max() * max_val
# Add silence at the end
speech = torch.concat([speech, torch.zeros(1, int(target_sr * 0.2))], dim=1)
# Save back
torchaudio.save(wav_path, speech, target_sr)
return wav_path
except Exception as e:
print(f"β οΈ Postprocess warning: {e}")
return wav_path
def process_audio(audio_input):
"""
Convert audio input to proper format for CosyVoice
Handles: stereo->mono, different dtypes, resampling
"""
if audio_input is None:
return None
try:
sr, audio_data = audio_input
print(f"π Input audio - shape: {audio_data.shape}, dtype: {audio_data.dtype}, sr: {sr}Hz")
# Step 1: Normalize data type to float32
if audio_data.dtype == np.int16:
audio_data = audio_data.astype(np.float32) / 32768.0
elif audio_data.dtype == np.int32:
audio_data = audio_data.astype(np.float32) / 2147483648.0
elif audio_data.dtype == np.float64:
audio_data = audio_data.astype(np.float32)
elif audio_data.dtype != np.float32:
audio_data = audio_data.astype(np.float32)
# Step 2: Convert stereo to mono if needed
if len(audio_data.shape) == 2:
print(f" Converting stereo ({audio_data.shape[1]} channels) to mono...")
if audio_data.shape[1] == 2:
audio_data = audio_data.mean(axis=1)
elif audio_data.shape[1] == 1:
audio_data = audio_data.squeeze()
else:
audio_data = audio_data[:, 0]
# Step 3: Ensure 1D array
audio_data = audio_data.flatten()
# Step 4: Check and adjust duration
duration = len(audio_data) / sr
print(f" Duration: {duration:.2f}s")
if duration < 1:
return None, "β Audio too short (minimum 1 second)"
if duration > 30:
print(f" β οΈ Truncating audio from {duration:.2f}s to 30s")
audio_data = audio_data[:sr * 30]
# Step 5: Convert to torch tensor
audio_tensor = torch.from_numpy(audio_data).float()
# Step 6: Add channel dimension (1, samples)
if audio_tensor.dim() == 1:
audio_tensor = audio_tensor.unsqueeze(0)
print(f" Tensor shape: {audio_tensor.shape}")
# Step 7: Resample if needed
if sr != target_sr:
print(f" π Resampling from {sr}Hz to {target_sr}Hz...")
resampler = T.Resample(sr, target_sr)
audio_tensor = resampler(audio_tensor)
sr = target_sr
# Step 8: Save to temporary file
temp_path = tempfile.mktemp(suffix='.wav')
torchaudio.save(temp_path, audio_tensor, sr)
# Step 9: Post-process (trim silence, normalize)
temp_path = postprocess(temp_path)
print(f" β
Audio processed and saved: {os.path.basename(temp_path)}")
return temp_path
except Exception as e:
print(f"β Error processing audio: {e}")
import traceback
traceback.print_exc()
return None
def zero_shot_tts(tts_text, prompt_text, prompt_audio, seed, speed):
"""Zero-shot TTS synthesis - following official code structure"""
try:
# Validation
if not tts_text or not tts_text.strip():
return None, "β Please provide text to synthesize"
if len(tts_text) > 200:
return None, "β Text too long, please keep within 200 characters"
if not prompt_audio:
return None, "β Please upload reference audio"
if not prompt_text or not prompt_text.strip():
return None, "β Please provide prompt text"
# Load model
model = load_model()
# Process audio
prompt_audio_path = process_audio(prompt_audio)
if prompt_audio_path is None:
return None, "β Failed to process audio"
# Check sample rate
info = torchaudio.info(prompt_audio_path)
if info.sample_rate < prompt_sr:
return None, f"β Audio sample rate {info.sample_rate} is below {prompt_sr}Hz"
# Check duration
duration = info.num_frames / info.sample_rate
if duration > 10:
return None, "β Please keep prompt audio within 10 seconds"
# Clean inputs
tts_text = tts_text.strip()
prompt_text = prompt_text.strip()
# Build prompt following official format
# IMPORTANT: This is the official format from the code
full_prompt = f"You are a helpful assistant.<|endofprompt|>{prompt_text}"
print(f"\nπ΅ Generating speech...")
print(f" TTS text: '{tts_text[:100]}{'...' if len(tts_text) > 100 else ''}'")
print(f" Prompt text: '{prompt_text[:50]}{'...' if len(prompt_text) > 50 else ''}'")
print(f" Full prompt: '{full_prompt[:80]}{'...' if len(full_prompt) > 80 else ''}'")
print(f" Seed: {seed}, Speed: {speed}")
# Set random seed
set_all_random_seed(seed)
# Generate - following official code exactly
speech_list = []
for i in model.inference_zero_shot(
tts_text, # Text to synthesize
full_prompt, # Prompt with special format
prompt_audio_path, # Processed prompt audio
stream=False,
speed=speed
):
speech_list.append(i["tts_speech"])
# Concatenate all speech segments
output_speech = torch.concat(speech_list, dim=1)
# Clean up
if os.path.exists(prompt_audio_path):
os.remove(prompt_audio_path)
print(f" β
Generated audio shape: {output_speech.shape}")
print("β
Speech generated successfully!\n")
# Return as numpy array for Gradio
return (target_sr, output_speech.numpy().flatten()), "β
Success!"
except Exception as e:
print(f"β Error in zero_shot_tts: {e}")
import traceback
traceback.print_exc()
# Clean up on error
try:
if prompt_audio_path and os.path.exists(prompt_audio_path):
os.remove(prompt_audio_path)
except:
pass
return None, f"β Error: {str(e)}"
def instruct_tts(tts_text, instruct_text, prompt_audio, seed, speed):
"""Instruction-based TTS - following official code structure"""
try:
# Validation
if not tts_text or not tts_text.strip():
return None, "β Please provide text to synthesize"
if len(tts_text) > 200:
return None, "β Text too long, please keep within 200 characters"
if not prompt_audio:
return None, "β Please upload reference audio"
if not instruct_text or not instruct_text.strip():
return None, "β Please provide instruction text"
# Load model
model = load_model()
# Process audio
prompt_audio_path = process_audio(prompt_audio)
if prompt_audio_path is None:
return None, "β Failed to process audio"
# Clean inputs
tts_text = tts_text.strip()
instruct_text = instruct_text.strip()
print(f"\nπ Generating speech with instruction...")
print(f" TTS text: '{tts_text[:100]}{'...' if len(tts_text) > 100 else ''}'")
print(f" Instruction: '{instruct_text}'")
print(f" Seed: {seed}, Speed: {speed}")
# Set random seed
set_all_random_seed(seed)
# Generate - following official code
speech_list = []
for i in model.inference_instruct2(
tts_text, # Text to synthesize
instruct_text, # Instruction
prompt_audio_path, # Processed prompt audio
stream=False,
speed=speed
):
speech_list.append(i["tts_speech"])
# Concatenate all speech segments
output_speech = torch.concat(speech_list, dim=1)
# Clean up
if os.path.exists(prompt_audio_path):
os.remove(prompt_audio_path)
print(f" β
Generated audio shape: {output_speech.shape}")
print("β
Speech generated successfully!\n")
# Return as numpy array for Gradio
return (target_sr, output_speech.numpy().flatten()), "β
Success!"
except Exception as e:
print(f"β Error: {e}")
import traceback
traceback.print_exc()
# Clean up on error
try:
if prompt_audio_path and os.path.exists(prompt_audio_path):
os.remove(prompt_audio_path)
except:
pass
return None, f"β Error: {str(e)}"
# Instruction options (from official code)
instruct_options = [
"You are a helpful assistant. θ―·η¨εΉΏδΈθ―葨达γ<|endofprompt|>",
"You are a helpful assistant. θ―·η¨ε°½ε―θ½εΏ«ε°θ―ιθ―΄δΈε₯θ―γ<|endofprompt|>",
"You are a helpful assistant. θ―·η¨ζ£εΈΈηθ―ιθ―΄δΈε₯θ―γ<|endofprompt|>",
"You are a helpful assistant. θ―·η¨ζ
’δΈηΉηθ―ιθ―΄δΈε₯θ―γ<|endofprompt|>",
"You are a helpful assistant. Please speak in a professional tone.<|endofprompt|>",
"You are a helpful assistant. Please speak in a friendly tone.<|endofprompt|>",
]
# Create Gradio interface
with gr.Blocks(title="Fun-CosyVoice3 TTS") as demo:
gr.Markdown("""
# ποΈ Fun-CosyVoice3-0.5B Text-to-Speech
Advanced multilingual zero-shot TTS system supporting **9 languages** and **18+ Chinese dialects**.
Based on the official [CosyVoice](https://github.com/FunAudioLLM/CosyVoice) implementation.
""")
with gr.Tabs():
# Tab 1: Zero-Shot TTS
with gr.Tab("π― Zero-Shot Voice Cloning (3s Fast Cloning)"):
gr.Markdown("""
### Clone any voice with 3-10 seconds of reference audio
**Steps:**
1. Upload or record reference audio (β€30s, β₯16kHz)
2. Enter the **prompt text** (transcription of the reference audio)
3. Enter the **text to synthesize** (what you want the voice to say)
4. Click Generate
""")
with gr.Row():
with gr.Column():
zs_tts_text = gr.Textbox(
label="Text to synthesize (what will be spoken)",
placeholder="Enter the text you want to synthesize...",
lines=2,
value="Her handwriting is very neat, which suggests she likes things tidy."
)
zs_prompt_audio = gr.Audio(
label="Reference audio (upload or record)",
type="numpy"
)
zs_prompt_text = gr.Textbox(
label="Prompt text (transcription of reference audio)",
placeholder="Enter what is said in the reference audio...",
lines=2,
value=""
)
with gr.Row():
zs_seed = gr.Number(label="Random seed", value=0, precision=0)
zs_speed = gr.Slider(label="Speed", minimum=0.5, maximum=2.0, value=1.0, step=0.1)
zs_btn = gr.Button("π΅ Generate Speech", variant="primary", size="lg")
with gr.Column():
zs_output = gr.Audio(label="Generated speech")
zs_status = gr.Textbox(label="Status", interactive=False)
zs_btn.click(
fn=zero_shot_tts,
inputs=[zs_tts_text, zs_prompt_text, zs_prompt_audio, zs_seed, zs_speed],
outputs=[zs_output, zs_status]
)
gr.Markdown("""
**Important:**
- **Text to synthesize**: The new text you want to hear in the cloned voice
- **Prompt text**: Transcription of what is said in your reference audio
- **Reference audio**: 3-10 seconds of clear speech
**Example:**
- Reference audio: Someone saying "Hello, how are you?"
- Prompt text: "Hello, how are you?"
- Text to synthesize: "This is a test of voice cloning"
- Result: "This is a test of voice cloning" in the cloned voice
""")
# Tab 2: Instruction-Based TTS
with gr.Tab("π Instruction-Based Control (Natural Language)"):
gr.Markdown("""
### Control voice characteristics with natural language instructions
**Steps:**
1. Upload or record reference audio
2. Select or enter instruction (speed, dialect, emotion)
3. Enter text to synthesize
4. Click Generate
""")
with gr.Row():
with gr.Column():
inst_tts_text = gr.Textbox(
label="Text to synthesize",
placeholder="Enter your text...",
lines=2,
value="Welcome to the natural language control demo."
)
inst_prompt_audio = gr.Audio(
label="Reference audio",
type="numpy"
)
inst_text = gr.Dropdown(
label="Instruction",
choices=instruct_options,
value=instruct_options[0]
)
with gr.Row():
inst_seed = gr.Number(label="Random seed", value=0, precision=0)
inst_speed = gr.Slider(label="Speed", minimum=0.5, maximum=2.0, value=1.0, step=0.1)
inst_btn = gr.Button("π΅ Generate Speech", variant="primary", size="lg")
with gr.Column():
inst_output = gr.Audio(label="Generated speech")
inst_status = gr.Textbox(label="Status", interactive=False)
inst_btn.click(
fn=instruct_tts,
inputs=[inst_tts_text, inst_text, inst_prompt_audio, inst_seed, inst_speed],
outputs=[inst_output, inst_status]
)
gr.Markdown("""
**Example instructions:**
- "θ―·η¨εΉΏδΈθ―葨达" (Speak in Cantonese)
- "θ―·η¨ε°½ε―θ½εΏ«ε°θ―ιθ―΄" (Speak as fast as possible)
- "Please speak in a professional tone"
""")
gr.Markdown("""
---
### π Supported Languages & Dialects
**Languages:** Chinese, English, Japanese, Korean, German, Spanish, French, Italian, Russian
**Chinese Dialects:** Guangdong, Minnan, Sichuan, Dongbei, Shanxi, Shanghai, Tianjin, Shandong, and more
### β‘ Performance
- Model: Fun-CosyVoice3-0.5B (500M parameters)
- Sample Rate: 24kHz
- Latency: ~5-10s on CPU, ~2-3s on GPU
### π Resources
[Paper](https://arxiv.org/abs/2505.17589) β’ [GitHub](https://github.com/FunAudioLLM/CosyVoice) β’ [Model](https://huggingface.co/FunAudioLLM/Fun-CosyVoice3-0.5B-2512)
""")
if __name__ == "__main__":
print("\nπ Launching Gradio interface...")
demo.queue(max_size=10, default_concurrency_limit=2)
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
) |