|
9 | 9 | TranscriptionSource, PrerecordedTranscriptionResponse,
|
10 | 10 | LiveTranscriptionResponse, Metadata, EventHandler)
|
11 | 11 | from ._enums import LiveTranscriptionEvent
|
12 |
| -from ._utils import _request, _make_query_string, _socket_connect |
| 12 | +from ._utils import _request, _sync_request, _make_query_string, _socket_connect |
13 | 13 |
|
14 | 14 |
|
15 | 15 | class PrerecordedTranscription:
|
16 |
| - """This class provides an interface for doing transcription on prerecorded audio files.""" |
| 16 | + """This class provides an interface for doing transcription asynchronously on prerecorded audio files.""" |
17 | 17 |
|
18 | 18 | _root = "/listen"
|
19 | 19 |
|
@@ -63,6 +63,58 @@ async def __call__(
|
63 | 63 | )
|
64 | 64 |
|
65 | 65 |
|
| 66 | +class SyncPrerecordedTranscription: |
| 67 | + """This class provides an interface for doing transcription synchronously on prerecorded audio files.""" |
| 68 | + |
| 69 | + _root = "/listen" |
| 70 | + |
| 71 | + def __init__(self, options: Options, |
| 72 | + transcription_options: PrerecordedOptions) -> None: |
| 73 | + """ |
| 74 | + This function initializes the options and transcription_options for the PrerecordedTranscription class. |
| 75 | +
|
| 76 | + :param options:Options: Used to Pass in the options for the transcription. |
| 77 | + :param transcription_options:PrerecordedOptions: Used to Specify the transcription options for a prerecorded audio file. |
| 78 | + :return: Nothing. |
| 79 | +
|
| 80 | + """ |
| 81 | + |
| 82 | + self.options = options |
| 83 | + self.transcription_options = transcription_options |
| 84 | + |
| 85 | + def __call__( |
| 86 | + self, source: TranscriptionSource |
| 87 | + ) -> PrerecordedTranscriptionResponse: |
| 88 | + |
| 89 | + """ |
| 90 | + The __call__ function is a special method that allows the class to be called |
| 91 | + as a function. This is useful for creating instances of the class, where we can |
| 92 | + call `SyncPrerecordedTranscription()` and pass in arguments to set up an instance of |
| 93 | + the class. For example: |
| 94 | + |
| 95 | + sync_prerecorded_transcription = SyncPrerecordedTranscription(...) |
| 96 | + |
| 97 | + :param source:TranscriptionSource: Used to Pass in the audio file. |
| 98 | + :return: A `prerecordedtranscriptionresponse` object, which contains the transcription results. |
| 99 | + |
| 100 | + """ |
| 101 | + |
| 102 | + if 'buffer' in source and 'mimetype' not in source: |
| 103 | + raise Exception( |
| 104 | + 'DG: Mimetype must be provided if the source is bytes' |
| 105 | + ) |
| 106 | + payload = cast( |
| 107 | + Union[bytes, Dict], |
| 108 | + source.get('buffer', {'url': source.get('url')}) |
| 109 | + ) |
| 110 | + content_type = cast(str, source.get('mimetype', 'application/json')) |
| 111 | + return _sync_request( |
| 112 | + f'{self._root}{_make_query_string(self.transcription_options)}', |
| 113 | + self.options, method='POST', payload=payload, |
| 114 | + headers={'Content-Type': content_type} |
| 115 | + ) |
| 116 | + |
| 117 | + |
66 | 118 | class LiveTranscription:
|
67 | 119 | """
|
68 | 120 | This class allows you to perform live transcription by connecting to Deepgram's Transcribe Streaming API.
|
@@ -283,6 +335,21 @@ async def prerecorded(
|
283 | 335 | self.options, full_options
|
284 | 336 | )(source)
|
285 | 337 |
|
| 338 | + |
| 339 | + def sync_prerecorded( |
| 340 | + self, source: TranscriptionSource, |
| 341 | + options: PrerecordedOptions = None, **kwargs |
| 342 | + ) -> PrerecordedTranscriptionResponse: |
| 343 | + """Retrieves a transcription for an already-existing audio file, |
| 344 | + local or web-hosted.""" |
| 345 | + if options is None: |
| 346 | + options = {} |
| 347 | + full_options = cast(PrerecordedOptions, {**options, **kwargs}) |
| 348 | + return SyncPrerecordedTranscription( |
| 349 | + self.options, full_options |
| 350 | + )(source) |
| 351 | + |
| 352 | + |
286 | 353 | async def live(
|
287 | 354 | self, options: LiveOptions = None, **kwargs
|
288 | 355 | ) -> LiveTranscription:
|
|
0 commit comments