This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
forked from pablodz/DNP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
102 lines (73 loc) · 2.88 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import streamlit
import os
import subprocess
import streamlit as st
import librosa
import librosa.display
import matplotlib
import matplotlib.pyplot as plt
max_length = 120 # seconds audio file
command_inference = 'python3 DNP.py --run_name demo --noisy_file input/input.wav --samples_dir samples --save_every 1000 --num_iter 5000 --LR 0.001'
def clean_temp():
"""
Remove all files in specific paths
"""
paths_to_remove = ['input/',
'output/']
try:
for path in paths_to_remove:
for f in os.listdir(path):
if not 'README' in f:
os.remove(os.path.join(path, f))
except Exception as e:
print(e)
if __name__ == '__main__':
clean_temp() # Clean temporal files on each upload
# General description
st.title("DNP")
st.text("Incompleted information about DNP...")
# Upload file
st.subheader("- Choose an audio file")
uploaded_file = st.file_uploader("Choose an audio file", type=[
'wav'])
if uploaded_file is not None: # File > 0 bytes
file_details = {"FileName": uploaded_file.name,
"FileType": uploaded_file.type,
"FileSize": uploaded_file.size}
st.write(file_details)
#######################
# WAV UPLOADED FILE
#######################
if file_details['FileType'] == 'audio/wav':
with open('input/input.wav', 'wb') as f:
f.write(uploaded_file.getbuffer())
st.subheader("Input audio:")
audio_file = open('input/input.wav', 'rb')
audio_bytes = audio_file.read()
st.audio(audio_bytes, format='audio/wav')
y, sr = librosa.load('input/input.wav')
# Tempo and beat
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
st.write('Estimated tempo: {:.2f} beats per minute'.format(tempo))
# Waveplot
fig, ax = plt.subplots(nrows=1, sharex=True, sharey=True)
fig.set_figheight(4)
fig.set_figwidth(16)
librosa.display.waveplot(y, sr=sr)
ax.set(title='Input audio')
st.pyplot(fig)
# Chroma
hop_length = 1024
x_1_chroma = librosa.feature.chroma_cqt(y=y, sr=sr,hop_length=hop_length)
fig, ax = plt.subplots(nrows=1, sharey=True)
fig.set_figheight(12)
fig.set_figwidth(12)
img = librosa.display.specshow(x_1_chroma, x_axis='time',
y_axis='chroma',
hop_length=hop_length, ax=ax)
ax.set(title='Chroma extraction')
fig.colorbar(img, ax=ax)
st.pyplot(fig)
# Inference
result = os.popen(command_inference).read()
st.text(result)