thewise samusander commited on
Commit
9dc988e
0 Parent(s):

Duplicate from samusander/Snore.Ai

Browse files

Co-authored-by: Samuel Sander <samusander@users.noreply.huggingface.co>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +58 -0
  4. finalized_model.sav +0 -0
  5. requirements.txt +10 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Snore.Ai
3
+ emoji: 🌍
4
+ colorFrom: yellow
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 3.19.1
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: samusander/Snore.Ai
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports
2
+ import gradio as gr
3
+ import tensorflow_io as tfio
4
+ from speechbrain.pretrained import EncoderClassifier
5
+ import torchaudio
6
+ from sklearn.linear_model import LogisticRegression
7
+ import joblib
8
+ import tensorflow as tf
9
+ import numpy as np
10
+
11
+
12
+
13
+ # Utility function for loading audio files and making sure the sample rate is correct.
14
+ @tf.function
15
+ def load_wav_16k_mono(filename):
16
+ """Load a WAV file, convert it to a float tensor, resample to 16 kHz single-channel audio."""
17
+ file_contents = tf.io.read_file(filename)
18
+ wav, sample_rate = tf.audio.decode_wav(file_contents, desired_channels=1)
19
+ wav = tf.squeeze(wav, axis=-1)
20
+ sample_rate = tf.cast(sample_rate, dtype=tf.int64)
21
+ wav = tfio.audio.resample(wav, rate_in=sample_rate, rate_out=16000)
22
+ return wav
23
+
24
+ def extract_audio_embeddings(model, wav_audio_file_path: str) -> tuple:
25
+ """Feature extractor that embeds audio into a vector."""
26
+ signal, fs = torchaudio.load(wav_audio_file_path) # Reformat audio signal into a tensor
27
+ embeddings = model.encode_batch(
28
+ signal
29
+ ) # Pass tensor through pretrained neural net and extract representation
30
+ return embeddings
31
+
32
+
33
+ def detect_snoring(audio):
34
+ feature_extractor = EncoderClassifier.from_hparams(
35
+ "speechbrain/spkrec-xvect-voxceleb",
36
+ # run_opts={"device":"cuda"} # Uncomment this to run on GPU if you have one (optional)
37
+ )
38
+ filename = 'finalized_model.sav'
39
+ model = joblib.load(filename)
40
+
41
+ embeddings = extract_audio_embeddings(feature_extractor, audio)
42
+ embeddings_array = embeddings.cpu().numpy()[0]
43
+ output = model.predict_proba(embeddings_array)
44
+ output = np.round(output[:, 1])
45
+ if 1 in output:
46
+ output = "Snoring detected"
47
+ else:
48
+ output = "Snoring is not detected"
49
+ return output
50
+
51
+
52
+
53
+ # Defining the audio filepaths
54
+ audio = gr.inputs.Audio(type="filepath")
55
+
56
+ # Loading the gradio framwork
57
+ iface = gr.Interface(fn=detect_snoring,inputs=audio, outputs="text", title="Snore.AI", description="Detect Snotring with artificial intelligence.")
58
+ iface.launch()
finalized_model.sav ADDED
Binary file (4.96 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ scikit-learn
2
+ joblib
3
+ pandas
4
+ numpy
5
+ tensorflow
6
+ torch
7
+ tensorflow_io
8
+ pathlib
9
+ speechbrain
10
+ torchaudio