Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
"""
|
| 2 |
-
Script ini dibuat oleh __drat dan BF667 di
|
| 3 |
Petunjuk:
|
| 4 |
-
1.
|
| 5 |
-
2.
|
| 6 |
-
3.
|
| 7 |
-
4. Antarmuka dibuat dengan menggunakan Gradio dengan tema kustom bernama IndonesiaTheme.
|
| 8 |
|
| 9 |
Cara Menggunakan:
|
| 10 |
-
1. Pilih model suara dari dropdown
|
| 11 |
-
2. Atur parameter
|
| 12 |
-
3. Masukkan teks
|
| 13 |
-
4. Klik
|
| 14 |
-
5. Dengarkan hasil
|
| 15 |
"""
|
| 16 |
|
| 17 |
import asyncio
|
|
@@ -20,12 +19,15 @@ import logging
|
|
| 20 |
import os
|
| 21 |
import time
|
| 22 |
import traceback
|
| 23 |
-
import warnings
|
|
|
|
| 24 |
|
| 25 |
import edge_tts
|
| 26 |
import gradio as gr
|
| 27 |
import librosa
|
| 28 |
import torch
|
|
|
|
|
|
|
| 29 |
|
| 30 |
from config import Config
|
| 31 |
from lib.infer_pack.models import (
|
|
@@ -37,188 +39,140 @@ from lib.infer_pack.models import (
|
|
| 37 |
from rmvpe import RMVPE
|
| 38 |
from vc_infer_pipeline import VC
|
| 39 |
|
| 40 |
-
#
|
| 41 |
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
# Mengatur level logging untuk berbagai pustaka
|
| 44 |
-
logging.getLogger("fairseq").setLevel(logging.ERROR)
|
| 45 |
-
logging.getLogger("numba").setLevel(logging.ERROR)
|
| 46 |
-
logging.getLogger("markdown_it").setLevel(logging.ERROR)
|
| 47 |
-
logging.getLogger("urllib3").setLevel(logging.ERROR)
|
| 48 |
-
logging.getLogger("matplotlib").setLevel(logging.ERROR)
|
| 49 |
-
|
| 50 |
-
# Memeriksa apakah ada batasan sistem (contoh: menjalankan di HuggingFace Spaces)
|
| 51 |
-
limitation = os.getenv("SYSTEM") == "spaces"
|
| 52 |
-
|
| 53 |
-
# Memuat konfigurasi
|
| 54 |
config = Config()
|
| 55 |
-
BASE_DIR =
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
# Edge TTS
|
| 58 |
-
|
| 59 |
-
tts_voice_list = asyncio.get_event_loop().run_until_complete(edge_tts.list_voices())
|
| 60 |
tts_voices = [f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list]
|
| 61 |
|
| 62 |
-
# Memuat model RVC dari direktori
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
tgt_sr = cpt["config"][-1]
|
| 78 |
-
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
| 79 |
-
if_f0 = cpt.get("f0", 1)
|
| 80 |
-
version = cpt.get("version", "v1")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
else
|
| 87 |
-
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
| 88 |
-
elif version == "v2":
|
| 89 |
-
if if_f0 == 1:
|
| 90 |
-
net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=config.is_half)
|
| 91 |
-
else:
|
| 92 |
-
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
| 93 |
-
else:
|
| 94 |
-
raise ValueError("Versi tidak diketahui")
|
| 95 |
-
|
| 96 |
-
# Menghapus bagian encoder
|
| 97 |
-
del net_g.enc_q
|
| 98 |
-
net_g.load_state_dict(cpt["weight"], strict=False)
|
| 99 |
-
print("Model dimuat")
|
| 100 |
-
net_g.eval().to(config.device)
|
| 101 |
-
|
| 102 |
-
# Mengatur tipe data model
|
| 103 |
-
if config.is_half:
|
| 104 |
-
net_g = net_g.half()
|
| 105 |
-
else:
|
| 106 |
-
net_g = net_g.float()
|
| 107 |
-
|
| 108 |
-
vc = VC(tgt_sr, config)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
f"{
|
| 113 |
-
for f in os.listdir(f"{model_root}/{model_name}")
|
| 114 |
-
if f.endswith(".index")
|
| 115 |
-
]
|
| 116 |
-
if len(index_files) == 0:
|
| 117 |
-
print("Tidak ada file indeks ditemukan")
|
| 118 |
-
index_file = ""
|
| 119 |
-
else:
|
| 120 |
-
index_file = index_files[0]
|
| 121 |
-
print(f"File indeks ditemukan: {index_file}")
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
# Fungsi untuk memuat model Hubert
|
| 126 |
def load_hubert():
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
# Fungsi utama TTS yang menggabungkan Edge TTS dan RVC
|
| 145 |
def tts(
|
| 146 |
-
model_name,
|
| 147 |
-
speed,
|
| 148 |
-
tts_text,
|
| 149 |
-
tts_voice,
|
| 150 |
-
f0_up_key,
|
| 151 |
-
index_rate,
|
| 152 |
-
protect,
|
| 153 |
-
filter_radius=3,
|
| 154 |
-
resample_sr=0,
|
| 155 |
-
rms_mix_rate=0.25,
|
| 156 |
):
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
print(datetime.datetime.now())
|
| 160 |
-
print("Teks TTS:")
|
| 161 |
-
print(tts_text)
|
| 162 |
-
print(f"Suara TTS: {tts_voice}, kecepatan: {speed}")
|
| 163 |
-
print(f"Nama model: {model_name}")
|
| 164 |
-
print(f"Key: {f0_up_key}\n, Index: {index_rate}\n, Protect: {protect}")
|
| 165 |
try:
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
return (
|
| 170 |
-
f"Teks harus kurang dari 500 karakter di space ini, tetapi didapatkan {len(tts_text)} karakter.",
|
| 171 |
-
None,
|
| 172 |
-
None,
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
t0 = time.time()
|
| 176 |
-
|
| 177 |
-
if speed >= 0:
|
| 178 |
-
speed_str = f"+{speed}%"
|
| 179 |
-
else:
|
| 180 |
-
speed_str = f"{speed}%"
|
| 181 |
-
|
| 182 |
-
# Menggunakan Edge TTS untuk menghasilkan file suara sementara
|
| 183 |
asyncio.run(
|
| 184 |
edge_tts.Communicate(
|
| 185 |
tts_text, "-".join(tts_voice.split("-")[:-1]), rate=speed_str
|
| 186 |
-
).save(
|
| 187 |
)
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
# Memuat file suara dan menghitung durasi
|
| 192 |
-
audio, sr = librosa.load(edge_output_filename, sr=16000, mono=True)
|
| 193 |
duration = len(audio) / sr
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
# Batasan durasi audio jika ada batasan sistem
|
| 197 |
-
if limitation and duration >= 50:
|
| 198 |
-
print("Error: Audio terlalu panjang")
|
| 199 |
-
return (
|
| 200 |
-
f"Audio harus kurang dari 50 detik di space ini, tetapi didapatkan {duration}s.",
|
| 201 |
-
edge_output_filename,
|
| 202 |
-
None,
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
f0_up_key = int(f0_up_key)
|
| 206 |
|
| 207 |
-
# Memuat model data
|
| 208 |
tgt_sr, net_g, vc, version, index_file, if_f0 = model_data(model_name)
|
| 209 |
vc.model_rmvpe = rmvpe_model
|
| 210 |
times = [0, 0, 0]
|
| 211 |
-
f0_method = "rmvpe"
|
| 212 |
-
# Menggunakan pipeline RVC untuk menghasilkan file suara akhir
|
| 213 |
audio_opt = vc.pipeline(
|
| 214 |
hubert_model,
|
| 215 |
net_g,
|
| 216 |
0,
|
| 217 |
audio,
|
| 218 |
-
|
| 219 |
times,
|
| 220 |
f0_up_key,
|
| 221 |
-
|
| 222 |
index_file,
|
| 223 |
index_rate,
|
| 224 |
if_f0,
|
|
@@ -230,150 +184,76 @@ def tts(
|
|
| 230 |
protect,
|
| 231 |
None,
|
| 232 |
)
|
| 233 |
-
|
| 234 |
-
# Meresample jika diperlukan
|
| 235 |
-
if tgt_sr != resample_sr >= 16000:
|
| 236 |
-
tgt_sr = resample_sr
|
| 237 |
-
|
| 238 |
-
info = f"Berhasil. Waktu: edge-tts: {edge_time}s, npy: {times[0]}s, f0: {times[1]}s, infer: {times[2]}s"
|
| 239 |
-
print(info)
|
| 240 |
-
return (
|
| 241 |
-
info,
|
| 242 |
-
edge_output_filename,
|
| 243 |
-
(tgt_sr, audio_opt),
|
| 244 |
-
)
|
| 245 |
-
except EOFError:
|
| 246 |
-
info = (
|
| 247 |
-
"Sepertinya output edge-tts tidak valid. "
|
| 248 |
-
"Ini bisa terjadi jika teks input dan pembicara tidak cocok. "
|
| 249 |
-
"Misalnya, mungkin Anda memasukkan teks dalam bahasa Jepang (tanpa huruf alfabet) tetapi memilih pembicara non-Jepang?"
|
| 250 |
-
)
|
| 251 |
-
print(info)
|
| 252 |
-
return info, None, None
|
| 253 |
-
except:
|
| 254 |
-
info = traceback.format_exc()
|
| 255 |
-
print(info)
|
| 256 |
-
return info, None, None
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
# Memuat model
|
| 264 |
-
|
|
|
|
|
|
|
| 265 |
rmvpe_model = RMVPE("rmvpe.pt", config.is_half, config.device)
|
| 266 |
-
print("Model rmvpe dimuat.")
|
| 267 |
-
|
| 268 |
|
| 269 |
-
def download_model(url, model_name):
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
output_path
|
| 273 |
-
|
| 274 |
|
| 275 |
-
|
| 276 |
-
# Initial markdown text untuk ditampilkan di antarmuka
|
| 277 |
initial_md = """
|
| 278 |
-
<h1 align="center"><b>
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
Pembuktian algoritma **Retrieval-based Voice Conversion (RVC)** dan teknologi **Edge TTS** yang dapat membuat clone dari suara artis & selebriti di Indonesia.
|
| 283 |
-
|
| 284 |
-
**Perhatian:** Harap tidak menyalahgunakan teknologi ini. **Limitasi:** Teks 500, Audio 50 detik.
|
| 285 |
"""
|
| 286 |
|
| 287 |
-
|
| 288 |
-
app = gr.Blocks(theme="Thatguy099/Sonix", title="TTS-RVC-Artis Indonesia")
|
| 289 |
-
with app:
|
| 290 |
gr.Markdown(initial_md)
|
| 291 |
-
|
| 292 |
-
label="Model",
|
| 293 |
-
|
| 294 |
-
value=models[0],
|
| 295 |
-
)
|
| 296 |
-
f0_key_up = gr.Number(
|
| 297 |
-
label="Tune (+12 = 1 oktaf dari edge-tts, nilai terbaik tergantung pada model dan pembicara)",
|
| 298 |
-
value=2,
|
| 299 |
-
)
|
| 300 |
with gr.Column():
|
| 301 |
with gr.Row():
|
| 302 |
with gr.Tab("Unduh Model"):
|
| 303 |
-
url = gr.Textbox(label="
|
| 304 |
-
model_nae = gr.Textbox(label="
|
| 305 |
dlm = gr.Button("Unduh Model")
|
| 306 |
dlm.click(fn=download_model, inputs=[url, model_nae], outputs=None)
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
step=0.01,
|
| 322 |
-
interactive=True,
|
| 323 |
-
)
|
| 324 |
-
with gr.Column():
|
| 325 |
-
tts_voice = gr.Dropdown(
|
| 326 |
-
label="Pembicara Edge-tts (format: bahasa-Negara-Nama-Jenis Kelamin), pastikan jenis kelamin cocok dengan model",
|
| 327 |
-
choices=tts_voices,
|
| 328 |
-
allow_custom_value=False,
|
| 329 |
-
value="id-ID-ArdiNeural-Male", # Set nilai default
|
| 330 |
-
)
|
| 331 |
-
speed = gr.Slider(
|
| 332 |
-
minimum=-100,
|
| 333 |
-
maximum=100,
|
| 334 |
-
label="Kecepatan bicara (%)",
|
| 335 |
-
value=0,
|
| 336 |
-
step=10,
|
| 337 |
-
interactive=True,
|
| 338 |
-
)
|
| 339 |
-
tts_text = gr.Textbox(label="Teks Input", value="Konversi dari teks ke suara dalam bahasa Indonesia.")
|
| 340 |
-
with gr.Column():
|
| 341 |
-
with gr.Row():
|
| 342 |
-
but0 = gr.Button("Konversi", variant="primary")
|
| 343 |
-
info_text = gr.Textbox(label="Informasi Output")
|
| 344 |
-
with gr.Column():
|
| 345 |
-
with gr.Row():
|
| 346 |
-
edge_tts_output = gr.Audio(label="Suara Edge", type="filepath")
|
| 347 |
-
tts_output = gr.Audio(label="Hasil")
|
| 348 |
but0.click(
|
| 349 |
tts,
|
| 350 |
-
[
|
| 351 |
-
model_name,
|
| 352 |
-
speed,
|
| 353 |
-
tts_text,
|
| 354 |
-
tts_voice,
|
| 355 |
-
f0_key_up,
|
| 356 |
-
index_rate,
|
| 357 |
-
protect0,
|
| 358 |
-
],
|
| 359 |
[info_text, edge_tts_output, tts_output],
|
| 360 |
)
|
| 361 |
-
|
| 362 |
-
examples
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
inputs=[tts_text, tts_voice],
|
| 369 |
-
)
|
| 370 |
-
|
| 371 |
-
# Tambahkan footer di bagian bawah
|
| 372 |
gr.HTML("""
|
| 373 |
<footer style="text-align: center; margin-top: 20px; color:silver;">
|
| 374 |
Energi Semesta Digital ยฉ 2024 __drat. | ๐ฎ๐ฉ Untuk Indonesia Jaya!
|
| 375 |
</footer>
|
| 376 |
""")
|
| 377 |
|
| 378 |
-
|
| 379 |
-
app.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
Script ini dibuat oleh __drat dan BF667 di GitHub.
|
| 3 |
Petunjuk:
|
| 4 |
+
1. Mengkonversi teks menjadi suara menggunakan Edge TTS dan Retrieval-based Voice Conversion (RVC).
|
| 5 |
+
2. Mendukung model text-to-speech (TTS) untuk bahasa Indonesia, Jawa, dan Sunda.
|
| 6 |
+
3. Antarmuka menggunakan Gradio dengan tema kustom IndonesiaTheme.
|
|
|
|
| 7 |
|
| 8 |
Cara Menggunakan:
|
| 9 |
+
1. Pilih model suara dari dropdown.
|
| 10 |
+
2. Atur parameter (kecepatan bicara, pitch, dll.).
|
| 11 |
+
3. Masukkan teks untuk dikonversi.
|
| 12 |
+
4. Klik "Convert" untuk menghasilkan suara.
|
| 13 |
+
5. Dengarkan hasil melalui komponen audio.
|
| 14 |
"""
|
| 15 |
|
| 16 |
import asyncio
|
|
|
|
| 19 |
import os
|
| 20 |
import time
|
| 21 |
import traceback
|
| 22 |
+
import warnings
|
| 23 |
+
from pathlib import Path
|
| 24 |
|
| 25 |
import edge_tts
|
| 26 |
import gradio as gr
|
| 27 |
import librosa
|
| 28 |
import torch
|
| 29 |
+
import tqdm
|
| 30 |
+
import requests
|
| 31 |
|
| 32 |
from config import Config
|
| 33 |
from lib.infer_pack.models import (
|
|
|
|
| 39 |
from rmvpe import RMVPE
|
| 40 |
from vc_infer_pipeline import VC
|
| 41 |
|
| 42 |
+
# Konfigurasi awal
|
| 43 |
warnings.filterwarnings("ignore")
|
| 44 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 45 |
+
for logger_name in ["fairseq", "numba", "markdown_it", "urllib3", "matplotlib"]:
|
| 46 |
+
logging.getLogger(logger_name).setLevel(logging.ERROR)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
config = Config()
|
| 49 |
+
BASE_DIR = Path.cwd()
|
| 50 |
+
MODEL_ROOT = BASE_DIR / "weights"
|
| 51 |
+
EDGE_OUTPUT_FILENAME = "edge_output.mp3"
|
| 52 |
+
LIMITATION = os.getenv("SYSTEM") == "spaces"
|
| 53 |
|
| 54 |
+
# Memuat daftar suara Edge TTS
|
| 55 |
+
tts_voice_list = asyncio.run(edge_tts.list_voices())
|
|
|
|
| 56 |
tts_voices = [f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list]
|
| 57 |
|
| 58 |
+
# Memuat model RVC dari direktori weights
|
| 59 |
+
models = sorted([d for d in MODEL_ROOT.iterdir() if d.is_dir()])
|
| 60 |
+
|
| 61 |
+
def model_data(model_name: str):
|
| 62 |
+
"""Memuat data model berdasarkan nama model."""
|
| 63 |
+
try:
|
| 64 |
+
pth_path = next(MODEL_ROOT / model_name).glob("*.pth")
|
| 65 |
+
logging.info(f"Memuat model: {pth_path}")
|
| 66 |
+
cpt = torch.load(pth_path, map_location="cpu")
|
| 67 |
+
tgt_sr = cpt["config"][-1]
|
| 68 |
+
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
|
| 69 |
+
if_f0 = cpt.get("f0", 1)
|
| 70 |
+
version = cpt.get("version", "v1")
|
| 71 |
|
| 72 |
+
# Pilih model berdasarkan versi dan f0
|
| 73 |
+
model_classes = {
|
| 74 |
+
("v1", 1): SynthesizerTrnMs256NSFsid,
|
| 75 |
+
("v1", 0): SynthesizerTrnMs256NSFsid_nono,
|
| 76 |
+
("v2", 1): SynthesizerTrnMs768NSFsid,
|
| 77 |
+
("v2", 0): SynthesizerTrnMs768NSFsid_nono,
|
| 78 |
+
}
|
| 79 |
+
model_class = model_classes.get((version, if_f0))
|
| 80 |
+
if not model_class:
|
| 81 |
+
raise ValueError(f"Versi model tidak valid: {version}, f0: {if_f0}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
net_g = model_class(*cpt["config"], is_half=config.is_half)
|
| 84 |
+
del net_g.enc_q
|
| 85 |
+
net_g.load_state_dict(cpt["weight"], strict=False)
|
| 86 |
+
net_g.eval().to(config.device)
|
| 87 |
+
net_g = net_g.half() if config.is_half else net_g.float()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
vc = VC(tgt_sr, config)
|
| 90 |
+
index_file = next((MODEL_ROOT / model_name).glob("*.index"), "")
|
| 91 |
+
logging.info(f"File indeks: {index_file or 'Tidak ditemukan'}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
return tgt_sr, net_g, vc, version, str(index_file), if_f0
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logging.error(f"Error memuat model: {e}")
|
| 96 |
+
raise
|
| 97 |
|
|
|
|
| 98 |
def load_hubert():
|
| 99 |
+
"""Memuat model Hubert."""
|
| 100 |
+
try:
|
| 101 |
+
from fairseq import checkpoint_utils
|
| 102 |
+
models, _, _ = checkpoint_utils.load_model_ensemble_and_task(
|
| 103 |
+
[str(BASE_DIR / "hubert_base.pt")], arg_overrides={"data": str(BASE_DIR)}
|
| 104 |
+
)
|
| 105 |
+
hubert_model = models[0].to(config.device)
|
| 106 |
+
hubert_model = hubert_model.half() if config.is_half else hubert_model.float()
|
| 107 |
+
return hubert_model.eval()
|
| 108 |
+
except Exception as e:
|
| 109 |
+
logging.error(f"Error memuat Hubert: {e}")
|
| 110 |
+
raise
|
| 111 |
|
| 112 |
+
def download_file(url: str, output_path: str = None):
|
| 113 |
+
"""Mengunduh file dari URL dengan progress bar."""
|
| 114 |
+
try:
|
| 115 |
+
url = url.replace("/blob/", "/resolve/").replace("?download=true", "").strip()
|
| 116 |
+
output_path = Path(output_path or os.path.basename(url))
|
| 117 |
+
response = requests.get(url, stream=True, timeout=300)
|
| 118 |
+
response.raise_for_status()
|
| 119 |
+
|
| 120 |
+
total_size = int(response.headers.get("content-length", 0))
|
| 121 |
+
with open(output_path, "wb") as f, tqdm.tqdm(
|
| 122 |
+
desc=output_path.name, total=total_size, unit="B", unit_scale=True
|
| 123 |
+
) as pbar:
|
| 124 |
+
for chunk in response.iter_content(chunk_size=10 * 1024 * 1024):
|
| 125 |
+
f.write(chunk)
|
| 126 |
+
pbar.update(len(chunk))
|
| 127 |
+
return str(output_path)
|
| 128 |
+
except Exception as e:
|
| 129 |
+
logging.error(f"Error mengunduh file: {e}")
|
| 130 |
+
raise
|
| 131 |
|
|
|
|
| 132 |
def tts(
|
| 133 |
+
model_name: str,
|
| 134 |
+
speed: int,
|
| 135 |
+
tts_text: str,
|
| 136 |
+
tts_voice: str,
|
| 137 |
+
f0_up_key: int,
|
| 138 |
+
index_rate: float,
|
| 139 |
+
protect: float,
|
| 140 |
+
filter_radius: int = 3,
|
| 141 |
+
resample_sr: int = 0,
|
| 142 |
+
rms_mix_rate: float = 0.25,
|
| 143 |
):
|
| 144 |
+
"""Fungsi utama untuk konversi teks ke suara."""
|
| 145 |
+
logging.info(f"Memulai TTS: {model_name}, teks: {tts_text[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
try:
|
| 147 |
+
if LIMITATION and len(tts_text) > 500:
|
| 148 |
+
return f"Teks terlalu panjang: {len(tts_text)} karakter (>500).", None, None
|
| 149 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
t0 = time.time()
|
| 151 |
+
speed_str = f"+{speed}%" if speed >= 0 else f"{speed}%"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
asyncio.run(
|
| 153 |
edge_tts.Communicate(
|
| 154 |
tts_text, "-".join(tts_voice.split("-")[:-1]), rate=speed_str
|
| 155 |
+
).save(EDGE_OUTPUT_FILENAME)
|
| 156 |
)
|
| 157 |
+
edge_time = time.time() - t0
|
| 158 |
+
|
| 159 |
+
audio, sr = librosa.load(EDGE_OUTPUT_FILENAME, sr=16000, mono=True)
|
|
|
|
|
|
|
| 160 |
duration = len(audio) / sr
|
| 161 |
+
if LIMITATION and duration >= 50:
|
| 162 |
+
return f"Audio terlalu panjang: {duration}s (>50s).", EDGE_OUTPUT_FILENAME, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
|
|
|
| 164 |
tgt_sr, net_g, vc, version, index_file, if_f0 = model_data(model_name)
|
| 165 |
vc.model_rmvpe = rmvpe_model
|
| 166 |
times = [0, 0, 0]
|
|
|
|
|
|
|
| 167 |
audio_opt = vc.pipeline(
|
| 168 |
hubert_model,
|
| 169 |
net_g,
|
| 170 |
0,
|
| 171 |
audio,
|
| 172 |
+
EDGE_OUTPUT_FILENAME,
|
| 173 |
times,
|
| 174 |
f0_up_key,
|
| 175 |
+
"rmvpe",
|
| 176 |
index_file,
|
| 177 |
index_rate,
|
| 178 |
if_f0,
|
|
|
|
| 184 |
protect,
|
| 185 |
None,
|
| 186 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
+
tgt_sr = resample_sr if resample_sr >= 16000 else tgt_sr
|
| 189 |
+
info = f"Berhasil. Waktu: edge-tts: {edge_time:.2f}s, npy: {times[0]:.2f}s, f0: {times[1]:.2f}s, infer: {times[2]:.2f}s"
|
| 190 |
+
return info, EDGE_OUTPUT_FILENAME, (tgt_sr, audio_opt)
|
| 191 |
+
except Exception as e:
|
| 192 |
+
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 193 |
+
logging.error(error_msg)
|
| 194 |
+
return error_msg, None, None
|
| 195 |
|
| 196 |
+
# Memuat model
|
| 197 |
+
logging.info("Memuat model Hubert...")
|
| 198 |
+
hubert_model = load_hubert()
|
| 199 |
+
logging.info("Memuat model RMVPE...")
|
| 200 |
rmvpe_model = RMVPE("rmvpe.pt", config.is_half, config.device)
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
def download_model(url: str, model_name: str):
|
| 203 |
+
"""Mengunduh dan menyimpan model ke direktori weights."""
|
| 204 |
+
output_path = MODEL_ROOT / model_name
|
| 205 |
+
output_path.mkdir(exist_ok=True)
|
| 206 |
+
return download_file(url, output_path / Path(url).name)
|
| 207 |
|
| 208 |
+
# Antarmuka Gradio
|
|
|
|
| 209 |
initial_md = """
|
| 210 |
+
<h1 align="center"><b>TTS RVC Indonesia ๐ต</b></h1>
|
| 211 |
+
<p align="center">Konversi teks ke suara menggunakan Edge TTS dan RVC untuk suara artis Indonesia.</p>
|
| 212 |
+
<p><b>Perhatian:</b> Jangan menyalahgunakan teknologi ini. <b>Limitasi:</b> Teks maks. 500 karakter, audio maks. 50 detik.</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
"""
|
| 214 |
|
| 215 |
+
with gr.Blocks(theme="Thatguy099/Sonix", title="TTS-RVC Indonesia") as app:
|
|
|
|
|
|
|
| 216 |
gr.Markdown(initial_md)
|
| 217 |
+
with gr.Row():
|
| 218 |
+
model_name = gr.Dropdown(label="Model", choices=models, value=models[0])
|
| 219 |
+
f0_key_up = gr.Number(label="Tune (oktaf dari edge-tts)", value=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
with gr.Column():
|
| 221 |
with gr.Row():
|
| 222 |
with gr.Tab("Unduh Model"):
|
| 223 |
+
url = gr.Textbox(label="URL Model")
|
| 224 |
+
model_nae = gr.Textbox(label="Nama Model")
|
| 225 |
dlm = gr.Button("Unduh Model")
|
| 226 |
dlm.click(fn=download_model, inputs=[url, model_nae], outputs=None)
|
| 227 |
+
index_rate = gr.Slider(minimum=0, maximum=1, label="Tingkat Indeks", value=0.5)
|
| 228 |
+
protect0 = gr.Slider(minimum=0, maximum=0.5, label="Perlindungan", value=0.33, step=0.01)
|
| 229 |
+
tts_voice = gr.Dropdown(
|
| 230 |
+
label="Pembicara Edge-TTS (bahasa-Negara-Nama-Jenis Kelamin)",
|
| 231 |
+
choices=tts_voices,
|
| 232 |
+
value="id-ID-ArdiNeural-Male",
|
| 233 |
+
)
|
| 234 |
+
speed = gr.Slider(minimum=-100, maximum=100, label="Kecepatan Bicara (%)", value=0, step=10)
|
| 235 |
+
tts_text = gr.Textbox(label="Teks Input", value="Konversi teks ke suara dalam bahasa Indonesia.")
|
| 236 |
+
but0 = gr.Button("Konversi", variant="primary")
|
| 237 |
+
info_text = gr.Textbox(label="Informasi Output")
|
| 238 |
+
with gr.Row():
|
| 239 |
+
edge_tts_output = gr.Audio(label="Suara Edge", type="filepath")
|
| 240 |
+
tts_output = gr.Audio(label="Hasil")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
but0.click(
|
| 242 |
tts,
|
| 243 |
+
[model_name, speed, tts_text, tts_voice, f0_key_up, index_rate, protect0],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
[info_text, edge_tts_output, tts_output],
|
| 245 |
)
|
| 246 |
+
gr.Examples(
|
| 247 |
+
examples=[
|
| 248 |
+
["Ini adalah demo percobaan menggunakan Bahasa Indonesia untuk pria.", "id-ID-ArdiNeural-Male"],
|
| 249 |
+
["Ini adalah teks percobaan menggunakan Bahasa Indonesia pada wanita.", "id-ID-GadisNeural-Female"],
|
| 250 |
+
],
|
| 251 |
+
inputs=[tts_text, tts_voice],
|
| 252 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
gr.HTML("""
|
| 254 |
<footer style="text-align: center; margin-top: 20px; color:silver;">
|
| 255 |
Energi Semesta Digital ยฉ 2024 __drat. | ๐ฎ๐ฉ Untuk Indonesia Jaya!
|
| 256 |
</footer>
|
| 257 |
""")
|
| 258 |
|
| 259 |
+
app.launch()
|
|
|