owiedotch commited on
Commit
1ddb890
1 Parent(s): d40dc15

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import demucs.separate
4
+ import shlex
5
+ import os
6
+ import spaces
7
+
8
+ # Check if CUDA is available
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+
11
+ # Define the inference function
12
+ @spaces.GPU
13
+ def inference(audio_file, model_name, two_stems, mp3, mp3_bitrate):
14
+ """
15
+ Performs inference using Demucs.
16
+
17
+ Args:
18
+ audio_file: The audio file to separate.
19
+ model_name: The name of the Demucs model to use.
20
+ two_stems: The name of the stem to separate (for two-stems mode).
21
+ mp3: Whether to save the output as MP3.
22
+ mp3_bitrate: The bitrate of the output MP3 file.
23
+
24
+ Returns:
25
+ A list of separated audio files.
26
+ """
27
+
28
+ # Construct the command line arguments
29
+ cmd = f"demucs -n {model_name} --clip-mode clamp --shifts=1"
30
+ if two_stems:
31
+ cmd += f" --two-stems={two_stems}"
32
+ if mp3:
33
+ cmd += f" --mp3 --mp3-bitrate={mp3_bitrate}"
34
+ cmd += f" {audio_file.name}"
35
+
36
+ # Run Demucs
37
+ demucs.separate.main(shlex.split(cmd))
38
+
39
+ # Get the output file paths
40
+ output_dir = os.path.join("separated", model_name, os.path.splitext(os.path.basename(audio_file.name))[0])
41
+ output_files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if os.path.isfile(os.path.join(output_dir, f))]
42
+
43
+ return output_files
44
+
45
+ # Define the Gradio interface
46
+ iface = gr.Interface(
47
+ fn=inference,
48
+ inputs=[
49
+ gr.Audio(source="upload", type="filepath"),
50
+ gr.Dropdown(["htdemucs", "htdemucs_ft", "htdemucs_6s", "hdemucs_mmi", "mdx", "mdx_extra", "mdx_q", "mdx_extra_q"], label="Model Name"),
51
+ gr.Dropdown(["vocals", "drums", "bass", "other"], label="Two Stems (Optional)", optional=True),
52
+ gr.Checkbox(label="Save as MP3"),
53
+ gr.Slider(128, 320, step=32, label="MP3 Bitrate", visible=False),
54
+ ],
55
+ outputs=gr.Files(),
56
+ title="Demucs Music Source Separation",
57
+ description="Separate vocals, drums, bass, and other instruments from your music using Demucs.",
58
+ )
59
+
60
+ # Launch the Gradio interface
61
+ iface.launch()