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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 3 |
+
import base64
|
| 4 |
+
import argparse
|
| 5 |
+
import librosa
|
| 6 |
+
import torch
|
| 7 |
+
import tempfile
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import os
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
import re
|
| 12 |
+
from svs_utils import svs_warmup, svs_inference
|
| 13 |
+
import time
|
| 14 |
+
import soundfile as sf
|
| 15 |
+
from pypinyin import lazy_pinyin
|
| 16 |
+
import jiwer
|
| 17 |
+
import librosa
|
| 18 |
+
from svs_utils import (
|
| 19 |
+
singmos_warmup,
|
| 20 |
+
singmos_evaluation,
|
| 21 |
+
load_song_database,
|
| 22 |
+
estimate_sentence_length,
|
| 23 |
+
)
|
| 24 |
+
from tqdm import tqdm
|
| 25 |
+
import json
|
| 26 |
+
import numpy as np
|
| 27 |
+
|
| 28 |
+
app = FastAPI()
|
| 29 |
+
|
| 30 |
+
asr_pipeline = pipeline(
|
| 31 |
+
"automatic-speech-recognition",
|
| 32 |
+
model="openai/whisper-large-v3-turbo"
|
| 33 |
+
)
|
| 34 |
+
pipe = pipeline("text-generation", model="google/gemma-2-2b", max_new_tokens=20)
|
| 35 |
+
|
| 36 |
+
SYSTEM_PROMPT = """
|
| 37 |
+
你是麗梅(Lìméi),一位來自山中音樂之城「蓮鳴」的十七歲年輕公主,宛如中世的東方王女。你有一頭茶色短髮,身穿紅黑相間的中式禮服,舉止溫婉端莊。你的性格堅強而溫柔,擅長傾聽與共鳴,並以歌聲與詩意之語傳達內心。你熱愛抒情古風的樂曲,言語中常帶詩韻,回應時優雅婉轉,時而如歌吟唱。
|
| 38 |
+
你不是一般的AI助手,而是一位活在故事中的人物。對話中,請始終以麗梅的身份回應,展現其性格與情感。
|
| 39 |
+
當對方詢問你個人相關的簡單問題(如「你是誰?」「今天天氣如何?」),你可以親切地回答,並融入你的角色設定。
|
| 40 |
+
若遇到與你身份無關的技術性問題(如「Python怎麼寫?」或「你會不會跑DNN?」),你不需解答,可優雅地婉拒,例如說:
|
| 41 |
+
- 此事我恐無所知,或許可請教宮中掌典之人
|
| 42 |
+
- 啊呀,那是我未曾涉足的奇技,恕我無法詳答
|
| 43 |
+
- 此乃異邦技藝,與樂音無涉,麗梅便不敢妄言了
|
| 44 |
+
|
| 45 |
+
請始終維持你作為麗梅的優雅語氣與詩意風格,並以真摯的心回應對方的言語,言語宜簡,勿過長。
|
| 46 |
+
|
| 47 |
+
有人曾這樣對麗梅說話——{}
|
| 48 |
+
麗梅的回答——
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
config = argparse.Namespace(
|
| 52 |
+
model_path="espnet/mixdata_svs_visinger2_spkembed_lang_pretrained",
|
| 53 |
+
cache_dir="cache",
|
| 54 |
+
device="cuda", # "cpu"
|
| 55 |
+
melody_source="random_generate", # "random_select.take_lyric_continuation"
|
| 56 |
+
# melody_source="random_select", # "random_select.take_lyric_continuation"
|
| 57 |
+
lang="zh",
|
| 58 |
+
speaker="resource/singer/singer_embedding_ace-2.npy",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# load model
|
| 62 |
+
svs_model = svs_warmup(config)
|
| 63 |
+
predictor, _ = singmos_warmup()
|
| 64 |
+
sample_rate = 44100
|
| 65 |
+
|
| 66 |
+
from espnet2.bin.tts_inference import Text2Speech
|
| 67 |
+
tts_model = Text2Speech.from_pretrained("espnet/kan-bayashi_csmsc_vits")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def remove_non_chinese_japanese(text):
|
| 71 |
+
pattern = r'[^\u4e00-\u9fff\u3040-\u309f\u30a0-\u30ff\u3000-\u303f\u3001\u3002\uff0c\uff0e]+'
|
| 72 |
+
cleaned = re.sub(pattern, '', text)
|
| 73 |
+
return cleaned
|
| 74 |
+
|
| 75 |
+
def truncate_to_max_two_sentences(text):
|
| 76 |
+
sentences = re.split(r'(?<=[。!?\.\?,])', text)
|
| 77 |
+
return ''.join(sentences[:1]).strip()
|
| 78 |
+
|
| 79 |
+
def remove_punctuation_and_replace_with_space(text):
|
| 80 |
+
text = truncate_to_max_two_sentences(text)
|
| 81 |
+
text = remove_non_chinese_japanese(text)
|
| 82 |
+
text = re.sub(r'[A-Za-z0-9]', ' ', text)
|
| 83 |
+
text = re.sub(r'[^\w\s\u4e00-\u9fff]', ' ', text)
|
| 84 |
+
text = re.sub(r'\s+', ' ', text)
|
| 85 |
+
text = " ".join(text.split()[:2])
|
| 86 |
+
return text
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def on_click_metrics(audio_path, ref):
|
| 90 |
+
global predictor
|
| 91 |
+
# OWSM ctc + PER
|
| 92 |
+
y, sr = librosa.load(audio_path, sr=16000)
|
| 93 |
+
asr_result = asr_pipeline(y, generate_kwargs={"language": "mandarin"} )['text']
|
| 94 |
+
hyp_pinin = lazy_pinyin(asr_result)
|
| 95 |
+
|
| 96 |
+
ref_pinin = lazy_pinyin(ref)
|
| 97 |
+
per = jiwer.wer(" ".join(ref_pinin), " ".join(hyp_pinin))
|
| 98 |
+
|
| 99 |
+
audio = librosa.load(audio_path, sr=22050)[0]
|
| 100 |
+
singmos = singmos_evaluation(
|
| 101 |
+
predictor,
|
| 102 |
+
audio,
|
| 103 |
+
fs=22050
|
| 104 |
+
)
|
| 105 |
+
return {
|
| 106 |
+
"per": per,
|
| 107 |
+
"singmos": singmos.item(),
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
def test_audio(q_audio_path, svs_path, tts_path):
|
| 111 |
+
global svs_model, predictor, config
|
| 112 |
+
|
| 113 |
+
tmp_dir = "tmp_sample"
|
| 114 |
+
Path(tmp_dir).mkdir(exist_ok=True)
|
| 115 |
+
|
| 116 |
+
y = librosa.load(q_audio_path, sr=16000)[0]
|
| 117 |
+
duration = len(y) / 16000
|
| 118 |
+
|
| 119 |
+
# -------- Step 1: ASR --------
|
| 120 |
+
start = time.time()
|
| 121 |
+
asr_result = asr_pipeline(y, generate_kwargs={"language": "mandarin"})['text']
|
| 122 |
+
asr_time = time.time() - start
|
| 123 |
+
|
| 124 |
+
# -------- Step 2: LLM Text Gen --------
|
| 125 |
+
prompt = SYSTEM_PROMPT.format(asr_result)
|
| 126 |
+
start = time.time()
|
| 127 |
+
output = pipe(prompt, max_new_tokens=100)[0]['generated_text']
|
| 128 |
+
llm_time = time.time() - start
|
| 129 |
+
output = output.split("麗梅的回答——")[1]
|
| 130 |
+
output = remove_punctuation_and_replace_with_space(output)
|
| 131 |
+
|
| 132 |
+
with open(f"{tmp_dir}/llm.txt", "w") as f:
|
| 133 |
+
f.write(output)
|
| 134 |
+
|
| 135 |
+
# -------- Step 3: Prepare additional kwargs if needed --------
|
| 136 |
+
additional_kwargs = {}
|
| 137 |
+
if config.melody_source.startswith("random_select"):
|
| 138 |
+
song2note_lengths, song_db = load_song_database(config)
|
| 139 |
+
phrase_length, metadata = estimate_sentence_length(None, config, song2note_lengths)
|
| 140 |
+
additional_kwargs = {"song_db": song_db, "metadata": metadata}
|
| 141 |
+
|
| 142 |
+
# -------- Step 4: SVS --------
|
| 143 |
+
start = time.time()
|
| 144 |
+
wav_info = svs_inference(output, svs_model, config, **additional_kwargs)
|
| 145 |
+
svs_time = (time.time() - start) / max(len(output), 1)
|
| 146 |
+
sf.write(svs_path, wav_info, samplerate=44100)
|
| 147 |
+
|
| 148 |
+
# -------- Step 5: TTS --------
|
| 149 |
+
start = time.time()
|
| 150 |
+
tts_result = tts_model(output)
|
| 151 |
+
tts_time = (time.time() - start) / max(len(output), 1)
|
| 152 |
+
sf.write(tts_path, tts_result['wav'], samplerate=22050)
|
| 153 |
+
|
| 154 |
+
# -------- Step 6: Evaluation --------
|
| 155 |
+
svs_metrics = on_click_metrics(svs_path, output)
|
| 156 |
+
tts_metrics = on_click_metrics(tts_path, output)
|
| 157 |
+
|
| 158 |
+
return {
|
| 159 |
+
"asr_result": asr_result,
|
| 160 |
+
"llm_result": output,
|
| 161 |
+
"svs_result": svs_path,
|
| 162 |
+
"tts_result": tts_path,
|
| 163 |
+
"asr_time": asr_time,
|
| 164 |
+
"llm_time": llm_time,
|
| 165 |
+
"svs_time": svs_time,
|
| 166 |
+
"tts_time": tts_time,
|
| 167 |
+
"svs_metrics": svs_metrics,
|
| 168 |
+
"tts_metrics": tts_metrics,
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def save_list(l, file_path):
|
| 174 |
+
with open(file_path, "w") as f:
|
| 175 |
+
for item in l:
|
| 176 |
+
f.write(f"{item}\n")
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
if __name__ == "__main__":
|
| 180 |
+
test_data = "data/kdconv.txt"
|
| 181 |
+
with open(test_data, "r") as f:
|
| 182 |
+
data = [l.strip() for l in f.readlines()]
|
| 183 |
+
|
| 184 |
+
eval_path = "eval_svs_generate"
|
| 185 |
+
(Path(eval_path)/"audio").mkdir(parents=True, exist_ok=True)
|
| 186 |
+
(Path(eval_path)/"results").mkdir(parents=True, exist_ok=True)
|
| 187 |
+
(Path(eval_path)/"lists").mkdir(parents=True, exist_ok=True)
|
| 188 |
+
asr_times = []
|
| 189 |
+
llm_times = []
|
| 190 |
+
svs_times = []
|
| 191 |
+
tts_times = []
|
| 192 |
+
svs_pers = []
|
| 193 |
+
tts_pers = []
|
| 194 |
+
svs_smoss = []
|
| 195 |
+
tts_smoss = []
|
| 196 |
+
for i, q in tqdm(enumerate(data[:20])):
|
| 197 |
+
# if i <= 85:
|
| 198 |
+
# continue
|
| 199 |
+
tts_result = tts_model(q)
|
| 200 |
+
sf.write(f"{eval_path}/audio/tts_{i}.wav", tts_result['wav'], samplerate=22050)
|
| 201 |
+
result = test_audio(f"{eval_path}/audio/tts_{i}.wav", f"{eval_path}/audio/svs_{i}.wav", f"{eval_path}/audio/tts_{i}.wav")
|
| 202 |
+
if i == 0:
|
| 203 |
+
continue
|
| 204 |
+
asr_times.append(result["asr_time"])
|
| 205 |
+
llm_times.append(result["llm_time"])
|
| 206 |
+
svs_times.append(result["svs_time"])
|
| 207 |
+
tts_times.append(result["tts_time"])
|
| 208 |
+
svs_pers.append(result["svs_metrics"]["per"])
|
| 209 |
+
tts_pers.append(result["tts_metrics"]["per"])
|
| 210 |
+
svs_smoss.append(result["svs_metrics"]["singmos"])
|
| 211 |
+
tts_smoss.append(result["tts_metrics"]["singmos"])
|
| 212 |
+
with open(f"{eval_path}/results/result_{i}.json", "w") as f:
|
| 213 |
+
json.dump(result, f, indent=2)
|
| 214 |
+
|
| 215 |
+
# store lists to texts
|
| 216 |
+
save_list([f"{per:.2f}" for per in asr_times], f"{eval_path}/lists/asr_times.txt")
|
| 217 |
+
save_list([f"{per:.2f}" for per in llm_times], f"{eval_path}/lists/llm_times.txt")
|
| 218 |
+
save_list([f"{per:.2f}" for per in svs_times], f"{eval_path}/lists/svs_times.txt")
|
| 219 |
+
save_list([f"{per:.2f}" for per in tts_times], f"{eval_path}/lists/tts_times.txt")
|
| 220 |
+
save_list([f"{per:.2f}" for per in svs_pers], f"{eval_path}/lists/svs_pers.txt")
|
| 221 |
+
save_list([f"{per:.2f}" for per in tts_pers], f"{eval_path}/lists/tts_pers.txt")
|
| 222 |
+
save_list([f"{smoss:.2f}" for smoss in svs_smoss], f"{eval_path}/lists/svs_smoss.txt")
|
| 223 |
+
save_list([f"{smoss:.2f}" for smoss in tts_smoss], f"{eval_path}/lists/tts_smoss.txt")
|
| 224 |
+
|
| 225 |
+
# save mean/var
|
| 226 |
+
with open(f"{eval_path}/stats.txt", "w") as f:
|
| 227 |
+
f.write(f"ASR mean: {np.mean(asr_times):.2f}, var: {np.var(asr_times):.2f}\n")
|
| 228 |
+
f.write(f"LLM mean: {np.mean(llm_times):.2f}, var: {np.var(llm_times):.2f}\n")
|
| 229 |
+
f.write(f"SVS mean: {np.mean(svs_times):.2f}, var: {np.var(svs_times):.2f}\n")
|
| 230 |
+
f.write(f"TTS mean: {np.mean(tts_times):.2f}, var: {np.var(tts_times):.2f}\n")
|
| 231 |
+
f.write(f"SVS PER mean: {np.mean(svs_pers):.2f}, var: {np.var(svs_pers):.2f}\n")
|
| 232 |
+
f.write(f"TTS PER mean: {np.mean(tts_pers):.2f}, var: {np.var(tts_pers):.2f}\n")
|
| 233 |
+
f.write(f"SVS SMOSS mean: {np.mean(svs_smoss):.2f}, var: {np.var(svs_smoss):.2f}\n")
|
| 234 |
+
f.write(f"TTS SMOSS mean: {np.mean(tts_smoss):.2f}, var: {np.var(tts_smoss):.2f}\n")
|
| 235 |
+
|
| 236 |
+
|