Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,515 Bytes
faadabf |
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 |
# Environment setup
from pathlib import Path
import os
import sys
sys.path.append(str(Path(__file__).parent))
# FIXME add weights_only=False in /usr/local/lib/python3.10/site-packages/fairseq/checkpoint_utils.py#315
if os.path.exists('/usr/local/lib/python3.10/site-packages/fairseq/checkpoint_utils.py'):
file_lines = []
with open('/usr/local/lib/python3.10/site-packages/fairseq/checkpoint_utils.py', 'r') as f:
for line in f:
file_lines.append(line.strip('\n'))
file_lines[314] = file_lines[314].replace(
"state = torch.load(f, map_location=torch.device(\"cpu\"))",
"state = torch.load(f, map_location=torch.device(\"cpu\"), weights_only=False)"
)
with open('/usr/local/lib/python3.10/site-packages/fairseq/checkpoint_utils.py', 'w') as f:
for line in file_lines:
f.write(line+'\n')
print('[DEBUG] added weights_only=False')
# Run
import spaces
import gradio as gr
from zipfile import ZipFile
from typing import Literal
from huggingface_hub import snapshot_download
from fireredtts.models.fireredtts import FireRedTTS
# NOTE disable verbose INFO logs
import logging
httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.WARNING)
# NOTE Some launching setups
# - install fairseq manually ("python -m pip install pip==24.0")
# - manually add weights_only=False in /usr/local/lib/python3.10/site-packages/fairseq/checkpoint_utils.py#315
# ================================================
# FireRedTTS1s Model
# ================================================
# Global model instance
tts_flow: FireRedTTS = None
tts_acollm: FireRedTTS = None
def initiate_model(pretrained_dir: str):
global tts_flow, tts_acollm
if tts_flow is None:
tts_flow = FireRedTTS(
config_path='configs/config_24k_flow.json',
pretrained_path=pretrained_dir,
)
if tts_acollm is None:
tts_acollm = FireRedTTS(
config_path='configs/config_24k.json',
pretrained_path=pretrained_dir,
)
# ================================================
# Gradio
# ================================================
# i18n
_i18n_key2lang_dict = dict(
# Title markdown
title_md_desc=dict(
en="FireRedTTS-1s 🔥 Streamable TTS",
zh="FireRedTTS-1s 🔥 可流式TTS",
),
# Decoder choice radio
decoder_choice_label=dict(
en="Decoder Choice",
zh="解码器选择",
),
decoder_choice_1=dict(
en="Flow Matching",
zh="Flow Matching",
),
decoder_choice_2=dict(
en="Acoustic LLM",
zh="Acoustic LLM",
),
# Speaker Prompt
spk_prompt_audio_label=dict(
en="Speaker Prompt Audio",
zh="参考语音",
),
spk_prompt_text_label=dict(
en="Speaker Prompt Text",
zh="参考语音的文本",
),
spk_prompt_text_placeholder=dict(
en="Speaker Prompt Text",
zh="参考语音的文本",
),
# Input textbox
target_text_input_label=dict(
en="Text To Synthesis",
zh="待合成文本",
),
target_text_input_placeholder=dict(
en="Text To Synthesis",
zh="待合成文本",
),
# Generate button
generate_btn_label=dict(
en="Generate Audio",
zh="合成",
),
# Generated audio
generated_audio_label=dict(
en="Generated Audio",
zh="合成的音频",
),
# Warining1: incomplete prompt info
warn_incomplete_prompt=dict(
en="Please provide prompt audio and text",
zh="请提供说话人参考语音与参考文本",
),
# Warining2: invalid text for target text input
warn_invalid_target_text=dict(
en="Empty input text",
zh="待合成文本为空",
),
)
global_lang: Literal['zh', 'en'] = 'zh'
def i18n(key):
global global_lang
return _i18n_key2lang_dict[key][global_lang]
def check_monologue_text(text:str, prefix:str=None)->bool:
text = text.strip()
# Check speaker tags
if prefix is not None and (not text.startswith(prefix)):
return False
# Remove prefix
if prefix is not None:
text = text.removeprefix(prefix)
text = text.strip()
# If empty?
if len(text) == 0:
return False
return True
@spaces.GPU(duration=60)
def synthesis_function(
spk_prompt_audio: str,
spk_prompt_text: str,
target_text: str,
decoder_choice: Literal[0, 1] = 0, # 0 means flow matching decoder
):
global tts_flow, tts_acollm
# Check prompt info
spk_prompt_text = spk_prompt_text.strip()
if spk_prompt_audio is None or spk_prompt_text == "":
gr.Warning(message=i18n('warn_incomplete_prompt'))
return None
# Check target text
target_text = target_text.strip()
if target_text == "":
gr.Warning(message=i18n('warn_invalid_target_text'))
return None
# Go synthesis
if decoder_choice == 0:
audio = tts_flow.synthesize(
prompt_wav=spk_prompt_audio,
prompt_text=spk_prompt_text,
text=target_text,
lang="zh",
use_tn=True
)
else:
audio = tts_acollm.synthesize(
prompt_wav=spk_prompt_audio,
prompt_text=spk_prompt_text,
text=target_text,
lang="zh",
use_tn=True
)
return (24000, audio.detach().cpu().squeeze(0).numpy())
# UI rendering
def render_interface()->gr.Blocks:
with gr.Blocks(title="FireRedTTS-2", theme=gr.themes.Default()) as page:
# ======================== UI ========================
# A large title
title_desc = gr.Markdown(value="# {}".format(i18n('title_md_desc')))
with gr.Row():
lang_choice = gr.Radio(
choices=['中文', 'English'],
value='中文',
label='Display Language/显示语言',
type="index",
interactive=True,
)
decoder_choice = gr.Radio(
choices=[i18n('decoder_choice_1'), i18n('decoder_choice_2')],
value=i18n('decoder_choice_1'),
label=i18n('decoder_choice_label'),
type="index",
interactive=True,
)
with gr.Row():
# ==== Speaker Prompt ====
spk_prompt_text = gr.Textbox(
label=i18n('spk_prompt_text_label'),
placeholder=i18n('spk_prompt_text_placeholder'),
lines=5,
)
spk_prompt_audio = gr.Audio(
label=i18n('spk_prompt_audio_label'),
type="filepath",
editable=False,
interactive=True,
) # Audio component returns tmp audio path
# ==== Target Text ====
target_text_input = gr.Textbox(
label=i18n('target_text_input_label'),
placeholder=i18n('target_text_input_placeholder'),
lines=5,
)
# Generate button
generate_btn = gr.Button(value=i18n('generate_btn_label'), variant="primary", size="lg")
# Long output audio
generate_audio = gr.Audio(
label=i18n('generated_audio_label'),
interactive=False,
)
# ======================== Action ========================
# Language action
def _change_component_language(lang):
global global_lang
global_lang = ['zh', 'en'][lang]
return [
# title_desc
gr.update(value="# {}".format(i18n('title_md_desc'))),
# decoder_choice
gr.update(label=i18n('decoder_choice_label')),
# spk_prompt_{audio,text}
gr.update(label=i18n('spk_prompt_text_label'), placeholder=i18n('spk_prompt_text_placeholder')),
gr.update(label=i18n('spk_prompt_audio_label')),
# target_text_input
gr.update(label=i18n('target_text_input_label'), placeholder=i18n('target_text_input_placeholder')),
# generate_btn
gr.update(value=i18n('generate_btn_label')),
# generate_audio
gr.update(label=i18n('generated_audio_label')),
]
lang_choice.change(
fn=_change_component_language,
inputs=[lang_choice],
outputs=[
title_desc, decoder_choice,
spk_prompt_text, spk_prompt_audio,
target_text_input,
generate_btn, generate_audio,
]
)
generate_btn.click(
fn=synthesis_function,
inputs=[spk_prompt_audio, spk_prompt_text, target_text_input, decoder_choice],
outputs=[generate_audio]
)
return page
if __name__ == '__main__':
# Download model
snapshot_download(repo_id='FireRedTeam/FireRedTTS-1S', local_dir='pretrained_models/FireRedTTS-1S')
# Unzip model, weights under "pretrained_models/FireRedTTS-1S/pretrained_models"
with ZipFile('pretrained_models/FireRedTTS-1S/pretrained_models.zip', 'r') as zipf:
zipf.extractall('pretrained_models/FireRedTTS-1S')
# Init model
initiate_model('pretrained_models/FireRedTTS-1S/pretrained_models')
print('[INFO] model loaded')
# UI
page = render_interface()
page.launch()
|