Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
MPAI-Private
MPAI-CAE
arp
Audio Analyzer
Commits
32789bf3
Commit
32789bf3
authored
Apr 11, 2023
by
Matteo
Browse files
update
parent
35dfb875
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Dockerfile
View file @
32789bf3
...
...
@@ -8,4 +8,4 @@ RUN poetry install --no-cache --only main
VOLUME
[ "/data" ]
CMD
["poetry", "run", "uvicorn", "src.server:app", "--host", "0.0.0.0", "--port", "80", "--log-config", "config/logger.yaml"]
\ No newline at end of file
CMD
["poetry", "run", "python", "src/server.py"]
\ No newline at end of file
poetry.lock
View file @
32789bf3
This diff is collapsed.
Click to expand it.
pyproject.toml
View file @
32789bf3
...
...
@@ -8,11 +8,12 @@ readme = "README.md"
[tool.poetry.dependencies]
python
=
"^3.10"
mpai-cae-arp
=
"^0.2.3"
numpy
=
"1.23.3"
rich
=
"^13.3.3"
pandas
=
"^2.0.0"
scikit-learn
=
"^1.2.2"
grpcio-tools
=
"^1.53.0"
mpai-cae-arp
=
"^0.3.0"
[tool.poetry.group.docs.dependencies]
...
...
@@ -25,9 +26,6 @@ pytest = "^7.3.0"
pytest-xdist
=
"^3.2.1"
[tool.poetry.group.server.dependencies]
fastapi
=
{
extras
=
["all"]
,
version
=
"^0.95.0"
}
[build-system]
requires
=
["poetry-core"]
build-backend
=
"poetry.core.masonry.api"
...
...
src/cli.py
View file @
32789bf3
...
...
@@ -9,7 +9,7 @@ from mpai_cae_arp.files import File, FileType
from
mpai_cae_arp.io
import
prettify
,
Style
def
get_args
()
->
tuple
[
str
,
str
]:
def
get_args
()
->
tuple
[
str
|
None
,
str
|
None
]:
if
len
(
sys
.
argv
)
>
1
:
parser
=
argparse
.
ArgumentParser
(
prog
=
"audio-analyzer"
,
...
...
@@ -24,11 +24,11 @@ def get_args() -> tuple[str, str]:
return
args
.
working_directory
,
args
.
files_name
else
:
return
os
.
getenv
(
"WORKING_DIRECTORY"
),
os
.
getenv
(
"FILES_NAME"
)
def
exit_with_error
(
error_message
:
str
,
console
)
->
None
:
console
.
print
(
f
"[red bold]Error:
{
error_message
}
:t-rex:
"
)
console
.
print
(
f
"[red bold]Error:
{
error_message
}
"
)
quit
(
os
.
EX_USAGE
)
...
...
@@ -42,6 +42,7 @@ def main() -> None:
"Working directory or files name not specified!"
,
"Try -h/--help to know more about Audio Analyser usage"
),
console
)
os
.
makedirs
(
os
.
path
.
join
(
working_directory
,
"temp"
,
files_name
))
with
console
.
status
(
"[purple]Reading input files"
,
spinner
=
"dots"
):
audio_src
=
os
.
path
.
join
(
working_directory
,
"PreservationAudioFile"
,
f
"
{
files_name
}
.wav"
)
...
...
@@ -54,17 +55,17 @@ def main() -> None:
case
True
,
True
:
console
.
print
(
"[green]Input files found!"
)
case
False
,
True
:
exit_with_error
(
"Audio file not found!"
,
console
)
exit_with_error
(
"Audio file not found!
:loud_sound:
"
,
console
)
case
True
,
False
:
exit_with_error
(
"Video file not found!"
,
console
)
exit_with_error
(
"Video file not found!
:vhs:
"
,
console
)
case
False
,
False
:
exit_with_error
(
"Input files not found!"
,
console
)
exit_with_error
(
"Input files not found!
:t-rex:
"
,
console
)
# create irregularity file 1
with
console
.
status
(
"[purple]Creating irregularity file 1"
,
spinner
=
"dots"
):
irreg1
=
sf
.
create_irreg_file
(
audio_src
,
video_src
)
console
.
log
(
f
"Found
{
len
(
irreg1
.
irregularities
)
}
irregularities from Audio source"
)
File
(
f
"
{
working_directory
}
/IrregularityFile1.json"
,
FileType
.
JSON
).
write_content
(
irreg1
.
to_json
())
File
(
f
"
{
working_directory
}
/
temp/
{
files_name
}
/
IrregularityFile1.json"
,
FileType
.
JSON
).
write_content
(
irreg1
.
to_json
())
console
.
log
(
"[geen]Irregularity file 1 created"
)
# create irregularity file 2
...
...
@@ -86,11 +87,11 @@ def main() -> None:
}
console
.
log
(
"Video irregularity file 1 found"
)
irreg2
=
sf
.
merge_irreg_files
(
irreg1
,
IrregularityFile
.
from_json
(
video_irreg_1
))
File
(
f
"
{
working_directory
}
/IrregularityFile2.json"
,
FileType
.
JSON
).
write_content
(
irreg2
.
to_json
())
console
.
log
(
"[geen]Irregularity file 2 created"
)
with
console
.
status
(
"[cyan]Extracting audio irregularities"
,
spinner
=
"dots"
):
sf
.
extract_audio_irregularities
(
audio_src
,
irreg2
,
working_directory
)
irreg2
=
sf
.
extract_audio_irregularities
(
audio_src
,
irreg2
,
working_directory
+
"/temp/"
+
files_name
)
File
(
f
"
{
working_directory
}
/temp/
{
files_name
}
/IrregularityFile2.json"
,
FileType
.
JSON
).
write_content
(
irreg2
.
to_json
())
console
.
log
(
"[green]Audio irregularities extracted"
)
# classify audio irregularities
...
...
src/segment_finder.py
View file @
32789bf3
...
...
@@ -106,7 +106,7 @@ def extract_audio_irregularities(
audio_src
:
str
,
irreg_file
:
IrregularityFile
,
path
:
str
)
->
Non
e
:
)
->
IrregularityFil
e
:
channels_map
=
File
(
TMP_CHANNELS_MAP
,
FileType
.
JSON
).
get_content
()
os
.
makedirs
(
f
"
{
path
}
/AudioBlocks"
,
exist_ok
=
True
)
...
...
@@ -115,9 +115,9 @@ def extract_audio_irregularities(
for
irreg
in
irreg_file
.
irregularities
:
if
channels_map
.
get
(
str
(
irreg
.
irregularity_ID
))
is
None
:
audio
[
seconds_to_frames
(
time_to_seconds
(
irreg
.
time_label
),
audio
.
samplerate
time_to_seconds
(
irreg
.
time_label
),
audio
.
samplerate
):
seconds_to_frames
(
time_to_seconds
(
irreg
.
time_label
),
audio
.
samplerate
)
+
audio
.
samplerate
//
2
]
\
time_to_seconds
(
irreg
.
time_label
),
audio
.
samplerate
)
+
audio
.
samplerate
//
2
]
\
.
save
(
f
"
{
path
}
/AudioBlocks/
{
irreg
.
irregularity_ID
}
.wav"
)
else
:
audio
.
get_channel
(
channels_map
[
str
(
irreg
.
irregularity_ID
)])[
...
...
@@ -126,4 +126,7 @@ def extract_audio_irregularities(
):
seconds_to_frames
(
time_to_seconds
(
irreg
.
time_label
),
audio
.
samplerate
)
+
audio
.
samplerate
//
2
]
\
.
save
(
f
"
{
path
}
/AudioBlocks/
{
irreg
.
irregularity_ID
}
.wav"
)
irreg
.
audio_block_URI
=
f
"
{
path
}
/AudioBlocks/
{
irreg
.
irregularity_ID
}
.wav"
os
.
remove
(
TMP_CHANNELS_MAP
)
return
irreg_file
src/server.py
View file @
32789bf3
from
fastapi
import
FastAPI
from
concurrent
import
futures
from
rich.console
import
Console
from
mpai_cae_arp.files
import
File
,
FileType
from
mpai_cae_arp.types.irregularity
import
IrregularityFile
from
mpai_cae_arp.types.schema
import
Info
import
grpc
from
mpai_cae_arp.network
import
arp_pb2_grpc
as
arp_pb2_grpc
from
mpai_cae_arp.network.arp_pb2
import
(
ComputationRequest
,
ComputationResult
,
Contact
,
Info
,
License
,
)
import
time
info
=
File
(
'config/server.yaml'
,
FileType
.
YAML
).
get_content
()
app
=
FastAPI
(
**
info
)
@
app
.
get
(
'/'
)
async
def
get_endpoints_list
()
->
list
[
str
]:
"""Get list of all endpoints."""
return
[
route
.
path
for
route
in
app
.
routes
]
@
app
.
get
(
'/description'
)
async
def
get_description
()
->
Info
:
"""Get description of the server."""
return
info
@
app
.
get
(
'/irregularityFile/{id}'
)
async
def
get_irregularity_file
(
id
:
int
)
->
IrregularityFile
:
"""Get irregularity file by id."""
return
IrregularityFile
(
irregularities
=
[],
offset
=
150
)
\ No newline at end of file
class
AudioAnalyserServicer
(
arp_pb2_grpc
.
AudioAnalyserServicer
):
def
__init__
(
self
,
console
:
Console
):
self
.
console
=
console
def
getAimInfo
(
self
,
request
,
context
)
->
Info
:
self
.
console
.
log
(
'Received request for AIM info'
)
return
Info
(
title
=
info
[
'title'
],
description
=
info
[
'description'
],
version
=
info
[
'version'
],
contact
=
Contact
(
name
=
info
[
'contact'
][
'name'
],
email
=
info
[
'contact'
][
'email'
],
),
license
=
License
(
name
=
info
[
'license_info'
][
'name'
],
url
=
info
[
'license_info'
][
'url'
],
)
)
def
analyse
(
self
,
request
:
ComputationRequest
,
context
):
self
.
console
.
log
(
'Received request for computation'
)
for
x
in
range
(
10
):
yield
ComputationResult
(
success
=
True
,
message
=
f
'Processing
{
x
*
10
}
%'
)
time
.
sleep
(
2
)
def
serve
(
console
):
server
=
grpc
.
server
(
futures
.
ThreadPoolExecutor
(
max_workers
=
10
))
arp_pb2_grpc
.
add_AudioAnalyserServicer_to_server
(
AudioAnalyserServicer
(
console
),
server
)
server
.
add_insecure_port
(
'[::]:50051'
)
server
.
start
()
server
.
wait_for_termination
()
if
__name__
==
'__main__'
:
console
=
Console
()
console
.
print
(
'Server started at localhost:50051 :satellite:'
)
serve
(
console
)
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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