suriya7 commited on
Commit
f00407e
·
verified ·
1 Parent(s): e25e7dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -22
app.py CHANGED
@@ -7,32 +7,40 @@ model = tf.keras.models.load_model('capuchin_bird_audio.h5')
7
  class_names = ['This Is Not A Capuchin bird','It is 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)
18
- spectrogram = tf.signal.stft(wav, frame_length=320, frame_step=32)
19
- spectrogram = tf.abs(spectrogram)
20
- spectrogram = tf.expand_dims(spectrogram, axis=2)
21
- spectrogram = tf.expand_dims(spectrogram, axis=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
@@ -45,5 +53,5 @@ iface = gr.Interface(
45
  )
46
 
47
  # Launch the interface on localhost
48
- iface.launch()
49
 
 
7
  class_names = ['This Is Not A Capuchin bird','It is a capuchin Bird']
8
  # Function to preprocess input for the model
9
  def test_preprocess_1(file_path):
10
+ _, file_extension = os.path.splitext(file_path)
11
+
12
+ if file_extension.lower() == '.wav':
13
+ file_contents = tf.io.read_file(file_path)
14
+ wav, sample_rate = tf.audio.decode_wav(file_contents, desired_channels=1)
15
+ wav = tf.squeeze(wav, axis=-1)
16
+ sample_rate = tf.cast(sample_rate, dtype=tf.int64)
17
+ wav = tfio.audio.resample(wav, rate_in=sample_rate, rate_out=16000)
18
+ wav = wav[:48000]
19
+ zero_padding = tf.zeros([48000] - tf.shape(wav), dtype=tf.float32)
20
+ wav = tf.concat([zero_padding, wav], 0)
21
+ spectrogram = tf.signal.stft(wav, frame_length=320, frame_step=32)
22
+ spectrogram = tf.abs(spectrogram)
23
+ spectrogram = tf.expand_dims(spectrogram, axis=2)
24
+ spectrogram = tf.expand_dims(spectrogram, axis=0)
25
+ return spectrogram
26
+ else:
27
+ return False
28
 
29
  # Function to make predictions
30
  def predict_audio(wav):
31
  input_data = test_preprocess_1(wav)
32
+ if input_data:
33
+ prediction = model.predict(input_data)
34
+
35
+ # Threshold logic
36
+ if prediction > 0.5:
37
+ result = class_names[1]
38
+ else:
39
+ result = class_names[0]
40
+
41
+ return result
42
  else:
43
+ return "please upload a wav format"
 
 
44
 
45
 
46
  # Gradio Interface
 
53
  )
54
 
55
  # Launch the interface on localhost
56
+ iface.launch(share=True)
57