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

Use tiktoken #1044

Merged
merged 9 commits into from
Mar 13, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tuple should be safer
  • Loading branch information
jongwook committed Mar 7, 2023
commit 67e8805f24e98c515cd6e1f6ebcbe5841044ea29
12 changes: 7 additions & 5 deletions whisper/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@
}


@dataclass(frozen=True)
@dataclass
class Tokenizer:
"""A thin wrapper around `GPT2TokenizerFast` providing quick access to special tokens"""
jongwook marked this conversation as resolved.
Show resolved Hide resolved

encoding: tiktoken.Encoding
language: Optional[str] = None
task: Optional[str] = None
sot_sequence: List[int] = field(default_factory=list)
sot_sequence: Tuple[int] = ()
special_tokens: Dict[str, int] = field(default_factory=dict)

def __post_init__(self):
Expand All @@ -149,12 +149,14 @@ def __post_init__(self):
transcribe: int = self.special_tokens["<|transcribe|>"]

langs = tuple(LANGUAGES.keys())
self.sot_sequence.append(sot)
sot_sequence = [sot]
if self.language is not None:
self.sot_sequence.append(sot + 1 + langs.index(self.language))
sot_sequence.append(sot + 1 + langs.index(self.language))
if self.task is not None:
task_token: int = transcribe if self.task == "transcribe" else translate
self.sot_sequence.append(task_token)
sot_sequence.append(task_token)

self.sot_sequence = tuple(sot_sequence)

def encode(self, text, **kwargs):
return self.encoding.encode(text, **kwargs)
Expand Down