Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,19 +4,14 @@ import gradio as gr
|
|
4 |
|
5 |
# Load your pre-trained model
|
6 |
model = tf.keras.models.load_model('capuchin_bird_audio.h5')
|
7 |
-
|
8 |
-
# Function to
|
9 |
-
def
|
10 |
-
file_contents = tf.io.read_file(
|
11 |
wav, sample_rate = tf.audio.decode_wav(file_contents, desired_channels=1)
|
12 |
wav = tf.squeeze(wav, axis=-1)
|
13 |
sample_rate = tf.cast(sample_rate, dtype=tf.int64)
|
14 |
wav = tfio.audio.resample(wav, rate_in=sample_rate, rate_out=16000)
|
15 |
-
return wav
|
16 |
-
|
17 |
-
# Function to preprocess input for the model
|
18 |
-
def test_preprocess_1(file_path):
|
19 |
-
wav = load_wav_mono(file_path)
|
20 |
wav = wav[:48000]
|
21 |
zero_padding = tf.zeros([48000] - tf.shape(wav), dtype=tf.float32)
|
22 |
wav = tf.concat([zero_padding, wav], 0)
|
@@ -27,27 +22,29 @@ def test_preprocess_1(file_path):
|
|
27 |
return spectrogram
|
28 |
|
29 |
# Function to make predictions
|
30 |
-
def predict_audio(
|
31 |
-
input_data = test_preprocess_1(
|
32 |
prediction = model.predict(input_data)
|
33 |
-
|
34 |
# Threshold logic
|
35 |
if prediction > 0.5:
|
36 |
-
result =
|
37 |
else:
|
38 |
-
result =
|
39 |
-
|
40 |
return result
|
41 |
|
|
|
42 |
# Gradio Interface
|
43 |
iface = gr.Interface(
|
44 |
fn=predict_audio,
|
45 |
title='Capuchin Bird Classification',
|
46 |
-
inputs=gr.Audio(sources=['upload'],
|
47 |
-
outputs=
|
48 |
-
live=True,
|
49 |
-
|
50 |
)
|
51 |
|
52 |
# Launch the interface on localhost
|
53 |
iface.launch()
|
|
|
|
|
|
|
|
4 |
|
5 |
# Load your pre-trained model
|
6 |
model = tf.keras.models.load_model('capuchin_bird_audio.h5')
|
7 |
+
class_names = ['It is a capuchin Bird','This Is Not A Capuchin bird']
|
8 |
+
# Function to preprocess input for the model
|
9 |
+
def test_preprocess_1(file_path):
|
10 |
+
file_contents = tf.io.read_file(file_path)
|
11 |
wav, sample_rate = tf.audio.decode_wav(file_contents, desired_channels=1)
|
12 |
wav = tf.squeeze(wav, axis=-1)
|
13 |
sample_rate = tf.cast(sample_rate, dtype=tf.int64)
|
14 |
wav = tfio.audio.resample(wav, rate_in=sample_rate, rate_out=16000)
|
|
|
|
|
|
|
|
|
|
|
15 |
wav = wav[:48000]
|
16 |
zero_padding = tf.zeros([48000] - tf.shape(wav), dtype=tf.float32)
|
17 |
wav = tf.concat([zero_padding, wav], 0)
|
|
|
22 |
return spectrogram
|
23 |
|
24 |
# Function to make predictions
|
25 |
+
def predict_audio(wav):
|
26 |
+
input_data = test_preprocess_1(wav)
|
27 |
prediction = model.predict(input_data)
|
28 |
+
|
29 |
# Threshold logic
|
30 |
if prediction > 0.5:
|
31 |
+
result = class_names[1]
|
32 |
else:
|
33 |
+
result = class_names[0]
|
34 |
+
|
35 |
return result
|
36 |
|
37 |
+
|
38 |
# Gradio Interface
|
39 |
iface = gr.Interface(
|
40 |
fn=predict_audio,
|
41 |
title='Capuchin Bird Classification',
|
42 |
+
inputs=gr.Audio(sources=['upload'],label="Input Audio",type="filepath"),
|
43 |
+
outputs='text',
|
|
|
|
|
44 |
)
|
45 |
|
46 |
# Launch the interface on localhost
|
47 |
iface.launch()
|
48 |
+
|
49 |
+
# Launch the interface on localhost
|
50 |
+
iface.launch()
|