New CLI tool

This commit is contained in:
Samuel Sloniker 2022-05-21 14:02:20 -07:00
parent e06f2def24
commit 4188045b75
2 changed files with 40 additions and 28 deletions

View File

@ -8,21 +8,23 @@ GPTC provides both a CLI tool and a Python library.
### Classifying text ### Classifying text
python -m gptc <modelfile> python -m gptc classify <compiled model file>
This will prompt for a string and classify it, then print (in JSON) a dict of
the format `{category: probability, category:probability, ...}` to stdout.
Alternatively, if you only need the most likely category, you can use this:
python -m gptc classify [-c|--category] <compiled model file>
This will prompt for a string and classify it, outputting the category on This will prompt for a string and classify it, outputting the category on
stdout (or "None" if it cannot determine anything). stdout (or "None" if it cannot determine anything).
Alternatively, if you need confidence data, use:
python -m gptc -j <modelfile>
This will print (in JSON) a dict of the format `{category: probability,
category:probability, ...}` to stdout.
### Compiling models ### Compiling models
python -m gptc <raw model file> -c|--compile <compiled model file> python -m gptc compile <raw model file>
This will print the compiled model in JSON to stdout.
## Library ## Library

View File

@ -6,34 +6,44 @@ import json
import sys import sys
import gptc import gptc
parser = argparse.ArgumentParser(description="General Purpose Text Classifier") parser = argparse.ArgumentParser(description="General Purpose Text Classifier", prog='gptc')
parser.add_argument("model", help="model to use") subparsers = parser.add_subparsers(dest="subparser_name", required=True)
parser.add_argument(
"-c", compile_parser = subparsers.add_parser('compile', help='compile a raw model')
"--compile", compile_parser.add_argument("model", help="raw model to compile")
help="compile raw model model to outfile",
metavar="outfile", classify_parser = subparsers.add_parser('classify', help='classify text')
) classify_parser.add_argument("model", help="compiled model to use")
parser.add_argument( group = classify_parser.add_mutually_exclusive_group()
group.add_argument(
"-j", "-j",
"--confidence", "--json",
help="output confidence dict in json", help="output confidence dict as JSON (default)",
action="store_true", action="store_true",
) )
group.add_argument(
"-c",
"--category",
help="output most likely category or `None`",
action="store_true",
)
args = parser.parse_args() args = parser.parse_args()
with open(args.model, "r") as f: with open(args.model, "r") as f:
raw_model = json.load(f) model = json.load(f)
if args.compile:
with open(args.compile, "w+") as f: if args.subparser_name == 'compile':
json.dump(gptc.compile(raw_model), f) print(json.dumps(gptc.compile(model)))
else: else:
classifier = gptc.Classifier(raw_model) classifier = gptc.Classifier(model)
if sys.stdin.isatty(): if sys.stdin.isatty():
text = input("Text to analyse: ") text = input("Text to analyse: ")
else: else:
text = sys.stdin.read() text = sys.stdin.read()
if args.confidence:
print(json.dumps(classifier.confidence(text))) if args.category:
else:
print(classifier.classify(text)) print(classifier.classify(text))
else:
print(json.dumps(classifier.confidence(text)))