Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| import librosa | |
| import numpy as np | |
| from PIL import Image | |
| import requests | |
| from io import BytesIO | |
| # 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" | |
| } | |
| # Fixed image URLs with working links | |
| image_urls = { | |
| "Lion": "https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg", | |
| "Donkey": "https://upload.wikimedia.org/wikipedia/commons/b/b2/Donkey_%281%29.jpg", | |
| "Cow": "https://upload.wikimedia.org/wikipedia/commons/0/0c/Cow_female_black_white.jpg", | |
| "Cat": "https://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg", | |
| "Dog": "https://upload.wikimedia.org/wikipedia/commons/d/d9/Collage_of_Nine_Dogs.jpg", | |
| "Sheep": "https://upload.wikimedia.org/wikipedia/commons/8/8f/Sheep_in_Norton%2C_Isle_of_Wight_02.jpg", | |
| "Frog": "https://upload.wikimedia.org/wikipedia/commons/0/0a/Rana_temporaria_%28Marek_Szczepanek%29.jpg", | |
| "Bird": "https://upload.wikimedia.org/wikipedia/commons/2/2e/Turkey_bird_J1.jpg", | |
| "Monkey": "https://upload.wikimedia.org/wikipedia/commons/4/43/Bonnet_macaque_%28Macaca_radiata%29_Photograph_By_Shantanu_Kuveskar.jpg", | |
| "Chicken": "https://upload.wikimedia.org/wikipedia/commons/5/5f/Kolm%C3%A5rden_Wolf.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 download_image(url): | |
| try: | |
| response = requests.get(url) | |
| return Image.open(BytesIO(response.content)) | |
| except: | |
| # Return a blank image if download fails | |
| return Image.new('RGB', (300, 200), color='gray') | |
| def predict(audio_path): | |
| try: | |
| # Preprocess audio | |
| X = preprocess(audio_path) | |
| # Make prediction | |
| pred = model.predict(X, verbose=0) # Disable prediction logs | |
| class_id = np.argmax(pred) | |
| confidence = pred[0][class_id] | |
| class_name = class_names.get(class_id, f"Unknown ({class_id})") | |
| # Get image | |
| img_url = image_urls.get(class_name) | |
| img = download_image(img_url) if img_url else None | |
| # Format output | |
| text_output = (f"Predicted animal: {class_name}\n" | |
| f"Confidence: {confidence*100:.2f}%") | |
| return text_output, img | |
| except Exception as e: | |
| return f"Error: {str(e)}", None | |
| # Create interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🐾 Animal Sound Classifier") | |
| gr.Markdown("Upload an animal sound to identify which animal it is!") | |
| with gr.Row(): | |
| audio_input = gr.Audio(type="filepath", label="Animal Sound") | |
| predict_btn = gr.Button("Identify Animal") | |
| with gr.Row(): | |
| text_output = gr.Textbox(label="Prediction Result", interactive=False) | |
| image_output = gr.Image(label="Animal Image", height=300) | |
| predict_btn.click( | |
| fn=predict, | |
| inputs=audio_input, | |
| outputs=[text_output, image_output] | |
| ) | |
| gr.Examples( | |
| examples=["lion_roar.wav", "dog_bark.wav", "bird_chirp.wav"], # Add your example files | |
| inputs=audio_input | |
| ) | |
| demo.launch(show_error=True) |