Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -36,7 +36,6 @@ def create_spectrogram(audio_path, title="Spectrogram"):
|
|
| 36 |
y, sr = librosa.load(audio_path, sr=22050, mono=True)
|
| 37 |
mel_spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)
|
| 38 |
mel_db = librosa.power_to_db(mel_spec, ref=np.max)
|
| 39 |
-
|
| 40 |
fig, ax = plt.subplots(figsize=(10, 4))
|
| 41 |
im = ax.imshow(mel_db, aspect="auto", origin="lower", cmap="viridis", interpolation="nearest")
|
| 42 |
ax.set_title(title)
|
|
@@ -44,7 +43,6 @@ def create_spectrogram(audio_path, title="Spectrogram"):
|
|
| 44 |
ax.set_ylabel("Mel Frequency")
|
| 45 |
plt.colorbar(im, ax=ax, format="%+2.0f dB")
|
| 46 |
plt.tight_layout()
|
| 47 |
-
|
| 48 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 49 |
plt.savefig(temp_file.name, dpi=100, bbox_inches="tight")
|
| 50 |
plt.close()
|
|
@@ -55,21 +53,19 @@ def create_spectrogram(audio_path, title="Spectrogram"):
|
|
| 55 |
|
| 56 |
|
| 57 |
def predict_anomaly(audio_file, machine_type):
|
| 58 |
-
"""Predict anomaly in audio."""
|
| 59 |
if audio_file is None:
|
| 60 |
return "Please upload an audio file.", None, None
|
| 61 |
-
|
| 62 |
-
# Gradio Audio can return dict with "path" key in some versions
|
| 63 |
if isinstance(audio_file, dict):
|
| 64 |
audio_file = audio_file.get("path") or audio_file.get("name")
|
| 65 |
if not audio_file:
|
| 66 |
return "Please upload an audio file.", None, None
|
| 67 |
-
|
| 68 |
import random
|
| 69 |
random.seed(hash(str(audio_file)) % 1000)
|
| 70 |
is_anomaly = random.random() > 0.5
|
| 71 |
confidence = random.uniform(0.75, 0.95)
|
| 72 |
-
|
| 73 |
if is_anomaly:
|
| 74 |
label = "ANOMALY β"
|
| 75 |
color = "#ff4444"
|
|
@@ -79,77 +75,54 @@ def predict_anomaly(audio_file, machine_type):
|
|
| 79 |
label = "NORMAL β"
|
| 80 |
color = "#44ff44"
|
| 81 |
result = f'<div style="text-align: center; padding: 20px;"><h2 style="color: {color}; font-size: 48px;">{label}</h2><p>Confidence: {confidence:.1%}</p></div>'
|
| 82 |
-
|
| 83 |
input_spec = create_spectrogram(audio_file, f"Input - {machine_type}")
|
| 84 |
ref_spec = None
|
| 85 |
-
|
| 86 |
examples_dir = Path(__file__).parent / "examples"
|
| 87 |
if examples_dir.exists():
|
| 88 |
ref_files = list(examples_dir.glob(f"{machine_type}_*.wav"))
|
| 89 |
if ref_files:
|
| 90 |
ref_spec = create_spectrogram(str(ref_files[0]), f"Reference - {machine_type}")
|
| 91 |
-
|
| 92 |
-
return result, input_spec, ref_spec
|
| 93 |
|
|
|
|
| 94 |
|
| 95 |
-
theme = gr.themes.Monochrome(primary_hue="red", secondary_hue="gray")
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
<div style="text-align: center; padding: 20px;">
|
| 100 |
-
<h1>
|
| 101 |
<p>Synthetic Industrial Machine Sound Anomaly Detection Dataset</p>
|
| 102 |
-
<p><a href="
|
| 103 |
</div>
|
| 104 |
""")
|
| 105 |
-
|
| 106 |
with gr.Tabs():
|
| 107 |
-
with gr.Tab("
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column():
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
with gr.Column():
|
| 115 |
-
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
inputs=[audio_input, machine_dropdown],
|
| 124 |
-
outputs=[result_html, input_spec, ref_spec],
|
| 125 |
-
api_name="predict",
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
with gr.Tab("π Explore Dataset"):
|
| 129 |
gr.Markdown(f"""
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
</ul>
|
| 138 |
-
<p style="text-align: center;">
|
| 139 |
-
<a href="https://huggingface.co/datasets/mandipgoswami/AnomalyMachine-50K" target="_blank">
|
| 140 |
-
<button style="padding: 10px 20px; font-size: 16px;">π₯ Download Dataset</button>
|
| 141 |
-
</a>
|
| 142 |
-
</p>
|
| 143 |
-
</div>
|
| 144 |
""")
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
<p style="color: #888; font-size: 14px;">
|
| 149 |
-
License: CC-BY 4.0 |
|
| 150 |
-
<a href="https://huggingface.co/datasets/mandipgoswami/AnomalyMachine-50K" target="_blank">Dataset</a> |
|
| 151 |
-
<a href="https://github.com/mandip42/anomaly-machine-50k" target="_blank">GitHub</a>
|
| 152 |
-
</p>
|
| 153 |
</div>
|
| 154 |
""")
|
| 155 |
|
|
|
|
| 36 |
y, sr = librosa.load(audio_path, sr=22050, mono=True)
|
| 37 |
mel_spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)
|
| 38 |
mel_db = librosa.power_to_db(mel_spec, ref=np.max)
|
|
|
|
| 39 |
fig, ax = plt.subplots(figsize=(10, 4))
|
| 40 |
im = ax.imshow(mel_db, aspect="auto", origin="lower", cmap="viridis", interpolation="nearest")
|
| 41 |
ax.set_title(title)
|
|
|
|
| 43 |
ax.set_ylabel("Mel Frequency")
|
| 44 |
plt.colorbar(im, ax=ax, format="%+2.0f dB")
|
| 45 |
plt.tight_layout()
|
|
|
|
| 46 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 47 |
plt.savefig(temp_file.name, dpi=100, bbox_inches="tight")
|
| 48 |
plt.close()
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
def predict_anomaly(audio_file, machine_type):
|
| 56 |
+
"""Predict anomaly in audio. Returns (result_html, input_spec_path, ref_spec_path)."""
|
| 57 |
if audio_file is None:
|
| 58 |
return "Please upload an audio file.", None, None
|
|
|
|
|
|
|
| 59 |
if isinstance(audio_file, dict):
|
| 60 |
audio_file = audio_file.get("path") or audio_file.get("name")
|
| 61 |
if not audio_file:
|
| 62 |
return "Please upload an audio file.", None, None
|
| 63 |
+
|
| 64 |
import random
|
| 65 |
random.seed(hash(str(audio_file)) % 1000)
|
| 66 |
is_anomaly = random.random() > 0.5
|
| 67 |
confidence = random.uniform(0.75, 0.95)
|
| 68 |
+
|
| 69 |
if is_anomaly:
|
| 70 |
label = "ANOMALY β"
|
| 71 |
color = "#ff4444"
|
|
|
|
| 75 |
label = "NORMAL β"
|
| 76 |
color = "#44ff44"
|
| 77 |
result = f'<div style="text-align: center; padding: 20px;"><h2 style="color: {color}; font-size: 48px;">{label}</h2><p>Confidence: {confidence:.1%}</p></div>'
|
| 78 |
+
|
| 79 |
input_spec = create_spectrogram(audio_file, f"Input - {machine_type}")
|
| 80 |
ref_spec = None
|
|
|
|
| 81 |
examples_dir = Path(__file__).parent / "examples"
|
| 82 |
if examples_dir.exists():
|
| 83 |
ref_files = list(examples_dir.glob(f"{machine_type}_*.wav"))
|
| 84 |
if ref_files:
|
| 85 |
ref_spec = create_spectrogram(str(ref_files[0]), f"Reference - {machine_type}")
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
return result, input_spec, ref_spec
|
| 88 |
|
|
|
|
| 89 |
|
| 90 |
+
# Use Blocks only; no api_name to avoid broken API schema path that causes "No API found"
|
| 91 |
+
with gr.Blocks(title="AnomalyMachine-50K Demo", theme=gr.themes.Monochrome(primary_hue="red", secondary_hue="gray")) as app:
|
| 92 |
+
gr.Markdown(f"""
|
| 93 |
<div style="text-align: center; padding: 20px;">
|
| 94 |
+
<h1>AnomalyMachine-50K</h1>
|
| 95 |
<p>Synthetic Industrial Machine Sound Anomaly Detection Dataset</p>
|
| 96 |
+
<p><a href="{DATASET_URL}" target="_blank">View Dataset</a></p>
|
| 97 |
</div>
|
| 98 |
""")
|
|
|
|
| 99 |
with gr.Tabs():
|
| 100 |
+
with gr.Tab("Detect Anomaly"):
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column():
|
| 103 |
+
audio_in = gr.Audio(label="Upload Audio", type="filepath", sources=["upload", "microphone"])
|
| 104 |
+
machine_in = gr.Dropdown(choices=DATASET_INFO["machines"], label="Machine Type", value=DATASET_INFO["machines"][0])
|
| 105 |
+
btn = gr.Button("Detect Anomaly", variant="primary")
|
|
|
|
| 106 |
with gr.Column():
|
| 107 |
+
result_out = gr.HTML()
|
|
|
|
| 108 |
with gr.Row():
|
| 109 |
+
spec_out = gr.Image(label="Input Spectrogram")
|
| 110 |
+
ref_out = gr.Image(label="Reference Spectrogram")
|
| 111 |
+
btn.click(fn=predict_anomaly, inputs=[audio_in, machine_in], outputs=[result_out, spec_out, ref_out])
|
| 112 |
+
|
| 113 |
+
with gr.Tab("Explore Dataset"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
gr.Markdown(f"""
|
| 115 |
+
**Dataset Statistics**
|
| 116 |
+
- Total Clips: {DATASET_INFO['total_clips']:,}
|
| 117 |
+
- Total Duration: {DATASET_INFO['total_hours']} hours
|
| 118 |
+
- Machine Types: {len(DATASET_INFO['machines'])}
|
| 119 |
+
- Normal: {DATASET_INFO['normal_ratio']:.0%} | Anomalous: {DATASET_INFO['anomalous_ratio']:.0%}
|
| 120 |
+
|
| 121 |
+
[Download Dataset]({DATASET_URL})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
""")
|
| 123 |
+
gr.Markdown(f"""
|
| 124 |
+
<div style="text-align: center; padding: 20px; border-top: 1px solid #333;">
|
| 125 |
+
<p style="color: #888;">License: CC-BY 4.0 | <a href="{DATASET_URL}" target="_blank">Dataset</a> | <a href="https://github.com/mandip42/anomaly-machine-50k" target="_blank">GitHub</a></p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
</div>
|
| 127 |
""")
|
| 128 |
|