amritn8 commited on
Commit
4bbddb8
·
verified ·
1 Parent(s): 35b6bff

Delete apppp.py

Browse files
Files changed (1) hide show
  1. apppp.py +0 -68
apppp.py DELETED
@@ -1,68 +0,0 @@
1
- import tensorflow as tf
2
- import joblib
3
- import numpy as np
4
- import gradio as gr
5
- import librosa
6
- import soundfile as sf
7
- import os
8
-
9
- # Load model and label encoder
10
- model = tf.keras.models.load_model("animal_sound_cnn.keras")
11
- label_encoder = joblib.load("label_encoder.joblib")
12
-
13
- def preprocess_audio(audio_path, target_shape=(64, 64)):
14
- """
15
- Convert audio file to spectrogram with correct shape for model
16
- """
17
- try:
18
- # 1. Load audio file
19
- y, sr = librosa.load(audio_path, sr=None)
20
-
21
- # 2. Create mel spectrogram
22
- S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=target_shape[0])
23
- log_S = librosa.power_to_db(S, ref=np.max)
24
-
25
- # 3. Resize to target dimensions (FIXED THIS LINE)
26
- if log_S.shape[1] < target_shape[1]:
27
- pad_width = target_shape[1] - log_S.shape[1]
28
- log_S = np.pad(log_S, ((0, 0), (0, pad_width)), mode='constant')
29
- else:
30
- log_S = log_S[:, :target_shape[1]]
31
-
32
- # 4. Add channel dimension and batch dimension
33
- spectrogram = log_S[np.newaxis, ..., np.newaxis]
34
-
35
- return spectrogram.astype(np.float32)
36
-
37
- except Exception as e:
38
- print(f"Preprocessing error: {str(e)}")
39
- return None
40
-
41
- def predict(audio_path):
42
- try:
43
- # 1. Preprocess audio
44
- spectrogram = preprocess_audio(audio_path)
45
- if spectrogram is None:
46
- return "Error processing audio"
47
-
48
- # 2. Check input shape matches model expectations
49
- print(f"Input shape: {spectrogram.shape}") # Debug log
50
-
51
- # 3. Predict
52
- pred = model.predict(spectrogram)
53
- animal = label_encoder.inverse_transform([np.argmax(pred)])[0]
54
-
55
- return animal
56
- except Exception as e:
57
- return f"Prediction error: {str(e)}"
58
-
59
- # Update requirements.txt to include:
60
- # librosa==0.10.1
61
- # soundfile==0.12.1
62
-
63
- gr.Interface(
64
- fn=predict,
65
- inputs=gr.Audio(type="filepath"),
66
- outputs="label",
67
- examples=["example1.wav", "example2.wav"] if os.path.exists("example1.wav") else None
68
- ).launch()