-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapiclient.py
More file actions
898 lines (780 loc) · 37.8 KB
/
apiclient.py
File metadata and controls
898 lines (780 loc) · 37.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# -*- coding: utf-8 -*-
"""Speech recognition tools for using Rev AI"""
import json
from . import utils
from .baseclient import BaseClient
from .models import Account, CaptionType, Job, Transcript, RevAiApiDeploymentConfigMap, \
RevAiApiDeployment
from .models.asynchronous.summarization_options import SummarizationOptions
from .models.asynchronous.summary import Summary
from .models.asynchronous.translation_options import TranslationOptions
try:
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin
class RevAiAPIClient(BaseClient):
"""Client which implements Rev AI API
Note that HTTPErrors can be thrown by methods of the API client. The HTTP
response payload attached to these error is a problem details. The problem
details information is represented as a JSON object with error specific
properties that help to troubleshoot the problem.
Problem details are defined at https://tools.ietf.org/html/rfc7807.
"""
# Default version of Rev AI
version = 'v1'
# Default url for US Rev AI deployment
default_url = RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']
# Rev AI transcript format
rev_json_content_type = 'application/vnd.rev.transcript.v1.0+json'
def __init__(self, access_token: str, url: str = None):
"""Constructor
:param access_token: access token which authorizes all requests and links them to your
account. Generated on the settings page of your account dashboard
on Rev AI.
:param url (optional): url of the Rev AI API deployment to use, defaults to the US
deployement, i.e. 'https://api.rev.ai', which can be referenced as
RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url'].
"""
# Default speech to text base url
self.base_url = f'{url if url else self.default_url}/speechtotext/{self.version}/'
BaseClient.__init__(self, access_token)
def submit_job_url(
self,
media_url=None,
metadata=None,
callback_url=None,
skip_diarization=False,
skip_punctuation=False,
speaker_channels_count=None,
custom_vocabularies=None,
filter_profanity=False,
remove_disfluencies=False,
delete_after_seconds=None,
language=None,
custom_vocabulary_id=None,
transcriber=None,
verbatim=None,
rush=None,
test_mode=None,
segments_to_transcribe=None,
speaker_names=None,
source_config=None,
notification_config=None,
skip_postprocessing=False,
remove_atmospherics=False,
speakers_count=None,
diarization_type=None,
summarization_config: SummarizationOptions = None,
translation_config: TranslationOptions = None):
"""Submit media given a URL for transcription.
The audio data is downloaded from the URL
:param media_url: web location of the media file
.. deprecated:: 2.16.0
Use source_config instead
:param metadata: info to associate with the transcription job
:param callback_url: callback url to invoke on job completion as a webhook
.. deprecated:: 2.16.0
Use notification_config instead
:param skip_diarization: should Rev AI skip diarization when transcribing this file
:param skip_punctuation: should Rev AI skip punctuation when transcribing this file
:param speaker_channels_count: the number of speaker channels in the
audio. If provided the given audio will have each channel
transcribed separately and each channel will be treated as a single
speaker. Valid values are integers 1-8 inclusive.
:param custom_vocabularies: a collection of phrase dictionaries.
Including custom vocabulary will inform and bias the speech
recognition to find those phrases. Each dictionary should consist
of a key "phrases" which maps to a list of strings, each of which
represents a phrase you would like the speech recognition to bias
itself toward. Cannot be used with the custom_vocabulary_id parameter.
:param filter_profanity: whether to mask profane words
:param remove_disfluencies: whether to exclude filler words like "uh"
:param delete_after_seconds: number of seconds after job completion when job is auto-deleted
:param language: specify language using the one of the supported ISO 639-1 (2-letter) or
ISO 639-3 (3-letter) language codes as defined in the API Reference
:param custom_vocabulary_id: The id of a pre-completed custom vocabulary
submitted through the custom vocabularies api. Cannot be used with the
custom_vocabularies parameter.
:param transcriber: type of transcriber to use to transcribe the media file
:param verbatim: Only available with "human" transcriber.
Whether human transcriber transcribes every syllable.
:param rush: Only available with "human" transcriber.
Whether job is given higher priority to be worked on sooner for higher pricing.
:param test_mode: Only available with "human" transcriber.
Whether human transcription job is mocked and no transcription actually happens.
:param segments_to_transcribe: Only available with "human" transcriber.
Sections of transcript needed to be transcribed.
:param speaker_names: Only available with "human" transcriber.
Human readable names of speakers in the file.
:param source_config: CustomerUrlData object containing url of the source media and
optional authentication headers to use when accessing the source url
:param notification_config: CustomerUrlData object containing the callback url to
invoke on job completion as a webhook and optional authentication headers to use when
calling the callback url
:param skip_postprocessing: skip all text postprocessing (punctuation, capitalization, ITN)
:param remove_atmospherics: Atmospherics such as <laugh>, <affirmative>, etc. will not
appear in the transcript.
:param speakers_count: Use to specify the total number of unique speakers in the audio.
:param diarization_type: Use to specify diarization type.
:param summarization_config: Use to request transcript summary.
:param translation_config: Use to request transcript translation.
:returns: raw response data
:raises: HTTPError
"""
payload = self._create_job_options_payload(media_url=media_url,
metadata=metadata,
callback_url=callback_url,
skip_diarization=skip_diarization,
skip_punctuation=skip_punctuation,
speaker_channels_count=speaker_channels_count,
custom_vocabularies=custom_vocabularies,
filter_profanity=filter_profanity,
remove_disfluencies=remove_disfluencies,
delete_after_seconds=delete_after_seconds,
language=language,
custom_vocabulary_id=custom_vocabulary_id,
transcriber=transcriber,
verbatim=verbatim,
rush=rush,
test_mode=test_mode,
segments_to_transcribe=segments_to_transcribe,
speaker_names=speaker_names,
source_config=source_config,
notification_config=notification_config,
skip_postprocessing=skip_postprocessing,
remove_atmospherics=remove_atmospherics,
speakers_count=speakers_count,
diarization_type=diarization_type,
summarization_config=summarization_config,
translation_config=translation_config)
response = self._make_http_request(
"POST",
urljoin(self.base_url, 'jobs'),
json=payload
)
return Job.from_json(response.json())
def submit_job_local_file(
self,
filename,
metadata=None,
callback_url=None,
skip_diarization=False,
skip_punctuation=False,
speaker_channels_count=None,
custom_vocabularies=None,
filter_profanity=False,
remove_disfluencies=False,
delete_after_seconds=None,
language=None,
custom_vocabulary_id=None,
transcriber=None,
verbatim=None,
rush=None,
test_mode=None,
segments_to_transcribe=None,
speaker_names=None,
notification_config=None,
skip_postprocessing=False,
remove_atmospherics=False,
speakers_count=None,
diarization_type=None,
summarization_config: SummarizationOptions = None,
translation_config: TranslationOptions = None):
"""Submit a local file for transcription.
Note that the content type is inferred if not provided.
:param filename: path to a local file on disk
:param metadata: info to associate with the transcription job
:param callback_url: callback url to invoke on job completion as a webhook
.. deprecated:: 2.16.0
Use notification_config instead
:param skip_diarization: should Rev AI skip diarization when transcribing this file
:param skip_punctuation: should Rev AI skip punctuation when transcribing this file
:param speaker_channels_count: the number of speaker channels in the
audio. If provided the given audio will have each channel
transcribed separately and each channel will be treated as a single
speaker. Valid values are integers 1-8 inclusive.
:param custom_vocabularies: a collection of phrase dictionaries.
Including custom vocabulary will inform and bias the speech
recognition to find those phrases. Each dictionary has the key
"phrases" which maps to a list of strings, each of which represents
a phrase you would like the speech recognition to bias itself toward.
Cannot be used with the custom_vocabulary_id parameter
:param filter_profanity: whether to mask profane words
:param remove_disfluencies: whether to exclude filler words like "uh"
:param delete_after_seconds: number of seconds after job completion when job is auto-deleted
:param language: specify language using the one of the supported ISO 639-1 (2-letter) or
ISO 639-3 (3-letter) language codes as defined in the API Reference
:param custom_vocabulary_id: The id of a pre-completed custom vocabulary
submitted through the custom vocabularies api. Cannot be used with the
custom_vocabularies parameter.
:param transcriber: type of transcriber to use to transcribe the media file
:param verbatim: Only available with "human" transcriber.
Whether human transcriber transcribes every syllable.
:param rush: Only available with "human" transcriber.
Whether job is given higher priority to be worked on sooner for higher pricing.
:param test_mode: Only available with "human" transcriber.
Whether human transcription job is mocked and no transcription actually happens.
:param segments_to_transcribe: Only available with "human" transcriber.
Sections of transcript needed to be transcribed.
:param speaker_names: Only available with "human" transcriber.
Human readable names of speakers in the file.
:param notification_config: CustomerUrlData object containing the callback url to
invoke on job completion as a webhook and optional authentication headers to use when
calling the callback url
:param skip_postprocessing: skip all text postprocessing (punctuation, capitalization, ITN)
:param remove_atmospherics: Atmospherics such as <laugh>, <affirmative>, etc. will not
appear in the transcript.
:param speakers_count: Use to specify the total number of unique speakers in the audio.
:param diarization_type: Use to specify diarization type.
:param summarization_config: Use to request transcript summary.
:param translation_config: Use to request transcript translation.
:returns: raw response data
:raises: HTTPError, ValueError
"""
if not filename:
raise ValueError('filename must be provided')
payload = self._create_job_options_payload(media_url=None,
metadata=metadata,
callback_url=callback_url,
skip_diarization=skip_diarization,
skip_punctuation=skip_punctuation,
speaker_channels_count=speaker_channels_count,
custom_vocabularies=custom_vocabularies,
filter_profanity=filter_profanity,
remove_disfluencies=remove_disfluencies,
delete_after_seconds=delete_after_seconds,
language=language,
custom_vocabulary_id=custom_vocabulary_id,
transcriber=transcriber,
verbatim=verbatim,
rush=rush,
test_mode=test_mode,
segments_to_transcribe=segments_to_transcribe,
speaker_names=speaker_names,
source_config=None,
notification_config=notification_config,
skip_postprocessing=skip_postprocessing,
remove_atmospherics=remove_atmospherics,
speakers_count=speakers_count,
diarization_type=diarization_type,
summarization_config=summarization_config,
translation_config=translation_config)
with open(filename, 'rb') as f:
files = {
'media': (filename, f),
'options': (None, json.dumps(payload, sort_keys=True))
}
response = self._make_http_request(
"POST",
urljoin(self.base_url, 'jobs'),
files=files
)
return Job.from_json(response.json())
def get_job_details(self, id_):
"""View information about a specific job.
The server will respond with the status and creation date.
:param id_: id of the job to be requested
:returns: Job object if available
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}'.format(id_))
)
return Job.from_json(response.json())
def get_list_of_jobs(self, limit=None, starting_after=None):
"""Get a list of transcription jobs submitted within the last week in reverse
chronological order up to the provided limit number of jobs per call.
Pagination is supported via passing the last job id from previous call into starting_after.
:param limit: optional, limits the number of jobs returned,
if none, a default of 100 jobs is returned, max limit if 1000
:param starting_after: optional, returns jobs created after the job with this id,
exclusive (job with this id is not included)
:returns: list of jobs response data
:raises: HTTPError
"""
params = []
if limit is not None:
params.append('limit={}'.format(limit))
if starting_after is not None:
params.append('starting_after={}'.format(starting_after))
query = '?{}'.format('&'.join(params))
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs{}'.format(query))
)
return [Job.from_json(job) for job in response.json()]
def get_transcript_text(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Get the transcript of a specific job as plain text.
:param id_: id of job to be requested
:param group_channels_by: optional, GroupChannelsType grouping strategy for
multichannel transcripts. None for default.
:param group_channels_threshold_ms: optional, grouping threshold in milliseconds.
None for default.
:returns: transcript data as text
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
url = self._build_transcript_url(
id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms
)
response = self._make_http_request(
"GET",
url,
headers={'Accept': 'text/plain'}
)
return response.text
def get_transcript_text_as_stream(self,
id_,
group_channels_by=None,
group_channels_threshold_ms=None):
"""Get the transcript of a specific job as a plain text stream.
:param id_: id of job to be requested
:param group_channels_by: optional, GroupChannelsType grouping strategy for
multichannel transcripts. None for default.
:param group_channels_threshold_ms: optional, grouping threshold in milliseconds.
None for default.
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
url = self._build_transcript_url(
id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms
)
response = self._make_http_request(
"GET",
url,
headers={'Accept': 'text/plain'},
stream=True
)
return response
def get_transcript_json(self,
id_,
group_channels_by=None,
group_channels_threshold_ms=None):
"""Get the transcript of a specific job as json.
:param id_: id of job to be requested
:param group_channels_by: optional, GroupChannelsType grouping strategy for
multichannel transcripts. None for default.
:param group_channels_threshold_ms: optional, grouping threshold in milliseconds.
None for default.
:returns: transcript data as json
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
url = self._build_transcript_url(
id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms
)
response = self._make_http_request(
"GET",
url,
headers={'Accept': self.rev_json_content_type}
)
return response.json()
def get_transcript_json_as_stream(self,
id_,
group_channels_by=None,
group_channels_threshold_ms=None):
"""Get the transcript of a specific job as streamed json.
:param id_: id of job to be requested
:param group_channels_by: optional, GroupChannelsType grouping strategy for
multichannel transcripts. None for default.
:param group_channels_threshold_ms: optional, grouping threshold in milliseconds.
None for default.
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
url = self._build_transcript_url(
id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms
)
response = self._make_http_request(
"GET",
url,
headers={'Accept': self.rev_json_content_type},
stream=True
)
return response
def get_transcript_object(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Get the transcript of a specific job as a python object`.
:param id_: id of job to be requested
:param group_channels_by: optional, GroupChannelsType grouping strategy for
multichannel transcripts. None for default.
:param group_channels_threshold_ms: optional, grouping threshold in milliseconds.
None for default.
:returns: transcript data as a python object
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
url = self._build_transcript_url(
id_,
group_channels_by=group_channels_by,
group_channels_threshold_ms=group_channels_threshold_ms
)
response = self._make_http_request(
"GET",
url,
headers={'Accept': self.rev_json_content_type}
)
return Transcript.from_json(response.json())
def get_captions(self, id_, content_type=CaptionType.SRT, channel_id=None):
"""Get the captions output of a specific job and return it as plain text
:param id_: id of job to be requested
:param content_type: caption type which should be returned. Defaults to SRT
:param channel_id: id of speaker channel to be captioned, only matters for multichannel jobs
:returns: caption data as text
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
query = self._create_captions_query(channel_id)
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{0}/captions{1}'.format(id_, query)),
headers={'Accept': content_type.value}
)
return response.text
def get_translated_captions(self, id_, language, content_type=CaptionType.SRT):
"""Get the captions output of a specific job and return it as plain text
:param id_: id of job to be requested
:param content_type: caption type which should be returned. Defaults to SRT
:returns: caption data as text
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url,
'jobs/{0}/captions/translation/{1}'.format(id_, language)),
headers={'Accept': content_type.value}
)
return response.text
def get_captions_as_stream(self, id_, content_type=CaptionType.SRT, channel_id=None):
"""Get the captions output of a specific job and return it as a plain text stream
:param id_: id of job to be requested
:param content_type: caption type which should be returned. Defaults to SRT
:param channel_id: id of speaker channel to be captioned, only matters for multichannel jobs
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
query = self._create_captions_query(channel_id)
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{0}/captions{1}'.format(id_, query)),
headers={'Accept': content_type.value},
stream=True
)
return response
def get_translated_captions_as_stream(
self,
id_,
language,
content_type=CaptionType.SRT,
channel_id=None):
"""Get the captions output of a specific job and return it as a plain text stream
:param id_: id of job to be requested
:param language: requested translation language
:param content_type: caption type which should be returned. Defaults to SRT
:param channel_id: id of speaker channel to be captioned, only matters for multichannel jobs
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
query = self._create_captions_query(channel_id)
response = self._make_http_request(
"GET",
urljoin(self.base_url,
'jobs/{0}/captions/translation/{1}{2}'.format(id_, language, query)),
headers={'Accept': content_type.value},
stream=True
)
return response
def delete_job(self, id_):
"""Delete a specific transcription job
All data related to the job, such as input media and transcript, will be permanently
deleted. A job can only by deleted once it's completed.
:param id_: id of job to be deleted
:returns: None if job was successfully deleted
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
self._make_http_request(
"DELETE",
urljoin(self.base_url, 'jobs/{}'.format(id_)),
)
return
def get_account(self):
"""Get account information, such as remaining credits.
:raises: HTTPError
"""
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'account')
)
return Account.from_json(response.json())
def get_transcript_summary_text(self, id_):
"""Get the transcript summary of a specific job as plain text.
:param id_: id of job to be requested
:returns: transcript data as text
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/summary'.format(id_)),
headers={'Accept': 'text/plain'}
)
return response.text
def get_transcript_summary_object(self, id_):
"""Get the transcript summary of a specific job as python object.
:param id_: id of job to be requested
:returns: transcript data as json
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/summary'.format(id_)),
headers={'Accept': 'application/json'}
)
return Summary.from_json(response.json())
def get_transcript_summary_json(self, id_):
"""Get the transcript summary of a specific job as json.
:param id_: id of job to be requested
:returns: transcript data as json
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/summary'.format(id_)),
headers={'Accept': 'application/json'}
)
return response.json()
def get_transcript_summary_json_as_stream(self, id_):
"""Get the transcript summary of a specific job as streamed json.
:param id_: id of job to be requested
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/summary'.format(id_)),
headers={'Accept': 'application/json'},
stream=True
)
return response
def get_translated_transcript_text(self, id_, language):
"""Get the translated transcript of a specific job as plain text.
:param id_: id of job to be requested
:param language: requested language
:returns: transcript data as text
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/translation/{}'.format(id_, language)),
headers={'Accept': 'text/plain'}
)
return response.text
def get_translated_transcript_text_as_stream(self, id_, language):
"""Get the translated transcript of a specific job as a plain text stream.
:param id_: id of job to be requested
:param language: requested language
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/translation/{}'.format(id_, language)),
headers={'Accept': 'text/plain'},
stream=True
)
return response
def get_translated_transcript_json(self, id_, language):
"""Get the translated transcript of a specific job as json.
:param id_: id of job to be requested
:param language: requested language
:returns: transcript data as json
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/translation/{}'.format(id_, language)),
headers={'Accept': self.rev_json_content_type}
)
return response.json()
def get_translated_transcript_json_as_stream(self, id_, language):
"""Get the translated transcript of a specific job as streamed json.
:param id_: id of job to be requested
:param language: requested language
:returns: requests.models.Response HTTP response which can be used to stream
the payload of the response
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/translation/{}'.format(id_, language)),
headers={'Accept': self.rev_json_content_type},
stream=True
)
return response
def get_translated_transcript_object(self, id_, language):
"""Get the translated transcript of a specific job as a python object`.
:param id_: id of job to be requested
:param language: requested language
:returns: transcript data as a python object
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')
response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/translation/{}'.format(id_, language)),
headers={'Accept': self.rev_json_content_type}
)
return Transcript.from_json(response.json())
def _create_job_options_payload(
self,
media_url=None,
metadata=None,
callback_url=None,
skip_diarization=None,
skip_punctuation=None,
speaker_channels_count=None,
custom_vocabularies=None,
filter_profanity=None,
remove_disfluencies=None,
delete_after_seconds=None,
language=None,
custom_vocabulary_id=None,
transcriber=None,
verbatim=None,
rush=None,
test_mode=None,
segments_to_transcribe=None,
speaker_names=None,
source_config=None,
notification_config=None,
skip_postprocessing=False,
remove_atmospherics=None,
speakers_count=None,
diarization_type=None,
summarization_config: SummarizationOptions = None,
translation_config: TranslationOptions = None):
payload = {}
if media_url:
if source_config:
raise ValueError(
'media_url is not compatible with source_config. '
'Use source_config for all URL-based submissions.')
payload['source_config'] = {'url': media_url}
if skip_diarization:
payload['skip_diarization'] = skip_diarization
if skip_punctuation:
payload['skip_punctuation'] = skip_punctuation
if metadata:
payload['metadata'] = metadata
if callback_url:
payload['callback_url'] = callback_url
if custom_vocabularies:
payload['custom_vocabularies'] = utils._process_vocabularies(custom_vocabularies)
if speaker_channels_count:
payload['speaker_channels_count'] = speaker_channels_count
if filter_profanity:
payload['filter_profanity'] = filter_profanity
if remove_disfluencies:
payload['remove_disfluencies'] = remove_disfluencies
if delete_after_seconds is not None:
payload['delete_after_seconds'] = delete_after_seconds
if language:
payload['language'] = language
if custom_vocabulary_id:
payload['custom_vocabulary_id'] = custom_vocabulary_id
if transcriber:
payload['transcriber'] = transcriber
if verbatim is not None:
payload['verbatim'] = verbatim
if rush:
payload['rush'] = rush
if test_mode:
payload['test_mode'] = test_mode
if segments_to_transcribe:
payload['segments_to_transcribe'] = segments_to_transcribe
if speaker_names:
payload['speaker_names'] = \
utils._process_speaker_names(speaker_names)
if source_config:
payload['source_config'] = source_config.to_dict()
if notification_config:
payload['notification_config'] = notification_config.to_dict()
if skip_postprocessing:
payload['skip_postprocessing'] = skip_postprocessing
if remove_atmospherics:
payload['remove_atmospherics'] = remove_atmospherics
if speakers_count:
payload['speakers_count'] = speakers_count
if diarization_type:
payload['diarization_type'] = diarization_type
if summarization_config:
payload['summarization_config'] = summarization_config.to_dict()
if translation_config:
payload['translation_config'] = translation_config.to_dict()
return payload
def _create_captions_query(self, speaker_channel):
return '' if speaker_channel is None else '?speaker_channel={}'.format(speaker_channel)
def _build_transcript_url(self, id_, group_channels_by=None, group_channels_threshold_ms=None):
"""Build the get transcript url.
:param id_: id of job to be requested
:param group_channels_by: optional, GroupChannelsType grouping strategy for
multichannel transcripts. None for default.
:param group_channels_threshold_ms: optional, grouping threshold in milliseconds.
None for default.
:returns: url for getting the transcript
"""
params = []
if group_channels_by is not None:
params.append('group_channels_by={}'.format(group_channels_by))
if group_channels_threshold_ms is not None:
params.append('group_channels_threshold_ms={}'.format(group_channels_threshold_ms))
query = '?{}'.format('&'.join(params))
return urljoin(self.base_url, 'jobs/{}/transcript{}'.format(id_, query))