Roketman / app3.py
amritn8's picture
Rename app.py to app3.py
58d745f verified
import gradio as gr
import tensorflow as tf
import librosa
import numpy as np
# Load model
model = tf.keras.models.load_model("animal_sound_cnn.h5", compile=False)
# Updated class mapping with English names
class_names = {
0: "Lion", 1: "Donkey", 2: "Cow", 3: "Cat", 4: "Dog",
5: "Sheep", 6: "Frog", 7: "Bird", 8: "Monkey", 9: "Chicken"
}
# Updated image URLs with English keys
image_urls = {
"Lion": "https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg",
"Donkey": "https://upload.wikimedia.org/wikipedia/commons/6/6f/Donkey_in_the_Garden.JPG",
"Cow": "https://upload.wikimedia.org/wikipedia/commons/1/12/Cow_female_black_white.jpg",
"Cat": "https://upload.wikimedia.org/wikipedia/commons/a/a3/81_INF_DIV_SSI.jpg",
"Dog": "https://upload.wikimedia.org/wikipedia/commons/3/32/Adult_Retriever_Dog.jpg",
"Sheep": "https://upload.wikimedia.org/wikipedia/commons/6/60/Sheep_in_a_field.JPG",
"Frog": "https://upload.wikimedia.org/wikipedia/commons/2/2d/European_common_frog_%28Rana_temporaria%29.jpg",
"Bird": "https://upload.wikimedia.org/wikipedia/commons/5/55/Passer_domesticus_male.jpg",
"Monkey": "https://upload.wikimedia.org/wikipedia/commons/e/e4/Macaque_in_Leipzig_Zoo_2008-03-09.jpg",
"Chicken": "https://upload.wikimedia.org/wikipedia/commons/3/37/Chicken_in_the_Netherlands.jpg"
}
def preprocess(audio_path):
y, sr = librosa.load(audio_path, sr=22050)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
mfcc_mean = np.mean(mfcc, axis=1)
reshaped = mfcc_mean.reshape(1, 40, 1, 1)
return reshaped.astype(np.float32)
def predict(audio_path):
try:
X = preprocess(audio_path)
pred = model.predict(X)
class_id = np.argmax(pred)
confidence = pred[0][class_id]
class_name = class_names.get(class_id, f"Unknown ({class_id})")
img_url = image_urls.get(class_name, None)
text_output = f"Predicted animal: {class_name}\nClass: {class_name} | Confidence: {confidence:.2f}"
return text_output, img_url
except Exception as e:
return f"Prediction failed: {e}", None
gr.Interface(
fn=predict,
inputs=gr.Audio(type="filepath"),
outputs=[gr.Textbox(), gr.Image()],
title="Animal Sound Classifier"
).launch()