Spaces:
Running
Running
aal-hawa commited on
Commit ·
eef4d32
1
Parent(s): a54038e
add
Browse files- app.py +120 -0
- requirements.txt +11 -0
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import tempfile
|
| 4 |
+
import torchaudio
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# ============================================================
|
| 10 |
+
# CosyVoice3 – Text-to-Speech with Voice Cloning
|
| 11 |
+
# ============================================================
|
| 12 |
+
WORK_DIR = Path.cwd()
|
| 13 |
+
COSYVOICE_DIR = WORK_DIR / "CosyVoice"
|
| 14 |
+
MODEL_DIR = COSYVOICE_DIR / "pretrained_models" / "Fun-CosyVoice3-0.5B"
|
| 15 |
+
|
| 16 |
+
cosyvoice = None
|
| 17 |
+
|
| 18 |
+
def setup_cosyvoice():
|
| 19 |
+
import subprocess
|
| 20 |
+
from huggingface_hub import snapshot_download
|
| 21 |
+
|
| 22 |
+
if not COSYVOICE_DIR.exists():
|
| 23 |
+
print("Cloning CosyVoice repository ...")
|
| 24 |
+
subprocess.run(
|
| 25 |
+
["git", "clone", "--recursive",
|
| 26 |
+
"https://github.com/FunAudioLLM/CosyVoice.git", str(COSYVOICE_DIR)],
|
| 27 |
+
check=True
|
| 28 |
+
)
|
| 29 |
+
if not MODEL_DIR.exists():
|
| 30 |
+
print("Downloading CosyVoice3 model weights ...")
|
| 31 |
+
snapshot_download(
|
| 32 |
+
"FunAudioLLM/Fun-CosyVoice3-0.5B-2512",
|
| 33 |
+
local_dir=str(MODEL_DIR),
|
| 34 |
+
)
|
| 35 |
+
sys.path.insert(0, str(COSYVOICE_DIR))
|
| 36 |
+
sys.path.insert(0, str(COSYVOICE_DIR / "third_party" / "Matcha-TTS"))
|
| 37 |
+
|
| 38 |
+
def load_cosyvoice():
|
| 39 |
+
global cosyvoice
|
| 40 |
+
if cosyvoice is not None:
|
| 41 |
+
return
|
| 42 |
+
setup_cosyvoice()
|
| 43 |
+
from cosyvoice.cli.cosyvoice import AutoModel
|
| 44 |
+
print("Loading CosyVoice3 model ...")
|
| 45 |
+
cosyvoice = AutoModel(
|
| 46 |
+
model_dir=str(MODEL_DIR),
|
| 47 |
+
load_trt=False,
|
| 48 |
+
fp16=False
|
| 49 |
+
)
|
| 50 |
+
print("CosyVoice3 loaded.")
|
| 51 |
+
|
| 52 |
+
def tts_speak(text, prompt_audio=None):
|
| 53 |
+
load_cosyvoice()
|
| 54 |
+
|
| 55 |
+
if not text.strip():
|
| 56 |
+
return None, "Please enter text."
|
| 57 |
+
|
| 58 |
+
if prompt_audio is None:
|
| 59 |
+
return None, "Please upload a short voice sample (3-10 seconds) for voice cloning."
|
| 60 |
+
|
| 61 |
+
sr, audio_data = prompt_audio
|
| 62 |
+
audio_tensor = torch.from_numpy(audio_data).float()
|
| 63 |
+
if audio_tensor.dim() == 2:
|
| 64 |
+
audio_tensor = audio_tensor.mean(dim=1)
|
| 65 |
+
if audio_tensor.dim() == 1:
|
| 66 |
+
audio_tensor = audio_tensor.unsqueeze(0)
|
| 67 |
+
|
| 68 |
+
if sr != 16000:
|
| 69 |
+
resampler = torchaudio.transforms.Resample(sr, 16000)
|
| 70 |
+
audio_tensor = resampler(audio_tensor)
|
| 71 |
+
|
| 72 |
+
prompt_path = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
| 73 |
+
torchaudio.save(prompt_path.name, audio_tensor, 16000)
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
prompt_text = "You are a helpful assistant.<|endofprompt|>"
|
| 77 |
+
speech_list = []
|
| 78 |
+
for result in cosyvoice.inference_zero_shot(
|
| 79 |
+
text, prompt_text, prompt_path.name, stream=False, speed=1.0
|
| 80 |
+
):
|
| 81 |
+
speech_list.append(result["tts_speech"])
|
| 82 |
+
output = torch.concat(speech_list, dim=1)
|
| 83 |
+
output_np = output.numpy().flatten()
|
| 84 |
+
return (24000, output_np), "Speech generated successfully!"
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return None, f"TTS Error: {str(e)}"
|
| 87 |
+
finally:
|
| 88 |
+
if os.path.exists(prompt_path.name):
|
| 89 |
+
os.remove(prompt_path.name)
|
| 90 |
+
|
| 91 |
+
# ============================================================
|
| 92 |
+
# Gradio Interface
|
| 93 |
+
# ============================================================
|
| 94 |
+
with gr.Blocks(title="CosyVoice3 TTS") as demo:
|
| 95 |
+
gr.Markdown("""
|
| 96 |
+
# 🔊 CosyVoice3 – Text-to-Speech
|
| 97 |
+
Upload a short voice sample (3-10 seconds), enter text, and generate speech in that voice.
|
| 98 |
+
""")
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column():
|
| 102 |
+
tts_text = gr.Textbox(
|
| 103 |
+
label="Text to Speak",
|
| 104 |
+
value="Hello, welcome to the text to speech demo.",
|
| 105 |
+
lines=3
|
| 106 |
+
)
|
| 107 |
+
prompt_audio = gr.Audio(
|
| 108 |
+
sources=["upload"],
|
| 109 |
+
type="numpy",
|
| 110 |
+
label="Voice Sample (3-10 sec)"
|
| 111 |
+
)
|
| 112 |
+
generate_btn = gr.Button("Generate Speech", variant="primary")
|
| 113 |
+
with gr.Column():
|
| 114 |
+
tts_audio = gr.Audio(label="Generated Speech")
|
| 115 |
+
tts_status = gr.Textbox(label="Status")
|
| 116 |
+
|
| 117 |
+
generate_btn.click(tts_speak, [tts_text, prompt_audio], [tts_audio, tts_status])
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
demo.launch(server_name="0.0.0.0")
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers.git@82a06db03535c49aa987719ed0746a76093b1ec4
|
| 2 |
+
torch
|
| 3 |
+
torchaudio
|
| 4 |
+
librosa
|
| 5 |
+
numpy
|
| 6 |
+
gradio
|
| 7 |
+
huggingface_hub
|
| 8 |
+
hyperpyyaml
|
| 9 |
+
modelscope
|
| 10 |
+
onnxruntime
|
| 11 |
+
soundfile
|