Spaces:
Runtime error
Runtime error
AbdullaShafeeg
commited on
Commit
·
28cc6c6
1
Parent(s):
6c938cd
big update
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ import wave
|
|
10 |
import io
|
11 |
from scipy.io import wavfile
|
12 |
import pydub
|
|
|
13 |
|
14 |
# MODEL LOADING and INITIALISATION
|
15 |
model = torch.jit.load("snorenetv1_small.ptl")
|
@@ -62,46 +63,55 @@ if upload_file is not None:
|
|
62 |
|
63 |
# with open("output.wav", 'wb') as wav_file:
|
64 |
# wav_file.write(wav_make)
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
|
107 |
|
|
|
10 |
import io
|
11 |
from scipy.io import wavfile
|
12 |
import pydub
|
13 |
+
import time
|
14 |
|
15 |
# MODEL LOADING and INITIALISATION
|
16 |
model = torch.jit.load("snorenetv1_small.ptl")
|
|
|
63 |
|
64 |
# with open("output.wav", 'wb') as wav_file:
|
65 |
# wav_file.write(wav_make)
|
66 |
+
file_uploaded = False
|
67 |
+
if file_uploaded == False:
|
68 |
+
st.write("Uploading File.....")
|
69 |
+
audio = pydub.AudioSegment.from_wav(upload_file)
|
70 |
+
file_uploaded = True
|
71 |
+
time.sleep(2)
|
72 |
+
st.write("File Uploaded!")
|
73 |
+
|
74 |
+
if file_uploaded == True:
|
75 |
+
st.write("Analysing...")
|
76 |
+
audio.export(upload_file.name, format='wav')
|
77 |
+
sr, waveform = wavfile.read(upload_file.name)
|
78 |
+
snore = 0
|
79 |
+
other = 0
|
80 |
+
s=0
|
81 |
+
n=16000
|
82 |
+
endReached = False
|
83 |
+
|
84 |
+
while(endReached==False):
|
85 |
+
input_tensor = torch.tensor(waveform[s:n]).unsqueeze(0).to(torch.float32)
|
86 |
+
result = model(input_tensor)
|
87 |
+
if np.abs(result[0][0]) > np.abs(result[0][1]):
|
88 |
+
other += 1
|
89 |
+
else:
|
90 |
+
snore += 1
|
91 |
+
s += 16000
|
92 |
+
n += 16000
|
93 |
+
if(n >= len(waveform)):
|
94 |
+
endReached = True
|
95 |
+
|
96 |
+
# PERCENTAGE OF SNORING PLOT
|
97 |
+
|
98 |
+
total = snore + other
|
99 |
+
snore_percentage = (snore / total) * 100
|
100 |
+
other_percentage = (other / total) * 100
|
101 |
+
|
102 |
+
categories = ["Snore", "Other"]
|
103 |
+
percentages = [snore_percentage, other_percentage]
|
104 |
+
|
105 |
+
plt.figure(figsize=(8, 4))
|
106 |
+
plt.barh(categories, percentages, color=['#ff0033', '#00ffee'])
|
107 |
+
plt.xlabel('Percentage')
|
108 |
+
plt.title('Percentage of Snoring')
|
109 |
+
plt.xlim(0, 100)
|
110 |
+
|
111 |
+
for i, percentage in enumerate(percentages):
|
112 |
+
plt.text(percentage, i, f' {percentage:.2f}%', va='center')
|
113 |
+
st.write("DONE")
|
114 |
+
st.pyplot(plt)
|
115 |
|
116 |
|
117 |
|