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 Encoder.js for NodeJs #16

Open
wants to merge 1 commit into
base: master
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
7 changes: 4 additions & 3 deletions Encoder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file includes code which was modified from https://github.com/openai/gpt-2
const fs = require('fs')
const path = require('path');
const util = require('util');

const encoder = JSON.parse(fs.readFileSync(path.join(__dirname, './encoder.json')));
const bpe_file = fs.readFileSync(path.join(__dirname, './vocab.bpe'), 'utf-8');
Expand All @@ -18,12 +19,12 @@ const chr = x => {
return String.fromCharCode(x)
}

const textEncoder = new TextEncoder("utf-8")
const textEncoder = new util.TextEncoder("utf-8")
const encodeStr = str => {
return Array.from(textEncoder.encode(str)).map(x => x.toString())
}

const textDecoder = new TextDecoder("utf-8")
const textDecoder = new util.TextDecoder("utf-8")
const decodeStr = arr => {
return textDecoder.decode(new Uint8Array(arr));
}
Expand Down Expand Up @@ -175,4 +176,4 @@ function decode(tokens) {
module.exports = {
encode,
decode
};
};