Spaces:
Running
Running
Update src/main.py
Browse files3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344451 unmodified line4748495051525354555657585960616263646566676869707172737475766 unmodified lines838485863 unmodified linesimport shleximport subprocessimport librosaimport numpy as npimport soundfile as sfimport gradio as grfrom rvc import Config, load_hubert, get_vc, rvc_inferBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))rvc_models_dir = os.path.join(BASE_DIR, 'rvc_models')output_dir = os.path.join(BASE_DIR, 'song_output')def get_rvc_model(voice_model):model_dir = os.path.join(rvc_models_dir, voice_model)rvc_model_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.pth')), None)rvc_index_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.index')), None)if rvc_model_path is None:error_msg = f'В каталоге {model_dir} отсутствует файл модели.'raise Exception(error_msg)return rvc_model_path, rvc_index_pathdef convert_to_stereo(audio_path):wave, sr = librosa.load(audio_path, mono=False, sr=44100)if type(wave[0]) != np.ndarray:stereo_path = f'Voice_stereo.wav'command = shlex.split(f'ffmpeg -y -loglevel error -i "{audio_path}" -ac 2 -f wav "{stereo_path}"')subprocess.run(command)return stereo_pathelse:return audio_pathdef get_hash(filepath):with open(filepath, 'rb') as f:file_hash = hashlib.blake2b()while chunk := f.read(8192):file_hash.update(chunk)return file_hash.hexdigest()[:11]def display_progress(percent, message, progress=gr.Progress()):1 unmodified linedef voice_change(voice_model, vocals_path, output_path, pitch_change, f0_method, index_rate, filter_radius, rms_mix_rate, protect, crepe_hop_length):rvc_model_path, rvc_index_path = get_rvc_model(voice_model)device = 'cuda:0'config = Config(device, True)hubert_model = load_hubert(device, config.is_half, os.path.join(rvc_models_dir, 'hubert_base.pt'))cpt, version, net_g, tgt_sr, vc = get_vc(device, config.is_half, config, rvc_model_path)rvc_infer(rvc_index_path, index_rate, vocals_path, output_path, pitch_change, f0_method, cpt, version, net_g,filter_radius, tgt_sr, rms_mix_rate, protect, crepe_hop_length, vc, hubert_model)del hubert_model, cptgc.collect()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',crepe_hop_length=128, protect=0.33, output_format='mp3', progress=gr.Progress()):if not uploaded_file or not voice_model:raise Exception('Убедитесь, что поле ввода песни и поле модели голоса заполнены.')display_progress(0, '[~] Запуск конвейера генерации AI-кавера...', progress)if not os.path.exists(uploaded_file):error_msg = f'{uploaded_file} не существует.'raise Exception(error_msg)song_id = get_hash(uploaded_file)song_dir = os.path.join(output_dir, song_id)os.makedirs(song_dir, exist_ok=True)orig_song_path = convert_to_stereo(uploaded_file)6 unmodified linesvoice_change(voice_model, orig_song_path, ai_cover_path, pitch_change, f0_method, index_rate,filter_radius, rms_mix_rate, protect, crepe_hop_length)return ai_cover_path3 unmodified lines456789101112131415161718192021222324252627282930313233343536373839404142431 unmodified line4546474849505152535455565758596061626364656667686970717273747576777879806 unmodified lines878889903 unmodified linesimport shleximport subprocessimport librosaimport torchimport numpy as npimport soundfile as sfimport gradio as grfrom rvc import Config, load_hubert, get_vc, rvc_inferBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))RVC_MODELS_DIR = os.path.join(BASE_DIR, 'rvc_models')OUTPUT_DIR = os.path.join(BASE_DIR, 'song_output')def get_rvc_model(voice_model):model_dir = os.path.join(RVC_MODELS_DIR, voice_model)rvc_model_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.pth')), None)rvc_index_path = next((os.path.join(model_dir, f) for f in os.listdir(model_dir) if f.endswith('.index')), None)if rvc_model_path is None:raise FileNotFoundError(f'В каталоге {model_dir} отсутствует файл модели.')return rvc_model_path, rvc_index_pathdef convert_to_stereo(audio_path):wave, sr = librosa.load(audio_path, mono=False, sr=44100)if type(wave[0]) != np.ndarray:stereo_path = 'Voice_stereo.wav'command = shlex.split(f'ffmpeg -y -loglevel error -i "{audio_path}" -ac 2 -f wav "{stereo_path}"')subprocess.run(command)return stereo_pathreturn audio_pathdef get_hash(filepath):file_hash = hashlib.blake2b()with open(filepath, 'rb') as f:while chunk := f.read(8192):file_hash.update(chunk)return file_hash.hexdigest()[:11]def display_progress(percent, message, progress=gr.Progress()):1 unmodified linedef voice_change(voice_model, vocals_path, output_path, pitch_change, f0_method, index_rate, filter_radius, rms_mix_rate, protect, crepe_hop_length):rvc_model_path, rvc_index_path = get_rvc_model(voice_model)if torch.cuda.is_available():device = 'cuda:0'else:device = 'cpu'config = Config(device, True)hubert_model = load_hubert(device, config.is_half, os.path.join(RVC_MODELS_DIR, 'hubert_base.pt'))cpt, version, net_g, tgt_sr, vc = get_vc(device, config.is_half, config, rvc_model_path)rvc_infer(rvc_index_path, index_rate, vocals_path, output_path, pitch_change, f0_method, cpt, version, net_g,filter_radius, tgt_sr, rms_mix_rate, protect, crepe_hop_length, vc, hubert_model)del hubert_model, cpt, net_g, vcgc.collect()torch.cuda.empty_cache()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',crepe_hop_length=128, protect=0.33, output_format='mp3', progress=gr.Progress()):if not uploaded_file or not voice_model:raise ValueError('Убедитесь, что поле ввода песни и поле модели голоса заполнены.')display_progress(0, '[~] Запуск конвейера генерации AI-кавера...', progress)if not os.path.exists(uploaded_file):raise FileNotFoundError(f'{uploaded_file} не существует.')song_id = get_hash(uploaded_file)song_dir = os.path.join(OUTPUT_DIR, song_id)os.makedirs(song_dir, exist_ok=True)orig_song_path = convert_to_stereo(uploaded_file)6 unmodified linesvoice_change(voice_model, orig_song_path, ai_cover_path, pitch_change, f0_method, index_rate,filter_radius, rms_mix_rate, protect, crepe_hop_length)return ai_cover_path
src/main.py+22-18