-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (66 loc) · 3.07 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
import argparse
import logging
from clams import Restifier
from clams.app import ClamsApp
from inaSpeechSegmenter import Segmenter
from mmif import DocumentTypes, AnnotationTypes, Mmif
import metadata
class INASSWrapper(ClamsApp):
def __init__(self):
super().__init__()
self.segmenter = Segmenter()
def _appmetadata(self):
pass
def _annotate(self, mmif: Mmif, **parameters) -> Mmif:
if not isinstance(mmif, Mmif):
mmif = Mmif(mmif)
self.segmenter.energy_ratio = parameters['silenceRatio']
# get AudioDocuments with locations
for document in mmif.get_documents_by_type(DocumentTypes.AudioDocument) + mmif.get_documents_by_type(DocumentTypes.VideoDocument):
filename = document.location_path()
self.logger.info(f"Processing {filename}...")
segments = self.segmenter(filename)
self.logger.info(f"Found {len(segments)} segments.")
v = mmif.new_view()
self.sign_view(v, parameters)
v.new_contain(AnnotationTypes.TimeFrame,
timeUnit=metadata.timeunit, document=document.id, labelset=metadata.wrapper_labels)
for label, start_sec, end_sec in segments:
duration = (end_sec - start_sec) * 1000
if duration < parameters['minTFDuration']:
continue
a = v.new_annotation(AnnotationTypes.TimeFrame)
a.add_property('start', int(start_sec * 1000))
a.add_property('end', int(end_sec * 1000))
if label == 'male' or label == 'female':
a.add_property('gender', label)
a.add_property('label', 'speech')
elif label == 'noEnergy':
a.add_property('label', 'silence')
else:
a.add_property('label', label)
return mmif
def get_app():
"""
This function effectively creates an instance of the app class, without any arguments passed in, meaning, any
external information such as initial app configuration should be set without using function arguments. The easiest
way to do this is to set global variables before calling this.
"""
return INASSWrapper()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--port", action="store", default="5000", help="set port to listen")
parser.add_argument("--production", action="store_true", help="run gunicorn server")
parsed_args = parser.parse_args()
# create the app instance
# if get_app() call requires any "configurations", they should be set now as global variables
# and referenced in the get_app() function. NOTE THAT you should not change the signature of get_app()
app = get_app()
http_app = Restifier(app, port=int(parsed_args.port))
# for running the application in production mode
if parsed_args.production:
http_app.serve_production()
# development mode
else:
app.logger.setLevel(logging.DEBUG)
http_app.run()