Update app.py
Browse files
app.py
CHANGED
|
@@ -111,9 +111,10 @@ def analyze_audio(audio_filepath):
|
|
| 111 |
ax1.set_xlabel("Time (s)")
|
| 112 |
ax1.set_ylabel("Amplitude")
|
| 113 |
|
| 114 |
-
|
|
|
|
| 115 |
S_db = librosa.power_to_db(S, ref=np.max)
|
| 116 |
-
img = librosa.display.specshow(S_db, sr=sr, x_axis="time", y_axis="mel", ax=ax2)
|
| 117 |
ax2.set_title("Mel Spectrogram")
|
| 118 |
fig.colorbar(img, ax=ax2, format="%+2.0f dB")
|
| 119 |
|
|
@@ -133,22 +134,34 @@ print(f"พบไฟล์ตัวอย่าง {len(example_files)} ไฟ
|
|
| 133 |
# ---------------------------------------------------------------------------
|
| 134 |
# 5) Gradio Interface
|
| 135 |
# ---------------------------------------------------------------------------
|
| 136 |
-
demo = gr.
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
gr.Plot(label="Waveform + Spectrogram"),
|
| 142 |
-
gr.Textbox(label="ผลการทำนาย"),
|
| 143 |
-
],
|
| 144 |
-
examples=example_files if example_files else None,
|
| 145 |
-
cache_examples=False,
|
| 146 |
-
title="🌱 HuBERT Audio Classifier: Plant_Sounds vs Normal vs Noise",
|
| 147 |
-
description=(
|
| 148 |
f"อัพโหลดไฟล์เสียง .wav ความยาว ~{MAX_DURATION:g} วินาที "
|
| 149 |
f"ระบบจะจำแนกออกเป็น {len(ID2LABEL)} คลาส: {list(ID2LABEL.values())}"
|
| 150 |
-
)
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
demo.launch()
|
|
|
|
| 111 |
ax1.set_xlabel("Time (s)")
|
| 112 |
ax1.set_ylabel("Amplitude")
|
| 113 |
|
| 114 |
+
fmax = sr / 2 # Nyquist frequency (8000 Hz สำหรับไฟล์ 16kHz) -> ให้แกน y โชว์ถึงความถี่สูงสุดจริง
|
| 115 |
+
S = librosa.feature.melspectrogram(y=wav_np, sr=sr, n_mels=128, fmax=fmax)
|
| 116 |
S_db = librosa.power_to_db(S, ref=np.max)
|
| 117 |
+
img = librosa.display.specshow(S_db, sr=sr, x_axis="time", y_axis="mel", fmax=fmax, ax=ax2)
|
| 118 |
ax2.set_title("Mel Spectrogram")
|
| 119 |
fig.colorbar(img, ax=ax2, format="%+2.0f dB")
|
| 120 |
|
|
|
|
| 134 |
# ---------------------------------------------------------------------------
|
| 135 |
# 5) Gradio Interface
|
| 136 |
# ---------------------------------------------------------------------------
|
| 137 |
+
demo = gr.Blocks(title="HuBERT Audio Classifier: Plant_Sounds vs Normal vs Noise")
|
| 138 |
+
|
| 139 |
+
with demo:
|
| 140 |
+
gr.Markdown("# 🌱 HuBERT Audio Classifier: Plant_Sounds vs Normal vs Noise")
|
| 141 |
+
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
f"อัพโหลดไฟล์เสียง .wav ความยาว ~{MAX_DURATION:g} วินาที "
|
| 143 |
f"ระบบจะจำแนกออกเป็น {len(ID2LABEL)} คลาส: {list(ID2LABEL.values())}"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
with gr.Row(equal_height=False):
|
| 147 |
+
with gr.Column(scale=1):
|
| 148 |
+
audio_input = gr.Audio(type="filepath", label="อัพโหลดไฟล์เสียง (.wav)")
|
| 149 |
+
with gr.Row():
|
| 150 |
+
clear_btn = gr.ClearButton(value="Clear")
|
| 151 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 152 |
+
|
| 153 |
+
if example_files:
|
| 154 |
+
gr.Examples(examples=example_files, inputs=audio_input, label="Examples")
|
| 155 |
+
|
| 156 |
+
with gr.Column(scale=1):
|
| 157 |
+
label_output = gr.Label(num_top_classes=len(ID2LABEL), label="ความน่าจะเป็นแต่ละคลาส")
|
| 158 |
+
plot_output = gr.Plot(label="Waveform + Spectrogram")
|
| 159 |
+
text_output = gr.Textbox(label="ผลการทำนาย")
|
| 160 |
+
|
| 161 |
+
outputs = [label_output, plot_output, text_output]
|
| 162 |
+
submit_btn.click(fn=analyze_audio, inputs=audio_input, outputs=outputs)
|
| 163 |
+
audio_input.change(fn=analyze_audio, inputs=audio_input, outputs=outputs)
|
| 164 |
+
clear_btn.add(outputs)
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
| 167 |
demo.launch()
|