forked from google/magika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·53 lines (49 loc) · 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/env node
// Command line tool to test MagikaJs. Please use the proper command line
// tool (`pip install magika`) for any real use.
// To run this, you need to install the optional dependencies too.
import { program } from "commander";
import { readFile } from "fs/promises";
// Load the node version of tensorflow, since we're running in the command line.
import * as tf from "@tensorflow/tfjs-node";
import chalk from "chalk";
import { Magika } from "./magika.js";
program
.description(
"Magika JS - file type detection with ML. https://google.github.io/magika",
)
.option("--json-output", "Format output in JSON")
.option(
"--model-url <model-url>",
"Model URL",
"https://google.github.io/magika/model/model.json",
)
.option(
"--config-url <config-url>",
"Config URL",
"https://google.github.io/magika/model/config.json",
)
.argument("<paths...>", "Paths of the files to detect");
program.parse();
const flags = program.opts();
const magika = new Magika();
await magika.load({ modelURL: flags.modelUrl, configURL: flags.configUrl });
await Promise.all(
program.args.map(async (path) => {
let data = null;
try {
data = await readFile(path);
} catch (error) {
console.error("Skipping file", path, error);
}
const prediction = await magika.identifyBytes(data);
if (flags.jsonOutput) {
console.log({ path, ...prediction });
} else {
console.log(
chalk.blue(path),
chalk.green(prediction?.["label"], chalk.white(prediction?.["score"])),
);
}
}),
);