-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
100 lines (80 loc) · 2.78 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
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
from json import load
from flask import Flask, render_template, send_from_directory, request
import os
from librosa.feature.spectral import mfcc
import pandas as pd
import pickle
import sys
import os
import shutil
import numpy as np
import librosa
# import tensorflow as tf
from tensorflow.keras.models import load_model
# import pyaudio
from deepspeech import Model
import scipy.io.wavfile as wav
import wave
from werkzeug.utils import secure_filename, send_file
from tensorflow.keras import backend as K
from scripts.utils import int_sequence_to_text
from scripts.Models import model_2
app = Flask(__name__)
MODEL_PATH = './model/deep.h5'
@app.route("/assets/<path:path>")
def static_dir(path):
return send_from_directory("static/assets", path)
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template("index.html")
def make_prediction():
deep_speech = model_2(input_dim=26,
filters=100,
kernel_size=4,
conv_stride=1,
conv_border_mode='valid',
units=250,
activation='tanh',
dropout_rate=0.2,
number_of_layers=2,
output_dim=29)
file_name = os.listdir("./uploads/audio")[0]
data,sample_rate=librosa.load(f'./uploads/audio/{file_name}')
mfccs = librosa.feature.mfcc(data, sr=16000,n_mfcc=26)
# def make_predictions(model,features):
predictions=[]
deep_speech.load_weights(MODEL_PATH)
# model = load_weight(MODEL_PATH)
for i in [mfccs]:
data_point=i.T
prediction = deep_speech.predict(np.expand_dims(i.T, axis=0))
output_length = [deep_speech.output_length(data_point.shape[0])]
pred_ints = (K.eval(K.ctc_decode(
prediction, output_length, greedy=False)[0][0])+1).flatten().tolist()
predicted = ''.join(int_sequence_to_text(pred_ints)).replace("<SPACE>", " ")
predictions.append(predicted)
return predictions
@app.route('/predict', methods=['GET', 'POST'])
def upload():
global class_names
if request.method == 'POST':
if os.path.isdir('./uploads/audio'):
shutil.rmtree('./uploads/audio')
os.mkdir('./uploads/audio')
# Get the file from post request
f = request.files['audio']
# Save the file to ./uploads
basepath = os.path.dirname(__file__)
file_path = os.path.join(
basepath, 'uploads/audio', secure_filename(f.filename))
f.save(file_path)
#Make Prediction
preds = make_prediction()
return preds[0]
@app.route("/about")
def about():
return "<h1>About</h1>"
if __name__ == '__main__':
port = int(os.environ.get("PORT", 33507))
app.run(host="0.0.0", debug=True,port=port)