Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-005
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
IT18019896 A.R.M.R.M.Mudalinayake
2021-005
Commits
28da7a05
Commit
28da7a05
authored
Nov 26, 2021
by
Dinushe Jayasekera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Audio API files
parent
29ddc2e0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
138 additions
and
0 deletions
+138
-0
FastAPI-Audio/client.py
FastAPI-Audio/client.py
+21
-0
FastAPI-Audio/modelFin.py
FastAPI-Audio/modelFin.py
+71
-0
FastAPI-Audio/model_weights_acc_new.h5
FastAPI-Audio/model_weights_acc_new.h5
+0
-0
FastAPI-Audio/server.py
FastAPI-Audio/server.py
+46
-0
No files found.
FastAPI-Audio/client.py
0 → 100644
View file @
28da7a05
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
FastAPI-Audio/modelFin.py
0 → 100644
View file @
28da7a05
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
FastAPI-Audio/model_weights_acc_new.h5
0 → 100644
View file @
28da7a05
File added
FastAPI-Audio/server.py
0 → 100644
View file @
28da7a05
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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment