Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: Ensure method _asdict also converts Word object into a native dict #667

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 32 additions & 2 deletions faster_whisper/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
import zlib

from inspect import signature
from typing import BinaryIO, Iterable, List, NamedTuple, Optional, Tuple, Union
from typing import (
Any,
BinaryIO,
Dict,
Iterable,
List,
NamedTuple,
Optional,
Tuple,
Union,
)

import ctranslate2
import numpy as np
Expand All @@ -30,7 +40,7 @@ class Word(NamedTuple):
probability: float


class Segment(NamedTuple):
class NamedTupleSegment(NamedTuple):
id: int
seek: int
start: float
Expand All @@ -44,6 +54,26 @@ class Segment(NamedTuple):
words: Optional[List[Word]]


class Segment(NamedTupleSegment):
def _asdict(self) -> Dict[str, Any]:
words: Optional[List[Dict[str, Any]]] = None
if self.words:
words = [word._asdict() for word in self.words]
return {
"id": self.id,
"seek": self.seek,
"start": self.start,
"end": self.end,
"text": self.text,
"tokens": self.tokens,
"temperature": self.temperature,
"avg_logprob": self.avg_logprob,
"compression_ratio": self.compression_ratio,
"no_speech_prob": self.no_speech_prob,
"words": words,
}


class TranscriptionOptions(NamedTuple):
beam_size: int
best_of: int
Expand Down
46 changes: 46 additions & 0 deletions tests/test_transcribe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from faster_whisper import WhisperModel, decode_audio
from faster_whisper.transcribe import Segment, Word


def test_supported_languages():
Expand Down Expand Up @@ -97,3 +98,48 @@ def test_stereo_diarization(data_dir):
segments, _ = model.transcribe(right)
transcription = "".join(segment.text for segment in segments).strip()
assert transcription == "The horizon seems extremely distant."


def test_segment_as_dict():
segment = Segment(
id=1,
seek=398,
start=0.0,
end=3.04,
text=" Hello world.",
tokens=[50364, 2425, 1002, 13, 50516],
temperature=0.9,
avg_logprob=-1.1086603999137878,
compression_ratio=0.6,
no_speech_prob=0.10812531411647797,
words=[
Word(start=0.0, end=1.82, word=" Hello", probability=0.8265082836151123),
Word(start=1.82, end=3.04, word=" world.", probability=0.31053248047828674),
],
)
assert segment._asdict() == {
"id": 1,
"seek": 398,
"start": 0.0,
"end": 3.04,
"text": " Hello world.",
"tokens": [50364, 2425, 1002, 13, 50516],
"temperature": 0.9,
"avg_logprob": -1.1086603999137878,
"compression_ratio": 0.6,
"no_speech_prob": 0.10812531411647797,
"words": [
{
"start": 0.0,
"end": 1.82,
"word": " Hello",
"probability": 0.8265082836151123,
},
{
"start": 1.82,
"end": 3.04,
"word": " world.",
"probability": 0.31053248047828674,
},
],
}
Loading