Skip to content

Commit

Permalink
Improve the CLI API of the target programming language
Browse files Browse the repository at this point in the history
  • Loading branch information
Darius Morawiec committed Sep 9, 2017
1 parent 91e7171 commit 41b93a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
22 changes: 10 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,26 +239,24 @@ joblib.dump(clf, 'estimator.pkl')
After that the model can be transpiled by using the following command:

```bash
python -m sklearn_porter --input <pickle_file> [--output <destination_dir>] [--language {c,go,java,js,php,ruby}]
python -m sklearn_porter -i <pickle_file> [-o <destination_dir>] [-l {c,go,java,js,php,ruby}]
python -m sklearn_porter --input <PICKLE_FILE> [--output <DEST_DIR>] [--language {c,go,java,js,php,ruby}] [--c] [--java] [--js] [--go] [--php] [--ruby]
python -m sklearn_porter -i <PICKLE_FILE> [-o <DEST_DIR>] [-l {c,go,java,js,php,ruby}] [--c] [--java] [--js] [--go] [--php] [--ruby]
```

The following commands have all the same result:
For instance the following command transpiles the estimator to the target programming language Java:

```bash
python -m sklearn_porter --input estimator.pkl --language java
python -m sklearn_porter -i estimator.pkl -l java
python -m sklearn_porter -i estimator.pkl --java
```

By changing the language parameter you can set the target programming language:
You can change the target programming language on the fly:

```bash
python -m sklearn_porter -i estimator.pkl -l c
python -m sklearn_porter -i estimator.pkl -l go
python -m sklearn_porter -i estimator.pkl -l java
python -m sklearn_porter -i estimator.pkl -l js
python -m sklearn_porter -i estimator.pkl -l php
python -m sklearn_porter -i estimator.pkl -l ruby
python -m sklearn_porter -i estimator.pkl --c
python -m sklearn_porter -i estimator.pkl --go
python -m sklearn_porter -i estimator.pkl --js
python -m sklearn_porter -i estimator.pkl --php
python -m sklearn_porter -i estimator.pkl --ruby
```

Further information will be shown by using the `--help` parameter:
Expand Down
41 changes: 34 additions & 7 deletions sklearn_porter/__main__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# -*- coding: utf-8 -*-

import os
import sys
import argparse

from . import Porter


def main():
def parse_args(args):
parser = argparse.ArgumentParser(
description=(
'Transpile trained scikit-learn models '
'Transpile trained scikit-learn estimators '
'to C, Java, JavaScript and others. '),
epilog=(
'More details on: '
Expand All @@ -25,17 +26,36 @@ def main():
required=False,
help=(
'Set the destination directory, '
'where the transpiled model will be '
'where the transpiled estimator will be '
'stored.'))
languages = {
'c': 'C',
'java': 'Java',
'js': 'JavaScript',
'go': 'Go',
'php': 'PHP',
'ruby': 'Ruby'
}
parser.add_argument(
'--language', '-l',
choices=['c', 'java', 'js', 'go', 'php', 'ruby'],
choices=languages.keys(),
default='java',
required=False,
help=(
'Set the target programming language '
'("c", "java", "js", "go", "php", "ruby").'))
args = vars(parser.parse_args())
'({}).'.format(', '.join(['"{}"'.format(key)
for key in languages.keys()]))))
for key, lang in list(languages.items()):
parser.add_argument(
'--{}'.format(key),
action='store_true',
help='Set {} as the target programming language.'.format(lang))
args = vars(parser.parse_args(args))
return args


def main():
args = parse_args(sys.argv[1:])

input_path = str(args['input'])
if input_path.endswith('.pkl') and os.path.isfile(input_path):
Expand All @@ -44,8 +64,15 @@ def main():
from sklearn.externals import joblib
model = joblib.load(input_path)

# Determine the target programming language:
language = str(args['language']) # with default language
languages = ['c', 'java', 'js', 'go', 'php', 'ruby']
for key in languages:
if args.get(key): # found ecplicit assignment
language = key
break

# Port model:
language = str(args['language'])
porter = Porter(model, language=language)
details = porter.export(details=True)
filename = details.get('filename')
Expand Down

0 comments on commit 41b93a0

Please sign in to comment.