nifty-coder commited on
Commit
0143af6
·
1 Parent(s): a95a00b

Deploy to HF Spaces

Browse files
Files changed (4) hide show
  1. Dockerfile +39 -0
  2. README.md +10 -0
  3. main.py +7 -2
  4. requirements.txt +1 -7
Dockerfile ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # Install system dependencies (ffmpeg is required for audio processing)
4
+ RUN apt-get update && apt-get install -y \
5
+ ffmpeg \
6
+ git \
7
+ build-essential \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ # Set up a new user named "user" with user ID 1000
11
+ RUN useradd -m -u 1000 user
12
+
13
+ # Switch to the "user" user
14
+ USER user
15
+
16
+ # Set home to the user's home directory
17
+ ENV HOME=/home/user \
18
+ PATH=/home/user/.local/bin:$PATH
19
+
20
+ # Set the working directory to the user's home directory
21
+ WORKDIR $HOME/app
22
+
23
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
24
+ COPY --chown=user:user . $HOME/app
25
+
26
+ # Install Python dependencies
27
+ RUN pip install --no-cache-dir --upgrade pip && \
28
+ pip install --no-cache-dir -r requirements.txt
29
+
30
+ # Create cache directory for demucs/temp files if main.py uses usage
31
+ # main.py uses tempfile.gettempdir(), but we might want a specific cache dir if configured
32
+ # Ensuring permissions are correct is handled by the user switch above
33
+
34
+ # Expose port 7860 for Hugging Face Spaces
35
+ EXPOSE 7860
36
+
37
+ # Run the application
38
+ # CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
39
+ CMD ["python", "main.py"]
README.md CHANGED
@@ -1,3 +1,13 @@
 
 
 
 
 
 
 
 
 
 
1
  # AI Music App Backend
2
 
3
  A FastAPI backend for processing audio files and YouTube videos to separate them into individual stems (drums, bass, vocals, etc.) using the Demucs AI model.
 
1
+ ---
2
+ title: StemSplit API
3
+ emoji: 🎵
4
+ colorFrom: purple
5
+ colorTo: pink
6
+ sdk: docker
7
+ pinned: false
8
+ app_port: 7860
9
+ ---
10
+
11
  # AI Music App Backend
12
 
13
  A FastAPI backend for processing audio files and YouTube videos to separate them into individual stems (drums, bass, vocals, etc.) using the Demucs AI model.
main.py CHANGED
@@ -215,7 +215,11 @@ def run_demucs_and_extract(input_path: str, tmpdir: str) -> str:
215
  output_dir = os.path.join(tmpdir, "stems")
216
  os.makedirs(output_dir, exist_ok=True)
217
 
218
- cmd = DEMUCS_CMD + ["-o", output_dir, input_path]
 
 
 
 
219
  logger.info(f"Running demucs: {' '.join(cmd)}")
220
  subprocess.run(cmd, check=True, capture_output=True, text=True)
221
 
@@ -355,4 +359,5 @@ def get_stem(key: str, filename: str):
355
 
356
  if __name__ == "__main__":
357
  import uvicorn
358
- uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
 
 
215
  output_dir = os.path.join(tmpdir, "stems")
216
  os.makedirs(output_dir, exist_ok=True)
217
 
218
+ # Memory optimization args:
219
+ # -n htdemucs: Use Hybrid Transformer model (efficient)
220
+ # --segment 4: Process 4 second chunks (default 10) to save RAM
221
+ # -j 2: Limit threads
222
+ cmd = DEMUCS_CMD + ["-n", "htdemucs", "--segment", "4", "-j", "2", "-o", output_dir, input_path]
223
  logger.info(f"Running demucs: {' '.join(cmd)}")
224
  subprocess.run(cmd, check=True, capture_output=True, text=True)
225
 
 
359
 
360
  if __name__ == "__main__":
361
  import uvicorn
362
+ # Default to 7860 for Hugging Face Spaces
363
+ uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
requirements.txt CHANGED
@@ -5,13 +5,7 @@ pydantic
5
  ffmpeg-python
6
  python-multipart
7
  soundfile
8
- yt-dlp
9
  firebase-admin
10
- torchcodec
11
  torch
12
  torchaudio
13
- pytubefix
14
-
15
- # NOTE: Demucs is installed separately via install.sh or build script
16
- # to avoid torchaudio version conflict (demucs requires <2.1, but only 2.6+ available)
17
- # See install.sh or deployment instructions in README
 
5
  ffmpeg-python
6
  python-multipart
7
  soundfile
 
8
  firebase-admin
 
9
  torch
10
  torchaudio
11
+ demucs