AyusmanSamasi commited on
Commit
afbe7e4
·
verified ·
1 Parent(s): 40d7017

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import librosa
4
+ from transformers import AutoModelForAudioClassification, AutoFeatureExtractor
5
+
6
+ model = AutoModelForAudioClassification.from_pretrained(".")
7
+ extractor = AutoFeatureExtractor.from_pretrained(".")
8
+
9
+ maps = {
10
+ 0:"blues",
11
+ 1:"classical",
12
+ 2:"country",
13
+ 3:"disco",
14
+ 4:"hiphop",
15
+ 5:"jazz",
16
+ 6:"metal",
17
+ 7:"pop",
18
+ 8:"reggae",
19
+ 9:"rock"
20
+ }
21
+
22
+ def predict(audio):
23
+
24
+ audio, sr = librosa.load(audio, sr=16000)
25
+
26
+ inputs = extractor(audio, sampling_rate=16000, return_tensors="pt")
27
+
28
+ with torch.no_grad():
29
+ logits = model(**inputs).logits
30
+
31
+ probs = torch.softmax(logits, dim=-1)[0].tolist()
32
+
33
+ result = {maps[i]: float(probs[i]) for i in range(10)}
34
+
35
+ return result
36
+
37
+
38
+ with gr.Blocks(title="AST_Audio_Classfication") as demo:
39
+
40
+ gr.Markdown("""
41
+ <div style="text-align:center;">
42
+ <h1 style="font-size:3rem;">🎵 Music Genre Detection</h1>
43
+ <p style="font-size:1.2rem; color:#555;">
44
+ Audio classification using <b>Audio Spectrogram Transformer (AST)</b><br>
45
+ Built as part of the <b>IIT Madras Intro to Deep Learning & GenAI Project (2026 Term 1)</b>
46
+ </p>
47
+ </div>
48
+ """)
49
+
50
+ with gr.Row():
51
+
52
+ with gr.Column(scale=1):
53
+
54
+ gr.HTML("""
55
+ <div style="
56
+ background:white;
57
+ padding:20px;
58
+ border-radius:14px;
59
+ box-shadow:0 2px 12px rgba(0,0,0,0.08);
60
+ margin-bottom:20px;
61
+ ">
62
+ <h2>📌 Model Overview</h2>
63
+ <ul style="line-height:1.6;">
64
+ <li><b>Architecture:</b> Audio Spectrogram Transformer</li>
65
+ <li><b>Base Model:</b> MIT AST</li>
66
+ <li><b>Task:</b> Music Genre Classification</li>
67
+ <li><b>Classes:</b> 10 Genres</li>
68
+ <li><b>Framework:</b> PyTorch + Hugging Face</li>
69
+ </ul>
70
+ </div>
71
+ """)
72
+
73
+ gr.HTML("""
74
+ <div style="
75
+ background:white;
76
+ padding:20px;
77
+ border-radius:14px;
78
+ box-shadow:0 2px 12px rgba(0,0,0,0.08);
79
+ margin-bottom:20px;
80
+ ">
81
+ <h2>🎧 Genres Detected by Model</h2>
82
+ <ul>
83
+ <li>🎷 Jazz</li>
84
+ <li>🎸 Rock</li>
85
+ <li>🎤 Pop</li>
86
+ <li>🎶 Blues</li>
87
+ <li>🎼 Classical</li>
88
+ <li>🤠 Country</li>
89
+ <li>💃 Disco</li>
90
+ <li>🔥 HipHop</li>
91
+ <li>🎛 Metal</li>
92
+ <li>🌴 Reggae</li>
93
+ </ul>
94
+ </div>
95
+ """)
96
+
97
+ gr.HTML("""
98
+ <div style="
99
+ background:white;
100
+ padding:20px;
101
+ border-radius:14px;
102
+ box-shadow:0 2px 12px rgba(0,0,0,0.08);
103
+ ">
104
+ <h2>🎓 Project Info</h2>
105
+ <ul style="line-height:1.6;">
106
+ <li><b>Course:</b> Deep Learning & Generative AI</li>
107
+ <li><b>Institution:</b> IIT Madras</li>
108
+ <li><b>Term:</b> 2026 Term 1</li>
109
+ <li><b>Student:</b> Ayusman Samasi</li>
110
+ <li><b>Roll:</b> 22f3001XXX</li>
111
+ <li><b>Email:</b> 22f3001XXX@ds.study.iitm.ac.in</li>
112
+ </ul>
113
+ </div>
114
+ """)
115
+
116
+ with gr.Column(scale=2):
117
+
118
+ audio_input = gr.Audio(
119
+ sources=["upload","microphone"],
120
+ type="filepath",
121
+ label="Upload or Record Audio"
122
+ )
123
+
124
+ btn = gr.Button("🎯 Detect Genre")
125
+
126
+ output = gr.Label(
127
+ num_top_classes=5,
128
+ label="Prediction Probabilities"
129
+ )
130
+
131
+ btn.click(predict, inputs=audio_input, outputs=output)
132
+
133
+ gr.Markdown("""
134
+ <br>
135
+ <p style="text-align:center; color:#777;">
136
+ Built with ❤️ using Hugging Face Transformers & Gradio<br>
137
+ <b>Ayusman Samasi • IIT Madras DL & GenAI Project T1 2026</b>
138
+ </p>
139
+ """)
140
+
141
+ demo.launch()