File size: 9,774 Bytes
d4b1959 | 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 | import os
import sys
import logging
import torch
import utils
import commons
import gradio as gr
from models import SynthesizerTrn
from text import text_to_sequence
from text.symbols import symbols
# Konfigurasi Dasar
logging.getLogger('matplotlib').setLevel(logging.WARNING)
device = torch.device("cpu")
def get_text(text, hps):
text_norm = text_to_sequence(text, hps.data.text_cleaners)
if hps.data.add_blank:
text_norm = commons.intersperse(text_norm, 0)
return torch.LongTensor(text_norm)
def create_tts_fn(model, hps, speaker_ids):
def tts_fn(text, speaker_idx, speed):
if not text: return "Teks tidak boleh kosong!", None
try:
speaker_id = speaker_ids[speaker_idx]
stn_tst = get_text(text, hps)
with torch.no_grad():
x_tst = stn_tst.unsqueeze(0).to(device)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(device)
sid = torch.LongTensor([speaker_id]).to(device)
audio = model.infer(
x_tst, x_tst_lengths, sid=sid,
noise_scale=.667, noise_scale_w=0.8,
length_scale=1.0/speed
)[0][0,0].data.cpu().float().numpy()
return "Berhasil Membuat β
!", (hps.data.sampling_rate, audio)
except Exception as e:
return f"Error: {str(e)}", None
return tts_fn
# --- CSS: MILKY WHITE & PRECISION BLUE ---
css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700;800&display=swap');
body, .gradio-container {
background-color: #fdfdfd !important;
font-family: 'Inter', sans-serif !important;
}
footer { display: none !important; }
/* Header */
.main-header { text-align: center; padding: 20px 0 5px 0; }
.main-header h1 { font-weight: 800; color: #1299ff; font-size: 2.4rem; margin: 0; }
.main-header p { color: #8a99af; letter-spacing: 2px; font-weight: 600; font-size: 10px; margin-top: 2px; }
/* Menghilangkan Shadow & Border Halus pada Input */
.gradio-container .block, .gradio-container .form, .gradio-container input, .gradio-container textarea, .gradio-container .secondary {
box-shadow: none !important;
border: 1px solid #e2e8f0 !important;
background-color: #ffffff !important;
}
/* --- Label Biru & Putih Susu (Target Semua Label Termasuk Hasil Audio) --- */
.gradio-container .label-wrap span,
.gradio-container span.svelte-1gfkn6j,
.gradio-container label span {
background-color: #e6f4ff !important;
color: #1299ff !important;
border-radius: 8px !important;
padding: 2px 10px !important;
box-shadow: none !important;
border: none !important;
font-weight: 700 !important;
font-size: 12px !important;
}
/* --- Audio Player (Wadah Putih Bersih) --- */
.gradio-container .audio-container {
background-color: #ffffff !important;
border: 1px solid #e2e8f0 !important;
}
/* Status Box */
.status-card {
background: #ffffff;
border: 1px solid #eef2f7;
border-radius: 12px;
padding: 12px 20px;
margin: 0 auto 15px auto;
max-width: 380px;
}
.status-item { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #4a5568; margin: 2px 0; }
.dot { height: 6px; width: 6px; background-color: #1299ff; border-radius: 50%; display: inline-block; }
/* Character Info Grid */
.char-scroll-box { max-height: 280px; overflow-y: auto; padding: 5px; }
.char-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.char-card {
background: white;
border-left: 4px solid #1299ff;
padding: 10px;
border-radius: 10px;
border-top: 1px solid #f0f4f8;
border-right: 1px solid #f0f4f8;
border-bottom: 1px solid #f0f4f8;
cursor: pointer;
transition: all 0.2s;
}
.char-card:hover { background: #f0f7ff; transform: translateY(-2px); border-color: #0072ff; }
.char-name-jp { font-weight: 700; font-size: 13px; color: #1a202c; display: block; }
.char-name-en { font-size: 11px; color: #718096; }
/* Tombol Generate */
.generate-btn {
background: linear-gradient(135deg, #00c6ff 0%, #0072ff 100%) !important;
border: none !important; color: white !important; font-weight: 700 !important;
border-radius: 12px !important; height: 45px !important;
}
#custom-img { border-radius: 15px !important; border: 3px solid #fff; }
"""
if __name__ == '__main__':
config_path = "saved_model/config.json"
model_path = "saved_model/model.pth"
cover_path = "saved_model/cover.png"
hps = utils.get_hparams_from_file(config_path)
posterior_channels = 80 if ("use_mel_posterior_encoder" in hps.model.keys() and hps.model.use_mel_posterior_encoder) else hps.data.filter_length // 2 + 1
model = SynthesizerTrn(
len(symbols), posterior_channels,
hps.train.segment_size // hps.data.hop_length,
n_speakers=hps.data.n_speakers, **hps.model).to(device)
utils.load_checkpoint(model_path, model, None)
model.eval()
speakers = [name for name in hps.speakers if name != "None"]
speaker_ids = [i for i, name in enumerate(hps.speakers) if name != "None"]
with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
# Header
gr.HTML("""<div class="main-header"><h1>Blue Archive</h1><p>PREMIUM TEXT-TO-SPEECH SERVICE</p></div>""")
# Status Box
gr.HTML(f"""
<div class="status-card">
<b style="font-size: 13px; color: #1299ff; display: block; margin-bottom: 4px;">System Status</b>
<div class="status-item"><span class="dot"></span> Model: LOADED</div>
<div class="status-item"><span class="dot"></span> Characters: {len(speakers)}</div>
<div style="font-size: 10px; color: #8a99af; margin-top: 4px; border-top: 1px solid #f7fafc; padding-top: 3px;">v3.0 β’ Ready for generation</div>
</div>
""")
# --- KOMPONEN PENGHUBUNG (BRIDGE) ---
# Ini textbox tersembunyi. Saat JS mengisi angka di sini, Python akan bereaksi.
hidden_choice = gr.Textbox(visible=False, elem_id="hidden_choice")
with gr.Row():
with gr.Column(scale=1):
if os.path.exists(cover_path):
gr.Image(cover_path, elem_id="custom-img", show_label=False, interactive=False)
input_text = gr.TextArea(label="Input Teks (Japanese)", placeholder="ε
ηγδ½γγζδΌγγγΎγγγγοΌ", lines=3)
with gr.Row():
# Dropdown Karakter
speaker_sel = gr.Dropdown(label="π·οΈ Pilih Karakter", choices=speakers, type="index", value=speakers[0], elem_id="spk_drop")
speed_sl = gr.Slider(label="Kecepatan", minimum=0.5, maximum=2.0, value=1.0, step=0.1)
btn = gr.Button("π Generate Voice π", variant="primary", elem_classes="generate-btn")
with gr.Column(scale=1):
out_msg = gr.Textbox(label="Status")
out_audio = gr.Audio(label="Hasil Audio", type="numpy")
with gr.Accordion("π Character Information π", open=True):
# Kita gunakan onclick="selectChar(urutan_angka)"
char_html = "".join([
f'<div class="char-card" onclick="selectChar({i})">'
f'<span class="char-name-jp">{name}</span>'
f'<span class="char-name-en">Character {i+1}</span>'
f'</div>' for i, name in enumerate(speakers)
])
gr.HTML(f'<div class="char-scroll-box"><div class="char-grid">{char_html}</div></div>')
gr.HTML("""
<div style="text-align: center; padding: 15px; margin-top: 25px;">
<div style="font-weight: 800; color: #2d3748; font-size: 13px;">CREATED BY PLANA-CHAN</div>
<div style="color: #718096; font-size: 10px; margin-top: 2px;">Blue Archive TTS v3.0 β’ Powered by VITS</div>
</div>
""")
# --- EVENT HANDLER (PYTHON) ---
# Fungsi ini menerima input dari hidden_choice dan mengubah nilai Dropdown
def move_selection(idx):
try:
# Pastikan dikonversi jadi integer agar Dropdown 'type=index' mengerti
return int(idx)
except:
return 0
# "Jika hidden_choice berubah, maka ubah speaker_sel"
hidden_choice.change(fn=move_selection, inputs=hidden_choice, outputs=speaker_sel)
# Fungsi Generate Audio
btn.click(create_tts_fn(model, hps, speaker_ids), inputs=[input_text, speaker_sel, speed_sl], outputs=[out_msg, out_audio])
# --- JAVASCRIPT (FIXED) ---
# Script ini mencari elemen input tersembunyi dan memaksa update nilai
app.load(None, None, None, js="""
() => {
window.selectChar = (index) => {
// Cari elemen textarea di dalam hidden_choice
// Gradio versi baru kadang pakai input, kadang textarea
let el = document.querySelector("#hidden_choice textarea");
if (!el) el = document.querySelector("#hidden_choice input");
if (el) {
el.value = index;
// Dispatch event 'input' agar Gradio mendeteksi perubahan
el.dispatchEvent(new Event('input', { bubbles: true }));
} else {
console.error("Hidden input not found");
}
}
}
""")
app.queue().launch() |