Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -243,31 +243,44 @@ def stream_predict(chunk, state):
|
|
| 243 |
def add_label_cb(label):
|
| 244 |
label = (label or "").strip()
|
| 245 |
if not label:
|
| 246 |
-
return gr.update(),
|
| 247 |
if label not in LABELS:
|
| 248 |
LABELS.append(label)
|
| 249 |
-
return gr.update(choices=LABELS, value=label),
|
| 250 |
|
| 251 |
def add_sample_cb(audio, label):
|
| 252 |
label = (label or "").strip()
|
| 253 |
if label not in LABELS:
|
| 254 |
-
return
|
| 255 |
if audio is None:
|
| 256 |
-
return
|
| 257 |
|
| 258 |
audio_n = normalize_audio_tuple(audio)
|
| 259 |
U = audio_to_sequence(audio_n)
|
| 260 |
if U is None or len(U) < 5:
|
| 261 |
-
return
|
| 262 |
|
| 263 |
DATA.append({"audio": audio_n, "U": U, "label": label})
|
| 264 |
-
return
|
| 265 |
|
| 266 |
def undo_last_cb():
|
| 267 |
if len(DATA) == 0:
|
| 268 |
-
return
|
| 269 |
-
|
| 270 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
def clear_rec_cb():
|
| 273 |
return gr.update(value=None)
|
|
@@ -294,20 +307,17 @@ def apply_relabel_cb(table, new_label):
|
|
| 294 |
def relabel_selected_cb(selected_idx, new_label):
|
| 295 |
new_label = (new_label or "").strip()
|
| 296 |
if selected_idx is None or selected_idx < 0 or selected_idx >= len(DATA):
|
| 297 |
-
return
|
| 298 |
if new_label not in LABELS:
|
| 299 |
-
return
|
| 300 |
-
old = DATA[selected_idx]["label"]
|
| 301 |
DATA[selected_idx]["label"] = new_label
|
| 302 |
-
return
|
| 303 |
|
| 304 |
def delete_selected_cb(selected_idx):
|
| 305 |
if selected_idx is None or selected_idx < 0 or selected_idx >= len(DATA):
|
| 306 |
-
return
|
| 307 |
-
lab = DATA[selected_idx]["label"]
|
| 308 |
DATA.pop(selected_idx)
|
| 309 |
-
|
| 310 |
-
return f"delete idx={selected_idx} ({lab})", dataset_stats_text(), dataset_table(), None, gr.update(value=None)
|
| 311 |
|
| 312 |
# Compact UI — Copy.py風フェミニン・パステルデザイン + 写真風ブルー→グリーングラデーション
|
| 313 |
HEAD = """
|
|
@@ -821,16 +831,14 @@ button[class*="primary"] * {
|
|
| 821 |
|
| 822 |
with gr.Blocks() as demo:
|
| 823 |
|
| 824 |
-
# ヒーローセクション
|
| 825 |
gr.HTML("""
|
| 826 |
<div class="hero-section">
|
| 827 |
<h1>Sound Classify</h1>
|
| 828 |
<p>Streaming ESN · Record · Learn · Predict</p>
|
| 829 |
</div>
|
| 830 |
""")
|
| 831 |
-
|
| 832 |
-
# ステータス
|
| 833 |
-
stats = gr.Markdown(dataset_stats_text(), elem_classes=["section-title"])
|
| 834 |
|
| 835 |
with gr.Tabs():
|
| 836 |
|
|
@@ -841,7 +849,6 @@ with gr.Blocks() as demo:
|
|
| 841 |
with gr.Row():
|
| 842 |
label_box = gr.Textbox(label="新ラベル", placeholder="例: yes", scale=3)
|
| 843 |
add_btn = gr.Button("追加", size="lg", scale=1)
|
| 844 |
-
log1 = gr.Textbox(label="ログ", interactive=False, lines=1)
|
| 845 |
|
| 846 |
# 録音 & サンプル追加
|
| 847 |
gr.Markdown("### 録音")
|
|
@@ -851,7 +858,6 @@ with gr.Blocks() as demo:
|
|
| 851 |
with gr.Row():
|
| 852 |
undo_btn = gr.Button("Undo", size="lg")
|
| 853 |
rerec_btn = gr.Button("Clear", size="lg")
|
| 854 |
-
log2 = gr.Textbox(label="ログ", interactive=False, lines=1)
|
| 855 |
|
| 856 |
# データ一覧 & 編集
|
| 857 |
gr.Markdown("### データ一覧")
|
|
@@ -870,7 +876,6 @@ with gr.Blocks() as demo:
|
|
| 870 |
with gr.Row():
|
| 871 |
relabel_btn = gr.Button("ラベル更新", size="lg")
|
| 872 |
del_btn = gr.Button("削除", size="lg")
|
| 873 |
-
log3 = gr.Textbox(label="ログ", interactive=False, lines=1)
|
| 874 |
|
| 875 |
# ── 学習タブ ──
|
| 876 |
with gr.Tab("学習"):
|
|
@@ -887,25 +892,26 @@ with gr.Blocks() as demo:
|
|
| 887 |
stream_audio.stream(stream_predict, inputs=[stream_audio, st], outputs=[pred_box, prob_box, st])
|
| 888 |
|
| 889 |
# wiring
|
| 890 |
-
add_btn.click(add_label_cb, inputs=[label_box], outputs=[label_dd,
|
| 891 |
-
add_sample_btn.click(add_sample_cb, inputs=[audio_rec, label_dd], outputs=[
|
| 892 |
-
undo_btn.click(undo_last_cb, inputs=[], outputs=[
|
| 893 |
rerec_btn.click(clear_rec_cb, inputs=[], outputs=[audio_rec])
|
|
|
|
| 894 |
|
| 895 |
# select row -> update state + replay + relabel dropdown value
|
| 896 |
def _select_and_store(evt: gr.SelectData):
|
| 897 |
if evt is None or evt.index is None:
|
| 898 |
-
return None, None, gr.update(value=None)
|
| 899 |
row = evt.index[0] if isinstance(evt.index, (tuple, list)) else int(evt.index)
|
| 900 |
if row < 0 or row >= len(DATA):
|
| 901 |
-
return None, None, gr.update(value=None)
|
| 902 |
item = DATA[row]
|
| 903 |
-
return row, item["audio"], gr.update(value=item["label"])
|
| 904 |
|
| 905 |
-
table.select(_select_and_store, inputs=None, outputs=[selected_idx_state, replay_audio, relabel_dd
|
| 906 |
|
| 907 |
-
relabel_btn.click(relabel_selected_cb, inputs=[selected_idx_state, relabel_dd], outputs=[
|
| 908 |
-
del_btn.click(delete_selected_cb, inputs=[selected_idx_state], outputs=[
|
| 909 |
|
| 910 |
train_btn.click(train_random, inputs=[trials], outputs=[train_log])
|
| 911 |
if __name__ == "__main__":
|
|
|
|
| 243 |
def add_label_cb(label):
|
| 244 |
label = (label or "").strip()
|
| 245 |
if not label:
|
| 246 |
+
return gr.update(), dataset_table(), gr.update()
|
| 247 |
if label not in LABELS:
|
| 248 |
LABELS.append(label)
|
| 249 |
+
return gr.update(choices=LABELS, value=label), dataset_table(), gr.update(choices=LABELS)
|
| 250 |
|
| 251 |
def add_sample_cb(audio, label):
|
| 252 |
label = (label or "").strip()
|
| 253 |
if label not in LABELS:
|
| 254 |
+
return dataset_table(), gr.update(value=None)
|
| 255 |
if audio is None:
|
| 256 |
+
return dataset_table(), gr.update(value=None)
|
| 257 |
|
| 258 |
audio_n = normalize_audio_tuple(audio)
|
| 259 |
U = audio_to_sequence(audio_n)
|
| 260 |
if U is None or len(U) < 5:
|
| 261 |
+
return dataset_table(), gr.update(value=None)
|
| 262 |
|
| 263 |
DATA.append({"audio": audio_n, "U": U, "label": label})
|
| 264 |
+
return dataset_table(), gr.update(value=None)
|
| 265 |
|
| 266 |
def undo_last_cb():
|
| 267 |
if len(DATA) == 0:
|
| 268 |
+
return dataset_table()
|
| 269 |
+
DATA.pop()
|
| 270 |
+
return dataset_table()
|
| 271 |
+
|
| 272 |
+
def reset_all_cb():
|
| 273 |
+
global MODEL
|
| 274 |
+
DATA.clear()
|
| 275 |
+
LABELS.clear()
|
| 276 |
+
MODEL = None
|
| 277 |
+
return (
|
| 278 |
+
dataset_table(),
|
| 279 |
+
gr.update(choices=[], value=None),
|
| 280 |
+
gr.update(choices=[], value=None),
|
| 281 |
+
gr.update(value=None),
|
| 282 |
+
None,
|
| 283 |
+
)
|
| 284 |
|
| 285 |
def clear_rec_cb():
|
| 286 |
return gr.update(value=None)
|
|
|
|
| 307 |
def relabel_selected_cb(selected_idx, new_label):
|
| 308 |
new_label = (new_label or "").strip()
|
| 309 |
if selected_idx is None or selected_idx < 0 or selected_idx >= len(DATA):
|
| 310 |
+
return dataset_table()
|
| 311 |
if new_label not in LABELS:
|
| 312 |
+
return dataset_table()
|
|
|
|
| 313 |
DATA[selected_idx]["label"] = new_label
|
| 314 |
+
return dataset_table()
|
| 315 |
|
| 316 |
def delete_selected_cb(selected_idx):
|
| 317 |
if selected_idx is None or selected_idx < 0 or selected_idx >= len(DATA):
|
| 318 |
+
return dataset_table(), None, gr.update(value=None)
|
|
|
|
| 319 |
DATA.pop(selected_idx)
|
| 320 |
+
return dataset_table(), None, gr.update(value=None)
|
|
|
|
| 321 |
|
| 322 |
# Compact UI — Copy.py風フェミニン・パステルデザイン + 写真風ブルー→グリーングラデーション
|
| 323 |
HEAD = """
|
|
|
|
| 831 |
|
| 832 |
with gr.Blocks() as demo:
|
| 833 |
|
| 834 |
+
# ヒーローセクション + リセットボタン
|
| 835 |
gr.HTML("""
|
| 836 |
<div class="hero-section">
|
| 837 |
<h1>Sound Classify</h1>
|
| 838 |
<p>Streaming ESN · Record · Learn · Predict</p>
|
| 839 |
</div>
|
| 840 |
""")
|
| 841 |
+
reset_btn = gr.Button("Reset All", size="sm")
|
|
|
|
|
|
|
| 842 |
|
| 843 |
with gr.Tabs():
|
| 844 |
|
|
|
|
| 849 |
with gr.Row():
|
| 850 |
label_box = gr.Textbox(label="新ラベル", placeholder="例: yes", scale=3)
|
| 851 |
add_btn = gr.Button("追加", size="lg", scale=1)
|
|
|
|
| 852 |
|
| 853 |
# 録音 & サンプル追加
|
| 854 |
gr.Markdown("### 録音")
|
|
|
|
| 858 |
with gr.Row():
|
| 859 |
undo_btn = gr.Button("Undo", size="lg")
|
| 860 |
rerec_btn = gr.Button("Clear", size="lg")
|
|
|
|
| 861 |
|
| 862 |
# データ一覧 & 編集
|
| 863 |
gr.Markdown("### データ一覧")
|
|
|
|
| 876 |
with gr.Row():
|
| 877 |
relabel_btn = gr.Button("ラベル更新", size="lg")
|
| 878 |
del_btn = gr.Button("削除", size="lg")
|
|
|
|
| 879 |
|
| 880 |
# ── 学習タブ ──
|
| 881 |
with gr.Tab("学習"):
|
|
|
|
| 892 |
stream_audio.stream(stream_predict, inputs=[stream_audio, st], outputs=[pred_box, prob_box, st])
|
| 893 |
|
| 894 |
# wiring
|
| 895 |
+
add_btn.click(add_label_cb, inputs=[label_box], outputs=[label_dd, table, relabel_dd])
|
| 896 |
+
add_sample_btn.click(add_sample_cb, inputs=[audio_rec, label_dd], outputs=[table, audio_rec])
|
| 897 |
+
undo_btn.click(undo_last_cb, inputs=[], outputs=[table])
|
| 898 |
rerec_btn.click(clear_rec_cb, inputs=[], outputs=[audio_rec])
|
| 899 |
+
reset_btn.click(reset_all_cb, inputs=[], outputs=[table, label_dd, relabel_dd, audio_rec, selected_idx_state])
|
| 900 |
|
| 901 |
# select row -> update state + replay + relabel dropdown value
|
| 902 |
def _select_and_store(evt: gr.SelectData):
|
| 903 |
if evt is None or evt.index is None:
|
| 904 |
+
return None, None, gr.update(value=None)
|
| 905 |
row = evt.index[0] if isinstance(evt.index, (tuple, list)) else int(evt.index)
|
| 906 |
if row < 0 or row >= len(DATA):
|
| 907 |
+
return None, None, gr.update(value=None)
|
| 908 |
item = DATA[row]
|
| 909 |
+
return row, item["audio"], gr.update(value=item["label"])
|
| 910 |
|
| 911 |
+
table.select(_select_and_store, inputs=None, outputs=[selected_idx_state, replay_audio, relabel_dd])
|
| 912 |
|
| 913 |
+
relabel_btn.click(relabel_selected_cb, inputs=[selected_idx_state, relabel_dd], outputs=[table])
|
| 914 |
+
del_btn.click(delete_selected_cb, inputs=[selected_idx_state], outputs=[table, selected_idx_state, relabel_dd])
|
| 915 |
|
| 916 |
train_btn.click(train_random, inputs=[trials], outputs=[train_log])
|
| 917 |
if __name__ == "__main__":
|