ray-006 commited on
Commit
11fa35c
·
verified ·
1 Parent(s): d50a13f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -45
app.py CHANGED
@@ -3,36 +3,26 @@ import gradio as gr
3
  from audio_separator.separator import Separator
4
  import tempfile
5
  import shutil
6
- from ftplib import FTP
7
- import uuid
8
-
9
- # --- CONFIGURATION (Update these with your Hostinger details) ---
10
- FTP_HOST = os.environ.get("FTP_HOST")
11
- FTP_USER = os.environ.get("FTP_USER")
12
- FTP_PASS = os.environ.get("FTP_PASS")
13
- FTP_DIR = "/basically/isolations"
14
- BASE_URL = "https://thinkstarter.online/basically/isolations"
15
- FTP_PORT = 21
16
- # ---------------------------------------------------------------
17
-
18
- def unique_filename(original):
19
- name, ext = os.path.splitext(original)
20
- return f"{name}_{uuid.uuid4().hex}{ext}"
21
-
22
- def upload_to_ftp(local_filepath):
23
- filename = unique_filename(os.path.basename(local_filepath))
24
-
25
- ftp = FTP(timeout=60)
26
- ftp.connect(FTP_HOST, FTP_PORT)
27
- ftp.login(FTP_USER, FTP_PASS)
28
- ftp.set_pasv(True)
29
- ftp.cwd(FTP_DIR)
30
-
31
- with open(local_filepath, "rb") as f:
32
- ftp.storbinary(f"STOR {filename}", f, blocksize=32768)
33
-
34
- ftp.quit()
35
- return f"{BASE_URL}/{filename}"
36
 
37
  def separate_audio(input_file, model_name):
38
  if input_file is None:
@@ -51,37 +41,31 @@ def separate_audio(input_file, model_name):
51
  output_files = separator.separate(input_file)
52
 
53
  stems = ["Vocals", "Drums", "Bass", "Other", "Guitar", "Piano"]
54
- results_urls = []
55
 
56
  for stem in stems:
57
- # Find the local file for this stem
58
  match = next(
59
  (os.path.join(temp_dir, f) for f in output_files if stem in f),
60
  None,
61
  )
62
-
63
- if match:
64
- # Upload to Hostinger and get URL [cite: 1, 2, 3]
65
- url = upload_to_ftp(match)
66
- results_urls.append(url)
67
- else:
68
- results_urls.append(None)
69
 
70
- # Removed delayed_delete call [cite: 4]
71
- return tuple(results_urls)
72
 
73
  except Exception as e:
74
  if os.path.exists(temp_dir):
75
  shutil.rmtree(temp_dir)
76
  raise RuntimeError(f"Separation failed: {e}") from e
77
 
 
78
  # ------------------ UI ------------------
79
 
80
  with gr.Blocks() as demo:
81
  gr.Markdown(
82
  """
83
- # 🎵 AI 6-Stem Audio Separator (FTP Upload)
84
- Split music into tracks and upload directly to **Hostinger**.
85
  """
86
  )
87
 
@@ -99,10 +83,10 @@ with gr.Blocks() as demo:
99
  label="Select Model",
100
  )
101
 
102
- separate_btn = gr.Button("🚀 Separate & Upload", variant="primary")
103
 
104
  with gr.Column(scale=2):
105
- gr.Markdown("### 🔊 Remote Results (Hostinger URLs)")
106
 
107
  with gr.Row():
108
  output_vocals = gr.Audio(label="🎤 Vocals", type="filepath")
@@ -129,6 +113,11 @@ with gr.Blocks() as demo:
129
  ],
130
  )
131
 
 
 
 
 
 
132
  if __name__ == "__main__":
133
  demo.launch(
134
  ssr_mode=False,
 
3
  from audio_separator.separator import Separator
4
  import tempfile
5
  import shutil
6
+ import threading
7
+ import time
8
+
9
+
10
+ def delayed_delete(path, delay=1800):
11
+ """Delete a file or directory after a delay (default: 30 minutes)."""
12
+
13
+ def command():
14
+ time.sleep(delay)
15
+ try:
16
+ if os.path.isdir(path):
17
+ shutil.rmtree(path)
18
+ elif os.path.isfile(path):
19
+ os.remove(path)
20
+ print(f"Cleanup: Deleted {path}")
21
+ except Exception as e:
22
+ print(f"Cleanup error for {path}: {e}")
23
+
24
+ threading.Thread(target=command, daemon=True).start()
25
+
 
 
 
 
 
 
 
 
 
 
26
 
27
  def separate_audio(input_file, model_name):
28
  if input_file is None:
 
41
  output_files = separator.separate(input_file)
42
 
43
  stems = ["Vocals", "Drums", "Bass", "Other", "Guitar", "Piano"]
44
+ results = []
45
 
46
  for stem in stems:
 
47
  match = next(
48
  (os.path.join(temp_dir, f) for f in output_files if stem in f),
49
  None,
50
  )
51
+ results.append(match)
 
 
 
 
 
 
52
 
53
+ delayed_delete(temp_dir, delay=1800)
54
+ return tuple(results)
55
 
56
  except Exception as e:
57
  if os.path.exists(temp_dir):
58
  shutil.rmtree(temp_dir)
59
  raise RuntimeError(f"Separation failed: {e}") from e
60
 
61
+
62
  # ------------------ UI ------------------
63
 
64
  with gr.Blocks() as demo:
65
  gr.Markdown(
66
  """
67
+ # 🎵 AI 6-Stem Audio Separator
68
+ Split your music into high-quality individual tracks using **Demucs 6s**.
69
  """
70
  )
71
 
 
83
  label="Select Model",
84
  )
85
 
86
+ separate_btn = gr.Button("🚀 Separate Stems", variant="primary")
87
 
88
  with gr.Column(scale=2):
89
+ gr.Markdown("### 🔊 Separation Results")
90
 
91
  with gr.Row():
92
  output_vocals = gr.Audio(label="🎤 Vocals", type="filepath")
 
113
  ],
114
  )
115
 
116
+ gr.Markdown(
117
+ "⚠️ **Note:** Processing may take 1–2 minutes depending on file length and Space hardware."
118
+ )
119
+
120
+
121
  if __name__ == "__main__":
122
  demo.launch(
123
  ssr_mode=False,