Rename easyfuncs.py to rvcfunc.py
Browse files- easyfuncs.py → rvcfunc.py +194 -242
easyfuncs.py → rvcfunc.py
RENAMED
|
@@ -1,242 +1,194 @@
|
|
| 1 |
-
import os, subprocess
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import shutil, time, torch, gc
|
| 4 |
-
from mega import Mega
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
import pandas as pd
|
| 7 |
-
import os, sys, subprocess, numpy as np
|
| 8 |
-
from pydub import AudioSegment
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
url
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return
|
| 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 |
-
subprocess.run(["wget", url, "-O", f'
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
url
|
| 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 |
-
venv_dir = "audiosr"
|
| 196 |
-
|
| 197 |
-
def split_audio(input_file, output_folder, chunk_duration=5.12):
|
| 198 |
-
os.makedirs(output_folder, exist_ok=True)
|
| 199 |
-
ffmpeg_command = f"ffmpeg -i {input_file} -f segment -segment_time {chunk_duration} -c:a pcm_s16le {output_folder}/out%03d.wav"
|
| 200 |
-
subprocess.run(ffmpeg_command, shell=True, check=True)
|
| 201 |
-
|
| 202 |
-
def create_file_list(output_folder):
|
| 203 |
-
file_list = os.path.join(output_folder, "file_list.txt")
|
| 204 |
-
with open(file_list, "w") as f:
|
| 205 |
-
for filename in sorted(os.listdir(output_folder)):
|
| 206 |
-
if filename.endswith(".wav"):
|
| 207 |
-
f.write(os.path.join(output_folder, filename) + "\n")
|
| 208 |
-
return file_list
|
| 209 |
-
|
| 210 |
-
def run_audiosr(file_list, model_name, suffix, guidance_scale, ddim_steps, output_folder, venv_dir):
|
| 211 |
-
command = f"{venv_dir}/bin/python -m audiosr --input_file_list {file_list} --model_name {model_name} --suffix {suffix} --guidance_scale {guidance_scale} --ddim_steps {ddim_steps} --save_path {output_folder}"
|
| 212 |
-
try:
|
| 213 |
-
subprocess.run(command, shell=True, check=True, stderr=subprocess.PIPE)
|
| 214 |
-
except subprocess.CalledProcessError as e:
|
| 215 |
-
print(f"Error running audiosr: {e.stderr.decode()}")
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
split_audio(temp_file, output_folder)
|
| 219 |
-
file_list = create_file_list(output_folder)
|
| 220 |
-
run_audiosr(file_list, model_name, suffix, guidance_scale, ddim_steps, output_folder, venv_dir)
|
| 221 |
-
|
| 222 |
-
output_file = None
|
| 223 |
-
time.sleep(1)
|
| 224 |
-
processed_chunks = []
|
| 225 |
-
for root, dirs, files in os.walk(output_folder):
|
| 226 |
-
for file in sorted(files):
|
| 227 |
-
if file.startswith("out") and file.endswith(f"{suffix}.wav"):
|
| 228 |
-
chunk_file = os.path.join(root, file)
|
| 229 |
-
processed_chunks.append(AudioSegment.from_wav(chunk_file))
|
| 230 |
-
|
| 231 |
-
if processed_chunks:
|
| 232 |
-
merged_audio = sum(processed_chunks)
|
| 233 |
-
output_file = os.path.join(output_folder, f"{choice}_merged{suffix}.wav")
|
| 234 |
-
merged_audio.export(output_file, format="wav")
|
| 235 |
-
|
| 236 |
-
display_file = AudioSegment.from_file(output_file)
|
| 237 |
-
sample_rate = display_file.frame_rate
|
| 238 |
-
audio_array = np.array(display_file.get_array_of_samples())
|
| 239 |
-
return (sample_rate, audio_array)
|
| 240 |
-
else:
|
| 241 |
-
print(f"Error: Could not find any processed audio chunks in {output_folder}")
|
| 242 |
-
return None
|
|
|
|
| 1 |
+
import os, subprocess
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import shutil, time, torch, gc
|
| 4 |
+
from mega import Mega
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import os, sys, subprocess, numpy as np
|
| 8 |
+
from pydub import AudioSegment
|
| 9 |
+
|
| 10 |
+
# Class to handle caching model urls from a spreadsheet
|
| 11 |
+
class CachedModels:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
csv_url = "https://docs.google.com/spreadsheets/d/1tAUaQrEHYgRsm1Lvrnj14HFHDwJWl0Bd9x0QePewNco/export?format=csv&gid=1977693859"
|
| 14 |
+
if os.path.exists("spreadsheet.csv"):
|
| 15 |
+
self.cached_data = pd.read_csv("spreadsheet.csv")
|
| 16 |
+
else:
|
| 17 |
+
self.cached_data = pd.read_csv(csv_url)
|
| 18 |
+
self.cached_data.to_csv("spreadsheet.csv", index=False)
|
| 19 |
+
# Cache model urls
|
| 20 |
+
self.models = {}
|
| 21 |
+
for _, row in self.cached_data.iterrows():
|
| 22 |
+
filename = row['Filename']
|
| 23 |
+
url = None
|
| 24 |
+
for value in row.values:
|
| 25 |
+
if isinstance(value, str) and "huggingface" in value:
|
| 26 |
+
url = value
|
| 27 |
+
break
|
| 28 |
+
if url:
|
| 29 |
+
self.models[filename] = url
|
| 30 |
+
# Get cached model urls
|
| 31 |
+
def get_models(self):
|
| 32 |
+
return self.models
|
| 33 |
+
|
| 34 |
+
def show(path,ext,on_error=None):
|
| 35 |
+
try:
|
| 36 |
+
return list(filter(lambda x: x.endswith(ext), os.listdir(path)))
|
| 37 |
+
except:
|
| 38 |
+
return on_error
|
| 39 |
+
|
| 40 |
+
def run_subprocess(command):
|
| 41 |
+
try:
|
| 42 |
+
subprocess.run(command, check=True)
|
| 43 |
+
return True, None
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return False, e
|
| 46 |
+
|
| 47 |
+
def download_from_url(url=None, model=None):
|
| 48 |
+
if not url:
|
| 49 |
+
try:
|
| 50 |
+
url = model[f'{model}']
|
| 51 |
+
except:
|
| 52 |
+
gr.Warning("Failed")
|
| 53 |
+
return ''
|
| 54 |
+
if model == '':
|
| 55 |
+
try:
|
| 56 |
+
model = url.split('/')[-1].split('?')[0]
|
| 57 |
+
except:
|
| 58 |
+
gr.Warning('Please name the model')
|
| 59 |
+
return
|
| 60 |
+
model = model.replace('.pth', '').replace('.index', '').replace('.zip', '')
|
| 61 |
+
url = url.replace('/blob/main/', '/resolve/main/').strip()
|
| 62 |
+
|
| 63 |
+
for directory in ["downloads", "unzips","zip"]:
|
| 64 |
+
#shutil.rmtree(directory, ignore_errors=True)
|
| 65 |
+
os.makedirs(directory, exist_ok=True)
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
if url.endswith('.pth'):
|
| 69 |
+
subprocess.run(["wget", url, "-O", f'assets/weights/{model}.pth'])
|
| 70 |
+
elif url.endswith('.index'):
|
| 71 |
+
os.makedirs(f'logs/{model}', exist_ok=True)
|
| 72 |
+
subprocess.run(["wget", url, "-O", f'logs/{model}/added_{model}.index'])
|
| 73 |
+
elif url.endswith('.zip'):
|
| 74 |
+
subprocess.run(["wget", url, "-O", f'downloads/{model}.zip'])
|
| 75 |
+
else:
|
| 76 |
+
if "drive.google.com" in url:
|
| 77 |
+
url = url.split('/')[0]
|
| 78 |
+
subprocess.run(["gdown", url, "--fuzzy", "-O", f'downloads/{model}'])
|
| 79 |
+
elif "mega.nz" in url:
|
| 80 |
+
Mega().download_url(url, 'downloads')
|
| 81 |
+
else:
|
| 82 |
+
subprocess.run(["wget", url, "-O", f'downloads/{model}'])
|
| 83 |
+
|
| 84 |
+
downloaded_file = next((f for f in os.listdir("downloads")), None)
|
| 85 |
+
if downloaded_file:
|
| 86 |
+
if downloaded_file.endswith(".zip"):
|
| 87 |
+
shutil.unpack_archive(f'downloads/{downloaded_file}', "unzips", 'zip')
|
| 88 |
+
for root, _, files in os.walk('unzips'):
|
| 89 |
+
for file in files:
|
| 90 |
+
file_path = os.path.join(root, file)
|
| 91 |
+
if file.endswith(".index"):
|
| 92 |
+
os.makedirs(f'logs/{model}', exist_ok=True)
|
| 93 |
+
shutil.copy2(file_path, f'logs/{model}')
|
| 94 |
+
elif file.endswith(".pth") and "G_" not in file and "D_" not in file:
|
| 95 |
+
shutil.copy(file_path, f'assets/weights/{model}.pth')
|
| 96 |
+
elif downloaded_file.endswith(".pth"):
|
| 97 |
+
shutil.copy(f'downloads/{downloaded_file}', f'assets/weights/{model}.pth')
|
| 98 |
+
elif downloaded_file.endswith(".index"):
|
| 99 |
+
os.makedirs(f'logs/{model}', exist_ok=True)
|
| 100 |
+
shutil.copy(f'downloads/{downloaded_file}', f'logs/{model}/added_{model}.index')
|
| 101 |
+
else:
|
| 102 |
+
gr.Warning("Failed to download file")
|
| 103 |
+
return 'Failed'
|
| 104 |
+
|
| 105 |
+
gr.Info("Done")
|
| 106 |
+
except Exception as e:
|
| 107 |
+
gr.Warning(f"There's been an error: {str(e)}")
|
| 108 |
+
finally:
|
| 109 |
+
shutil.rmtree("downloads", ignore_errors=True)
|
| 110 |
+
shutil.rmtree("unzips", ignore_errors=True)
|
| 111 |
+
shutil.rmtree("zip", ignore_errors=True)
|
| 112 |
+
return 'Done'
|
| 113 |
+
|
| 114 |
+
def speak(audio, text):
|
| 115 |
+
print(f"({audio}, {text})")
|
| 116 |
+
current_dir = os.getcwd()
|
| 117 |
+
os.chdir('./gpt_sovits_demo')
|
| 118 |
+
process = subprocess.Popen([
|
| 119 |
+
"python", "./zero.py",
|
| 120 |
+
"--input_file", audio,
|
| 121 |
+
"--audio_lang", "English",
|
| 122 |
+
"--text", text,
|
| 123 |
+
"--text_lang", "English"
|
| 124 |
+
], stdout=subprocess.PIPE, text=True)
|
| 125 |
+
|
| 126 |
+
for line in process.stdout:
|
| 127 |
+
line = line.strip()
|
| 128 |
+
if "All keys matched successfully" in line:
|
| 129 |
+
continue
|
| 130 |
+
if line.startswith("(") and line.endswith(")"):
|
| 131 |
+
path, finished = line[1:-1].split(", ")
|
| 132 |
+
if finished:
|
| 133 |
+
os.chdir(current_dir)
|
| 134 |
+
return path
|
| 135 |
+
os.chdir(current_dir)
|
| 136 |
+
return None
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def stereo_process(audio1,audio2,choice):
|
| 140 |
+
audio = audio1 if choice == "Input" else audio2
|
| 141 |
+
print(audio)
|
| 142 |
+
sample_rate, audio_array = audio
|
| 143 |
+
if len(audio_array.shape) == 1:
|
| 144 |
+
audio_bytes = audio_array.tobytes()
|
| 145 |
+
segment = AudioSegment(
|
| 146 |
+
data=audio_bytes,
|
| 147 |
+
sample_width=audio_array.dtype.itemsize, # 2 bytes for int16
|
| 148 |
+
frame_rate=sample_rate, # Use the sample rate from your tuple
|
| 149 |
+
channels=1 # Adjust if your audio has more channels
|
| 150 |
+
)
|
| 151 |
+
samples = np.array(segment.get_array_of_samples())
|
| 152 |
+
delay_samples = int(segment.frame_rate * (0.6 / 1000.0))
|
| 153 |
+
left_channel = np.zeros_like(samples)
|
| 154 |
+
right_channel = samples
|
| 155 |
+
left_channel[delay_samples:] = samples[:-delay_samples]
|
| 156 |
+
stereo_samples = np.column_stack((left_channel, right_channel))
|
| 157 |
+
return (sample_rate, stereo_samples.astype(np.int16))
|
| 158 |
+
else:
|
| 159 |
+
return audio
|
| 160 |
+
|
| 161 |
+
def sr_process(audio1, audio2, choice):
|
| 162 |
+
torch.cuda.empty_cache()
|
| 163 |
+
gc.collect()
|
| 164 |
+
if "tts_pipe" in locals(): del tts_pipe
|
| 165 |
+
audio = audio1 if choice == "Input" else audio2
|
| 166 |
+
sample_rate, audio_array = audio
|
| 167 |
+
audio_segment = AudioSegment(
|
| 168 |
+
audio_array.tobytes(),
|
| 169 |
+
frame_rate=sample_rate,
|
| 170 |
+
sample_width=audio_array.dtype.itemsize,
|
| 171 |
+
channels=1 if len(audio_array.shape) == 1 else 2
|
| 172 |
+
)
|
| 173 |
+
temp_file = os.path.join('TEMP', f'{choice}_{datetime.now().strftime("%Y%m%d_%H%M%S")}.wav')
|
| 174 |
+
audio_segment.export(temp_file, format="wav")
|
| 175 |
+
output_folder = "SR"
|
| 176 |
+
model_name = "speech"
|
| 177 |
+
suffix = "_ldm"
|
| 178 |
+
guidance_scale = 2.7
|
| 179 |
+
ddim_steps = 50
|
| 180 |
+
venv_dir = "audiosr"
|
| 181 |
+
|
| 182 |
+
def split_audio(input_file, output_folder, chunk_duration=5.12):
|
| 183 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 184 |
+
ffmpeg_command = f"ffmpeg -i {input_file} -f segment -segment_time {chunk_duration} -c:a pcm_s16le {output_folder}/out%03d.wav"
|
| 185 |
+
subprocess.run(ffmpeg_command, shell=True, check=True)
|
| 186 |
+
|
| 187 |
+
def create_file_list(output_folder):
|
| 188 |
+
file_list = os.path.join(output_folder, "file_list.txt")
|
| 189 |
+
with open(file_list, "w") as f:
|
| 190 |
+
for filename in sorted(os.listdir(output_folder)):
|
| 191 |
+
if filename.endswith(".wav"):
|
| 192 |
+
f.write(os.path.join(output_folder, filename) + "\n")
|
| 193 |
+
return file_list
|
| 194 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|