File size: 6,206 Bytes
7f167fb acde4c3 7f167fb acde4c3 7f167fb acde4c3 7f167fb 7de38d8 7f167fb 7de38d8 7f167fb 7de38d8 7f167fb 7de38d8 7f167fb 7de38d8 7f167fb 7de38d8 7f167fb acde4c3 7f167fb acde4c3 7f167fb c6116e3 7f167fb c6116e3 7f167fb 89b8fd1 7f167fb c6116e3 7f167fb c6116e3 7f167fb acde4c3 7f167fb acde4c3 |
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 |
import re
import os
import gc
import tempfile
from uuid import uuid4
import spaces
import gradio as gr
import torchaudio
import numpy as np
from df.enhance import enhance, load_audio, save_audio
from config import Config
from .load_models import *
from .modules.CosyVoice.cosyvoice.utils.file_utils import load_wav
# Helper functions
def create_temp_file():
return tempfile.NamedTemporaryFile(delete=False)
def assign_language_tags(text):
return text
# # Process the text
# # based on the language assign <|zh|><|en|><|jp|><|yue|><|ko|> for Chinese/English/Japanese/Cantonese/Korean
# # at the start of the text for that language
# # e.g. input: δ½ ε₯½ Hello γγγ«γ‘γ― δ½ ε₯½ μλ
νμΈμ
# # output: <|zh|>δ½ ε₯½<|en|>Hello<|jp|>γγγ«γ‘γ―<|yue|>δ½ ε₯½<|ko|>μλ
νμΈμ
# # Define language patterns
# patterns = {
# 'zh': r'[\u4e00-\u9fff]+', # Chinese characters
# 'en': r'[a-zA-Z]+', # English letters
# 'jp': r'[\u3040-\u30ff\u31f0-\u31ff]+', # Japanese characters
# 'ko': r'[\uac00-\ud7a3]+', # Korean characters
# }
# # Find all matches
# matches = []
# for lang, pattern in patterns.items():
# for match in re.finditer(pattern, text):
# matches.append((match.start(), match.end(), lang, match.group()))
# # Sort matches by start position
# matches.sort(key=lambda x: x[0])
# # Build the result string
# result = []
# last_end = 0
# zh_count = 0
# for start, end, lang, content in matches:
# if start > last_end:
# result.append(text[last_end:start])
# if lang == 'zh':
# zh_count += 1
# if zh_count > 1:
# lang = 'yue'
# result.append(f'<|{lang}|>{content}')
# last_end = end
# if last_end < len(text):
# result.append(text[last_end:])
# return ''.join(result)
def update_mode(mode, sft_speaker, speaker_audio, voice_instructions):
if mode == 'SFT':
return (
gr.update( # sft_speaker
),
gr.update( # speaker_audio,
visible=False,
),
gr.update( # voice_instructions,
visible=False,
),
)
elif mode == 'VC':
return (
gr.update( # sft_speaker,
visible=False,
),
gr.update( # speaker_audio,
visible=True,
),
gr.update( # voice_instructions,
visible=True,
),
)
elif mode == 'VC-CrossLingual':
return (
gr.update( # sft_speaker,
visible=False,
),
gr.update( # speaker_audio,
visible=True,
),
gr.update( # voice_instructions,
visible=False,
),
)
elif mode == 'Instruct':
return (
gr.update( # sft_speaker,
visible=True,
),
gr.update( # speaker_audio,
visible=False,
),
gr.update( # voice_instructions,
visible=True,
),
)
else:
raise gr.Error('Invalid mode')
@spaces.GPU(duration=10)
def clear_audio(audio: np.ndarray):
# Save the audio file
audio_file = create_temp_file()
np.save(audio_file.name, audio)
# Load the audio file
audio, _ = load_audio(audio_file.name, sr=df_state.sr())
enhanced = enhance(df_model, df_state, audio)
# Save the enhanced audio file
save_audio(audio_file.name, enhanced, df_state.sr())
return gr.update( # speaker_audio, output_audio
value=audio_file.name,
)
@spaces.GPU(duration=20)
def gen_audio(text, mode, sft_speaker = None, speaker_audio = None, voice_instructions = None):
if mode == any(['VC', 'VC-CrossLingual']):
# Save the speaker audio file
speaker_audio_file = create_temp_file()
np.save(speaker_audio_file.name, speaker_audio)
prompt_speech_16k = load_wav('zero_shot_prompt.wav', 16000)
else:
speaker_audio_file = None
prompt_speech_16k = None
# Assign language tags
text = assign_language_tags(text)
# Generate the audio
out_file = create_temp_file()
if mode == 'SFT':
if not sft_speaker:
raise gr.Error('Please select a speaker')
for i, j in enumerate(cosyvoice_sft.inference_sft(
tts_text=text,
spk_id=sft_speaker,
)):
torchaudio.save(
out_file.name.format(i),
j['tts_speech'],
22050,
)
elif mode == 'VC':
if not speaker_audio_file:
raise gr.Error('Please upload an audio')
for i, j in enumerate(cosyvoice.inference_zero_shot(
tts_text=text,
prompt_text=voice_instructions,
prompt_speech_16k=prompt_speech_16k,
)):
torchaudio.save(
out_file.name.format(i),
j['tts_speech'],
22050,
)
elif mode == 'VC-CrossLingual':
if not speaker_audio_file:
raise gr.Error('Please upload an audio')
for i, j in enumerate(cosyvoice.inference_cross_lingual(
tts_text=text,
prompt_speech_16k=prompt_speech_16k,
)):
torchaudio.save(
out_file.name.format(i),
j['tts_speech'],
22050,
)
elif mode == 'Instruct':
if not voice_instructions:
raise gr.Error('Please enter voice instructions')
for i, j in enumerate(cosyvoice_instruct.inference_instruct(
tts_text=text,
spk_id=sft_speaker,
instruct_text=voice_instructions,
)):
torchaudio.save(
out_file.name.format(i),
j['tts_speech'],
22050,
)
return gr.update( # output_audio
value=out_file.name,
)
|