Download app.py from AIOmarRehan/Deep_Audio_Classifier_using_CNN: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/spaces/AIOmarRehan/Deep_Audio_Classifier_using_CNN/resolve/6fa015bf82143668666259400a1b254d6418f3bd/app.py
- Command line
-
hf download hf://spaces/AIOmarRehan/Deep_Audio_Classifier_using_CNN@6fa015bf82143668666259400a1b254d6418f3bd/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AIOmarRehan/Deep_Audio_Classifier_using_CNN/resolve/6fa015bf82143668666259400a1b254d6418f3bd/app.py
3.68 kB
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| from app.preprocess import preprocess_audio | |
| from app.model import predict | |
| from collections import Counter, defaultdict | |
| import librosa | |
| import random | |
| from datasets import load_dataset | |
| # Load Hugging Face datasets | |
| audio_ds = load_dataset("AIOmarRehan/General_Audio_Dataset", split="train") | |
| image_ds = load_dataset("AIOmarRehan/Mel_Spectrogram_Images_for_Audio_Classification", split="train") | |
| # IMAGE HANDLING | |
| def safe_load_image(img): | |
| if img is None: | |
| return None | |
| if isinstance(img, np.ndarray): | |
| img = Image.fromarray(img) | |
| img = img.convert("RGBA") | |
| return img | |
| # PROCESS SPECTROGRAM IMAGE | |
| def process_image_input(img): | |
| img = safe_load_image(img) | |
| label, confidence, probs = predict(img) | |
| return label, round(confidence, 3), probs | |
| # PROCESS RAW AUDIO | |
| def process_audio_input(audio_path): | |
| imgs = preprocess_audio(audio_path) | |
| all_preds, all_confs, all_probs = [], [], [] | |
| for img in imgs: | |
| label, conf, probs = predict(img) | |
| all_preds.append(label) | |
| all_confs.append(conf) | |
| all_probs.append(probs) | |
| # Majority vote | |
| counter = Counter(all_preds) | |
| max_count = max(counter.values()) | |
| candidates = [k for k, v in counter.items() if v == max_count] | |
| if len(candidates) == 1: | |
| final_label = candidates[0] | |
| else: | |
| conf_sums = defaultdict(float) | |
| for i, lbl in enumerate(all_preds): | |
| if lbl in candidates: | |
| conf_sums[lbl] += all_confs[i] | |
| final_label = max(conf_sums, key=conf_sums.get) | |
| final_conf = float(np.mean([all_confs[i] for i, lbl in enumerate(all_preds) if lbl == final_label])) | |
| return final_label, round(final_conf, 3), all_preds, [round(c, 3) for c in all_confs] | |
| # MAIN CLASSIFIER | |
| def classify(audio_path, image, random_audio, random_image): | |
| # Load random audio if selected | |
| if random_audio: | |
| rand_sample = random.choice(audio_ds) | |
| audio_path = rand_sample["audio"]["path"] | |
| # Load random image if selected | |
| if random_image: | |
| rand_sample = random.choice(image_ds) | |
| img_bytes = rand_sample["image"] | |
| image = Image.open(img_bytes).convert("RGBA") | |
| # If spectrogram image | |
| if image is not None: | |
| label, conf, probs = process_image_input(image) | |
| return { | |
| "Final Label": label, | |
| "Confidence": conf, | |
| "Details": probs | |
| }, label | |
| # If raw audio | |
| if audio_path is not None: | |
| label, conf, all_preds, all_confs = process_audio_input(audio_path) | |
| return { | |
| "Final Label": label, | |
| "Confidence": conf, | |
| "All Chunk Labels": all_preds, | |
| "All Chunk Confidences": all_confs | |
| }, label | |
| return "Please upload an audio file OR a spectrogram image.", "" | |
| # GRADIO UI | |
| interface = gr.Interface( | |
| fn=classify, | |
| inputs=[ | |
| gr.Audio(type="filepath", label="Upload Audio (WAV/MP3)"), | |
| gr.Image(type="pil", label="Upload Spectrogram Image (PNG RGBA Supported)"), | |
| gr.Checkbox(label="Pick Random Audio from Dataset"), | |
| gr.Checkbox(label="Pick Random Image from Dataset"), | |
| ], | |
| outputs=[ | |
| gr.JSON(label="Prediction Results"), | |
| gr.Textbox(label="Final Label", interactive=False) | |
| ], | |
| title="General Audio Classifier (Audio + Spectrogram Support)", | |
| description=( | |
| "Upload a raw audio file OR a spectrogram image.\n" | |
| "You can also select random samples from the Hugging Face datasets.\n" | |
| "The output shows a JSON with all details and a separate field for the final label." | |
| ), | |
| ) | |
| interface.launch() |