File size: 9,808 Bytes
8c8860a
 
 
 
 
 
 
 
 
 
6ad1e89
8c8860a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ad1e89
 
8c8860a
 
6ad1e89
8c8860a
 
 
 
 
 
 
e02355a
8c8860a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02355a
8c8860a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02355a
8c8860a
 
 
 
 
e02355a
8c8860a
 
 
 
 
6ad1e89
8c8860a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02355a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
75
76
77
78
79
80
81
82
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import os
import re
import random
from scipy.io.wavfile import write
from scipy.io.wavfile import read
import numpy as np
import gradio as gr
import yt_dlp
import subprocess



mdxnet_models = [
    'UVR-MDX-NET-Inst_full_292.onnx',
    'UVR-MDX-NET_Inst_187_beta.onnx',
    'UVR-MDX-NET_Inst_82_beta.onnx',
    'UVR-MDX-NET_Inst_90_beta.onnx',
    'UVR-MDX-NET_Main_340.onnx',
    'UVR-MDX-NET_Main_390.onnx',
    'UVR-MDX-NET_Main_406.onnx',
    'UVR-MDX-NET_Main_427.onnx',
    'UVR-MDX-NET_Main_438.onnx',
    'UVR-MDX-NET-Inst_HQ_1.onnx',
    'UVR-MDX-NET-Inst_HQ_2.onnx',
    'UVR-MDX-NET-Inst_HQ_3.onnx',
    'UVR-MDX-NET-Inst_HQ_4.onnx',
    'UVR_MDXNET_Main.onnx',
    'UVR-MDX-NET-Inst_Main.onnx',
    'UVR_MDXNET_1_9703.onnx',
    'UVR_MDXNET_2_9682.onnx',
    'UVR_MDXNET_3_9662.onnx',
    'UVR-MDX-NET-Inst_1.onnx',
    'UVR-MDX-NET-Inst_2.onnx',
    'UVR-MDX-NET-Inst_3.onnx',
    'UVR_MDXNET_KARA.onnx',
    'UVR_MDXNET_KARA_2.onnx',
    'UVR_MDXNET_9482.onnx',
    'UVR-MDX-NET-Voc_FT.onnx',
    'Kim_Vocal_1.onnx',
    'Kim_Vocal_2.onnx',
    'Kim_Inst.onnx',
    'Reverb_HQ_By_FoxJoy.onnx',
    'UVR-MDX-NET_Crowd_HQ_1.onnx',
    'kuielab_a_vocals.onnx',
    'kuielab_a_other.onnx',
    'kuielab_a_bass.onnx',
    'kuielab_a_drums.onnx',
    'kuielab_b_vocals.onnx',
    'kuielab_b_other.onnx',
    'kuielab_b_bass.onnx',
    'kuielab_b_drums.onnx',
]



output_format = [
    'wav',
    'flac',
    'mp3',
]

mdxnet_overlap_values = [
    '0.25',
    '0.5',
    '0.75',
    '0.99',
]



def download_audio(url):
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': 'ytdl/%(title)s.%(ext)s',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'wav',
            'preferredquality': '192',
        }],
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(url, download=True)
        file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
        sample_rate, audio_data = read(file_path)
        audio_array = np.asarray(audio_data, dtype=np.int16)

        return sample_rate, audio_array


def mdxnet_separator(mdxnet_audio, mdxnet_model, mdxnet_output_format, mdxnet_segment_size, mdxnet_overlap, mdxnet_denoise):
  files_list = []
  files_list.clear()
  directory = "./outputs"
  random_id = str(random.randint(10000, 99999))
  pattern = f"{random_id}"
  os.makedirs("outputs", exist_ok=True)
  write(f'{random_id}.wav', mdxnet_audio[0], mdxnet_audio[1])
  prompt = f"audio-separator {random_id}.wav --model_filename {mdxnet_model} --output_dir=./outputs --output_format={mdxnet_output_format} --normalization=0.9 --mdx_segment_size={mdxnet_segment_size} --mdx_overlap={mdxnet_overlap}"
  
  if mdxnet_denoise:
    prompt += " --mdx_enable_denoise"
  
  os.system(prompt)

  for file in os.listdir(directory):
    if re.search(pattern, file):
      files_list.append(os.path.join(directory, file))

  stem1_file = files_list[0]
  stem2_file = files_list[1]

  return stem1_file, stem2_file


def mdxnet_batch(path_input, path_output, model, output_format, overlap, segment_size, denoise):
  found_files = []
  logs = []
  logs.clear()

  extensions = (".mp3", ".wav", ".flac")

  for audio_files in os.listdir(path_input):
    if audio_files.endswith(extensions):
      found_files.append(audio_files)
  total_files = len(found_files)

  if total_files == 0:
    logs.append("No valid audio files.")
    yield "\n".join(logs)
  else:
    logs.append(f"{total_files} audio files found")
    found_files.sort()

    for audio_files in found_files:
      file_path = os.path.join(path_input, audio_files)
      prompt = ["audio-separator", file_path, "-m", f"{model}", f"--output_dir={path_output}", f"--output_format={output_format}", "--normalization=0.9", f"--mdx_overlap={overlap}", f"--mdx_segment_size={segment_size}"]

      if denoise:
        prompt.append("--mdx_enable_denoise")

      logs.append(f"Processing file: {audio_files}")
      yield "\n".join(logs)
      subprocess.run(prompt)
      logs.append(f"File: {audio_files} processed!")
      yield "\n".join(logs)


with gr.Blocks(theme="Blane187/fuchsia", title="🎵 UVR5 MDX 🎵") as app:
    gr.Markdown("<h1> 🎵 UVR MDX 🎵 </h1>")
    gr.Markdown("If you liked this HF Space you can give me a ❤️")
    gr.Markdown("Try UVR5 UI using Colab [here](https://colab.research.google.com/github/Eddycrack864/UVR5-UI/blob/main/UVR_UI.ipynb)")
    with gr.Tabs():       
        with gr.TabItem("MDX-NET"):
            with gr.Row():
                mdxnet_model = gr.Dropdown(
                    label = "Select the Model",
                    choices = mdxnet_models,
                    interactive = True
                )
            with gr.Row():
                mdxnet_output_format = gr.Dropdown(
                    label = "Select the Output Format",
                    choices = output_format,
                    interactive = True
                )
            with gr.Row():
                mdxnet_segment_size = gr.Slider(
                    minimum = 32,
                    maximum = 4000,
                    step = 32,
                    label = "Segment Size",
                    info = "Larger consumes more resources, but may give better results.",
                    value = 256,
                    interactive = True
                )
            with gr.Row():
                mdxnet_overlap = gr.Dropdown(
                        label = "Overlap",
                        choices = mdxnet_overlap_values,
                        value = mdxnet_overlap_values[0],
                        interactive = True
                )
                mdxnet_denoise = gr.Checkbox(
                    label = "Denoise",
                    info = "Enable denoising during separation.",
                    value = True,
                    interactive = True
                )
            with gr.Row():
                mdxnet_audio = gr.Audio(
                    label = "Input Audio",
                    type = "numpy",
                    interactive = True
                )
            with gr.Accordion("Separation by Link", open = False):
                with gr.Row():
                    mdxnet_link = gr.Textbox(
                    label = "Link",
                    placeholder = "Paste the link here",
                    interactive = True
                )
                with gr.Row():
                   gr.Markdown("You can paste the link to the video/audio from many sites, check the complete list [here](https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md)")
                with gr.Row():
                    mdxnet_download_button = gr.Button(
                    "Download!",
                    variant = "primary"
                )

            mdxnet_download_button.click(download_audio, [mdxnet_link], [mdxnet_audio])

            with gr.Accordion("Batch Separation", open = False):
                with gr.Row():
                    mdxnet_input_path = gr.Textbox(
                        label = "Input Path",
                        placeholder = "Place the input path here",
                        interactive = True
                    )
                    mdxnet_output_path = gr.Textbox(
                        label = "Output Path",
                        placeholder = "Place the output path here",
                        interactive = True
                    )
                with gr.Row():
                    mdxnet_bath_button = gr.Button("Separate!", variant = "primary")
                with gr.Row():
                    mdxnet_info = gr.Textbox(
                        label = "Output Information",
                        interactive = False
                    )

            mdxnet_bath_button.click(mdxnet_batch, [mdxnet_input_path, mdxnet_output_path, mdxnet_model, mdxnet_output_format, mdxnet_overlap, mdxnet_segment_size, mdxnet_denoise], [mdxnet_info])

            with gr.Row():
                mdxnet_button = gr.Button("Separate!", variant = "primary")
            with gr.Row():
                mdxnet_stem1 = gr.Audio(
                    show_download_button = True,
                    interactive = False,
                    label = "instrumental",
                    type = "filepath"
                )
                mdxnet_stem2 = gr.Audio(
                    show_download_button = True,
                    interactive = False,
                    label = "vocal",
                    type = "filepath"
                )

            mdxnet_button.click(mdxnet_separator, [mdxnet_audio, mdxnet_model, mdxnet_output_format, mdxnet_segment_size, mdxnet_overlap, mdxnet_denoise], [mdxnet_stem1, mdxnet_stem2])

        

        with gr.TabItem("Credits"):
           gr.Markdown(
              """
              UVR5 UI created by **[Eddycrack 864](https://github.com/Eddycrack864).** Join **[AI HUB](https://discord.gg/aihub)** community.
              * python-audio-separator by [beveradb](https://github.com/beveradb).
              * Special thanks to [Ilaria](https://github.com/TheStingerX) for hosting this space and help.
              * Thanks to [Mikus](https://github.com/cappuch) for the help with the code.
              * Thanks to [Nick088](https://huggingface.co/Nick088) for the help to fix roformers.
              * Thanks to [yt_dlp](https://github.com/yt-dlp/yt-dlp) devs.
              * Separation by link source code and improvements by [Blane187](https://huggingface.co/Blane187).

              You can donate to the original UVR5 project here:
              [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/uvr5)
              """
           )


app.launch(share=True, debug=True)