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 - added caching | |
| model = tf.keras.models.load_model("animal_sound_cnn.h5", compile=False) | |
| # Updated class mapping | |
| 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 | |
| # Updated reliable image URLs | |
| # Fully working image URLs | |
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| import requests | |
| from io import BytesIO | |
| # Working image URLs | |
| image_urls = { | |
| "Lion": "https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg", | |
| "Donkey": "https://upload.wikimedia.org/wikipedia/commons/8/85/%C3%82ne_d%27Ethiopie.jpg", | |
| "Cow": "https://upload.wikimedia.org/wikipedia/commons/0/0c/Cow_female_black_white.jpg", | |
| "Cat": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Black_and_white_cat_on_rug.jpg", | |
| "Dog": "https://upload.wikimedia.org/wikipedia/commons/d/db/Heterochromia_dog%2C_Struga.jpg", | |
| "Sheep": "https://upload.wikimedia.org/wikipedia/commons/2/2c/Flock_of_sheep.jpg", | |
| "Frog": "https://upload.wikimedia.org/wikipedia/commons/c/cb/The_Green_and_Golden_Bell_Frog.jpg", | |
| "Bird": "https://upload.wikimedia.org/wikipedia/commons/f/fb/Brown_thrasher_in_CP_%2802147%29.jpg", | |
| "Monkey": "https://upload.wikimedia.org/wikipedia/commons/c/c8/Monkey_eating.jpg", | |
| "Chicken": "https://upload.wikimedia.org/wikipedia/commons/b/b2/Gallus_gallus_domesticus_487567856.jpg" | |
| } | |
| def download_image(url): | |
| """Download and resize image, with reliable request headers""" | |
| try: | |
| headers = {"User-Agent": "Mozilla/5.0"} | |
| response = requests.get(url, headers=headers, timeout=10) | |
| response.raise_for_status() | |
| img = Image.open(BytesIO(response.content)).convert("RGB") | |
| img = img.resize((300, 300)) | |
| return np.array(img) | |
| except Exception as e: | |
| print(f"Image download failed: {e}") | |
| return np.full((300, 300, 3), 128, dtype=np.uint8) # gray | |
| def preprocess(audio_path): | |
| """Audio preprocessing with error handling""" | |
| try: | |
| y, sr = librosa.load(audio_path, sr=22050, duration=3) # Limit to 3s | |
| mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40) | |
| mfcc_mean = np.mean(mfcc, axis=1) | |
| return mfcc_mean.reshape(1, 40, 1, 1).astype(np.float32) | |
| except Exception as e: | |
| raise ValueError(f"Audio processing error: {str(e)}") | |
| def predict(audio_path): | |
| try: | |
| # Preprocess audio | |
| X = preprocess(audio_path) | |
| # Make prediction | |
| pred = model.predict(X, verbose=0) # Disable verbose output | |
| class_id = np.argmax(pred) | |
| confidence = pred[0][class_id] | |
| class_name = class_names.get(class_id, f"Unknown ({class_id})") | |
| # Get image | |
| img = download_image(image_urls.get(class_name, "")) | |
| # 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)}", download_image("") # Return error with blank image | |
| # Create interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🐾 Animal Sound Classifier") | |
| gr.Markdown("Upload an animal sound (3-5 seconds) to identify the animal") | |
| with gr.Row(): | |
| audio_input = gr.Audio( | |
| sources=["upload", "microphone"], | |
| type="filepath", | |
| label="Animal Sound", | |
| max_length=5 # Limit recording to 5 seconds | |
| ) | |
| btn = gr.Button("Identify Animal", variant="primary") | |
| with gr.Row(): | |
| text_output = gr.Textbox(label="Prediction Result", interactive=False) | |
| image_output = gr.Image(label="Animal Image", height=300) | |
| btn.click( | |
| fn=predict, | |
| inputs=audio_input, | |
| outputs=[text_output, image_output] | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["examples/lion_roar.wav"], | |
| ["examples/dog_bark.wav"], | |
| ["examples/bird_chirp.wav"] | |
| ], | |
| inputs=audio_input, | |
| outputs=[text_output, image_output], | |
| fn=predict, | |
| cache_examples=True | |
| ) | |
| # Launch with error display enabled | |
| demo.launch(show_error=True) |