-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
51 lines (34 loc) · 1.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import pydub
import streamlit as st
import tensorflow as tf
from main import model
batching_size = 12000
def handle_uploaded_audio_file(uploaded_file):
a = pydub.AudioSegment.from_wav(uploaded_file)
samples = a.get_array_of_samples()
fp_arr = np.array(samples).T.astype(np.float32)
fp_arr /= np.iinfo(samples.typecode).max
fp_arr = fp_arr.reshape(fp_arr.shape[0], 1)
fp_arr = tf.convert_to_tensor(fp_arr, dtype=tf.float32)
st.write(a)
return fp_arr
def audio_to_display(audio):
audio_file = open(audio, 'rb')
audio_bytes = audio_file.read()
return audio_bytes
def inference_preprocess(uploaded_file):
audio = handle_uploaded_audio_file(uploaded_file)
audio_len = audio.shape[0]
batches = []
for i in range(0, audio_len - batching_size, batching_size):
batches.append(audio[i:i + batching_size])
batches.append(audio[-batching_size:])
diff = audio_len - (i + batching_size)
return tf.stack(batches), diff
def predict(uploaded_file):
test_data, diff = inference_preprocess(uploaded_file)
predictions = model.predict(test_data)
final_op = tf.reshape(predictions[:-1], ((predictions.shape[0] - 1) * predictions.shape[1], 1))
final_op = tf.concat((final_op, predictions[-1][-diff:]), axis=0)
return final_op