Commit 28da7a05 authored by Dinushe Jayasekera's avatar Dinushe Jayasekera

Audio API files

parent 29ddc2e0
import requests
# server url
URL = "http://127.0.0.1:5050/predict"
# audio file send for predicting keyword
FILE_PATH = "audio/fold1/100263-2-0-117.wav"
if __name__ == "__main__":
# open files
file = open(FILE_PATH, "rb")
# package stuff to send and perform POST request
values = {"file": (FILE_PATH, file, "audio/fold1/100263-2-0-117.wav")}
response = requests.post(URL, files=values)
data = response.json()
print("Predicted keyword: {}".format(data["keyword"]))
\ No newline at end of file
import librosa
import tensorflow as tf
import numpy as np
SAVED_MODEL_PATH = "model_weights_acc_new.h5"
SAMPLES_TO_CONSIDER = 22050
class _modelFin:
model = None
_mapping = [
"children_playing",
"children_playing2",
"children_playing3",
"children_playing4",
"children_playing5"
]
_instance = None
def predict(self, file_path):
# extract MFCC
MFCCs = self.preprocess(file_path)
# 4-dim array to feed to the model for prediction: (# samples, # time steps, # coefficients, 1)
MFCCs = MFCCs[np.newaxis, ..., np.newaxis]
# get the predicted label
predictions = self.model.predict(MFCCs)
predicted_index = np.argmax(predictions)
predicted_keyword = self._mapping[predicted_index]
return predicted_keyword
def preprocess(self, file_path, num_mfcc=40, n_fft=2048, hop_length=512):
# load audio file
signal, sample_rate = librosa.load(file_path)
if len(signal) >= SAMPLES_TO_CONSIDER:
# ensure consistency of the length of the signal
signal = signal[:SAMPLES_TO_CONSIDER]
# extract MFCCs
MFCCs = librosa.feature.mfcc(signal, sample_rate, n_mfcc=num_mfcc, n_fft=n_fft,
hop_length=hop_length)
return MFCCs.T
def modelFin():
# ensure an instance is created only the first time the factory function is called
if _modelFin._instance is None:
_modelFin._instance = _modelFin()
_modelFin.model = tf.keras.models.load_model(SAVED_MODEL_PATH)
return _modelFin._instance
if __name__ == "__main__":
# create 2 instances of service
kss = modelFin()
kss1 = modelFin()
# check that different instances of the service point back to the same object
assert kss is kss1
# make a prediction
keyword = kss.predict("100263-2-0-117.wav")
print(keyword)
\ No newline at end of file
from fastapi import FastAPI
from fastapi import UploadFile, File
import uvicorn
from fastapi.responses import StreamingResponse
import random
import os
from modelFin import modelFin
from pydantic import BaseModel
app = FastAPI()
# @app.on_event("startup")
# def load_model():
# global model
# model = pickle.load(open("model_tree.pkl", "rb"))
@app.get('/index')
def hello_world(name:str):
return f"Hello {name}!"
@app.post('/audio/predict')
def predict_audio(file:UploadFile = File(...)):
#file_name = str(random.randint(0, 100000))
#file.save(file_name)
mod = modelFin()
pre_key = modelFin.predict(file)
received = file.dict()
normal = received['children_playing']
label2 = received['children_playing2']
label3 = received['children_playing3']
label4 = received['children_playing4']
label5 = received['children_playing5']
pred_name = modelFin.predict([[normal, label2, label3,
label4, label5]]).tolist()[0]
return {'prediction': pred_name}
if __name__ == "__main__":
uvicorn.run(app, port=8080, host='0.0.0.0')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment