Jaman commited on
Commit
c5d94bc
·
verified ·
1 Parent(s): c8af8d1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import librosa
3
+ import soundfile as sf
4
+ import os
5
+ import time
6
+ import numpy as np
7
+
8
+ # Set the home directory path for moonarch
9
+ home_directory = os.getcwd()
10
+
11
+ # Function to ensure directory exists
12
+ def ensure_directory_exists(directory):
13
+ if not os.path.exists(directory):
14
+ os.makedirs(directory)
15
+
16
+ # Function to process the audio
17
+ def process_audio(file_path, choice):
18
+ # Ensure to navigate to the home directory first
19
+ os.chdir(home_directory)
20
+
21
+ file_name_without_ext = os.path.splitext(os.path.basename(file_path))[0]
22
+
23
+ if choice == 'Vocal Remove':
24
+ output_dir = os.path.join('vocal_remover', file_name_without_ext)
25
+ ensure_directory_exists(output_dir)
26
+ os.chdir('vocal_remover')
27
+ from moonarch_vocal_remover import VocalRemover
28
+ splitter = VocalRemover(file_path)
29
+ splitter.run()
30
+ st.write("Vocal removal process completed.")
31
+ os.chdir(home_directory)
32
+ return [os.path.join(output_dir, 'vocals.wav'), os.path.join(output_dir, 'accompaniment.wav')]
33
+
34
+
35
+
36
+
37
+
38
+ # Streamlit app
39
+ st.title('Monarch Music Analysis')
40
+
41
+ task_choice = st.selectbox('Choose a task', ['Vocal Remove', 'Basic Split'])
42
+
43
+ uploaded_file = st.file_uploader('Upload an audio file', type=['mp3', 'wav'])
44
+
45
+ if uploaded_file is not None:
46
+ # Save uploaded file to a temporary location
47
+ audio_file_path = os.path.join(home_directory, f'{uploaded_file.name}')
48
+ with open(audio_file_path, 'wb') as f:
49
+ f.write(uploaded_file.getbuffer())
50
+
51
+ y, sr = librosa.load(audio_file_path, sr=None) # sr=None to use the native sample rate
52
+
53
+ if st.button('Start'):
54
+ progress_bar = st.progress(0)
55
+
56
+ # Simulate progress bar for processing
57
+ for percent_complete in range(100):
58
+ time.sleep(0.01)
59
+ progress_bar.progress(percent_complete + 1)
60
+
61
+ output_files = process_audio(audio_file_path, task_choice)
62
+
63
+ st.success('Audio processed successfully!')
64
+
65
+ for file in output_files:
66
+ file_name = os.path.basename(file)
67
+ if 'vocals.wav' in file:
68
+ st.write('Vocal')
69
+ elif 'accompaniment.wav' in file:
70
+ st.write('Music')
71
+
72
+
73
+ st.audio(file)
74
+ with open(file, 'rb') as f:
75
+ new_file_name = 'music.wav' if file_name == 'accompaniment.wav' else ('strings_pads.wav' if file_name == 'other.wav' else file_name)
76
+ st.download_button(label=f'Download {new_file_name}', data=f, file_name=new_file_name)