Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,9 +42,8 @@ os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
|
| 42 |
# ==========================================
|
| 43 |
# 4. INISIALISASI AI & MASTERING ENGINE
|
| 44 |
# ==========================================
|
| 45 |
-
print("Memuat Model AI DeepFilterNet ke memori
|
| 46 |
model, df_state, _ = init_df()
|
| 47 |
-
target_sr = df_state.sr()
|
| 48 |
|
| 49 |
# Engine Penjernih V2 (Studio Crisp)
|
| 50 |
v2_engine = Pedalboard([
|
|
@@ -66,10 +65,11 @@ v3_engine = Pedalboard([
|
|
| 66 |
# ==========================================
|
| 67 |
# 5. FUNGSI UTAMA PEMROSESAN AUDIO
|
| 68 |
# ==========================================
|
| 69 |
-
def process_audio(input_path, output_path, version):
|
| 70 |
try:
|
| 71 |
-
# Load audio pakai
|
| 72 |
-
|
|
|
|
| 73 |
wav_tensor = torch.from_numpy(wav_np)
|
| 74 |
if wav_np.ndim == 1:
|
| 75 |
wav_tensor = wav_tensor.unsqueeze(0)
|
|
@@ -86,14 +86,25 @@ def process_audio(input_path, output_path, version):
|
|
| 86 |
if version == "VERSI_1":
|
| 87 |
final_audio = audio_np
|
| 88 |
elif version == "VERSI_2":
|
| 89 |
-
final_audio = v2_engine(audio_np,
|
| 90 |
elif version == "VERSI_3":
|
| 91 |
-
final_audio = v3_engine(audio_np,
|
| 92 |
else:
|
| 93 |
-
final_audio = audio_np
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return True
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error memproses {input_path}: {e}")
|
|
@@ -105,7 +116,6 @@ def process_audio(input_path, output_path, version):
|
|
| 105 |
# ==========================================
|
| 106 |
@app.route('/')
|
| 107 |
def index():
|
| 108 |
-
# Menampilkan halaman web UI/UX
|
| 109 |
return render_template('index.html')
|
| 110 |
|
| 111 |
@app.route('/process', methods=['POST'])
|
|
@@ -113,9 +123,13 @@ def process_files():
|
|
| 113 |
files = request.files.getlist('files')
|
| 114 |
versions = json.loads(request.form.get('versions', '[]'))
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
download_links = []
|
| 117 |
|
| 118 |
-
# Bikin folder unik per request agar file antar user tidak tertukar
|
| 119 |
req_id = str(uuid.uuid4())[:8]
|
| 120 |
req_upload_dir = os.path.join(UPLOAD_FOLDER, req_id)
|
| 121 |
req_output_dir = os.path.join(OUTPUT_FOLDER, req_id)
|
|
@@ -136,43 +150,40 @@ def process_files():
|
|
| 136 |
out_zip_path = os.path.join(req_output_dir, out_zip_name)
|
| 137 |
|
| 138 |
with zipfile.ZipFile(out_zip_path, 'w') as out_zip:
|
| 139 |
-
# Telusuri semua file di dalam ZIP yang diekstrak
|
| 140 |
for root, _, extracted_files in os.walk(extract_dir):
|
| 141 |
for file in extracted_files:
|
| 142 |
if file.lower().endswith(('.wav', '.mp3', '.m4a', '.flac', '.ogg')):
|
| 143 |
in_file_path = os.path.join(root, file)
|
| 144 |
-
# Dapatkan path aslinya di dalam folder
|
| 145 |
rel_path = os.path.relpath(root, extract_dir)
|
| 146 |
base_name = os.path.splitext(file)[0]
|
| 147 |
|
| 148 |
-
# Proses masing-masing versi
|
| 149 |
for v in versions:
|
| 150 |
-
|
| 151 |
-
|
|
|
|
| 152 |
|
| 153 |
if success:
|
| 154 |
-
# Susun agar masuk ke folder VERSI di dalam hasil ZIP
|
| 155 |
-
# Contoh: VERSI_1/Nama_Mc/Audio1.wav
|
| 156 |
zip_folder = v.replace('_', ' ') if rel_path == '.' else f"{v.replace('_', ' ')}/{rel_path}"
|
| 157 |
-
|
|
|
|
| 158 |
|
| 159 |
-
# Tambahkan link download ZIP hasil
|
| 160 |
download_links.append({"filename": out_zip_name, "url": f"/download/{req_id}/{out_zip_name}"})
|
| 161 |
|
| 162 |
else:
|
| 163 |
-
# --- LOGIKA UNTUK INPUT AUDIO LANGSUNG
|
| 164 |
for file in files:
|
| 165 |
safe_name = secure_filename(file.filename)
|
| 166 |
-
base_name,
|
| 167 |
in_path = os.path.join(req_upload_dir, safe_name)
|
| 168 |
file.save(in_path)
|
| 169 |
|
| 170 |
for v in versions:
|
| 171 |
v_clean = v.replace('_', ' ')
|
| 172 |
-
|
|
|
|
| 173 |
out_path = os.path.join(req_output_dir, out_name)
|
| 174 |
|
| 175 |
-
success = process_audio(in_path, out_path, v)
|
| 176 |
if success:
|
| 177 |
download_links.append({"filename": out_name, "url": f"/download/{req_id}/{out_name}"})
|
| 178 |
|
|
@@ -180,10 +191,8 @@ def process_files():
|
|
| 180 |
|
| 181 |
@app.route('/download/<req_id>/<filename>')
|
| 182 |
def download_file(req_id, filename):
|
| 183 |
-
# Mengirim file hasil proses ke pengguna
|
| 184 |
directory = os.path.join(OUTPUT_FOLDER, req_id)
|
| 185 |
return send_from_directory(directory, filename, as_attachment=True)
|
| 186 |
|
| 187 |
if __name__ == '__main__':
|
| 188 |
-
# WAJIB host '0.0.0.0' dan port 7860 untuk Hugging Face Spaces!
|
| 189 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 42 |
# ==========================================
|
| 43 |
# 4. INISIALISASI AI & MASTERING ENGINE
|
| 44 |
# ==========================================
|
| 45 |
+
print("Memuat Model AI DeepFilterNet ke memori...")
|
| 46 |
model, df_state, _ = init_df()
|
|
|
|
| 47 |
|
| 48 |
# Engine Penjernih V2 (Studio Crisp)
|
| 49 |
v2_engine = Pedalboard([
|
|
|
|
| 65 |
# ==========================================
|
| 66 |
# 5. FUNGSI UTAMA PEMROSESAN AUDIO
|
| 67 |
# ==========================================
|
| 68 |
+
def process_audio(input_path, output_path, version, target_sr):
|
| 69 |
try:
|
| 70 |
+
# Load audio (selalu pakai sample rate bawaan model AI untuk proses reduksi)
|
| 71 |
+
model_sr = df_state.sr()
|
| 72 |
+
wav_np, _ = librosa.load(input_path, sr=model_sr, mono=False)
|
| 73 |
wav_tensor = torch.from_numpy(wav_np)
|
| 74 |
if wav_np.ndim == 1:
|
| 75 |
wav_tensor = wav_tensor.unsqueeze(0)
|
|
|
|
| 86 |
if version == "VERSI_1":
|
| 87 |
final_audio = audio_np
|
| 88 |
elif version == "VERSI_2":
|
| 89 |
+
final_audio = v2_engine(audio_np, model_sr)
|
| 90 |
elif version == "VERSI_3":
|
| 91 |
+
final_audio = v3_engine(audio_np, model_sr)
|
| 92 |
else:
|
| 93 |
+
final_audio = audio_np
|
| 94 |
|
| 95 |
+
# Resample jika user meminta sample rate spesifik dari UI
|
| 96 |
+
if int(target_sr) != model_sr:
|
| 97 |
+
# Convert ke format librosa (channels first) lalu resample
|
| 98 |
+
if final_audio.ndim > 1:
|
| 99 |
+
librosa_format = final_audio.T
|
| 100 |
+
else:
|
| 101 |
+
librosa_format = final_audio
|
| 102 |
+
resampled_audio = librosa.resample(librosa_format, orig_sr=model_sr, target_sr=int(target_sr))
|
| 103 |
+
# Kembalikan format untuk soundfile (channels last)
|
| 104 |
+
final_audio = resampled_audio.T if resampled_audio.ndim > 1 else resampled_audio
|
| 105 |
+
|
| 106 |
+
# Simpan audio
|
| 107 |
+
sf.write(output_path, final_audio, int(target_sr))
|
| 108 |
return True
|
| 109 |
except Exception as e:
|
| 110 |
print(f"Error memproses {input_path}: {e}")
|
|
|
|
| 116 |
# ==========================================
|
| 117 |
@app.route('/')
|
| 118 |
def index():
|
|
|
|
| 119 |
return render_template('index.html')
|
| 120 |
|
| 121 |
@app.route('/process', methods=['POST'])
|
|
|
|
| 123 |
files = request.files.getlist('files')
|
| 124 |
versions = json.loads(request.form.get('versions', '[]'))
|
| 125 |
|
| 126 |
+
# Ambil konfigurasi dari UI Frontend
|
| 127 |
+
out_format = request.form.get('outFormat', '.wav')
|
| 128 |
+
out_rate = request.form.get('outRate', '48000')
|
| 129 |
+
|
| 130 |
download_links = []
|
| 131 |
|
| 132 |
+
# Bikin folder unik per request agar file antar user tidak tertukar
|
| 133 |
req_id = str(uuid.uuid4())[:8]
|
| 134 |
req_upload_dir = os.path.join(UPLOAD_FOLDER, req_id)
|
| 135 |
req_output_dir = os.path.join(OUTPUT_FOLDER, req_id)
|
|
|
|
| 150 |
out_zip_path = os.path.join(req_output_dir, out_zip_name)
|
| 151 |
|
| 152 |
with zipfile.ZipFile(out_zip_path, 'w') as out_zip:
|
|
|
|
| 153 |
for root, _, extracted_files in os.walk(extract_dir):
|
| 154 |
for file in extracted_files:
|
| 155 |
if file.lower().endswith(('.wav', '.mp3', '.m4a', '.flac', '.ogg')):
|
| 156 |
in_file_path = os.path.join(root, file)
|
|
|
|
| 157 |
rel_path = os.path.relpath(root, extract_dir)
|
| 158 |
base_name = os.path.splitext(file)[0]
|
| 159 |
|
|
|
|
| 160 |
for v in versions:
|
| 161 |
+
# Terapkan format yang dipilih user dari UI (misal: .wav, .mp3, .flac)
|
| 162 |
+
temp_out = os.path.join(req_output_dir, f"temp_{v}_{file}{out_format}")
|
| 163 |
+
success = process_audio(in_file_path, temp_out, v, out_rate)
|
| 164 |
|
| 165 |
if success:
|
|
|
|
|
|
|
| 166 |
zip_folder = v.replace('_', ' ') if rel_path == '.' else f"{v.replace('_', ' ')}/{rel_path}"
|
| 167 |
+
# Nama di dalam zip pakai format baru yang dipilih user
|
| 168 |
+
out_zip.write(temp_out, arcname=f"{zip_folder}/{base_name}{out_format}")
|
| 169 |
|
|
|
|
| 170 |
download_links.append({"filename": out_zip_name, "url": f"/download/{req_id}/{out_zip_name}"})
|
| 171 |
|
| 172 |
else:
|
| 173 |
+
# --- LOGIKA UNTUK INPUT AUDIO LANGSUNG ---
|
| 174 |
for file in files:
|
| 175 |
safe_name = secure_filename(file.filename)
|
| 176 |
+
base_name, _ = os.path.splitext(safe_name)
|
| 177 |
in_path = os.path.join(req_upload_dir, safe_name)
|
| 178 |
file.save(in_path)
|
| 179 |
|
| 180 |
for v in versions:
|
| 181 |
v_clean = v.replace('_', ' ')
|
| 182 |
+
# Output pakai format pilihan user
|
| 183 |
+
out_name = f"{base_name}_{v_clean}{out_format}"
|
| 184 |
out_path = os.path.join(req_output_dir, out_name)
|
| 185 |
|
| 186 |
+
success = process_audio(in_path, out_path, v, out_rate)
|
| 187 |
if success:
|
| 188 |
download_links.append({"filename": out_name, "url": f"/download/{req_id}/{out_name}"})
|
| 189 |
|
|
|
|
| 191 |
|
| 192 |
@app.route('/download/<req_id>/<filename>')
|
| 193 |
def download_file(req_id, filename):
|
|
|
|
| 194 |
directory = os.path.join(OUTPUT_FOLDER, req_id)
|
| 195 |
return send_from_directory(directory, filename, as_attachment=True)
|
| 196 |
|
| 197 |
if __name__ == '__main__':
|
|
|
|
| 198 |
app.run(host='0.0.0.0', port=7860)
|