File size: 13,679 Bytes
12e4312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b28929
51604a3
 
2b28929
 
 
 
0aca757
2b28929
 
eb7e85b
2b28929
 
eb7e85b
2b28929
 
 
 
eb7e85b
2b28929
 
 
eb7e85b
2b28929
 
 
 
67e06ea
2b28929
12e4312
387abab
5b2a83c
f06136b
ffea720
 
eb7e85b
 
 
 
 
 
 
 
 
 
bd069b2
eb7e85b
 
 
 
 
 
 
 
 
 
 
608bb92
1f54f90
eb7e85b
004a828
 
aaf6873
2fae0ef
ffea720
2fae0ef
ffea720
aaf6873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d3811a
 
aaf6873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a920567
aaf6873
 
 
 
 
 
 
 
 
 
 
c84f8d3
aaf6873
 
 
a920567
aaf6873
 
 
 
 
 
 
 
 
 
 
 
5d6a04e
aaf6873
 
 
 
6d7f1ec
aaf6873
 
 
 
 
 
 
 
 
 
c84f8d3
aaf6873
 
1d3811a
 
 
aaf6873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d3811a
5d6a04e
 
aaf6873
 
 
1d3811a
aaf6873
 
 
 
eb7e85b
aaf6873
 
 
608bb92
aaf6873
 
 
 
 
 
 
 
 
 
 
 
 
4370d02
aaf6873
 
 
3d93f92
aaf6873
 
 
608bb92
aaf6873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4deb1ff
4776803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4deb1ff
4776803
 
 
 
 
 
 
 
 
 
4deb1ff
 
 
aaf6873
4deb1ff
 
aaf6873
 
608bb92
aaf6873
 
 
608bb92
aaf6873
 
 
 
 
 
 
 
 
 
3d93f92
aaf6873
 
 
 
 
 
 
 
 
12e4312
 
879cbf5
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import json
import os
import subprocess
from pathlib import Path

import gradio as gr
import librosa
import numpy as np
import torch
from demucs.apply import apply_model
from demucs.pretrained import DEFAULT_MODEL, get_model
from huggingface_hub import hf_hub_download, list_repo_files

from so_vits_svc_fork.hparams import HParams
from so_vits_svc_fork.inference.core import Svc


if os.environ.get("HF_TOKEN"):
    login(os.environ.get("HF_TOKEN"))
###################################################################
# REPLACE THESE VALUES TO CHANGE THE MODEL REPO/CKPT NAME/SETTINGS
###################################################################
# The Hugging Face Hub repo ID - 在这里修改repo_id,可替换成任何已经训练好的模型!
repo_ids = ["nijisakai/testzjl","nijisakai/testsyz","nijisakai/testwj","nijisakai/Eric_Cartman"]

# If None, Uses latest ckpt in the repo
ckpt_name = None

# If None, Uses "kmeans.pt" if it exists in the repo
cluster_model_name = None

# Set the default f0 type to use - use the one it was trained on.
# The default for so-vits-svc-fork is "dio".
# Options: "crepe", "crepe-tiny", "parselmouth", "dio", "harvest"
default_f0_method = "crepe"

# The default ratio of cluster inference to SVC inference.
# If cluster_model_name is not found in the repo, this is set to 0.
default_cluster_infer_ratio = 0.5

# Limit on duration of audio at inference time. increase if you can
# In this parent app, we set the limit with an env var to 30 seconds
# If you didnt set env var + you go OOM try changing 9e9 to <=300ish
duration_limit = int(os.environ.get("MAX_DURATION_SECONDS", 9e9))
###################################################################

models = []
speakers = []
for repo_id in repo_ids:
    # Figure out the latest generator by taking highest value one.
    # Ex. if the repo has: G_0.pth, G_100.pth, G_200.pth, we'd use G_200.pth
    if ckpt_name is None:
        latest_id = sorted(
            [
                int(Path(x).stem.split("_")[1])
                for x in list_repo_files(repo_id)
                if x.startswith("G_") and x.endswith(".pth")
            ]
        )[-1]
        ckpt_name = f"G_{latest_id}.pth"

    cluster_model_name = cluster_model_name or "kmeans.pt"
    if cluster_model_name in list_repo_files(repo_id):
        print(f"Found Cluster model - Downloading {cluster_model_name} from {repo_id}")
        cluster_model_path = hf_hub_download(repo_id, cluster_model_name)
    else:
        print(f"Could not find {cluster_model_name} in {repo_id}. Using None")
        cluster_model_path = None
    default_cluster_infer_ratio = default_cluster_infer_ratio if cluster_model_path else 0

    generator_path = hf_hub_download(repo_id, ckpt_name)
    config_path = hf_hub_download(repo_id, "config.json")
    hparams = HParams(**json.loads(Path(config_path).read_text()))
    speaker = list(hparams.spk.keys())
    speakers.extend(speaker)
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model = Svc(net_g_path=generator_path, config_path=config_path, device=device, cluster_model_path=cluster_model_path)
    models.append(model)

    # Reset ckpt_name and cluster_model_name for the next iteration
    ckpt_name = None
    cluster_model_name = None

demucs_model = get_model(DEFAULT_MODEL)

def extract_vocal_demucs(model, filename, sr=44100, device=None, shifts=1, split=True, overlap=0.25, jobs=0):
    wav, sr = librosa.load(filename, mono=False, sr=sr)
    wav = torch.tensor(wav)
    ref = wav.mean(0)
    wav = (wav - ref.mean()) / ref.std()
    sources = apply_model(
        model, wav[None], device=device, shifts=shifts, split=split, overlap=overlap, progress=True, num_workers=jobs
    )[0]
    sources = sources * ref.std() + ref.mean()
    vocal_wav = sources[-1]
    vocal_wav = vocal_wav / max(1.01 * vocal_wav.abs().max(), 1)
    vocal_wav = vocal_wav.numpy()
    vocal_wav = librosa.to_mono(vocal_wav)
    vocal_wav = vocal_wav.T
    instrumental_wav = sources[:-1].sum(0).numpy().T
    return vocal_wav, instrumental_wav

def download_youtube_clip(
    video_identifier,
    start_time,
    end_time,
    output_filename,
    num_attempts=5,
    url_base="https://www.youtube.com/watch?v=",
    quiet=False,
    force=False,
):
    output_path = Path(output_filename)
    if output_path.exists():
        if not force:
            return output_path
        else:
            output_path.unlink()

    quiet = "--quiet --no-warnings" if quiet else ""
    command = f"""
        yt-dlp {quiet} -x --audio-format wav -f bestaudio -o "{output_filename}" --download-sections "*{start_time}-{end_time}" "{url_base}{video_identifier}"  # noqa: E501
    """.strip()

    attempts = 0
    while True:
        try:
            _ = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError:
            attempts += 1
            if attempts == num_attempts:
                return None
        else:
            break

    if output_path.exists():
        return output_path
    else:
        return None

def predict(
    speaker,
    audio,
    transpose: int = 0,
    auto_predict_f0: bool = False,
    cluster_infer_ratio: float = 0,
    noise_scale: float = 0.4,
    f0_method: str = "crepe",
    db_thresh: int = -40,
    pad_seconds: float = 0.5,
    chunk_seconds: float = 0.5,
    absolute_thresh: bool = False,
):
    model = models[speakers.index(speaker)]
    audio, _ = librosa.load(audio, sr=model.target_sample, duration=duration_limit)
    audio = model.infer_silence(
        audio.astype(np.float32),
        speaker=speaker,
        transpose=transpose,
        auto_predict_f0=auto_predict_f0,
        cluster_infer_ratio=cluster_infer_ratio,
        noise_scale=noise_scale,
        f0_method=f0_method,
        db_thresh=db_thresh,
        pad_seconds=pad_seconds,
        chunk_seconds=chunk_seconds,
        absolute_thresh=absolute_thresh,
    )
    return model.target_sample, audio


def predict_song_from_yt(
    ytid_or_url,
    start,
    end,
    speaker=speakers[0],
    transpose: int = 0,
    auto_predict_f0: bool = False,
    cluster_infer_ratio: float = 0,
    noise_scale: float = 0.4,
    f0_method: str = "dio",
    db_thresh: int = -40,
    pad_seconds: float = 0.5,
    chunk_seconds: float = 0.5,
    absolute_thresh: bool = False,
):
    model = models[speakers.index(speaker)]
    end = min(start + duration_limit, end)
    original_track_filepath = download_youtube_clip(
        ytid_or_url,
        start,
        end,
        "track.wav",
        force=True,
        url_base="" if ytid_or_url.startswith("http") else "https://www.youtube.com/watch?v=",
    )
    vox_wav, inst_wav = extract_vocal_demucs(demucs_model, original_track_filepath)
    if transpose != 0:
        inst_wav = librosa.effects.pitch_shift(inst_wav.T, sr=model.target_sample, n_steps=transpose).T
    cloned_vox = model.infer_silence(
        vox_wav.astype(np.float32),
        speaker=speaker,
        transpose=transpose,
        auto_predict_f0=auto_predict_f0,
        cluster_infer_ratio=cluster_infer_ratio,
        noise_scale=noise_scale,
        f0_method=f0_method,
        db_thresh=db_thresh,
        pad_seconds=pad_seconds,
        chunk_seconds=chunk_seconds,
        absolute_thresh=absolute_thresh,
    )
    full_song = inst_wav + np.expand_dims(cloned_vox, 1)
    return (model.target_sample, full_song), (model.target_sample, cloned_vox)



description = f"""
<center>💡 - 如何使用此程序:在页面上方选择“从B站视频上传”模块,填写视频网址和视频起止时间后,点击“submit”按键即可!您还可以点击页面最下方的示例快速预览效果</center>
""".strip()

article = """
<p style='text-align: center'> 注意❗:请不要生成会对个人以及组织造成侵害的内容,此程序仅供科研、学习及个人娱乐使用。
</p>
""".strip()

interface_mic = gr.Interface(
    predict,
    inputs=[
        gr.Dropdown(speakers, value=speakers[0], label="🎤AI歌手选择🎶"),
        gr.Audio(type="filepath", source="microphone", label="请用麦克风上传您想转换的歌曲"),
        gr.Slider(-12, 12, value=0, step=1, label="变调 (默认为0;有正负值,+2为升高两个key)"),
        gr.Checkbox(False, label="是否开启自动f0预测", info="勾选即为开启;配合聚类模型f0预测效果更好,仅限语音转换时使用", visible=False),
        gr.Slider(0.0, 1.0, value=default_cluster_infer_ratio, step=0.1, label="聚类模型混合比例", info="0-1之间,0即不启用聚类。使用聚类模型能提升音色相似度,但会导致咬字下降 (如果使用,建议0.5左右)"),
        gr.Slider(0.0, 1.0, value=0.4, step=0.1, label="noise scale (建议保持不变)", visible=False),
        gr.Dropdown(
            choices=["crepe", "crepe-tiny", "parselmouth", "dio", "harvest"],
            value=default_f0_method,
            label="模型推理方法 (crepe推理效果最好)", visible=False
        ),
    ],
    outputs="audio",
    cache_examples=False,
    title="可从B站直接上传素材,无需分离背景音",
    description=description,
    article=article,
)

interface_file = gr.Interface(
    predict,
    inputs=[
        gr.Dropdown(speakers, value=speakers[0], label="🎤AI歌手选择🎶"),
        gr.Audio(type="filepath", source="upload", label="请上传您想转换的歌曲 (仅人声部分)"),
        gr.Slider(-12, 12, value=0, step=1, label="变调 (默认为0;有正负值,+2为升高两个key)"),
        gr.Checkbox(False, label="是否开启自动f0预测", info="勾选即为开启;配合聚类模型f0预测效果更好,仅限语音转换时使用", visible=False),
        gr.Slider(0.0, 1.0, value=default_cluster_infer_ratio, step=0.1, label="聚类模型混合比例", info="0-1之间,0即不启用聚类。使用聚类模型能提升音色相似度,但会导致咬字下降 (如果使用,建议0.5左右)"),
        gr.Slider(0.0, 1.0, value=0.4, step=0.1, label="noise scale (建议保持不变)", visible=False),
        gr.Dropdown(
            choices=["crepe", "crepe-tiny", "parselmouth", "dio", "harvest"],
            value=default_f0_method,
            label="模型推理方法 (crepe推理效果最好)", visible=False
        ),
    ],
    outputs="audio",
    cache_examples=False,
    title="🌊💕🎶 可从B站直接上传素材,无需分离背景音",
    description=description,
    article=article,
)

def combined_output(
    ytid_or_url,
    start,
    end,
    speaker=speakers[0],
    transpose: int = 0,
    auto_predict_f0: bool = False,
    cluster_infer_ratio: float = 0,
    noise_scale: float = 0.4,
    f0_method: str = "dio",
    db_thresh: int = -40,
    pad_seconds: float = 0.5,
    chunk_seconds: float = 0.5,
    absolute_thresh: bool = False,
):
    # 调用原来的函数
    full_song_output, cloned_vox_output = predict_song_from_yt(
        ytid_or_url,
        start,
        end,
        speaker,
        transpose,
        auto_predict_f0,
        cluster_infer_ratio,
        noise_scale,
        f0_method,
        db_thresh,
        pad_seconds,
        chunk_seconds,
        absolute_thresh,
    )
    
    # 这里我们直接提取人声和伴奏,因为它们已经在predict_song_from_yt中被提取
    original_track_filepath = download_youtube_clip(
        ytid_or_url, start, end, "track.wav", force=True,
        url_base="" if ytid_or_url.startswith("http") else "https://www.youtube.com/watch?v="
    )
    vox_wav, inst_wav = extract_vocal_demucs(demucs_model, original_track_filepath)

    # 返回所有输出
    return full_song_output, cloned_vox_output, (model.target_sample, vox_wav), (model.target_sample, inst_wav)




interface_yt = gr.Interface(
#    predict_song_from_yt,
    combined_output,
    inputs=[
        gr.Textbox(
            label="Bilibili网址", info="请填写含有您喜欢歌曲的Bilibili网址,可直接填写相应的BV号"
        ),
        gr.Number(value=0, label="起始时间 (秒)"),
        gr.Number(value=15, label="结束时间 (秒)"),
        gr.Dropdown(speakers, value=speakers[0], label="🎤AI歌手选择🎶"),
        gr.Slider(-12, 12, value=0, step=1, label="变调 (默认为0;有正负值,+2为升高两个key)"),
        gr.Checkbox(False, label="是否开启自动f0预测", info="勾选即为开启;配合聚类模型f0预测效果更好,仅限语音转换时使用", visible=False),
        gr.Slider(0.0, 1.0, value=default_cluster_infer_ratio, step=0.1, label="聚类模型混合比例", info="0-1之间,0即不启用聚类。使用聚类模型能提升音色相似度,但会导致咬字下降"),
        gr.Slider(0.0, 1.0, value=0.4, step=0.1, label="noise scale (建议保持不变)", visible=False),
        gr.Dropdown(
            choices=["crepe", "crepe-tiny", "parselmouth", "dio", "harvest"],
            value=default_f0_method,
            label="模型推理方法 (crepe推理效果最好)", visible=False
        ),
    ],
    outputs=[gr.Audio(label="AI歌手+伴奏🎵"), gr.Audio(label="AI歌手人声部分🎤"),gr.Audio(label="原视频人声"),gr.Audio(label="原视频伴奏")],
    title="🌊💕🎶 - 可从B站直接上传素材,无需分离背景音",
    description=description,
    article=article,
    cache_examples=False,
)
interface = gr.TabbedInterface(
    [interface_yt, interface_mic, interface_file],
    ["📺 - 从B站视频上传 ⭐推荐⭐", "🎙️ - 从麦克风上传", "🎵 - 从文件上传"],
)

if __name__ == "__main__":
    interface.launch(show_error=True)