Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -16,11 +16,25 @@ def load_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:
         | 
| @@ -64,12 +78,13 @@ if uploaded_file is not None: | |
| 64 | 
             
                    st.write("Predicting...")
         | 
| 65 | 
             
                    spectrogram = np.expand_dims(spectrogram, axis=0)  # Add batch dimension
         | 
| 66 | 
             
                    predictions = model.predict(spectrogram)
         | 
| 67 | 
            -
                     | 
|  | |
| 68 |  | 
| 69 | 
             
                    # Display the results
         | 
| 70 | 
             
                    st.write("Prediction Results:")
         | 
| 71 | 
            -
                    st.write(f"Predicted Class: { | 
| 72 | 
            -
                    st.write(f"Raw Model Output | 
| 73 | 
             
                else:
         | 
| 74 | 
             
                    st.write("Failed to process the audio file. Please try again with a different file.")
         | 
| 75 |  | 
|  | |
| 16 |  | 
| 17 | 
             
            model = load_model()
         | 
| 18 |  | 
| 19 | 
            +
            # Map Class Labels
         | 
| 20 | 
            +
            CLASS_LABELS = {
         | 
| 21 | 
            +
                0: 'Air Conditioner',
         | 
| 22 | 
            +
                1: 'Car Horn',
         | 
| 23 | 
            +
                2: 'Children Playing',
         | 
| 24 | 
            +
                3: 'Dog Bark',
         | 
| 25 | 
            +
                4: 'Drilling',
         | 
| 26 | 
            +
                5: 'Engine Idling',
         | 
| 27 | 
            +
                6: 'Gun Shot',
         | 
| 28 | 
            +
                7: 'Jackhammer',
         | 
| 29 | 
            +
                8: 'Siren',
         | 
| 30 | 
            +
                9: 'Street Music'
         | 
| 31 | 
            +
            }
         | 
| 32 | 
            +
             | 
| 33 | 
             
            # Preprocess audio into a spectrogram
         | 
| 34 | 
             
            def preprocess_audio(file_path, n_mels=128, fixed_time_steps=128):
         | 
| 35 | 
             
                try:
         | 
| 36 | 
             
                    y, sr = librosa.load(file_path, sr=None)
         | 
| 37 | 
            +
                    mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_mels, fmax=sr / 2)
         | 
| 38 | 
             
                    log_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
         | 
| 39 | 
             
                    log_spectrogram = log_spectrogram / np.max(np.abs(log_spectrogram))
         | 
| 40 | 
             
                    if log_spectrogram.shape[1] < fixed_time_steps:
         | 
|  | |
| 78 | 
             
                    st.write("Predicting...")
         | 
| 79 | 
             
                    spectrogram = np.expand_dims(spectrogram, axis=0)  # Add batch dimension
         | 
| 80 | 
             
                    predictions = model.predict(spectrogram)
         | 
| 81 | 
            +
                    predicted_class_index = np.argmax(predictions, axis=-1)[0]
         | 
| 82 | 
            +
                    predicted_class_label = CLASS_LABELS.get(predicted_class_index, "Unknown")
         | 
| 83 |  | 
| 84 | 
             
                    # Display the results
         | 
| 85 | 
             
                    st.write("Prediction Results:")
         | 
| 86 | 
            +
                    st.write(f"**Predicted Class:** {predicted_class_label} (Index: {predicted_class_index})")
         | 
| 87 | 
            +
                    st.write(f"**Raw Model Output:** {predictions}")
         | 
| 88 | 
             
                else:
         | 
| 89 | 
             
                    st.write("Failed to process the audio file. Please try again with a different file.")
         | 
| 90 |  |