Skip to content

Commit d8a7425

Browse files
committed
Wrap Word data class
1 parent 500f983 commit d8a7425

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

modules/whisper/data_classes.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,23 @@ class Segment(BaseModel):
2929
avg_logprob: Optional[float] = Field(default=None, description="Average log probability of the tokens")
3030
compression_ratio: Optional[float] = Field(default=None, description="Compression ratio of the segment")
3131
no_speech_prob: Optional[float] = Field(default=None, description="Probability that it's not speech")
32-
words: Optional[List['Word']] = Field(default=[], description="List of words contained in the segment")
32+
words: Optional[List['Word']] = Field(default=None, description="List of words contained in the segment")
3333

3434
@classmethod
3535
def from_faster_whisper(cls,
3636
seg: faster_whisper.transcribe.Segment):
37+
if seg.words is not None:
38+
words = [
39+
Word(
40+
start=w.start,
41+
end=w.end,
42+
word=w.word,
43+
probability=w.probability
44+
) for w in seg.words
45+
]
46+
else:
47+
words = None
48+
3749
return cls(
3850
id=seg.id,
3951
seek=seg.seek,
@@ -45,15 +57,15 @@ def from_faster_whisper(cls,
4557
avg_logprob=seg.avg_logprob,
4658
compression_ratio=seg.compression_ratio,
4759
no_speech_prob=seg.no_speech_prob,
48-
words=[] if seg.words is None else seg.words
60+
words=words
4961
)
5062

5163

52-
class Word(NamedTuple):
53-
start: Optional[float] = None
54-
end: Optional[float] = None
55-
word: Optional[str] = None
56-
probability: Optional[float] = None
64+
class Word(BaseModel):
65+
start: Optional[float] = Field(default=None, description="Start time of the word")
66+
end: Optional[float] = Field(default=None, description="Start time of the word")
67+
word: Optional[str] = Field(default=None, description="Word text")
68+
probability: Optional[float] = Field(default=None, description="Probability of the word")
5769

5870

5971
class BaseParams(BaseModel):

0 commit comments

Comments
 (0)