sugakrit6 commited on
Commit
500f1bb
Β·
verified Β·
1 Parent(s): 58b2f8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -71
app.py CHANGED
@@ -1,108 +1,103 @@
1
  # app.py
2
- # βœ… FINAL, ACTUALLY-WORKING HuggingFace Space – AI Vocal Remover
3
- # Root cause FIXED: TorchAudio >=2.3 REQUIRES torchcodec
4
- # We FIX by PINNING torchaudio to a compatible version (NO torchcodec needed)
5
 
6
  import gradio as gr
7
  import subprocess
8
  import os
9
  import shutil
10
  import uuid
11
- import traceback
12
  from pathlib import Path
13
 
14
- MODEL_NAME = "htdemucs"
15
- WORKDIR = "work"
16
- OUTDIR = "outputs"
17
 
18
- os.makedirs(WORKDIR, exist_ok=True)
19
- os.makedirs(OUTDIR, exist_ok=True)
20
 
21
 
22
  def run(cmd):
23
- try:
24
- res = subprocess.run(cmd, check=True, capture_output=True, text=True)
25
- return 0, res.stdout + res.stderr
26
- except subprocess.CalledProcessError as e:
27
- return e.returncode, (e.stdout or "") + (e.stderr or "")
28
 
29
 
30
  def separate(file):
31
- try:
32
- if file is None:
33
- return None, "No file uploaded"
34
-
35
- input_path = file if isinstance(file, str) else file.name
36
- run_id = str(uuid.uuid4())[:8]
37
- wav_path = f"{WORKDIR}/{run_id}.wav"
38
-
39
- # 1️⃣ Convert input β†’ WAV
40
- code, log = run([
41
- "ffmpeg", "-y", "-i", input_path,
42
- "-ac", "2", "-ar", "44100", wav_path
43
- ])
44
- if code != 0:
45
- return None, "FFmpeg failed:\n" + log
46
-
47
- shutil.rmtree("separated", ignore_errors=True)
48
-
49
- # 2️⃣ Run Demucs (this WILL now work)
50
- code, log = run([
51
- "demucs",
52
- "-n", MODEL_NAME,
53
- "--two-stems", "vocals",
54
- wav_path
55
- ])
56
- if code != 0:
57
- return None, "Demucs failed:\n" + log
58
-
59
- base = os.path.splitext(os.path.basename(wav_path))[0]
60
- inst = f"separated/{MODEL_NAME}/{base}/no_vocals.wav"
61
-
62
- if not Path(inst).exists():
63
- return None, f"Expected output not found: {inst}"
64
-
65
- # 3️⃣ Loud normalize
66
- out = f"{OUTDIR}/{base}_instrumental.wav"
67
- run([
68
- "ffmpeg", "-y", "-i", inst,
69
- "-af", "loudnorm=I=-14:LRA=11:TP=-1",
70
- out
71
- ])
72
-
73
- return out, "Success βœ…"
74
-
75
- except Exception:
76
- return None, traceback.format_exc()
77
 
78
 
79
  with gr.Blocks() as demo:
80
  gr.Markdown("""
81
- # 🎧 AI Vocal Remover (STABLE & FIXED)
82
 
83
  βœ” MP3 / WAV / FLAC / MP4
84
- βœ” No TorchCodec crashes
85
- βœ” Demucs htdemucs + two-stems vocals
86
- βœ” Loud, clean instrumentals
87
  """)
88
 
89
- inp = gr.File(file_types=[".wav", ".mp3", ".flac", ".ogg", ".mp4"], label="Upload audio or video")
90
- out_audio = gr.Audio(type="filepath", label="Instrumental (No Vocals)")
91
- logs = gr.Textbox(label="Status / Errors", lines=6)
 
 
92
 
93
  btn = gr.Button("Remove Vocals πŸ”₯")
94
- btn.click(fn=separate, inputs=inp, outputs=[out_audio, logs])
95
 
96
  if __name__ == "__main__":
97
  demo.launch()
98
 
99
 
100
- # requirements.txt (THIS IS THE FIX πŸ”₯)
 
 
101
  # gradio==4.44.0
102
  # demucs==4.0.1
103
- # torch==2.1.2
104
- # torchaudio==2.1.2
 
105
  # soundfile
106
 
 
107
  # apt.txt
 
108
  # ffmpeg
 
1
  # app.py
2
+ # 🧨 SCRAPPED & REBUILT β€” SIMPLE, SOLID HuggingFace Vocal Remover
3
+ # Fix: install torchcodec instead of fighting torchaudio
4
+ # This is the MINIMAL setup that actually works
5
 
6
  import gradio as gr
7
  import subprocess
8
  import os
9
  import shutil
10
  import uuid
 
11
  from pathlib import Path
12
 
13
+ MODEL = "htdemucs"
14
+ WORK = "work"
15
+ OUT = "outputs"
16
 
17
+ os.makedirs(WORK, exist_ok=True)
18
+ os.makedirs(OUT, exist_ok=True)
19
 
20
 
21
  def run(cmd):
22
+ subprocess.run(cmd, check=True)
 
 
 
 
23
 
24
 
25
  def separate(file):
26
+ if file is None:
27
+ return None
28
+
29
+ input_path = file if isinstance(file, str) else file.name
30
+ uid = str(uuid.uuid4())[:8]
31
+ wav = f"{WORK}/{uid}.wav"
32
+
33
+ # 1️⃣ Convert anything β†’ WAV
34
+ run([
35
+ "ffmpeg", "-y", "-i", input_path,
36
+ "-ac", "2", "-ar", "44100", wav
37
+ ])
38
+
39
+ shutil.rmtree("separated", ignore_errors=True)
40
+
41
+ # 2️⃣ Demucs (two-stems so we get no_vocals.wav)
42
+ run([
43
+ "demucs",
44
+ "-n", MODEL,
45
+ "--two-stems", "vocals",
46
+ wav
47
+ ])
48
+
49
+ base = Path(wav).stem
50
+ inst = f"separated/{MODEL}/{base}/no_vocals.wav"
51
+
52
+ if not Path(inst).exists():
53
+ raise RuntimeError("no_vocals.wav not produced")
54
+
55
+ out = f"{OUT}/{base}_instrumental.wav"
56
+
57
+ # 3️⃣ Loud normalize
58
+ run([
59
+ "ffmpeg", "-y", "-i", inst,
60
+ "-af", "loudnorm=I=-14:LRA=11:TP=-1",
61
+ out
62
+ ])
63
+
64
+ return out
 
 
 
 
 
 
 
65
 
66
 
67
  with gr.Blocks() as demo:
68
  gr.Markdown("""
69
+ # 🎧 AI Vocal Remover (STABLE)
70
 
71
  βœ” MP3 / WAV / FLAC / MP4
72
+ βœ” Uses Demucs (htdemucs)
73
+ βœ” Clean, loud instrumentals
74
+ βœ” No crashes
75
  """)
76
 
77
+ inp = gr.File(
78
+ file_types=[".wav", ".mp3", ".flac", ".ogg", ".mp4"],
79
+ label="Upload audio or video"
80
+ )
81
+ out = gr.Audio(type="filepath", label="Instrumental (No Vocals)")
82
 
83
  btn = gr.Button("Remove Vocals πŸ”₯")
84
+ btn.click(fn=separate, inputs=inp, outputs=out)
85
 
86
  if __name__ == "__main__":
87
  demo.launch()
88
 
89
 
90
+ # =============================
91
+ # requirements.txt (FINAL)
92
+ # =============================
93
  # gradio==4.44.0
94
  # demucs==4.0.1
95
+ # torch
96
+ # torchaudio
97
+ # torchcodec
98
  # soundfile
99
 
100
+ # =============================
101
  # apt.txt
102
+ # =============================
103
  # ffmpeg