fadzwan commited on
Commit
2fceb69
·
verified ·
1 Parent(s): 824df80

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import librosa
5
+ import matplotlib.pyplot as plt
6
+ import librosa.display
7
+ import tempfile
8
+ import os
9
+
10
+ # Load the trained model
11
+ @st.cache_resource
12
+ def load_model():
13
+ model_path = "sound_classification_model.h5" # Replace with the path to your .h5 file
14
+ model = tf.keras.models.load_model(model_path)
15
+ return model
16
+
17
+ model = load_model()
18
+
19
+ # Preprocess audio into a spectrogram
20
+ def preprocess_audio(file_path, n_mels=128, fixed_time_steps=128):
21
+ try:
22
+ y, sr = librosa.load(file_path, sr=None)
23
+ mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_mels, fmax=sr/2)
24
+ log_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
25
+ log_spectrogram = log_spectrogram / np.max(np.abs(log_spectrogram))
26
+ if log_spectrogram.shape[1] < fixed_time_steps:
27
+ padding = fixed_time_steps - log_spectrogram.shape[1]
28
+ log_spectrogram = np.pad(log_spectrogram, ((0, 0), (0, padding)), mode='constant')
29
+ else:
30
+ log_spectrogram = log_spectrogram[:, :fixed_time_steps]
31
+ return np.expand_dims(log_spectrogram, axis=-1) # Add channel dimension for CNNs
32
+ except Exception as e:
33
+ print(f"Error processing {file_path}: {e}")
34
+ return None
35
+
36
+ # Streamlit app UI
37
+ st.title("Audio Spectrogram Prediction")
38
+ st.write("Upload an audio file to generate a spectrogram and predict its class using your trained model.")
39
+
40
+ # File upload widget
41
+ uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3"])
42
+
43
+ if uploaded_file is not None:
44
+ # Save the uploaded audio file to a temporary location
45
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
46
+ temp_audio_file.write(uploaded_file.read())
47
+ temp_audio_path = temp_audio_file.name
48
+
49
+ # Preprocess the audio into a spectrogram
50
+ st.write("Processing audio into a spectrogram...")
51
+ spectrogram = preprocess_audio(temp_audio_path)
52
+
53
+ if spectrogram is not None:
54
+ # Display the spectrogram
55
+ st.write("Generated Spectrogram:")
56
+ plt.figure(figsize=(10, 4))
57
+ librosa.display.specshow(spectrogram[:, :, 0], sr=22050, x_axis='time', y_axis='mel', fmax=8000, cmap='plasma')
58
+ plt.colorbar(format='%+2.0f dB')
59
+ plt.title('Mel-Spectrogram')
60
+ plt.tight_layout()
61
+ st.pyplot(plt)
62
+
63
+ # Predict using the model
64
+ st.write("Predicting...")
65
+ spectrogram = np.expand_dims(spectrogram, axis=0) # Add batch dimension
66
+ predictions = model.predict(spectrogram)
67
+ predicted_class = np.argmax(predictions, axis=-1) # Assuming classification
68
+
69
+ # Display the results
70
+ st.write("Prediction Results:")
71
+ st.write(f"Predicted Class: {int(predicted_class[0])}")
72
+ st.write(f"Raw Model Output: {predictions}")
73
+ else:
74
+ st.write("Failed to process the audio file. Please try again with a different file.")
75
+
76
+ # Optional: Clean up temporary file
77
+ os.remove(temp_audio_path)