Jaman commited on
Commit
6b9f6b3
·
verified ·
1 Parent(s): cf358ff

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 == 'Basic Split':
24
+ output_dir = os.path.join('basic_splits', file_name_without_ext)
25
+ ensure_directory_exists(output_dir)
26
+ os.chdir('basic_splits')
27
+ from moonarch_basic import BasicSplitter
28
+ splitter = BasicSplitter(file_path)
29
+ splitter.run()
30
+ st.write("Basic split process completed.")
31
+ os.chdir(home_directory)
32
+ return [
33
+ os.path.join('basic_splits', file_name_without_ext, 'vocals.wav'),
34
+ os.path.join('basic_splits', file_name_without_ext, 'other.wav'),
35
+ os.path.join('basic_splits', file_name_without_ext, 'bass.wav'),
36
+ os.path.join('basic_splits', file_name_without_ext, 'drums.wav')
37
+ ]
38
+
39
+ # Streamlit app
40
+ st.title('Monarch Music Analysis: Basic Splitter')
41
+
42
+ task_choice = st.selectbox('Choose a task', ['Basic Split'])
43
+
44
+ uploaded_file = st.file_uploader('Upload an audio file', type=['mp3', 'wav'])
45
+
46
+ if uploaded_file is not None:
47
+ # Save uploaded file to a temporary location
48
+ audio_file_path = os.path.join(home_directory, f'{uploaded_file.name}')
49
+ with open(audio_file_path, 'wb') as f:
50
+ f.write(uploaded_file.getbuffer())
51
+
52
+ y, sr = librosa.load(audio_file_path, sr=None) # sr=None to use the native sample rate
53
+
54
+ if st.button('Start'):
55
+ progress_bar = st.progress(0)
56
+
57
+ # Simulate progress bar for processing
58
+ for percent_complete in range(100):
59
+ time.sleep(0.01)
60
+ progress_bar.progress(percent_complete + 1)
61
+
62
+ output_files = process_audio(audio_file_path, task_choice)
63
+
64
+ st.success('Audio processed successfully!')
65
+
66
+ for file in output_files:
67
+ file_name = os.path.basename(file)
68
+ if 'vocals.wav' in file:
69
+ st.write('Vocal')
70
+ elif 'other.wav' in file:
71
+ st.write('Music')
72
+ elif 'bass.wav' in file:
73
+ st.write('Bass')
74
+ elif 'drums.wav' in file:
75
+ st.write('Drums')
76
+ elif 'piano.wav' in file:
77
+ st.write('Piano')
78
+ elif 'strings_or_pads.wav' in file:
79
+ st.write('Strings or Pads')
80
+
81
+ st.audio(file)
82
+ with open(file, 'rb') as f:
83
+ new_file_name = 'music.wav' if file_name == 'other.wav' else file_name
84
+ st.download_button(label=f'Download {new_file_name}', data=f, file_name=new_file_name)