Skip to content

Commit c8d7843

Browse files
Merge pull request #5 from SEERNET/from_file
Added from_file method to diarizeAudio
2 parents 37139c9 + ba1db0b commit c8d7843

4 files changed

Lines changed: 96 additions & 8 deletions

File tree

deepaffects/models/audio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ def __ne__(self, other):
199199
return not self == other
200200

201201
@staticmethod
202-
def from_file(file_name):
202+
def from_file(file_name, language_code='en-US'):
203203
media_info = MediaInfo.parse(file_name)
204204
codec = media_info.tracks[0].__dict__['codec']
205205
sampling_rate = media_info.tracks[1].__dict__['sampling_rate']
206206
fout = SIO.StringIO()
207207
with open(file_name, 'rb') as fin:
208208
audio_content = fin.read()
209-
audio = Audio(encoding=codec, sample_rate=sampling_rate, language_code='en-US',
209+
audio = Audio(encoding=codec, sample_rate=sampling_rate, language_code=language_code,
210210
content=base64.b64encode(audio_content).decode('utf-8'))
211211
fout.close()
212212
return audio

deepaffects/models/diarize_audio.py

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@
88
"""
99

1010

11+
try:
12+
import StringIO as SIO
13+
except ImportError:
14+
import io as SIO
15+
import base64
16+
import json
1117
from pprint import pformat
18+
19+
from pymediainfo import MediaInfo
1220
from six import iteritems
1321

22+
1423
class DiarizeAudio(object):
1524

16-
def __init__(self, encoding=None, sample_rate=None, language_code='en-US', content=None, speakers=-1):
25+
def __init__(self, encoding=None, sample_rate=None, language_code='en-US', content=None, speakers=-1,
26+
merge_segments=True, audio_type="default"):
1727
"""
1828
DiarizeAudio - a model defined in Swagger
1929
@@ -27,22 +37,28 @@ def __init__(self, encoding=None, sample_rate=None, language_code='en-US', conte
2737
'sample_rate': 'int',
2838
'language_code': 'str',
2939
'content': 'str',
30-
'speakers': 'int'
40+
'speakers': 'int',
41+
'audio_type': 'str',
42+
'merge_segments': 'bool'
3143
}
3244

3345
self.attribute_map = {
3446
'encoding': 'encoding',
3547
'sample_rate': 'sampleRate',
3648
'language_code': 'languageCode',
3749
'content': 'content',
38-
'speakers': 'speakers'
50+
'speakers': 'speakers',
51+
'audio_type': 'audioType',
52+
'merge_segments': 'vad'
3953
}
4054

4155
self._encoding = encoding
4256
self._sample_rate = sample_rate
4357
self._language_code = language_code
4458
self._content = content
4559
self._speakers = speakers
60+
self._merge_segments = merge_segments
61+
self._audio_type = audio_type
4662

4763
@property
4864
def encoding(self):
@@ -169,6 +185,55 @@ def speakers(self, speakers):
169185

170186
self._speakers = speakers
171187

188+
@property
189+
def audio_type(self):
190+
"""
191+
Gets the corresponding type of audio file
192+
example: meeting, call-center, default
193+
194+
:return: The audio_type of this DiarizeAudio.
195+
:rtype: str
196+
"""
197+
return self._audio_type
198+
199+
@audio_type.setter
200+
def audio_type(self, audio_type):
201+
"""
202+
Sets the audio_type of this DiarizeAudio.
203+
Corresponding type of audio file like meeting, call-center, default
204+
205+
:param encoding: The audio_type of this DiarizeAudio.
206+
:type: str
207+
"""
208+
if audio_type is None:
209+
raise ValueError("Invalid value for `audio_type`, must not be `None`")
210+
211+
self._audio_type = audio_type
212+
213+
@property
214+
def merge_segments(self):
215+
"""
216+
Whether the consecutive segments of same speaker should be merged
217+
218+
:return: The merge_segments of this DiarizeAudio.
219+
:rtype: bool
220+
"""
221+
return self._merge_segments
222+
223+
@merge_segments.setter
224+
def merge_segments(self, merge_segments):
225+
"""
226+
Sets the merge_segments of this DiarizeAudio.
227+
Whether the consecutive segments of same speaker should be merged
228+
229+
:param encoding: The merge_segments of this DiarizeAudio.
230+
:type: str
231+
"""
232+
if merge_segments is None:
233+
raise ValueError("Invalid value for `merge_segments`, must not be `None`")
234+
235+
self._merge_segments = merge_segments
236+
172237
def to_dict(self):
173238
"""
174239
Returns the model properties as a dict
@@ -221,3 +286,26 @@ def __ne__(self, other):
221286
Returns true if both objects are not equal
222287
"""
223288
return not self == other
289+
290+
@staticmethod
291+
def from_file(file_name, language_code='en-US', speakers=-1, merge_segments=True, audio_type='default'):
292+
media_info = MediaInfo.parse(file_name)
293+
codec = media_info.tracks[0].__dict__['codec']
294+
sampling_rate = media_info.tracks[1].__dict__['sampling_rate']
295+
fout = SIO.StringIO()
296+
with open(file_name, 'rb') as fin:
297+
audio_content = fin.read()
298+
audio = DiarizeAudio(encoding=codec, sample_rate=sampling_rate, language_code=language_code,
299+
content=base64.b64encode(audio_content).decode('utf-8'), speakers=speakers,
300+
merge_segments=merge_segments, audio_type=audio_type)
301+
fout.close()
302+
return audio
303+
304+
@staticmethod
305+
def from_json(content_str):
306+
content = json.loads(content_str)
307+
audio = DiarizeAudio(encoding=content['encoding'], sample_rate=content['sample_rate'],
308+
language_code=content['language_code'], content=content['content'],
309+
speakers=content['speakers'], merge_segments=content['merge_segments'],
310+
audio_type=content['audio_type'])
311+
return audio

docs/DiarizeApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ deepaffects.configuration.api_key['apikey'] = 'YOUR_API_KEY'
2626

2727
# create an instance of the API class
2828
api_instance = deepaffects.DiarizeApi()
29-
body = deepaffects.Audio.from_file(file_name="/path/to/file") # Audio | Audio object that needs to be diarized.
29+
body = deepaffects.DiarizeAudio.from_file(file_name="/path/to/file") # Audio | Audio object that needs to be diarized.
3030
webhook = 'https://your_webhook.url' # str | The webhook url where result from async resource is posted
3131
request_id = 'request_id_example' # str | Unique identifier for the request (optional)
3232

@@ -81,7 +81,7 @@ deepaffects.configuration.api_key['apikey'] = 'YOUR_API_KEY'
8181

8282
# create an instance of the API class
8383
api_instance = deepaffects.DiarizeApi()
84-
body = deepaffects.Audio.from_file(file_name="/path/to/file") # Audio | Audio object that needs to be diarized.
84+
body = deepaffects.DiarizeAudio.from_file(file_name="/path/to/file") # Audio | Audio object that needs to be diarized.
8585

8686
try:
8787
# Diarize an audio file

docs/DiarizeApiV2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ deepaffects.configuration.api_key['apikey'] = 'YOUR_API_KEY'
2525

2626
# create an instance of the API class
2727
api_instance = deepaffects.DiarizeApiV2()
28-
body = deepaffects.Audio.from_file(file_name="/path/to/file") # Audio | Audio object that needs to be diarized.
28+
body = deepaffects.DiarizeAudio.from_file(file_name="/path/to/file") # DiarizeAudio | audio object that needs to be diarized.
2929
webhook = 'https://your_webhook.url' # str | The webhook url where result from async resource is posted
3030
request_id = 'request_id_example' # str | Unique identifier for the request (optional)
3131

0 commit comments

Comments
 (0)