humanifyjs
Version:
> Deobfuscate Javascript code using LLMs ("AI")
71 lines (70 loc) • 2.57 kB
JavaScript
import fs from "fs/promises";
import { createWriteStream, existsSync } from "fs";
import { basename } from "path";
import { Readable } from "stream";
import { finished } from "stream/promises";
import { url } from "./url.js";
import { showProgress } from "./progress.js";
import { err } from "./cli-error.js";
import { homedir } from "os";
import { join } from "path";
import { Llama3_1ChatWrapper } from "node-llama-cpp";
const MODEL_DIRECTORY = join(homedir(), ".humanifyjs", "models");
export const MODELS = {
"2b": {
url: url `https://huggingface.co/bartowski/Phi-3.1-mini-4k-instruct-GGUF/resolve/main/Phi-3.1-mini-4k-instruct-Q4_K_M.gguf?download=true`
},
"8b": {
url: url `https://huggingface.co/lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf?download=true`,
wrapper: new Llama3_1ChatWrapper()
}
};
async function ensureModelDirectory() {
await fs.mkdir(MODEL_DIRECTORY, { recursive: true });
}
export function getModelWrapper(model) {
if (!(model in MODELS)) {
err(`Model ${model} not found`);
}
return MODELS[model].wrapper;
}
export async function downloadModel(model) {
var _a, _b;
await ensureModelDirectory();
const url = MODELS[model].url;
if (url === undefined) {
err(`Model ${model} not found`);
}
const path = getModelPath(model);
if (existsSync(path)) {
console.log(`Model "${model}" already downloaded`);
return;
}
const response = await fetch(url);
if (!response.ok || !response.body) {
err(`Failed to download model ${model}`);
}
const tmpPath = `${path}.part`;
const fileStream = createWriteStream(tmpPath);
const readStream = Readable.fromWeb(response.body);
showProgress(readStream);
await finished(readStream.pipe(fileStream));
await fs.rename(tmpPath, path);
(_b = (_a = process.stdout).clearLine) === null || _b === void 0 ? void 0 : _b.call(_a, 0);
console.log(`Model "${model}" downloaded to ${path}`);
}
export const DEFAULT_MODEL = Object.keys(MODELS)[0];
export function getModelPath(model) {
if (!(model in MODELS)) {
err(`Model ${model} not found`);
}
const filename = basename(MODELS[model].url.pathname);
return `${MODEL_DIRECTORY}/${filename}`;
}
export function getEnsuredModelPath(model) {
const path = getModelPath(model);
if (!existsSync(path)) {
err(`Model "${model}" not found. Run "humanify download ${model}" to download the model.`);
}
return path;
}