Spaces:
Sleeping
Sleeping
Create backup app.py
Browse files- backup app.py +112 -0
backup app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import librosa
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import requests
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
# Load model - added caching
|
| 10 |
+
model = tf.keras.models.load_model("animal_sound_cnn.h5", compile=False)
|
| 11 |
+
|
| 12 |
+
# Updated class mapping
|
| 13 |
+
class_names = {
|
| 14 |
+
0: "Lion", 1: "Donkey", 2: "Cow", 3: "Cat", 4: "Dog",
|
| 15 |
+
5: "Sheep", 6: "Frog", 7: "Bird", 8: "Monkey", 9: "Chicken"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Fixed image URLs with working links
|
| 19 |
+
image_urls = {
|
| 20 |
+
"Lion": "https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg",
|
| 21 |
+
"Donkey": "https://upload.wikimedia.org/wikipedia/commons/8/85/%C3%82ne_d%27Ethiopie.jpg",
|
| 22 |
+
"Cow": "https://upload.wikimedia.org/wikipedia/commons/0/0c/Cow_female_black_white.jpg",
|
| 23 |
+
"Cat": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Black_and_white_cat_on_rug.jpg",
|
| 24 |
+
"Dog": "https://upload.wikimedia.org/wikipedia/commons/d/db/Heterochromia_dog%2C_Struga.jpg",
|
| 25 |
+
"Sheep": "https://upload.wikimedia.org/wikipedia/commons/2/2c/Flock_of_sheep.jpg",
|
| 26 |
+
"Frog": "https://upload.wikimedia.org/wikipedia/commons/c/cb/The_Green_and_Golden_Bell_Frog.jpg",
|
| 27 |
+
"Bird": "https://upload.wikimedia.org/wikipedia/commons/f/fb/Brown_thrasher_in_CP_%2802147%29.jpg",
|
| 28 |
+
"Monkey": "https://upload.wikimedia.org/wikipedia/commons/c/c8/Monkey_eating.jpg",
|
| 29 |
+
"Chicken": "https://upload.wikimedia.org/wikipedia/commons/b/b2/Gallus_gallus_domesticus_487567856.jpg"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
def download_image(url):
|
| 33 |
+
"""Download image with error handling"""
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(url, timeout=10)
|
| 36 |
+
response.raise_for_status()
|
| 37 |
+
return Image.open(BytesIO(response.content))
|
| 38 |
+
except Exception:
|
| 39 |
+
# Return blank image if download fails
|
| 40 |
+
return Image.new('RGB', (300, 200), color='gray')
|
| 41 |
+
|
| 42 |
+
def preprocess(audio_path):
|
| 43 |
+
"""Audio preprocessing with error handling"""
|
| 44 |
+
try:
|
| 45 |
+
y, sr = librosa.load(audio_path, sr=22050, duration=3) # Limit to 3s
|
| 46 |
+
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
|
| 47 |
+
mfcc_mean = np.mean(mfcc, axis=1)
|
| 48 |
+
return mfcc_mean.reshape(1, 40, 1, 1).astype(np.float32)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
raise ValueError(f"Audio processing error: {str(e)}")
|
| 51 |
+
|
| 52 |
+
def predict(audio_path):
|
| 53 |
+
try:
|
| 54 |
+
# Preprocess audio
|
| 55 |
+
X = preprocess(audio_path)
|
| 56 |
+
|
| 57 |
+
# Make prediction
|
| 58 |
+
pred = model.predict(X, verbose=0) # Disable verbose output
|
| 59 |
+
class_id = np.argmax(pred)
|
| 60 |
+
confidence = pred[0][class_id]
|
| 61 |
+
class_name = class_names.get(class_id, f"Unknown ({class_id})")
|
| 62 |
+
|
| 63 |
+
# Get image
|
| 64 |
+
img = download_image(image_urls.get(class_name, ""))
|
| 65 |
+
|
| 66 |
+
# Format output
|
| 67 |
+
text_output = (f"Predicted animal: {class_name}\n"
|
| 68 |
+
f"Confidence: {confidence*100:.2f}%")
|
| 69 |
+
return text_output, img
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"Error: {str(e)}", download_image("") # Return error with blank image
|
| 73 |
+
|
| 74 |
+
# Create interface
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("# 🐾 Animal Sound Classifier")
|
| 77 |
+
gr.Markdown("Upload an animal sound (3-5 seconds) to identify the animal")
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
audio_input = gr.Audio(
|
| 81 |
+
sources=["upload", "microphone"],
|
| 82 |
+
type="filepath",
|
| 83 |
+
label="Animal Sound",
|
| 84 |
+
max_length=5 # Limit recording to 5 seconds
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
btn = gr.Button("Identify Animal", variant="primary")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
text_output = gr.Textbox(label="Prediction Result", interactive=False)
|
| 91 |
+
image_output = gr.Image(label="Animal Image", height=300)
|
| 92 |
+
|
| 93 |
+
btn.click(
|
| 94 |
+
fn=predict,
|
| 95 |
+
inputs=audio_input,
|
| 96 |
+
outputs=[text_output, image_output]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
gr.Examples(
|
| 100 |
+
examples=[
|
| 101 |
+
["examples/lion_roar.wav"],
|
| 102 |
+
["examples/dog_bark.wav"],
|
| 103 |
+
["examples/bird_chirp.wav"]
|
| 104 |
+
],
|
| 105 |
+
inputs=audio_input,
|
| 106 |
+
outputs=[text_output, image_output],
|
| 107 |
+
fn=predict,
|
| 108 |
+
cache_examples=True
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Launch with error display enabled
|
| 112 |
+
demo.launch(show_error=True)
|