Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
import os, shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
'''import subprocess, os
|
5 |
assets_folder = "assets"
|
6 |
if not os.path.exists(assets_folder):
|
7 |
os.makedirs(assets_folder)
|
@@ -19,28 +45,104 @@ for file, link in files.items():
|
|
19 |
try:
|
20 |
subprocess.run(['wget', link, '-O', file_path], check=True)
|
21 |
except subprocess.CalledProcessError as e:
|
22 |
-
print(f"Error downloading {file}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def show_available(filepath):
|
25 |
return os.listdir(filepath)
|
26 |
|
27 |
def upload_file(file):
|
28 |
audio_formats = ['.wav', '.mp3', '.ogg', '.flac', '.aac']
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
shutil.move(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
else:
|
37 |
-
|
38 |
-
return {"choices":show_available('audios'),"__type__": "update"}
|
39 |
|
|
|
|
|
|
|
40 |
with gr.Blocks() as app:
|
41 |
with gr.Row():
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import os, shutil
|
3 |
+
import subprocess
|
4 |
+
|
5 |
+
def convert(audio_picker,model_picker):
|
6 |
+
command = [
|
7 |
+
"python",
|
8 |
+
"tools/infer_cli.py",
|
9 |
+
"--f0up_key", "0",
|
10 |
+
"--input_path", f"audios/{audio_picker}",
|
11 |
+
"--index_path", "",
|
12 |
+
"--f0method", "rmvpe",
|
13 |
+
"--opt_path", "cli_output.wav",
|
14 |
+
"--model_name", f"{model_picker}",
|
15 |
+
"--index_rate", "0.8",
|
16 |
+
"--device", "cpu",
|
17 |
+
"--is_half", "False",
|
18 |
+
"--filter_radius", "3",
|
19 |
+
"--resample_sr", "0",
|
20 |
+
"--rms_mix_rate", "0.21",
|
21 |
+
"--protect", "0"
|
22 |
+
]
|
23 |
+
|
24 |
+
try:
|
25 |
+
process = subprocess.Popen(command)
|
26 |
+
process.wait() # Wait for the subprocess to finish
|
27 |
+
print("Script executed successfully.")
|
28 |
+
except subprocess.CalledProcessError as e:
|
29 |
+
print(f"Error: {e}")
|
30 |
|
|
|
31 |
assets_folder = "assets"
|
32 |
if not os.path.exists(assets_folder):
|
33 |
os.makedirs(assets_folder)
|
|
|
45 |
try:
|
46 |
subprocess.run(['wget', link, '-O', file_path], check=True)
|
47 |
except subprocess.CalledProcessError as e:
|
48 |
+
print(f"Error downloading {file}: {e}")
|
49 |
+
|
50 |
+
def download_from_url(url, model):
|
51 |
+
if url == '':
|
52 |
+
return "URL cannot be left empty."
|
53 |
+
if model =='':
|
54 |
+
return "You need to name your model. For example: My-Model"
|
55 |
+
url = url.strip()
|
56 |
+
zip_dirs = ["zips", "unzips"]
|
57 |
+
for directory in zip_dirs:
|
58 |
+
if os.path.exists(directory):
|
59 |
+
shutil.rmtree(directory)
|
60 |
+
os.makedirs("zips", exist_ok=True)
|
61 |
+
os.makedirs("unzips", exist_ok=True)
|
62 |
+
zipfile = model + '.zip'
|
63 |
+
zipfile_path = './zips/' + zipfile
|
64 |
+
try:
|
65 |
+
if "drive.google.com" in url:
|
66 |
+
subprocess.run(["gdown", url, "--fuzzy", "-O", zipfile_path])
|
67 |
+
elif "mega.nz" in url:
|
68 |
+
m = Mega()
|
69 |
+
m.download_url(url, './zips')
|
70 |
+
else:
|
71 |
+
subprocess.run(["wget", url, "-O", zipfile_path])
|
72 |
+
for filename in os.listdir("./zips"):
|
73 |
+
if filename.endswith(".zip"):
|
74 |
+
zipfile_path = os.path.join("./zips/",filename)
|
75 |
+
shutil.unpack_archive(zipfile_path, "./unzips", 'zip')
|
76 |
+
else:
|
77 |
+
return "No zipfile found."
|
78 |
+
for root, dirs, files in os.walk('./unzips'):
|
79 |
+
for file in files:
|
80 |
+
file_path = os.path.join(root, file)
|
81 |
+
if file.endswith(".index"):
|
82 |
+
os.mkdir(f'./logs/{model}')
|
83 |
+
shutil.copy2(file_path,f'./logs/{model}')
|
84 |
+
elif "G_" not in file and "D_" not in file and file.endswith(".pth"):
|
85 |
+
shutil.copy(file_path,f'./assets/weights/{model}.pth')
|
86 |
+
shutil.rmtree("zips")
|
87 |
+
shutil.rmtree("unzips")
|
88 |
+
return "Success."
|
89 |
+
except:
|
90 |
+
return "There's been an error."
|
91 |
|
92 |
def show_available(filepath):
|
93 |
return os.listdir(filepath)
|
94 |
|
95 |
def upload_file(file):
|
96 |
audio_formats = ['.wav', '.mp3', '.ogg', '.flac', '.aac']
|
97 |
+
_, ext = os.path.splitext(file.name)
|
98 |
+
filename = os.path.basename(file.name)
|
99 |
+
file_path = file.name
|
100 |
+
if ext.lower() in audio_formats:
|
101 |
+
if os.path.exists(f'audios/{filename}'):
|
102 |
+
os.remove(f'audios/{filename}')
|
103 |
+
shutil.move(file_path,'audios')
|
104 |
+
elif ext.lower().endswith('.pth'):
|
105 |
+
if os.path.exists(f'assets/weights/{filename}'):
|
106 |
+
os.remove(f'assets/weights/{filename}')
|
107 |
+
shutil.move(file_path,'assets/weights')
|
108 |
+
elif ext.lower().endswith('.index'):
|
109 |
+
if os.path.exists(f'logs/{filename}'):
|
110 |
+
os.remove(f'logs/{filename}')
|
111 |
+
shutil.move(file_path,'logs')
|
112 |
else:
|
113 |
+
gr.Warning('File incompatible')
|
114 |
+
return {"choices":show_available('audios'),"__type__": "update"},{"choices":show_available('assets/weights'),"__type__": "update"},None
|
115 |
|
116 |
+
def refresh():
|
117 |
+
return {"choices":show_available('audios'),"__type__": "update"},{"choices":show_available('assets/weights'),"__type__": "update"}
|
118 |
+
|
119 |
with gr.Blocks() as app:
|
120 |
with gr.Row():
|
121 |
+
with gr.Column():
|
122 |
+
with gr.Tabs():
|
123 |
+
with gr.TabItem("1.Choose a voice model:"):
|
124 |
+
model_picker = gr.Dropdown(label="",choices=show_available('assets/weights'),value='',interactive=True)
|
125 |
+
with gr.TabItem("(Or download a model here)"):
|
126 |
+
with gr.Row():
|
127 |
+
url = gr.Textbox(label="Paste the URL here:",value="",placeholder="(i.e. https://huggingface.co/repo/model/resolve/main/model.zip)")
|
128 |
+
with gr.Row():
|
129 |
+
with gr.Column():
|
130 |
+
model_rename = gr.Textbox(placeholder="My-Model", label="Name your model:",value="")
|
131 |
+
with gr.Column():
|
132 |
+
download_button = gr.Button("Download")
|
133 |
+
download_button.click(fn=download_from_url,inputs=[url,model_rename],outputs=[])
|
134 |
+
|
135 |
+
with gr.Row():
|
136 |
+
with gr.Tabs():
|
137 |
+
with gr.TabItem("2.Choose an audio file:"):
|
138 |
+
audio_picker = gr.Dropdown(label="",choices=show_available('audios'),value='',interactive=True)
|
139 |
+
with gr.TabItem("(Or upload a new file here)"):
|
140 |
+
dropbox = gr.File(label="Drop an audio here. (You can also drop a .pth or .index file here)")
|
141 |
+
dropbox.upload(fn=upload_file, inputs=[dropbox],outputs=[audio_picker,model_picker,dropbox])
|
142 |
+
audio_refresher = gr.Button("Refresh")
|
143 |
+
audio_refresher.click(fn=refresh,inputs=[],outputs=[audio_picker,model_picker])
|
144 |
+
convert_button = gr.Button("Convert")
|
145 |
+
convert_button.click(convert, inputs=[audio_picker,model_picker])
|
146 |
+
|
147 |
+
app.queue()
|
148 |
+
app.launch()
|