Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions deepgram_captions/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
from .helpers import chunk_array, replace_text_with_word


class ConverterException(Exception):
pass


class DeepgramConverter:
def __init__(self, dg_response):
def __init__(self, dg_response, use_exception: bool = True):
if not isinstance(dg_response, dict):
self.response = json.loads(dg_response.to_json())
else:
self.response = dg_response

if use_exception:
one_valid_transcription = False
for channel in self.response["results"]["channels"]:
if channel["alternatives"][0]["transcript"] != "":
one_valid_transcription = True
break
if "utterances" in self.response["results"]:
for utterance in self.response["results"]["utterances"]:
if utterance["transcript"] != "":
one_valid_transcription = True
break

if not one_valid_transcription:
raise ConverterException("No valid transcriptions found in response")

def get_lines(self, line_length):
results = self.response["results"]
content = []
Expand All @@ -19,7 +38,6 @@ def get_lines(self, line_length):
content.extend(chunk_array(utterance["words"], line_length))
else:
content.append(utterance["words"])

else:
words = results["channels"][0]["alternatives"][0]["words"]
diarize = (
Expand Down
4 changes: 4 additions & 0 deletions deepgram_captions/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from datetime import datetime


class EmptyTranscriptException(Exception):
pass


def seconds_to_timestamp(seconds, format="%H:%M:%S.%f"):
seconds = round(seconds, 3)
dt = datetime.utcfromtimestamp(seconds)
Expand Down
5 changes: 4 additions & 1 deletion deepgram_captions/srt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .helpers import seconds_to_timestamp
from .helpers import seconds_to_timestamp, EmptyTranscriptException


def srt(converter, line_length=None):
Expand All @@ -12,6 +12,9 @@ def srt(converter, line_length=None):

current_speaker = None

if not lines[0]:
raise EmptyTranscriptException("No transcript data found")

for words in lines:
output.append(str(entry))
entry += 1
Expand Down
5 changes: 4 additions & 1 deletion deepgram_captions/webvtt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .helpers import seconds_to_timestamp
from .helpers import seconds_to_timestamp, EmptyTranscriptException


def webvtt(converter, line_length=None):
Expand All @@ -22,6 +22,9 @@ def webvtt(converter, line_length=None):
if hasattr(converter, "get_lines") and callable(getattr(converter, "get_lines")):
lines = converter.get_lines(line_length)

if not lines[0]:
raise EmptyTranscriptException("No transcript data found")

speaker_labels = "speaker" in lines[0][0]

for words in lines:
Expand Down