Spaces:
Running
Running
Update src/main.py
Browse files- src/main.py +22 -18
src/main.py
CHANGED
@@ -4,42 +4,40 @@ import os
|
|
4 |
import shlex
|
5 |
import subprocess
|
6 |
import librosa
|
|
|
7 |
import numpy as np
|
8 |
import soundfile as sf
|
9 |
import gradio as gr
|
10 |
from rvc import Config, load_hubert, get_vc, rvc_infer
|
11 |
|
12 |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
13 |
-
|
14 |
-
|
15 |
|
16 |
def get_rvc_model(voice_model):
|
17 |
-
model_dir = os.path.join(
|
18 |
rvc_model_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.pth')), None)
|
19 |
rvc_index_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.index')), None)
|
20 |
|
21 |
if rvc_model_path is None:
|
22 |
-
|
23 |
-
raise Exception(error_msg)
|
24 |
|
25 |
return rvc_model_path, rvc_index_path
|
26 |
|
27 |
def convert_to_stereo(audio_path):
|
28 |
wave, sr = librosa.load(audio_path, mono=False, sr=44100)
|
29 |
if type(wave[0]) != np.ndarray:
|
30 |
-
stereo_path =
|
31 |
command = shlex.split(f'ffmpeg -y -loglevel error -i "{audio_path}" -ac 2 -f wav "{stereo_path}"')
|
32 |
subprocess.run(command)
|
33 |
return stereo_path
|
34 |
-
|
35 |
-
return audio_path
|
36 |
|
37 |
def get_hash(filepath):
|
|
|
38 |
with open(filepath, 'rb') as f:
|
39 |
-
file_hash = hashlib.blake2b()
|
40 |
while chunk := f.read(8192):
|
41 |
file_hash.update(chunk)
|
42 |
-
|
43 |
return file_hash.hexdigest()[:11]
|
44 |
|
45 |
def display_progress(percent, message, progress=gr.Progress()):
|
@@ -47,30 +45,36 @@ def display_progress(percent, message, progress=gr.Progress()):
|
|
47 |
|
48 |
def voice_change(voice_model, vocals_path, output_path, pitch_change, f0_method, index_rate, filter_radius, rms_mix_rate, protect, crepe_hop_length):
|
49 |
rvc_model_path, rvc_index_path = get_rvc_model(voice_model)
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
51 |
config = Config(device, True)
|
52 |
-
hubert_model = load_hubert(device, config.is_half, os.path.join(
|
53 |
cpt, version, net_g, tgt_sr, vc = get_vc(device, config.is_half, config, rvc_model_path)
|
54 |
|
55 |
rvc_infer(rvc_index_path, index_rate, vocals_path, output_path, pitch_change, f0_method, cpt, version, net_g,
|
56 |
filter_radius, tgt_sr, rms_mix_rate, protect, crepe_hop_length, vc, hubert_model)
|
57 |
-
|
|
|
58 |
gc.collect()
|
|
|
59 |
|
60 |
def song_cover_pipeline(uploaded_file, voice_model, pitch_change, index_rate=0.5, filter_radius=3, rms_mix_rate=0.25, f0_method='rmvpe',
|
61 |
crepe_hop_length=128, protect=0.33, output_format='mp3', progress=gr.Progress()):
|
62 |
|
63 |
if not uploaded_file or not voice_model:
|
64 |
-
raise
|
65 |
|
66 |
display_progress(0, '[~] Запуск конвейера генерации AI-кавера...', progress)
|
67 |
|
68 |
if not os.path.exists(uploaded_file):
|
69 |
-
|
70 |
-
raise Exception(error_msg)
|
71 |
|
72 |
song_id = get_hash(uploaded_file)
|
73 |
-
song_dir = os.path.join(
|
74 |
os.makedirs(song_dir, exist_ok=True)
|
75 |
|
76 |
orig_song_path = convert_to_stereo(uploaded_file)
|
@@ -83,4 +87,4 @@ def song_cover_pipeline(uploaded_file, voice_model, pitch_change, index_rate=0.5
|
|
83 |
voice_change(voice_model, orig_song_path, ai_cover_path, pitch_change, f0_method, index_rate,
|
84 |
filter_radius, rms_mix_rate, protect, crepe_hop_length)
|
85 |
|
86 |
-
return ai_cover_path
|
|
|
4 |
import shlex
|
5 |
import subprocess
|
6 |
import librosa
|
7 |
+
import torch
|
8 |
import numpy as np
|
9 |
import soundfile as sf
|
10 |
import gradio as gr
|
11 |
from rvc import Config, load_hubert, get_vc, rvc_infer
|
12 |
|
13 |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
14 |
+
RVC_MODELS_DIR = os.path.join(BASE_DIR, 'rvc_models')
|
15 |
+
OUTPUT_DIR = os.path.join(BASE_DIR, 'song_output')
|
16 |
|
17 |
def get_rvc_model(voice_model):
|
18 |
+
model_dir = os.path.join(RVC_MODELS_DIR, voice_model)
|
19 |
rvc_model_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.pth')), None)
|
20 |
rvc_index_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.index')), None)
|
21 |
|
22 |
if rvc_model_path is None:
|
23 |
+
raise FileNotFoundError(f'В каталоге {model_dir} отсутствует файл модели.')
|
|
|
24 |
|
25 |
return rvc_model_path, rvc_index_path
|
26 |
|
27 |
def convert_to_stereo(audio_path):
|
28 |
wave, sr = librosa.load(audio_path, mono=False, sr=44100)
|
29 |
if type(wave[0]) != np.ndarray:
|
30 |
+
stereo_path = 'Voice_stereo.wav'
|
31 |
command = shlex.split(f'ffmpeg -y -loglevel error -i "{audio_path}" -ac 2 -f wav "{stereo_path}"')
|
32 |
subprocess.run(command)
|
33 |
return stereo_path
|
34 |
+
return audio_path
|
|
|
35 |
|
36 |
def get_hash(filepath):
|
37 |
+
file_hash = hashlib.blake2b()
|
38 |
with open(filepath, 'rb') as f:
|
|
|
39 |
while chunk := f.read(8192):
|
40 |
file_hash.update(chunk)
|
|
|
41 |
return file_hash.hexdigest()[:11]
|
42 |
|
43 |
def display_progress(percent, message, progress=gr.Progress()):
|
|
|
45 |
|
46 |
def voice_change(voice_model, vocals_path, output_path, pitch_change, f0_method, index_rate, filter_radius, rms_mix_rate, protect, crepe_hop_length):
|
47 |
rvc_model_path, rvc_index_path = get_rvc_model(voice_model)
|
48 |
+
|
49 |
+
if torch.cuda.is_available():
|
50 |
+
device = 'cuda:0'
|
51 |
+
else:
|
52 |
+
device = 'cpu'
|
53 |
+
|
54 |
config = Config(device, True)
|
55 |
+
hubert_model = load_hubert(device, config.is_half, os.path.join(RVC_MODELS_DIR, 'hubert_base.pt'))
|
56 |
cpt, version, net_g, tgt_sr, vc = get_vc(device, config.is_half, config, rvc_model_path)
|
57 |
|
58 |
rvc_infer(rvc_index_path, index_rate, vocals_path, output_path, pitch_change, f0_method, cpt, version, net_g,
|
59 |
filter_radius, tgt_sr, rms_mix_rate, protect, crepe_hop_length, vc, hubert_model)
|
60 |
+
|
61 |
+
del hubert_model, cpt, net_g, vc
|
62 |
gc.collect()
|
63 |
+
torch.cuda.empty_cache()
|
64 |
|
65 |
def song_cover_pipeline(uploaded_file, voice_model, pitch_change, index_rate=0.5, filter_radius=3, rms_mix_rate=0.25, f0_method='rmvpe',
|
66 |
crepe_hop_length=128, protect=0.33, output_format='mp3', progress=gr.Progress()):
|
67 |
|
68 |
if not uploaded_file or not voice_model:
|
69 |
+
raise ValueError('Убедитесь, что поле ввода песни и поле модели голоса заполнены.')
|
70 |
|
71 |
display_progress(0, '[~] Запуск конвейера генерации AI-кавера...', progress)
|
72 |
|
73 |
if not os.path.exists(uploaded_file):
|
74 |
+
raise FileNotFoundError(f'{uploaded_file} не существует.')
|
|
|
75 |
|
76 |
song_id = get_hash(uploaded_file)
|
77 |
+
song_dir = os.path.join(OUTPUT_DIR, song_id)
|
78 |
os.makedirs(song_dir, exist_ok=True)
|
79 |
|
80 |
orig_song_path = convert_to_stereo(uploaded_file)
|
|
|
87 |
voice_change(voice_model, orig_song_path, ai_cover_path, pitch_change, f0_method, index_rate,
|
88 |
filter_radius, rms_mix_rate, protect, crepe_hop_length)
|
89 |
|
90 |
+
return ai_cover_path
|