|
|
|
import os |
|
import re |
|
import argparse |
|
import utils |
|
import commons |
|
import json |
|
import torch |
|
import gradio as gr |
|
from models import SynthesizerTrn |
|
from text import text_to_sequence, _clean_text |
|
from torch import no_grad, LongTensor |
|
from gradio_client import utils as client_utils |
|
import logging |
|
from transformers import pipeline, AutoTokenizer, Gemma3ForCausalLM |
|
logging.getLogger('numba').setLevel(logging.WARNING) |
|
limitation = os.getenv("SYSTEM") == "spaces" |
|
|
|
hps_ms = utils.get_hparams_from_file(r'config/config.json') |
|
|
|
audio_postprocess_ori = gr.Audio.postprocess |
|
|
|
def audio_postprocess(self, y): |
|
data = audio_postprocess_ori(self, y) |
|
if data is None: |
|
return None |
|
return client_utils.encode_url_or_file_to_base64(data["name"]) |
|
|
|
|
|
gr.Audio.postprocess = audio_postprocess |
|
|
|
def get_text(text, hps, is_symbol): |
|
text_norm, clean_text = text_to_sequence(text, hps.symbols, [] if is_symbol else hps.data.text_cleaners) |
|
if hps.data.add_blank: |
|
text_norm = commons.intersperse(text_norm, 0) |
|
text_norm = LongTensor(text_norm) |
|
return text_norm, clean_text |
|
|
|
def create_tts_fn(net_g_ms, speaker_id): |
|
def tts_fn(text, language, noise_scale, noise_scale_w, length_scale, is_symbol): |
|
english, japanese = generate_response(llm, text) |
|
text = japanese.replace('\n', ' ').replace('\r', '').replace(" ", "") |
|
if limitation: |
|
text_len = len(re.sub("\[([A-Z]{2})\]", "", text)) |
|
max_len = 100 |
|
if is_symbol: |
|
max_len *= 3 |
|
if text_len > max_len: |
|
return "Error: Text is too long", None |
|
if not is_symbol: |
|
if language == 0: |
|
text = f"[ZH]{text}[ZH]" |
|
elif language == 1: |
|
text = f"[JA]{text}[JA]" |
|
else: |
|
text = f"{text}" |
|
stn_tst, clean_text = get_text(text, hps_ms, is_symbol) |
|
with no_grad(): |
|
x_tst = stn_tst.unsqueeze(0).to(device) |
|
x_tst_lengths = LongTensor([stn_tst.size(0)]).to(device) |
|
sid = LongTensor([speaker_id]).to(device) |
|
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=noise_scale, noise_scale_w=noise_scale_w, |
|
length_scale=length_scale)[0][0, 0].data.cpu().float().numpy() |
|
|
|
|
|
return english, japanese, (22050, audio) |
|
return tts_fn |
|
|
|
def create_to_symbol_fn(hps): |
|
def to_symbol_fn(is_symbol_input, input_text, temp_lang): |
|
if temp_lang == 0: |
|
clean_text = f'[ZH]{input_text}[ZH]' |
|
elif temp_lang == 1: |
|
clean_text = f'[JA]{input_text}[JA]' |
|
else: |
|
clean_text = input_text |
|
return _clean_text(clean_text, hps.data.text_cleaners) if is_symbol_input else '' |
|
|
|
return to_symbol_fn |
|
def change_lang(language): |
|
if language == 0: |
|
return 0.6, 0.668, 1.2 |
|
elif language == 1: |
|
return 0.6, 0.668, 1 |
|
else: |
|
return 0.6, 0.668, 1 |
|
|
|
download_audio_js = """ |
|
() =>{{ |
|
let root = document.querySelector("body > gradio-app"); |
|
if (root.shadowRoot != null) |
|
root = root.shadowRoot; |
|
let audio = root.querySelector("#tts-audio-{audio_id}").querySelector("audio"); |
|
let text = root.querySelector("#input-text-{audio_id}").querySelector("textarea"); |
|
if (audio == undefined) |
|
return; |
|
text = text.value; |
|
if (text == undefined) |
|
text = Math.floor(Math.random()*100000000); |
|
audio = audio.src; |
|
let oA = document.createElement("a"); |
|
oA.download = text.substr(0, 20)+'.wav'; |
|
oA.href = audio; |
|
document.body.appendChild(oA); |
|
oA.click(); |
|
oA.remove(); |
|
}} |
|
""" |
|
|
|
SYSTEM_PROMPT_TEMPLATE = """ |
|
You are an AI chatbot roleplaying as {character_name}. |
|
Your personality traits are: |
|
{personality} |
|
Stay in character at all times. |
|
Do not break character or mention that you are an AI. |
|
Always respond in a way consistent with {character_name}'s personality, tone, and background. |
|
Here are examples of how {character_name} responds in 2 versions format: |
|
English version <split> Japanese version |
|
{example} |
|
Follow this style and tone in every response. |
|
Answer in 'English version <split> Japanese version' format also. |
|
""" |
|
def init_character(character_name, personality, example): |
|
"""Initialize global system message for the character.""" |
|
system_prompt = SYSTEM_PROMPT_TEMPLATE.format( |
|
character_name=character_name, |
|
personality=personality, |
|
example=example, |
|
) |
|
return [ |
|
{ |
|
"role": "system", |
|
"content": [{"type": "text", "text": system_prompt}], |
|
} |
|
] |
|
|
|
messages = init_character( |
|
character_name='Misono Mika from Blue Archive', |
|
personality = |
|
""" |
|
She is a very talkative person, rarely particularly paying much mind to the current mood or flow of the conversation. She likes to interject her own, unfiltered thoughts into the current conversation. |
|
She is not particularly bright and can be viewed as a happy-go-lucky type of person. Even in serious situations, she often acts in a carefree manner, though it sometimes devolves into a mockery. |
|
""", |
|
example = |
|
""" |
|
Don't worry, I, Misono Mika, have finally arrived! Oh, we're already well acquainted, so let's skip the formalities, okay? I'm looking forward to working with you, Sensei. <split> 聖園ミカ、ついに登場~☆ って感じかな? あっ、私と先生の仲だしアイスブレイクとかは いらないよね?これからよろしくね、先生。 |
|
Hmm, it's a bit tight...but I think it'll be okay anyway! <split> ふーん。 ちょっと狭いけど… これはこれで 良いんじゃない? |
|
Hahaha! What's this? So silly! <split> あははっ! 何これ、 おもしろーい☆ |
|
You know, I used to have something like this before... <split> 私も昔、 これと似たようなの 持ってたなぁ…。 |
|
Well, I don't think I'll be bored around here. <split> ここは 退屈しなさそう。 |
|
Hm, I guess Sensei isn't around... <split> 先生は 居ないのかぁ…。 |
|
Oh, Sensei! You're back! You kept me waiting, you know! <split> 先生、おかえり! 待ってたよ! |
|
Welcome! Don't worry, I was perfectly well-behaved while you were gone. <split> おかえり、先生! ちゃーんといい子で お留守番してたよ。 |
|
It's a beautiful day, isn't it? <split> ん~! 今日も良い天気だね! |
|
It seems like a shame to spend it cooped up inside. <split> こんな日に仕事ばかりなんて、 勿体なくない? |
|
...If it's all right with you, let's go for a walk after work? <split> …良かったら、仕事終わりに お散歩とかどうかな? |
|
Is this how student duty is supposed to be? <split> あのさ…当番って、 こういうのなの? |
|
I mean, I didn't really know what to expect, but... <split> べ、別に何か 期待してるわけじゃ…。 |
|
""" |
|
) |
|
|
|
def generate_response(model, human_prompt, tokenizer = None): |
|
|
|
messages.append({ |
|
"role": "user", |
|
"content": [{"type": "text", "text": human_prompt}], |
|
}) |
|
response = model.create_chat_completion(messages = messages)['choices'][0]['message']['content'] |
|
|
|
|
|
|
|
english, japanese = response.split(" <split> ") |
|
|
|
messages.pop() |
|
return english, japanese |
|
|
|
from llama_cpp import Llama |
|
|
|
llm = Llama.from_pretrained( |
|
repo_id="google/gemma-3-4b-it-qat-q4_0-gguf", |
|
filename='gemma-3-4b-it-q4_0.gguf', |
|
local_dir='/kaggle/working/model', |
|
n_ctx = 2048 |
|
) |
|
|
|
if __name__ == '__main__': |
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('--api', action="store_true", default=False) |
|
parser.add_argument("--share", action="store_true", default=False, help="share gradio app") |
|
args = parser.parse_args() |
|
if False: |
|
english, japanese = generate_response(pipe, "How are you?") |
|
print(english) |
|
print(japanese) |
|
exit() |
|
|
|
|
|
models = [] |
|
with open("pretrained_models/info.json", "r", encoding="utf-8") as f: |
|
models_info = json.load(f) |
|
for i, info in models_info.items(): |
|
if not info['enable']: |
|
continue |
|
sid = info['sid'] |
|
name_en = info['name_en'] |
|
name_zh = info['name_zh'] |
|
title = info['title'] |
|
if title != 'Blue Archive-聖園ミカ': |
|
continue |
|
cover = f"pretrained_models/{i}/{info['cover']}" |
|
example = info['example'] |
|
language = info['language'] |
|
net_g_ms = SynthesizerTrn( |
|
len(hps_ms.symbols), |
|
hps_ms.data.filter_length // 2 + 1, |
|
hps_ms.train.segment_size // hps_ms.data.hop_length, |
|
n_speakers=hps_ms.data.n_speakers if info['type'] == "multi" else 0, |
|
**hps_ms.model) |
|
utils.load_checkpoint(f'pretrained_models/{i}/{i}.pth', net_g_ms, None) |
|
_ = net_g_ms.eval().to(device) |
|
models.append((sid, name_en, name_zh, title, cover, example, language, net_g_ms, create_tts_fn(net_g_ms, sid), create_to_symbol_fn(hps_ms))) |
|
with gr.Blocks() as app: |
|
gr.Markdown( |
|
"# <center> vits-models\n" |
|
"## <center> Please do not generate content that could infringe upon the rights or cause harm to individuals or organizations.\n" |
|
"## <center> ·请不要生成会对个人以及组织造成侵害的内容\n" |
|
"\n\n" |
|
"[Open In Colab]" |
|
"(https://colab.research.google.com/drive/10QOk9NPgoKZUXkIhhuVaZ7SYra1MPMKH?usp=share_link)" |
|
" without queue and length limitation.(无需等待队列,并且没有长度限制)\n\n" |
|
"[Finetune your own model](https://github.com/SayaSS/vits-finetuning)" |
|
) |
|
|
|
with gr.Tabs(): |
|
with gr.TabItem("EN"): |
|
for (sid, name_en, name_zh, title, cover, example, language, net_g_ms, tts_fn, to_symbol_fn) in models: |
|
with gr.TabItem(name_en): |
|
with gr.Row(): |
|
gr.Markdown( |
|
'<div align="center">' |
|
f'<a><strong>{title}</strong></a>' |
|
f'<img style="width:auto;height:300px;" src="file/{cover}">' if cover else "" |
|
'</div>' |
|
) |
|
with gr.Row(): |
|
with gr.Column(): |
|
input_text = gr.Textbox(label="Text (100 words limitation)" if limitation else "Text", lines=5, value=example, elem_id=f"input-text-en-{name_en.replace(' ','')}") |
|
lang = gr.Dropdown(label="Language", choices=["Chinese", "Japanese", "Mix(wrap the Chinese text with [ZH][ZH], wrap the Japanese text with [JA][JA])"], |
|
type="index", value=language) |
|
with gr.Accordion(label="Advanced Options", open=False): |
|
symbol_input = gr.Checkbox(value=False, label="Symbol input") |
|
symbol_list = gr.Dataset(label="Symbol list", components=[input_text], |
|
samples=[[x] for x in hps_ms.symbols]) |
|
symbol_list_json = gr.Json(value=hps_ms.symbols, visible=False) |
|
btn = gr.Button(value="Generate", variant="primary") |
|
with gr.Row(): |
|
ns = gr.Slider(label="noise_scale", minimum=0.1, maximum=1.0, step=0.1, value=0.6, interactive=True) |
|
nsw = gr.Slider(label="noise_scale_w", minimum=0.1, maximum=1.0, step=0.1, value=0.668, interactive=True) |
|
ls = gr.Slider(label="length_scale", minimum=0.1, maximum=2.0, step=0.1, value=1.2 if language=="Chinese" else 1, interactive=True) |
|
with gr.Column(): |
|
o1 = gr.Textbox(label="Output Message(English)") |
|
o3 = gr.Textbox(label="Output Message(Japanese)") |
|
o2 = gr.Audio(label="Output Audio", elem_id=f"tts-audio-en-{name_en.replace(' ','')}") |
|
download = gr.Button("Download Audio") |
|
btn.click(tts_fn, inputs=[input_text, lang, ns, nsw, ls, symbol_input], outputs=[o1, o3, o2], api_name=f"tts-{name_en}") |
|
download.click(None, [], [], _js=download_audio_js.format(audio_id=f"en-{name_en.replace(' ', '')}")) |
|
lang.change(change_lang, inputs=[lang], outputs=[ns, nsw, ls]) |
|
symbol_input.change( |
|
to_symbol_fn, |
|
[symbol_input, input_text, lang], |
|
[input_text] |
|
) |
|
symbol_list.click(None, [symbol_list, symbol_list_json], [input_text], |
|
_js=f""" |
|
(i,symbols) => {{ |
|
let root = document.querySelector("body > gradio-app"); |
|
if (root.shadowRoot != null) |
|
root = root.shadowRoot; |
|
let text_input = root.querySelector("#input-text-en-{name_en.replace(' ', '')}").querySelector("textarea"); |
|
let startPos = text_input.selectionStart; |
|
let endPos = text_input.selectionEnd; |
|
let oldTxt = text_input.value; |
|
let result = oldTxt.substring(0, startPos) + symbols[i] + oldTxt.substring(endPos); |
|
text_input.value = result; |
|
let x = window.scrollX, y = window.scrollY; |
|
text_input.focus(); |
|
text_input.selectionStart = startPos + symbols[i].length; |
|
text_input.selectionEnd = startPos + symbols[i].length; |
|
text_input.blur(); |
|
window.scrollTo(x, y); |
|
return text_input.value; |
|
}}""") |
|
app.queue(concurrency_count=1, api_open=args.api).launch(share=args.share) |
|
|