import gradio as gr import torch import librosa from transformers import AutoModelForAudioClassification, AutoFeatureExtractor model = AutoModelForAudioClassification.from_pretrained(".") extractor = AutoFeatureExtractor.from_pretrained(".") maps = { 0:"blues", 1:"classical", 2:"country", 3:"disco", 4:"hiphop", 5:"jazz", 6:"metal", 7:"pop", 8:"reggae", 9:"rock" } def predict(audio): audio, sr = librosa.load(audio, sr=16000) inputs = extractor(audio, sampling_rate=16000, return_tensors="pt") with torch.no_grad(): logits = model(**inputs).logits probs = torch.softmax(logits, dim=-1)[0].tolist() result = {maps[i]: float(probs[i]) for i in range(10)} return result with gr.Blocks(title="AST_Audio_Classfication") as demo: gr.Markdown("""

🎵 Music Genre Detection

Audio classification using Audio Spectrogram Transformer (AST)
Built as part of the IIT Madras Intro to Deep Learning & GenAI Project (2026 Term 1)

""") with gr.Row(): with gr.Column(scale=1): gr.HTML("""

📌 Model Overview

""") gr.HTML("""

🎧 Genres Detected by Model

""") gr.HTML("""

🎓 Project Info

""") with gr.Column(scale=2): audio_input = gr.Audio( sources=["upload","microphone"], type="filepath", label="Upload or Record Audio" ) btn = gr.Button("🎯 Detect Genre") output = gr.Label( num_top_classes=5, label="Prediction Probabilities" ) btn.click(predict, inputs=audio_input, outputs=output) gr.Markdown("""

Built with ❤️ using Hugging Face Transformers & Gradio
Ayusman Samasi • IIT Madras DL & GenAI Project T1 2026

""") demo.launch()