-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move node specific code into separate file
- Loading branch information
Loïc Mangeonjean
committed
Apr 15, 2024
1 parent
bb49b83
commit 1be1a2b
Showing
3 changed files
with
54 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ModelOperations, ModelOperationsOptions } from './index'; | ||
export * from './index' | ||
|
||
type NodeModelOperationsOptions = Omit<ModelOperationsOptions, 'modelJsonLoaderFunc' | 'weightsLoaderFunc'> & Partial<ModelOperationsOptions> | ||
|
||
class NodeModelOperations extends ModelOperations { | ||
private static NODE_MODEL_JSON_FUNC: () => Promise<{ [key:string]: any }> = async () => { | ||
const fs = await import('fs'); | ||
const path = await import('path'); | ||
|
||
return new Promise<any>((resolve, reject) => { | ||
fs.readFile(path.join(__dirname, '..', '..', 'model', 'model.json'), (err, data) => { | ||
if(err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(JSON.parse(data.toString())); | ||
}); | ||
}); | ||
} | ||
|
||
private static NODE_WEIGHTS_FUNC: () => Promise<ArrayBuffer> = async () => { | ||
const fs = await import('fs'); | ||
const path = await import('path'); | ||
|
||
return new Promise<ArrayBuffer>((resolve, reject) => { | ||
fs.readFile(path.join(__dirname, '..', '..', 'model', 'group1-shard1of1.bin'), (err, data) => { | ||
if(err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(data.buffer); | ||
}); | ||
}); | ||
} | ||
|
||
constructor(modelOptions?: NodeModelOperationsOptions) { | ||
super({ | ||
modelJsonLoaderFunc: modelOptions?.modelJsonLoaderFunc ?? NodeModelOperations.NODE_MODEL_JSON_FUNC, | ||
weightsLoaderFunc: modelOptions?.weightsLoaderFunc ?? NodeModelOperations.NODE_WEIGHTS_FUNC, | ||
...modelOptions | ||
}) | ||
} | ||
} | ||
|
||
export { | ||
NodeModelOperations as ModelOperations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters