|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""PyMilo main.""" |
| 3 | +import re |
| 4 | +import argparse |
| 5 | +from art import tprint |
| 6 | +from .pymilo_param import PYMILO_VERSION, URL_REGEX |
| 7 | +from .pymilo_func import print_supported_ml_models, pymilo_help |
| 8 | +from .pymilo_obj import Import |
| 9 | +from .utils.util import get_sklearn_class |
| 10 | + |
| 11 | +ml_streaming_support = True |
| 12 | +try: |
| 13 | + from .streaming import PymiloServer, Compression, CommunicationProtocol |
| 14 | +except BaseException: |
| 15 | + ml_streaming_support = False |
| 16 | + |
| 17 | + |
| 18 | +def main(): |
| 19 | + """ |
| 20 | + CLI main function. |
| 21 | +
|
| 22 | + :return: None |
| 23 | + """ |
| 24 | + parser = argparse.ArgumentParser(description='Run the Pymilo server with a specified compression method.') |
| 25 | + parser.add_argument( |
| 26 | + '--compression', |
| 27 | + type=str, |
| 28 | + choices=['NULL', 'GZIP', 'ZLIB', 'LZMA', 'BZ2'], |
| 29 | + default='NULL', |
| 30 | + help='Specify the compression method (NULL, GZIP, ZLIB, LZMA, or BZ2). Default is NULL.' |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + '--port', |
| 34 | + type=int, |
| 35 | + default=8000, |
| 36 | + help='Specify PyMiloServer port number', |
| 37 | + metavar="", |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + '--protocol', |
| 41 | + type=str, |
| 42 | + choices=['REST', 'WEBSOCKET'], |
| 43 | + default='REST', |
| 44 | + help='Specify the communication protocol (REST or WEBSOCKET). Default is REST.' |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + '--load', |
| 48 | + type=str, |
| 49 | + default=None, |
| 50 | + help='the `load` command specifies the path to the JSON file of the previously exported ML model by PyMilo.', |
| 51 | + metavar="", |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + '--init', |
| 55 | + type=str, |
| 56 | + default=None, |
| 57 | + help='the `init` command specifies the ML model to initialize the PyMilo Server with.', |
| 58 | + metavar="", |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + '--bare', |
| 62 | + default=False, |
| 63 | + action='store_true', |
| 64 | + help='The `bare` command starts the PyMilo Server without an internal ML model.', |
| 65 | + ) |
| 66 | + parser.add_argument('--version', action='store_true', default=False, help='PyMilo version') |
| 67 | + parser.add_argument('-v', action='store_true', default=False, help='PyMilo version') |
| 68 | + args = parser.parse_args() |
| 69 | + if args.version or args.v: |
| 70 | + print(PYMILO_VERSION) |
| 71 | + return |
| 72 | + if not ml_streaming_support: |
| 73 | + print("Error: ML Streaming is not installed.") |
| 74 | + print("To install ML Streaming, run the following command:") |
| 75 | + print("pip install pymilo[streaming]") |
| 76 | + print("For more information, visit the PyMilo README at https://github.com/openscilab/pymilo") |
| 77 | + tprint("PyMilo") |
| 78 | + tprint("V:" + PYMILO_VERSION) |
| 79 | + pymilo_help() |
| 80 | + parser.print_help() |
| 81 | + return |
| 82 | + run_ps = False |
| 83 | + _model = None |
| 84 | + _port = args.port |
| 85 | + _compressor = Compression[args.compression] |
| 86 | + _communication_protocol = CommunicationProtocol[args.protocol] |
| 87 | + if args.load: |
| 88 | + path = args.load |
| 89 | + run_ps = True |
| 90 | + _model = Import(url=path) if re.match(URL_REGEX, path) else Import(file_adr=path) |
| 91 | + elif args.init: |
| 92 | + model_name = args.init |
| 93 | + model_class = get_sklearn_class(model_name) |
| 94 | + if model_class is None: |
| 95 | + print( |
| 96 | + "The given ML model name is neither valid nor supported, use the list below: \n{print_supported_ml_models}") |
| 97 | + print_supported_ml_models() |
| 98 | + return |
| 99 | + run_ps = True |
| 100 | + _model = model_class() |
| 101 | + elif args.bare: |
| 102 | + run_ps = True |
| 103 | + _model = model_class() |
| 104 | + if not run_ps: |
| 105 | + tprint("PyMilo") |
| 106 | + tprint("V:" + PYMILO_VERSION) |
| 107 | + pymilo_help() |
| 108 | + parser.print_help() |
| 109 | + else: |
| 110 | + PymiloServer( |
| 111 | + model=_model, |
| 112 | + port=_port, |
| 113 | + compressor=_compressor, |
| 114 | + communication_protocol=_communication_protocol, |
| 115 | + ).communicator.run() |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + main() |
0 commit comments