Skip to content

Commit

Permalink
feat: 設定下載上傳機制(#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keycatowo committed Apr 9, 2023
1 parent 9a17a17 commit bba5bda
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 9 deletions.
1 change: 1 addition & 0 deletions home.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
st.session_state.first_run = True
st.session_state["use_segment"] = False
st.session_state["use_plotly"] = False
st.session_state["file_name"] = ""

st.session_state["0-file"] = {
}
Expand Down
11 changes: 9 additions & 2 deletions pages/1-Basic_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import librosa
import pandas as pd
from src.st_helper import convert_df, show_readme, get_shift
from src.st_helper import convert_df, get_shift, update_sessions
from src.basic_info import plot_waveform, signal_RMS_analysis, plot_spectrogram


Expand All @@ -23,6 +23,7 @@
st.audio(file, format="audio/ogg")
st.subheader("File information")
st.write(f"File name: `{file.name}`", )
st.session_state["file_name"] = file.name
st.write(f"File type: `{file.type}`")
st.write(f"File size: `{file.size}`")

Expand All @@ -35,8 +36,13 @@
start_time = 0
end_time = duration

#%% 片段模式
#%% 更新session
update_sessions()

#%%
if file is not None:

# 片段模式
use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment")
st.session_state["use_segment"] = use_segment

Expand Down Expand Up @@ -71,6 +77,7 @@
st.write(f"Selected segment: `{start_time}` ~ `{end_time}`, duration: `{end_time-start_time}`")
st.audio(y_sub, format="audio/ogg", sample_rate=sr)




#%% 功能分頁
Expand Down
6 changes: 5 additions & 1 deletion pages/2-Pitch_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import librosa
import pandas as pd
import seaborn as sns
from src.st_helper import convert_df, show_readme, get_shift
from src.st_helper import convert_df, get_shift, update_sessions
from src.pitch_estimation import (
plot_mel_spectrogram,
plot_constant_q_transform,
Expand All @@ -29,6 +29,7 @@
st.audio(file, format="audio/ogg")
st.subheader("File information")
st.write(f"File name: `{file.name}`", )
st.session_state["file_name"] = file.name
st.write(f"File type: `{file.type}`")
st.write(f"File size: `{file.size}`")

Expand All @@ -40,6 +41,9 @@
y_all = y
start_time = 0
end_time = duration

#%% 更新session
update_sessions()

#%% 片段模式
if file is not None:
Expand Down
7 changes: 5 additions & 2 deletions pages/3-Time_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import librosa
import pandas as pd
from src.beat_track import onsets_detection, plot_onset_strength, beat_analysis, predominant_local_pulse, static_tempo_estimation, plot_tempogram, onset_click_plot, beat_plot, plot_bpm
from src.st_helper import convert_df, show_readme, get_shift
from src.st_helper import convert_df, get_shift, update_sessions
import numpy as np

st.title('Time Analysis')
Expand All @@ -23,6 +23,7 @@
st.audio(file, format="audio/ogg")
st.subheader("File information")
st.write(f"File name: `{file.name}`", )
st.session_state["file_name"] = file.name
st.write(f"File type: `{file.type}`")
st.write(f"File size: `{file.size}`")

Expand All @@ -34,7 +35,9 @@
y_all = y
start_time = 0
end_time = duration


#%% 更新session
update_sessions()
#%% 片段模式
if file is not None:
use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment")
Expand Down
6 changes: 5 additions & 1 deletion pages/4-Chord_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import librosa
import pandas as pd
import seaborn as sns
from src.st_helper import convert_df, show_readme, get_shift
from src.st_helper import convert_df, get_shift, update_sessions
from src.chord_recognition import (
plot_chord_recognition,
plot_binary_template_chord_recognition,
Expand All @@ -31,6 +31,7 @@
st.audio(file, format="audio/ogg")
st.subheader("File information")
st.write(f"File name: `{file.name}`", )
st.session_state["file_name"] = file.name
st.write(f"File type: `{file.type}`")
st.write(f"File size: `{file.size}`")

Expand All @@ -43,6 +44,9 @@
start_time = 0
end_time = duration

#%% 更新session
update_sessions()

#%% 片段模式
if file is not None:
use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment")
Expand Down
6 changes: 5 additions & 1 deletion pages/5-Structure_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import librosa
import pandas as pd
from src.st_helper import convert_df, show_readme
from src.st_helper import convert_df, get_shift, update_sessions
from src.structure_analysis import (
plot_self_similarity
)
Expand All @@ -24,6 +24,7 @@
st.audio(file, format="audio/ogg")
st.subheader("File information")
st.write(f"File name: `{file.name}`", )
st.session_state["file_name"] = file.name
st.write(f"File type: `{file.type}`")
st.write(f"File size: `{file.size}`")

Expand All @@ -36,6 +37,9 @@
start_time = 0
end_time = duration

#%% 更新session
update_sessions()

#%% 片段模式
if file is not None:
use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment")
Expand Down
6 changes: 5 additions & 1 deletion pages/6-Timbre_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import librosa
import pandas as pd
from src.st_helper import convert_df, show_readme, get_shift
from src.st_helper import convert_df, get_shift, update_sessions
from src.timbre_analysis import (
spectral_centroid_analysis,
rolloff_frequency_analysis,
Expand All @@ -27,6 +27,7 @@
st.audio(file, format="audio/ogg")
st.subheader("File information")
st.write(f"File name: `{file.name}`", )
st.session_state["file_name"] = file.name
st.write(f"File type: `{file.type}`")
st.write(f"File size: `{file.size}`")

Expand All @@ -39,6 +40,9 @@
start_time = 0
end_time = duration

#%% 更新session
update_sessions()

#%% 片段模式
if file is not None:
use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment")
Expand Down
20 changes: 20 additions & 0 deletions pages/999-dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,23 @@


st.session_state["use_plotly"] = st.checkbox("Use plotly", value=st.session_state["use_plotly"])
st.session_state["debug"] = st.checkbox("Debug", value=st.session_state["debug"])



with st.expander("Session state"):
st.write(type(st.session_state))
st.write(st.session_state)
session_state = dict(st.session_state)
st.write(type(session_state))
st.write(session_state)


import pickle




# from src.st_helper import download_upload_settings

# download_upload_settings()
28 changes: 27 additions & 1 deletion src/st_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import streamlit as st
import pickle

@st.cache_data
def convert_df(df):
Expand Down Expand Up @@ -30,4 +31,29 @@ def get_shift(start_time, end_time):

shift_array = np.round(shift_array, 1)
return start_time, shift_array



def update_sessions():
"""
Update the session state.
Download/Upload button
"""
if st.session_state["file_name"]:

with st.expander("Config Download/Upload"):

col1, col2 = st.columns([2, 6])

col1.download_button(label="Download Current Settings",
data=pickle.dumps(dict(st.session_state)),
file_name=st.session_state["file_name"]+"_config.pkl",
help="Click to Download Current Settings")

session_file = col2.file_uploader(label="Upload Previous Settings",
help="Upload a previously saved settings file"
)

if session_file is not None:
new_session = pickle.loads(session_file.read())
st.write(new_session)
st.session_state.update(new_session)

0 comments on commit bba5bda

Please sign in to comment.