Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import random | |
import os | |
import zipfile # built in module for unzipping files (thank god) | |
import librosa | |
import time | |
from infer_rvc_python import BaseLoader | |
from pydub import AudioSegment | |
# fucking dogshit toggle | |
try: | |
import spaces | |
spaces_status = True | |
except ImportError: | |
spaces_status = False | |
converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None) # <- yeah so like this handles rvc | |
global pth_file | |
global index_file | |
pth_file = "model.pth" | |
index_file = "model.index" | |
#CONFIGS | |
TEMP_DIR = "temp" | |
MODEL_PREFIX = "model" | |
PITCH_ALGO_OPT = [ | |
"pm", | |
"harvest", | |
"crepe", | |
"rmvpe", | |
"rmvpe+", | |
] | |
os.makedirs(TEMP_DIR, exist_ok=True) | |
def unzip_file(file): | |
filename = os.path.basename(file).split(".")[0] # converts "model.zip" to "model" so we can do things | |
with zipfile.ZipFile(file, 'r') as zip_ref: | |
zip_ref.extractall(os.path.join(TEMP_DIR, filename)) # might not be very ram efficient... | |
return True | |
def progress_bar(total, current): # best progress bar ever trust me sunglasses emoji π | |
return "[" + "=" * int(current / total * 20) + ">" + " " * (20 - int(current / total * 20)) + "] " + str(int(current / total * 100)) + "%" | |
def download_from_url(url, filename=None): | |
if "huggingface" not in url: | |
return ["The URL must be from huggingface", "Failed", "Failed"] | |
if filename is None: | |
filename = os.path.join(TEMP_DIR, MODEL_PREFIX + str(random.randint(1, 1000)) + ".zip") | |
response = requests.get(url) | |
total = int(response.headers.get('content-length', 0)) # bytes to download (length of the file) | |
if total > 500000000: | |
return ["The file is too large. You can only download files up to 500 MB in size.", "Failed", "Failed"] | |
current = 0 | |
with open(filename, "wb") as f: | |
for data in response.iter_content(chunk_size=4096): # download in chunks of 4096 bytes (4kb - helps with memory usage and speed) | |
f.write(data) | |
current += len(data) | |
print(progress_bar(total, current), end="\r") # \r is a carriage return, it moves the cursor to the start of the line so its like tqdm sunglasses emoji π | |
# unzip because the model is in a zip file lel | |
try: | |
unzip_file(filename) | |
except Exception as e: | |
return ["Failed to unzip the file", "Failed", "Failed"] # return early if it fails and like tell the user but its dogshit hahahahahahaha π According to all known laws aviation, there is no way a bee should be able to fly. | |
unzipped_dir = os.path.join(TEMP_DIR, os.path.basename(filename).split(".")[0]) # just do what we did in unzip_file because we need the directory | |
pth_files = [] | |
index_files = [] | |
for root, dirs, files in os.walk(unzipped_dir): # could be done more efficiently because nobody stores models in subdirectories but like who cares (it's a futureproofing thing lel) | |
for file in files: | |
if file.endswith(".pth"): | |
pth_files.append(os.path.join(root, file)) | |
elif file.endswith(".index"): | |
index_files.append(os.path.join(root, file)) | |
print(pth_files, index_files) # debug print because im fucking stupid and i need to see what is going on | |
global pth_file | |
global index_file | |
pth_file = pth_files[0] | |
index_file = index_files[0] | |
return ["Downloaded as " + filename, pth_files[0], index_files[0]] | |
if spaces_status: | |
def convert_now(audio_files, random_tag, converter): | |
return converter( | |
audio_files, | |
random_tag, | |
overwrite=False, | |
parallel_workers=8 | |
) | |
else: | |
def convert_now(audio_files, random_tag, converter): | |
return converter( | |
audio_files, | |
random_tag, | |
overwrite=False, | |
parallel_workers=8 | |
) | |
def run( | |
audio_files, | |
file_m, | |
pitch_alg, | |
pitch_lvl, | |
file_index, | |
index_inf, | |
r_m_f, | |
e_r, | |
c_b_p, | |
): | |
if not audio_files: | |
raise ValueError("The audio pls") | |
if isinstance(audio_files, str): | |
audio_files = [audio_files] | |
try: | |
duration_base = librosa.get_duration(filename=audio_files[0]) | |
print("Duration:", duration_base) | |
except Exception as e: | |
print(e) | |
random_tag = "USER_"+str(random.randint(10000000, 99999999)) | |
converter.apply_conf( | |
tag=random_tag, | |
file_model=file_m, | |
pitch_algo=pitch_alg, | |
pitch_lvl=pitch_lvl, | |
file_index=file_index, | |
index_influence=index_inf, | |
respiration_median_filtering=r_m_f, | |
envelope_ratio=e_r, | |
consonant_breath_protection=c_b_p, | |
resample_sr=44100 if audio_files[0].endswith('.mp3') else 0, | |
) | |
time.sleep(0.1) | |
result = convert_now(audio_files, random_tag, converter) | |
return result[0] | |
with gr.Blocks() as demo: | |
gr.Markdown("## Ilaria RVC π") | |
with gr.Tab("Inference"): | |
sound_gui = gr.Audio(value=None,type="filepath",autoplay=False,visible=True,) | |
pth_file_ui = gr.Textbox(label="Model pth file",value=pth_file,visible=False,interactive=False,) # gradio is fucking weird (im with stupid v) | |
index_file_ui = gr.Textbox(label="Index pth file",value=index_file,visible=False,interactive=False,) # gradio is fucking weird (im with stupid ^) | |
pitch_algo_conf = gr.Dropdown(PITCH_ALGO_OPT,value=PITCH_ALGO_OPT[4],label="Pitch algorithm",visible=True,interactive=True,) | |
pitch_lvl_conf = gr.Slider(label="Pitch level",minimum=-24,maximum=24,step=1,value=0,visible=True,interactive=True,) | |
index_inf_conf = gr.Slider(minimum=0,maximum=1,label="Index influence",value=0.75,) | |
respiration_filter_conf = gr.Slider(minimum=0,maximum=7,label="Respiration median filtering",value=3,step=1,interactive=True,) | |
envelope_ratio_conf = gr.Slider(minimum=0,maximum=1,label="Envelope ratio",value=0.25,interactive=True,) | |
consonant_protec_conf = gr.Slider(minimum=0,maximum=0.5,label="Consonant breath protection",value=0.5,interactive=True,) | |
button_conf = gr.Button("Convert",variant="primary",) | |
output_conf = gr.Audio(type="filepath",label="Output",) | |
button_conf.click( | |
run, | |
inputs=[ | |
sound_gui, | |
pth_file_ui, | |
pitch_algo_conf, | |
pitch_lvl_conf, | |
index_file_ui, # put a bullet through my head | |
index_inf_conf, | |
respiration_filter_conf, | |
envelope_ratio_conf, | |
consonant_protec_conf, | |
], | |
outputs=[output_conf], | |
) | |
with gr.Tab("Download Model"): | |
# markdown | |
gr.Markdown( | |
"Download the model from the following URL and upload it here. (Hugginface RVC model)" | |
) | |
model = gr.Textbox(lines=1, label="Model URL") | |
download_button = gr.Button("Download Model") | |
status = gr.Textbox(lines=1, label="Status", placeholder="Waiting....", interactive=False) | |
model_pth = gr.Textbox(lines=1, label="Model pth file", placeholder="Waiting....", interactive=False) | |
index_pth = gr.Textbox(lines=1, label="Index pth file", placeholder="Waiting....", interactive=False) | |
download_button.click(download_from_url, model, outputs=[status, model_pth, index_pth]) | |
set_model_button = gr.Button("Set Model") | |
#set_model_button.click( | |
with gr.Tab("Credits"): | |
gr.Markdown( | |
""" | |
Ilaria RVC made by [Ilaria](https://huggingface.co/TheStinger) suport her on [ko-fi](https://ko-fi.com/ilariaowo) | |
The Inference code is made by [r3gm](https://huggingface.co/r3gm) (his module helped form this space π) | |
made with β€οΈ by [mikus](https://github.com/cappuch) - i hacked it up lel | |
""" | |
) | |
demo.queue(api_open=False).launch(show_api=False) # idk ilaria if you want or dont want to | |