vskode commited on
Commit ·
ec99a0c
1
Parent(s): bb948a4
gui changes for huggingface deployment
Browse files- acodet/annotate.py +13 -7
- acodet/front_end/st_annotate.py +11 -11
- acodet/front_end/utils.py +1 -1
- acodet/funcs.py +13 -5
- streamlit_app.py +1 -1
acodet/annotate.py
CHANGED
|
@@ -74,9 +74,7 @@ class MetaData:
|
|
| 74 |
Amount of time that prediction took, by default "not calculated"
|
| 75 |
"""
|
| 76 |
self.df.loc[f_ind, self.f_dt] = str(get_dt_filename(file).date())
|
| 77 |
-
self.df.loc[f_ind, self.filename] =
|
| 78 |
-
relativ_path
|
| 79 |
-
)
|
| 80 |
# TODO relative_path muss noch dauerhaft geändert werden
|
| 81 |
self.df.loc[f_ind, self.n_pred_col] = len(annot)
|
| 82 |
df_clean = remove_str_flags_from_predictions(annot)
|
|
@@ -95,10 +93,18 @@ class MetaData:
|
|
| 95 |
.joinpath(timestamp_foldername)
|
| 96 |
.joinpath("stats.csv")
|
| 97 |
)
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
def run_annotation(train_date=None, **kwargs):
|
| 101 |
-
files = get_files(location=conf.SOUND_FILES_SOURCE, search_str="**/*")
|
|
|
|
| 102 |
if not "timestamp_folder" in conf.session:
|
| 103 |
timestamp_foldername = time.strftime(
|
| 104 |
"%Y-%m-%d_%H-%M-%S", time.gmtime()
|
|
@@ -143,8 +149,8 @@ def run_annotation(train_date=None, **kwargs):
|
|
| 143 |
|
| 144 |
st.session_state.progbar1 = 0
|
| 145 |
for i, file in enumerate(files):
|
| 146 |
-
if file.is_dir():
|
| 147 |
-
|
| 148 |
|
| 149 |
if conf.STREAMLIT:
|
| 150 |
import streamlit as st
|
|
|
|
| 74 |
Amount of time that prediction took, by default "not calculated"
|
| 75 |
"""
|
| 76 |
self.df.loc[f_ind, self.f_dt] = str(get_dt_filename(file).date())
|
| 77 |
+
self.df.loc[f_ind, self.filename] = file.name
|
|
|
|
|
|
|
| 78 |
# TODO relative_path muss noch dauerhaft geändert werden
|
| 79 |
self.df.loc[f_ind, self.n_pred_col] = len(annot)
|
| 80 |
df_clean = remove_str_flags_from_predictions(annot)
|
|
|
|
| 93 |
.joinpath(timestamp_foldername)
|
| 94 |
.joinpath("stats.csv")
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
def get_files_for_huggingface_deployment():
|
| 98 |
+
import streamlit as st
|
| 99 |
+
files = st.file_uploader("Choose your sound files (make sure they a time stamp of some format)",
|
| 100 |
+
accept_multiple_files=True)
|
| 101 |
+
if not files:
|
| 102 |
+
st.stop()
|
| 103 |
+
return files
|
| 104 |
|
| 105 |
def run_annotation(train_date=None, **kwargs):
|
| 106 |
+
# files = get_files(location=conf.SOUND_FILES_SOURCE, search_str="**/*")
|
| 107 |
+
files = get_files_for_huggingface_deployment()
|
| 108 |
if not "timestamp_folder" in conf.session:
|
| 109 |
timestamp_foldername = time.strftime(
|
| 110 |
"%Y-%m-%d_%H-%M-%S", time.gmtime()
|
|
|
|
| 149 |
|
| 150 |
st.session_state.progbar1 = 0
|
| 151 |
for i, file in enumerate(files):
|
| 152 |
+
# if file.is_dir():
|
| 153 |
+
# continue
|
| 154 |
|
| 155 |
if conf.STREAMLIT:
|
| 156 |
import streamlit as st
|
acodet/front_end/st_annotate.py
CHANGED
|
@@ -115,8 +115,8 @@ class PresetInterfaceSettings:
|
|
| 115 |
# "tests/test_files",
|
| 116 |
# help=help_strings.ENTER_PATH,
|
| 117 |
# )
|
| 118 |
-
files = st.file_uploader("Choose your sound files (make sure they a time stamp of some format)",
|
| 119 |
-
|
| 120 |
# also hier kommen die files als so file objekte rein
|
| 121 |
# ich muss irgendwie festlegen dass sie immer am selben ordner landen
|
| 122 |
# und das er sich die ganze verzeichnis check sache spart. ich kann die
|
|
@@ -124,16 +124,16 @@ class PresetInterfaceSettings:
|
|
| 124 |
# oder ich lad sie erst da hoch wo sie gebraucht werden
|
| 125 |
# oder ich speicher sie hier erstmal ab. ist dann bisschen doppelt gemoppelt
|
| 126 |
# aber dafuer muss sich die struktur so wenig wie moeglich aendern
|
| 127 |
-
import librosa as lb
|
| 128 |
-
import soundfile as sf
|
| 129 |
-
self.config["sound_files_source"] = 'user_audio_uploads'
|
| 130 |
|
| 131 |
-
if len(files) > 0:
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
self.config["thresh"] = utils.validate_float(
|
| 139 |
utils.user_input(
|
|
|
|
| 115 |
# "tests/test_files",
|
| 116 |
# help=help_strings.ENTER_PATH,
|
| 117 |
# )
|
| 118 |
+
# files = st.file_uploader("Choose your sound files (make sure they a time stamp of some format)",
|
| 119 |
+
# accept_multiple_files=True)
|
| 120 |
# also hier kommen die files als so file objekte rein
|
| 121 |
# ich muss irgendwie festlegen dass sie immer am selben ordner landen
|
| 122 |
# und das er sich die ganze verzeichnis check sache spart. ich kann die
|
|
|
|
| 124 |
# oder ich lad sie erst da hoch wo sie gebraucht werden
|
| 125 |
# oder ich speicher sie hier erstmal ab. ist dann bisschen doppelt gemoppelt
|
| 126 |
# aber dafuer muss sich die struktur so wenig wie moeglich aendern
|
| 127 |
+
# import librosa as lb
|
| 128 |
+
# import soundfile as sf
|
| 129 |
+
# self.config["sound_files_source"] = 'user_audio_uploads'
|
| 130 |
|
| 131 |
+
# if len(files) > 0:
|
| 132 |
+
# # if len(list(Path(self.config["sound_files_source"]).iterdir())) == 0:
|
| 133 |
+
# for ind, file in enumerate(files):
|
| 134 |
+
# st.progress(ind/len(files), 'Checking files...')
|
| 135 |
+
# audio, sr = lb.load(file)
|
| 136 |
+
# sf.write('user_audio_uploads/' + file.name, audio, sr)
|
| 137 |
|
| 138 |
self.config["thresh"] = utils.validate_float(
|
| 139 |
utils.user_input(
|
acodet/front_end/utils.py
CHANGED
|
@@ -125,7 +125,7 @@ def make_nested_btns_false_on_click(btn_id):
|
|
| 125 |
def prepare_run():
|
| 126 |
if st.session_state.run_option == 1:
|
| 127 |
st.markdown("""---""")
|
| 128 |
-
st.markdown("## Computation
|
| 129 |
if st.session_state.preset_option in [0, 1]:
|
| 130 |
kwargs = {
|
| 131 |
"callbacks": TFPredictProgressBar,
|
|
|
|
| 125 |
def prepare_run():
|
| 126 |
if st.session_state.run_option == 1:
|
| 127 |
st.markdown("""---""")
|
| 128 |
+
st.markdown("## Computation will start once files are uploaded, please wait.")
|
| 129 |
if st.session_state.preset_option in [0, 1]:
|
| 130 |
kwargs = {
|
| 131 |
"callbacks": TFPredictProgressBar,
|
acodet/funcs.py
CHANGED
|
@@ -51,8 +51,11 @@ def get_annots_for_file(annots: pd.DataFrame, file: str) -> pd.DataFrame:
|
|
| 51 |
|
| 52 |
|
| 53 |
def get_dt_filename(file):
|
|
|
|
| 54 |
if isinstance(file, Path):
|
| 55 |
stem = file.stem
|
|
|
|
|
|
|
| 56 |
else:
|
| 57 |
stem = file
|
| 58 |
|
|
@@ -126,6 +129,7 @@ def load_audio(file, channel=0, **kwargs) -> np.ndarray:
|
|
| 126 |
audio_flat: np.ndarray
|
| 127 |
audio array
|
| 128 |
"""
|
|
|
|
| 129 |
try:
|
| 130 |
if conf.DOWNSAMPLE_SR and conf.SR != conf.DOWNSAMPLE_SR:
|
| 131 |
with open(file, "rb") as f:
|
|
@@ -138,6 +142,10 @@ def load_audio(file, channel=0, **kwargs) -> np.ndarray:
|
|
| 138 |
audio_flat = lb.resample(
|
| 139 |
audio_flat, orig_sr=conf.DOWNSAMPLE_SR, target_sr=conf.SR
|
| 140 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
else:
|
| 142 |
with open(file, "rb") as f:
|
| 143 |
audio_flat, _ = lb.load(f, sr=conf.SR, mono=False, **kwargs)
|
|
@@ -718,11 +726,11 @@ def gen_annotations(
|
|
| 718 |
date time string foldername corresponding to the time the annotations were
|
| 719 |
computed
|
| 720 |
"""
|
| 721 |
-
parent_dirs = manage_dir_structure(file)
|
| 722 |
|
| 723 |
-
channel = get_channel(get_top_dir(parent_dirs))
|
| 724 |
|
| 725 |
-
audio = load_audio(file
|
| 726 |
if audio is None:
|
| 727 |
raise ImportError(
|
| 728 |
f"The audio file `{str(file)}` cannot be loaded. Check if file has "
|
|
@@ -737,11 +745,11 @@ def gen_annotations(
|
|
| 737 |
Path(conf.GEN_ANNOTS_DIR)
|
| 738 |
.joinpath(timestamp_foldername)
|
| 739 |
.joinpath(conf.THRESH_LABEL)
|
| 740 |
-
.joinpath(
|
| 741 |
)
|
| 742 |
save_path.mkdir(exist_ok=True, parents=True)
|
| 743 |
annotation_df.to_csv(
|
| 744 |
-
save_path.joinpath(f"{file.stem}_annot_{mod_label}.txt"), sep="\t"
|
| 745 |
)
|
| 746 |
|
| 747 |
return annotation_df
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def get_dt_filename(file):
|
| 54 |
+
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
| 55 |
if isinstance(file, Path):
|
| 56 |
stem = file.stem
|
| 57 |
+
elif isinstance(file, UploadedFile):
|
| 58 |
+
stem = Path(file.name).stem
|
| 59 |
else:
|
| 60 |
stem = file
|
| 61 |
|
|
|
|
| 129 |
audio_flat: np.ndarray
|
| 130 |
audio array
|
| 131 |
"""
|
| 132 |
+
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
| 133 |
try:
|
| 134 |
if conf.DOWNSAMPLE_SR and conf.SR != conf.DOWNSAMPLE_SR:
|
| 135 |
with open(file, "rb") as f:
|
|
|
|
| 142 |
audio_flat = lb.resample(
|
| 143 |
audio_flat, orig_sr=conf.DOWNSAMPLE_SR, target_sr=conf.SR
|
| 144 |
)
|
| 145 |
+
elif isinstance(file, UploadedFile):
|
| 146 |
+
audio_flat, _ = lb.load(file, sr=conf.SR, mono=False, **kwargs)
|
| 147 |
+
if len(audio_flat.shape) > 1:
|
| 148 |
+
audio_flat = audio_flat[channel]
|
| 149 |
else:
|
| 150 |
with open(file, "rb") as f:
|
| 151 |
audio_flat, _ = lb.load(f, sr=conf.SR, mono=False, **kwargs)
|
|
|
|
| 726 |
date time string foldername corresponding to the time the annotations were
|
| 727 |
computed
|
| 728 |
"""
|
| 729 |
+
# parent_dirs = manage_dir_structure(file)
|
| 730 |
|
| 731 |
+
# channel = get_channel(get_top_dir(parent_dirs))
|
| 732 |
|
| 733 |
+
audio = load_audio(file)
|
| 734 |
if audio is None:
|
| 735 |
raise ImportError(
|
| 736 |
f"The audio file `{str(file)}` cannot be loaded. Check if file has "
|
|
|
|
| 745 |
Path(conf.GEN_ANNOTS_DIR)
|
| 746 |
.joinpath(timestamp_foldername)
|
| 747 |
.joinpath(conf.THRESH_LABEL)
|
| 748 |
+
.joinpath('UploadedFiles')
|
| 749 |
)
|
| 750 |
save_path.mkdir(exist_ok=True, parents=True)
|
| 751 |
annotation_df.to_csv(
|
| 752 |
+
save_path.joinpath(f"{Path(file.name).stem}_annot_{mod_label}.txt"), sep="\t"
|
| 753 |
)
|
| 754 |
|
| 755 |
return annotation_df
|
streamlit_app.py
CHANGED
|
@@ -31,7 +31,7 @@ def select_preset():
|
|
| 31 |
|
| 32 |
|
| 33 |
def run_computions(**kwargs):
|
| 34 |
-
utils.next_button(id=4, text="
|
| 35 |
if st.session_state.b4:
|
| 36 |
display_not_implemented_text()
|
| 37 |
kwargs = utils.prepare_run()
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
def run_computions(**kwargs):
|
| 34 |
+
utils.next_button(id=4, text="Next step")
|
| 35 |
if st.session_state.b4:
|
| 36 |
display_not_implemented_text()
|
| 37 |
kwargs = utils.prepare_run()
|