Update app.py
Browse files
app.py
CHANGED
|
@@ -64,43 +64,6 @@ feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(MODEL_NAME)
|
|
| 64 |
print(f"โหลดโมเดลเรียบร้อย | คลาส: {list(ID2LABEL.values())} | sample_rate={SAMPLE_RATE} | max_duration={MAX_DURATION}s")
|
| 65 |
|
| 66 |
|
| 67 |
-
# ---------------------------------------------------------------------------
|
| 68 |
-
# 1.5) ไอคอน + สี ประจำแต่ละคลาส (แก้ตรงนี้ได้ถ้าอยากเปลี่ยน)
|
| 69 |
-
# ---------------------------------------------------------------------------
|
| 70 |
-
CLASS_STYLE = {
|
| 71 |
-
"Plant_Sounds": {"icon": "🌱", "color": "#22c55e"}, # เขียว
|
| 72 |
-
"Normal": {"icon": "🔊", "color": "#3b82f6"}, # ฟ้า
|
| 73 |
-
"Noise": {"icon": "🔇", "color": "#ef4444"}, # แดง
|
| 74 |
-
}
|
| 75 |
-
DEFAULT_STYLE = {"icon": "🎧", "color": "#6b7280"} # เทา (เผื่อมีคลาสอื่นที่ไม่ได้ตั้งค่าไว้)
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
def get_class_style(class_name):
|
| 79 |
-
return CLASS_STYLE.get(class_name, DEFAULT_STYLE)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
def build_probability_html(prob_dict):
|
| 83 |
-
"""สร้างแถบความน่าจะเป็นแบบมีไอคอน+สีต่อคลาส (gr.Label ปกติกำหนดสี/ไอคอนเองไม่ได้ เลยทำ HTML เอง)"""
|
| 84 |
-
rows = sorted(prob_dict.items(), key=lambda kv: kv[1], reverse=True)
|
| 85 |
-
html = '<div style="display:flex; flex-direction:column; gap:10px; padding:6px 2px;">'
|
| 86 |
-
for class_name, prob in rows:
|
| 87 |
-
style = get_class_style(class_name)
|
| 88 |
-
pct = prob * 100
|
| 89 |
-
html += f"""
|
| 90 |
-
<div>
|
| 91 |
-
<div style="display:flex; justify-content:space-between; font-weight:600; margin-bottom:4px;">
|
| 92 |
-
<span>{style['icon']} {class_name}</span>
|
| 93 |
-
<span>{pct:.1f}%</span>
|
| 94 |
-
</div>
|
| 95 |
-
<div style="background:#e5e7eb; border-radius:6px; height:10px; overflow:hidden;">
|
| 96 |
-
<div style="width:{pct:.1f}%; background:{style['color']}; height:100%;"></div>
|
| 97 |
-
</div>
|
| 98 |
-
</div>
|
| 99 |
-
"""
|
| 100 |
-
html += "</div>"
|
| 101 |
-
return html
|
| 102 |
-
|
| 103 |
-
|
| 104 |
# ---------------------------------------------------------------------------
|
| 105 |
# 2) ฟังก์ชันรัน inference บน GPU (ติด @spaces.GPU เพื่อให้ ZeroGPU จ่าย GPU มาให้ตอนเรียกฟังก์ชันนี้)
|
| 106 |
# ---------------------------------------------------------------------------
|
|
@@ -120,7 +83,7 @@ def _run_inference(input_values):
|
|
| 120 |
# ---------------------------------------------------------------------------
|
| 121 |
def analyze_audio(audio_filepath):
|
| 122 |
if audio_filepath is None:
|
| 123 |
-
return
|
| 124 |
|
| 125 |
waveform, sr = librosa.load(audio_filepath, sr=SAMPLE_RATE, mono=True)
|
| 126 |
wav_np = waveform
|
|
@@ -138,9 +101,7 @@ def analyze_audio(audio_filepath):
|
|
| 138 |
|
| 139 |
pred_id = int(np.argmax(probs))
|
| 140 |
pred_label = ID2LABEL[pred_id]
|
| 141 |
-
pred_style = get_class_style(pred_label)
|
| 142 |
prob_dict = {ID2LABEL[i]: float(probs[i]) for i in range(len(probs))}
|
| 143 |
-
prob_html = build_probability_html(prob_dict)
|
| 144 |
|
| 145 |
# ---- waveform + spectrogram ----
|
| 146 |
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(9, 6))
|
|
@@ -162,8 +123,8 @@ def analyze_audio(audio_filepath):
|
|
| 162 |
|
| 163 |
plt.tight_layout()
|
| 164 |
|
| 165 |
-
result_text = f"
|
| 166 |
-
return
|
| 167 |
|
| 168 |
|
| 169 |
# ---------------------------------------------------------------------------
|
|
@@ -209,23 +170,14 @@ print(
|
|
| 209 |
# ---------------------------------------------------------------------------
|
| 210 |
# 5) Gradio Interface
|
| 211 |
# ---------------------------------------------------------------------------
|
| 212 |
-
demo = gr.Blocks(title="HuBERT
|
| 213 |
|
| 214 |
with demo:
|
| 215 |
-
gr.Markdown("# 🌱 HuBERT
|
| 216 |
gr.Markdown(
|
| 217 |
f"อัพโหลดไฟล์เสียง .wav ความยาว ~{MAX_DURATION:g} วินาที "
|
| 218 |
f"ระบบจะจำแนกออกเป็น {len(ID2LABEL)} คลาส: {list(ID2LABEL.values())}"
|
| 219 |
)
|
| 220 |
-
legend_html = "".join(
|
| 221 |
-
f'<span style="display:inline-flex; align-items:center; gap:6px; '
|
| 222 |
-
f'margin-right:18px; font-weight:600;">'
|
| 223 |
-
f'<span style="width:12px; height:12px; border-radius:50%; '
|
| 224 |
-
f'background:{get_class_style(name)["color"]}; display:inline-block;"></span>'
|
| 225 |
-
f'{get_class_style(name)["icon"]} {name}</span>'
|
| 226 |
-
for name in ID2LABEL.values()
|
| 227 |
-
)
|
| 228 |
-
gr.HTML(f'<div style="margin-bottom:6px;">{legend_html}</div>')
|
| 229 |
|
| 230 |
with gr.Row(equal_height=False):
|
| 231 |
with gr.Column(scale=1):
|
|
@@ -247,9 +199,7 @@ with demo:
|
|
| 247 |
|
| 248 |
with gr.Row():
|
| 249 |
class_dropdown = gr.Dropdown(
|
| 250 |
-
choices=
|
| 251 |
-
(f"{get_class_style(c)['icon']} {c}", c) for c in class_names_sorted
|
| 252 |
-
],
|
| 253 |
value=first_class,
|
| 254 |
label="คลาส",
|
| 255 |
scale=1,
|
|
@@ -277,10 +227,7 @@ with demo:
|
|
| 277 |
)
|
| 278 |
|
| 279 |
with gr.Column(scale=1):
|
| 280 |
-
label_output = gr.
|
| 281 |
-
value="<div style='min-height:120px;'></div>",
|
| 282 |
-
label="ความน่าจะเป็นแต่ละคลาส",
|
| 283 |
-
)
|
| 284 |
plot_output = gr.Plot(label="Waveform + Spectrogram")
|
| 285 |
text_output = gr.Textbox(label="ผลการทำนาย")
|
| 286 |
|
|
|
|
| 64 |
print(f"โหลดโมเดลเรียบร้อย | คลาส: {list(ID2LABEL.values())} | sample_rate={SAMPLE_RATE} | max_duration={MAX_DURATION}s")
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# ---------------------------------------------------------------------------
|
| 68 |
# 2) ฟังก์ชันรัน inference บน GPU (ติด @spaces.GPU เพื่อให้ ZeroGPU จ่าย GPU มาให้ตอนเรียกฟังก์ชันนี้)
|
| 69 |
# ---------------------------------------------------------------------------
|
|
|
|
| 83 |
# ---------------------------------------------------------------------------
|
| 84 |
def analyze_audio(audio_filepath):
|
| 85 |
if audio_filepath is None:
|
| 86 |
+
return {}, None, "กรุณาอัพโหลดไฟล์เสียง .wav ก่อนครับ"
|
| 87 |
|
| 88 |
waveform, sr = librosa.load(audio_filepath, sr=SAMPLE_RATE, mono=True)
|
| 89 |
wav_np = waveform
|
|
|
|
| 101 |
|
| 102 |
pred_id = int(np.argmax(probs))
|
| 103 |
pred_label = ID2LABEL[pred_id]
|
|
|
|
| 104 |
prob_dict = {ID2LABEL[i]: float(probs[i]) for i in range(len(probs))}
|
|
|
|
| 105 |
|
| 106 |
# ---- waveform + spectrogram ----
|
| 107 |
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(9, 6))
|
|
|
|
| 123 |
|
| 124 |
plt.tight_layout()
|
| 125 |
|
| 126 |
+
result_text = f"ทำนาย: {pred_label} (ความมั่นใจ {probs[pred_id] * 100:.2f}%)"
|
| 127 |
+
return prob_dict, fig, result_text
|
| 128 |
|
| 129 |
|
| 130 |
# ---------------------------------------------------------------------------
|
|
|
|
| 170 |
# ---------------------------------------------------------------------------
|
| 171 |
# 5) Gradio Interface
|
| 172 |
# ---------------------------------------------------------------------------
|
| 173 |
+
demo = gr.Blocks(title="HuBERT Audio Classifier: Plant_Sounds vs Normal vs Noise")
|
| 174 |
|
| 175 |
with demo:
|
| 176 |
+
gr.Markdown("# 🌱 HuBERT Audio Classifier: Plant_Sounds vs Normal vs Noise")
|
| 177 |
gr.Markdown(
|
| 178 |
f"อัพโหลดไฟล์เสียง .wav ความยาว ~{MAX_DURATION:g} วินาที "
|
| 179 |
f"ระบบจะจำแนกออกเป็น {len(ID2LABEL)} คลาส: {list(ID2LABEL.values())}"
|
| 180 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
with gr.Row(equal_height=False):
|
| 183 |
with gr.Column(scale=1):
|
|
|
|
| 199 |
|
| 200 |
with gr.Row():
|
| 201 |
class_dropdown = gr.Dropdown(
|
| 202 |
+
choices=class_names_sorted,
|
|
|
|
|
|
|
| 203 |
value=first_class,
|
| 204 |
label="คลาส",
|
| 205 |
scale=1,
|
|
|
|
| 227 |
)
|
| 228 |
|
| 229 |
with gr.Column(scale=1):
|
| 230 |
+
label_output = gr.Label(num_top_classes=len(ID2LABEL), label="ความน่าจะเป็นแต่ละคลาส")
|
|
|
|
|
|
|
|
|
|
| 231 |
plot_output = gr.Plot(label="Waveform + Spectrogram")
|
| 232 |
text_output = gr.Textbox(label="ผลการทำนาย")
|
| 233 |
|