Spaces:
Sleeping
Sleeping
Create appp.py
Browse files
appp.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from scipy.io import wavfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load model and label encoder
|
| 9 |
+
model = tf.keras.models.load_model("animal_sound_cnn.keras")
|
| 10 |
+
label_encoder = joblib.load("label_encoder.joblib")
|
| 11 |
+
|
| 12 |
+
def preprocess_audio(audio_path, target_shape=(64, 64)):
|
| 13 |
+
"""
|
| 14 |
+
Simplified audio preprocessing using only numpy/scipy
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
# 1. Read WAV file
|
| 18 |
+
sr, y = wavfile.read(audio_path)
|
| 19 |
+
|
| 20 |
+
# Convert to mono if stereo
|
| 21 |
+
if len(y.shape) > 1:
|
| 22 |
+
y = y.mean(axis=1)
|
| 23 |
+
|
| 24 |
+
# 2. Simple spectrogram using STFT
|
| 25 |
+
f, t, spec = tf.signal.stft(
|
| 26 |
+
y,
|
| 27 |
+
frame_length=256,
|
| 28 |
+
frame_step=128,
|
| 29 |
+
fft_length=256
|
| 30 |
+
)
|
| 31 |
+
spectrogram = np.abs(spec)
|
| 32 |
+
|
| 33 |
+
# 3. Resize to target dimensions
|
| 34 |
+
spectrogram = tf.image.resize(
|
| 35 |
+
tf.expand_dims(spectrogram, -1),
|
| 36 |
+
target_shape
|
| 37 |
+
).numpy()
|
| 38 |
+
|
| 39 |
+
# 4. Normalize and add batch dimension
|
| 40 |
+
spectrogram = (spectrogram - spectrogram.min()) / (spectrogram.max() - spectrogram.min())
|
| 41 |
+
return spectrogram[np.newaxis, ..., np.newaxis].astype(np.float32)
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Preprocessing error: {str(e)}")
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
def predict(audio_path):
|
| 48 |
+
try:
|
| 49 |
+
# 1. Preprocess audio
|
| 50 |
+
spectrogram = preprocess_audio(audio_path)
|
| 51 |
+
if spectrogram is None:
|
| 52 |
+
return "Error: Could not process audio"
|
| 53 |
+
|
| 54 |
+
# 2. Debug log input shape
|
| 55 |
+
print(f"Input shape: {spectrogram.shape}")
|
| 56 |
+
|
| 57 |
+
# 3. Predict
|
| 58 |
+
pred = model.predict(spectrogram)
|
| 59 |
+
animal = label_encoder.inverse_transform([np.argmax(pred)])[0]
|
| 60 |
+
|
| 61 |
+
return animal
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Prediction error: {str(e)}"
|
| 65 |
+
|
| 66 |
+
# Minimal requirements.txt needed:
|
| 67 |
+
# tensorflow>=2.16.0
|
| 68 |
+
# scikit-learn
|
| 69 |
+
# joblib
|
| 70 |
+
# numpy
|
| 71 |
+
# gradio
|
| 72 |
+
# scipy
|
| 73 |
+
|
| 74 |
+
gr.Interface(
|
| 75 |
+
fn=predict,
|
| 76 |
+
inputs=gr.Audio(type="filepath"),
|
| 77 |
+
outputs="label",
|
| 78 |
+
title="Animal Sound Classifier",
|
| 79 |
+
description="Upload a short audio clip (3-5 seconds) of an animal sound",
|
| 80 |
+
examples=["example.wav"] if os.path.exists("example.wav") else None
|
| 81 |
+
).launch()
|