Update module.py
Browse files
module.py
CHANGED
|
@@ -15,11 +15,149 @@ import tempfile
|
|
| 15 |
import anyio
|
| 16 |
from pathlib import Path
|
| 17 |
from lib.language_tts import language_dict
|
| 18 |
-
import os
|
| 19 |
-
import zipfile
|
| 20 |
import shutil
|
| 21 |
-
import urllib.request
|
| 22 |
-
import gdown
|
| 23 |
-
import subprocess
|
| 24 |
import time
|
| 25 |
-
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
import anyio
|
| 16 |
from pathlib import Path
|
| 17 |
from lib.language_tts import language_dict
|
|
|
|
|
|
|
| 18 |
import shutil
|
|
|
|
|
|
|
|
|
|
| 19 |
import time
|
| 20 |
+
from argparse import ArgumentParser
|
| 21 |
+
from download_model import download_online_model
|
| 22 |
+
|
| 23 |
+
main_dir = Path().resolve()
|
| 24 |
+
print(main_dir)
|
| 25 |
+
|
| 26 |
+
os.chdir(main_dir)
|
| 27 |
+
models_dir = main_dir / "rvc_models"
|
| 28 |
+
audio_separat_dir = main_dir / "audio_input"
|
| 29 |
+
AUDIO_DIR = main_dir / 'audio_input'
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# Function to list all folders in the models directory
|
| 33 |
+
def get_folders():
|
| 34 |
+
if models_dir.exists() and models_dir.is_dir():
|
| 35 |
+
return [folder.name for folder in models_dir.iterdir() if folder.is_dir()]
|
| 36 |
+
return []
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Function to refresh and return the list of folders
|
| 40 |
+
def refresh_folders():
|
| 41 |
+
return gr.Dropdown.update(choices=get_folders())
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Function to get the list of audio files in the specified directory
|
| 45 |
+
def get_audio_files():
|
| 46 |
+
if not os.path.exists(AUDIO_DIR):
|
| 47 |
+
os.makedirs(AUDIO_DIR)
|
| 48 |
+
return [f for f in os.listdir(AUDIO_DIR) if f.lower().endswith(('.mp3', '.wav', '.flac', '.ogg', '.aac'))]
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Function to return the full path of audio files for playback
|
| 52 |
+
def load_audio_files():
|
| 53 |
+
audio_files = get_audio_files()
|
| 54 |
+
return [os.path.join(AUDIO_DIR, f) for f in audio_files]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def refresh_audio_list():
|
| 58 |
+
audio_files = load_audio_files()
|
| 59 |
+
return gr.Dropdown.update(choices=audio_files)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def download_audio(url):
|
| 63 |
+
ydl_opts = {
|
| 64 |
+
'format': 'bestaudio/best',
|
| 65 |
+
'outtmpl': 'ytdl/%(title)s.%(ext)s',
|
| 66 |
+
'postprocessors': [{
|
| 67 |
+
'key': 'FFmpegExtractAudio',
|
| 68 |
+
'preferredcodec': 'wav',
|
| 69 |
+
'preferredquality': '192',
|
| 70 |
+
}],
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 74 |
+
info_dict = ydl.extract_info(url, download=True)
|
| 75 |
+
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 76 |
+
return file_path
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
async def text_to_speech_edge(text, language_code):
|
| 80 |
+
voice = language_dict.get(language_code, "default_voice")
|
| 81 |
+
communicate = edge_tts.Communicate(text, voice)
|
| 82 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 83 |
+
tmp_path = tmp_file.name
|
| 84 |
+
await communicate.save(tmp_path)
|
| 85 |
+
return tmp_path
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == '__main__':
|
| 89 |
+
parser = ArgumentParser()
|
| 90 |
+
parser.add_argument("--share", action="store_true", dest="share_enabled", default=False)
|
| 91 |
+
parser.add_argument("--listen", action="store_true", default=False)
|
| 92 |
+
parser.add_argument('--listen-host', type=str)
|
| 93 |
+
parser.add_argument('--listen-port', type=int)
|
| 94 |
+
args = parser.parse_args()
|
| 95 |
+
|
| 96 |
+
# Gradio Interface
|
| 97 |
+
with gr.Blocks(title="Hex RVC", theme=gr.themes.Base(primary_hue="red", secondary_hue="pink")) as app:
|
| 98 |
+
gr.Markdown("# Hex RVC")
|
| 99 |
+
gr.Markdown("Join [AIHub](https://discord.gg/aihub) to get the RVC model!")
|
| 100 |
+
|
| 101 |
+
with gr.Tab("Inference"):
|
| 102 |
+
with gr.Row():
|
| 103 |
+
MODEL_NAME = gr.Dropdown(
|
| 104 |
+
label="Select a Model",
|
| 105 |
+
choices=get_folders(),
|
| 106 |
+
interactive=True
|
| 107 |
+
)
|
| 108 |
+
SOUND_PATH = gr.Dropdown(
|
| 109 |
+
choices=load_audio_files(),
|
| 110 |
+
label="Select an audio file",
|
| 111 |
+
interactive=True
|
| 112 |
+
)
|
| 113 |
+
upload_audio = gr.Audio(label="Upload Audio", type='filepath')
|
| 114 |
+
|
| 115 |
+
with gr.Accordion("Hex TTS"):
|
| 116 |
+
input_text = gr.Textbox(lines=5, label="Input Text")
|
| 117 |
+
language = gr.Dropdown(choices=list(language_dict.keys()), label="Choose the Voice Model")
|
| 118 |
+
tts_convert = gr.Button("Convert")
|
| 119 |
+
tts_output = gr.Audio(label="Generated TTS Audio", type='filepath')
|
| 120 |
+
|
| 121 |
+
tts_convert.click(
|
| 122 |
+
fn=text_to_speech_edge,
|
| 123 |
+
inputs=[input_text, language],
|
| 124 |
+
outputs=tts_output
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
output_audio = gr.Audio(label="Generated Audio", type='filepath')
|
| 128 |
+
with gr.Row():
|
| 129 |
+
refresh_btn = gr.Button("Refresh")
|
| 130 |
+
run_button = gr.Button("Convert")
|
| 131 |
+
|
| 132 |
+
refresh_btn.click(
|
| 133 |
+
lambda: (refresh_audio_list(), refresh_folders()),
|
| 134 |
+
outputs=[SOUND_PATH, MODEL_NAME]
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
with gr.Tab("Download RVC Model"):
|
| 138 |
+
url = gr.Textbox(label="Your Model URL")
|
| 139 |
+
dirname = gr.Textbox(label="Your Model Name")
|
| 140 |
+
download_button = gr.Button("Download Model")
|
| 141 |
+
download_output = gr.Textbox(label="Download Status")
|
| 142 |
+
|
| 143 |
+
download_button.click(
|
| 144 |
+
download_online_model,
|
| 145 |
+
inputs=[url, dirname],
|
| 146 |
+
outputs=download_output
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
with gr.Tab("UVR5 (Demo)"):
|
| 150 |
+
input_audio = gr.Audio(type="filepath", label="Upload Audio")
|
| 151 |
+
roformer_link = gr.Textbox(label="Audio Link")
|
| 152 |
+
roformer_download_button = gr.Button("Download")
|
| 153 |
+
|
| 154 |
+
separate_button = gr.Button("Separate Audio")
|
| 155 |
+
separation_output = gr.Textbox(label="Separation Output Path")
|
| 156 |
+
|
| 157 |
+
roformer_download_button.click(download_audio, inputs=[roformer_link], outputs=[input_audio])
|
| 158 |
+
|
| 159 |
+
app.launch(
|
| 160 |
+
share=args.share_enabled,
|
| 161 |
+
server_name=None if not args.listen else (args.listen_host or '0.0.0.0'),
|
| 162 |
+
server_port=args.listen_port,
|
| 163 |
+
)
|