-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
287 lines (240 loc) · 10.1 KB
/
app.py
File metadata and controls
287 lines (240 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import streamlit as st
import numpy as np
import librosa
import tensorflow as tf
import hashlib
import os
from skimage.transform import resize
def add_custom_css():
st.markdown("""
<style>
@keyframes gradient {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
@keyframes rainbow {
0% { color: #ff0000; }
17% { color: #ff8800; }
33% { color: #ffff00; }
50% { color: #00ff00; }
67% { color: #0000ff; }
83% { color: #8800ff; }
100% { color: #ff0000; }
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
40% { transform: translateY(-20px); }
60% { transform: translateY(-10px); }
}
.main {
padding-bottom: 100px;
}
.stApp {
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
background-size: 400% 400%;
animation: gradient 15s ease infinite;
color: white;
}
.title-text {
font-size: 3.5em;
font-weight: bold;
text-align: center;
animation: rainbow 8s linear infinite;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
margin-bottom: 2rem;
}
.genre-box {
background: rgba(255,255,255,0.15);
padding: 25px;
border-radius: 15px;
backdrop-filter: blur(10px);
margin: 15px 0;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
transition: transform 0.3s ease;
}
.genre-box:hover {
transform: translateY(-5px);
}
.confidence-bar {
height: 25px;
background: linear-gradient(90deg, #00ff87 0%, #60efff 100%);
border-radius: 12px;
transition: width 1s ease-in-out;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.genre-icon {
font-size: 50px;
margin-bottom: 15px;
animation: bounce 2s infinite;
}
.progress-label {
position: absolute;
right: 10px;
color: white;
text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
line-height: 25px;
padding-right: 10px;
}
.footer {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background: rgba(0,0,0,0.9);
backdrop-filter: blur(10px);
color: white;
padding: 20px;
text-align: center;
z-index: 999;
box-shadow: 0 -5px 25px rgba(0,0,0,0.3);
}
</style>
""", unsafe_allow_html=True)
# Genre icons with animations
genre_icons = {
'blues': '🎺', 'classical': '🎻', 'country': '🤠',
'disco': '🕺', 'hiphop': '🎤', 'jazz': '🎷',
'metal': '🤘', 'pop': '🎵', 'reggae': '🌴', 'rock': '🎸'
}
# Disable tensorflow logging
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
@st.cache_resource
def load_model():
"""Load the model once and cache it"""
try:
model = tf.keras.models.load_model("iotamodel1.h5")
return model
except Exception as e:
st.error(f"Error loading model: {str(e)}")
return None
def get_file_hash(file_content):
"""Generate a unique hash for the file content"""
return hashlib.md5(file_content).hexdigest()
def save_uploaded_file(uploaded_file):
"""Save uploaded file temporarily and return the path"""
import tempfile
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
tmp_file.write(uploaded_file.getvalue())
return tmp_file.name
def predict_genre(audio_path, target_shape=(100, 100)):
"""predict music genre from audio file"""
model = load_model()
if model is None:
return None
# Load audio file
audio_data, sample_rate = librosa.load(audio_path, sr=None)
# Compute spectrogram
mel_spectrogram = librosa.feature.melspectrogram(y=audio_data, sr=sample_rate)
# Resizing spectrogram
mel_spectrogram = resize(np.expand_dims(mel_spectrogram, axis=-1), target_shape)
# Preparing the input
mel_spectrogram = np.expand_dims(mel_spectrogram, axis=0)
# Make predictions
prediction = model.predict(mel_spectrogram)
return prediction
def main():
# Add CSS
add_custom_css()
st.markdown("""
<div class="title-text">
🎵 Music Genre Classifier
</div>
""", unsafe_allow_html=True)
st.markdown("""
<div class="upload-zone">
<div class="upload-content">
<center class="upload-text">Drop your audio file here</center>
<center class="upload-subtext">or click to browse</center>
<center class="upload-subtext">(Supports MP3 & WAV)</center>
</div>
</div>
""", unsafe_allow_html=True)
uploaded_file = st.file_uploader("", type=["mp3", "wav"], key="music_uploader", label_visibility="collapsed")
if uploaded_file is not None:
# Get file hash for uniqueness
file_content = uploaded_file.getvalue()
file_hash = get_file_hash(file_content)
# Use file hash in session state
if 'last_processed_file' not in st.session_state:
st.session_state.last_processed_file = None
# Check if this is a new file
if st.session_state.last_processed_file != file_hash:
st.session_state.last_processed_file = file_hash
col1, col2 = st.columns(2)
with col1:
st.markdown("""
<div class="genre-box">
<h3>🎧 Now Playing</h3>
</div>
""", unsafe_allow_html=True)
st.audio(uploaded_file, format="audio/wav")
with col2:
# Save uploaded file temporarily
temp_file_path = save_uploaded_file(uploaded_file)
try:
# Get predictions using predict_genre function
predictions = predict_genre(temp_file_path)
if predictions is None:
st.error("Could not process the audio file. Please check if the model is loaded correctly.")
return
genre_labels = ['blues', 'classical', 'country', 'disco',
'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock']
# Get the predicted genre and probabilities
genre_index = np.argmax(predictions[0])
probabilities = predictions[0]
predicted_genre = genre_labels[genre_index]
confidence = probabilities[genre_index] * 100
# Display predicted genre
st.markdown(f"""
<div class="genre-box">
<div class="genre-icon">{genre_icons.get(predicted_genre, '🎵')}</div>
<h2 style="animation: rainbow 8s linear infinite;">
{predicted_genre.upper()}
</h2>
<div style="position: relative; background: rgba(255,255,255,0.1); border-radius: 12px; margin: 10px 0;">
<div class="confidence-bar" style="width: {confidence}%;">
<span class="progress-label">{confidence:.1f}%</span>
</div>
</div>
</div>
""", unsafe_allow_html=True)
# Display all genre probabilities
st.markdown("### Genre Probabilities")
for label, prob in zip(genre_labels, probabilities):
prob_percentage = prob * 100
st.markdown(f"""
<div class="genre-box" style="padding: 10px; margin: 5px 0;">
<div style="display: flex; align-items: center;">
<div style="width: 100px;">{genre_icons.get(label, '🎵')} {label}</div>
<div style="flex-grow: 1; background: rgba(255,255,255,0.1); border-radius: 10px; margin-left: 10px;">
<div class="confidence-bar" style="width: {prob_percentage}%;">
<span class="progress-label">{prob_percentage:.1f}%</span>
</div>
</div>
</div>
</div>
""", unsafe_allow_html=True)
except Exception as e:
st.error(f"Error processing audio: {str(e)}")
finally:
# Clean up temporary file
if os.path.exists(temp_file_path):
os.remove(temp_file_path)
st.markdown(f"""
<div class="footer">
<p>🎵 Music Genre Classifier | Made with ❤️ </p>
<p style="animation: rainbow 8s linear infinite;">Powered by VEDANT KASAR✨</p>
</div>
""", unsafe_allow_html=True)
if __name__ == "__main__":
# Configure page
st.set_page_config(
page_title="MusicClassifier",
layout="wide",
initial_sidebar_state="collapsed"
)
# Clear tensorflow session at startup
tf.keras.backend.clear_session()
# Run main app
main()