Skip to content

Implement from_bytes and read_bytes Methods in WordPiece Tokenizer for WebAssembly Compatibility #1758

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions tokenizers/src/models/wordpiece/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ impl WordPiece {
Ok(vocab)
}

pub fn read_bytes(vocab: &[u8]) -> Result<Vocab> {
let file = BufReader::new(vocab);

let mut vocab = HashMap::new();
for (index, line) in file.lines().enumerate() {
let line = line?;
vocab.insert(line.trim_end().to_owned(), index as u32);
}

Ok(vocab)
}
pub fn from_bytes(vocab: &[u8]) -> Result<WordPieceBuilder> {
let vocab = WordPiece::read_bytes(vocab)?;
Ok(WordPiece::builder().vocab(vocab))
}

/// Initialize a `WordPiece` model from a vocab mapping file.
pub fn from_file(vocab: &str) -> WordPieceBuilder {
WordPiece::builder().files(vocab.to_owned())
Expand Down