Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import librosa
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def get_audio(audio):
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import library
|
| 2 |
import gradio as gr
|
| 3 |
+
import librosa
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
from tensorflow.keras.layers.experimental import preprocessing
|
| 9 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 10 |
+
from tensorflow.keras.models import Sequential
|
| 11 |
+
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization, Input
|
| 12 |
+
|
| 13 |
+
def get_waveform_label(file):
|
| 14 |
+
lab = tf.strings.split(file, os.path.sep)[-2]
|
| 15 |
+
audio_binary = tf.io.read_file(file)
|
| 16 |
+
audio, _ = tf.audio.decode_wav(audio_binary)
|
| 17 |
+
waveform=tf.squeeze(audio, axis=-1)
|
| 18 |
+
return waveform
|
| 19 |
+
|
| 20 |
+
def get_spectrogram_label(audio):
|
| 21 |
+
padding = tf.zeros([300000]-tf.shape(audio), dtype=tf.float32)
|
| 22 |
+
wave = tf.cast(audio, tf.float32)
|
| 23 |
+
eq_length = tf.concat([wave, padding], 0)
|
| 24 |
+
spectrogram = tf.signal.stft(eq_length, frame_length=210, frame_step=110)
|
| 25 |
+
spectrogram = tf.abs(spectrogram)
|
| 26 |
+
spectrogram = tf.expand_dims(spectrogram, -1)
|
| 27 |
+
return spectrogram
|
| 28 |
+
|
| 29 |
+
|
| 30 |
def get_audio(audio):
|
| 31 |
+
audio_waveform = get_waveform_label(audio)
|
| 32 |
+
audio_spect = get_spectrogram_label(audio_waveform)
|
| 33 |
+
final_feat = np.array([audio_spect])
|
| 34 |
+
res = np.argmax(model.predict(final_feat),axis=1)
|
| 35 |
+
if res == 1:
|
| 36 |
+
res ="Dog Audio";
|
| 37 |
+
else:
|
| 38 |
+
res = "Cat Audio"
|
| 39 |
+
return res
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
iface = gr.Interface(fn=get_audio,inputs=["audio", "audio"],title="CAT_DOG AUDIO CLASSIFICATION",outputs="html").launch()
|