none
Python error while publishing API get source code RRS feed

  • Question


  • Hi Everyone,

    I’m getting error generating a Python API with parameters as an input.  I have followed the step-by-step instruction found in following white page:

    Github -> Microsoft -> ML-Server-Python-Samples ->blob -> master -> operationalize -> Quickstart_Publish_Python_Web_Service.ipynb

    Yet, I’m receiving an error message on the “service.deploy()” line of code.

     

    Always this:

     Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\site-packages\azureml\deploy\operationalization.py", line 473, in deploy
        return self._op.deploy_service(self._name, **self._defs)
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\site-packages\azureml\deploy\server\mlserver.py", line 297, in deploy_service
        self._service.create_service(name, **opts)
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\site-packages\azureml\deploy\server\services.py", line 58, in create_service
        pub_request = env.create(PublishRequest(), **kwargs)
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\site-packages\azureml\deploy\server\services.py", line 330, in create
        init = to_self_execution_block(init)
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\site-packages\azureml\common\utils.py", line 122, in to_self_execution_block
        src = inspect.getsource(fn).lstrip()
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\inspect.py", line 944, in getsource
        lines, lnum = getsourcelines(object)
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\inspect.py", line 931, in getsourcelines
        lines, lnum = findsource(object)
      File "C:\Program Files\Microsoft\ML Server\PYTHON_SERVER\lib\inspect.py", line 762, in findsource
        raise OSError('could not get source code')
    OSError: could not get source code


    Could you please help me on this, Thanks..





    Wednesday, April 4, 2018 1:06 PM

All replies

  • The trick is to run in Jupyter Notebook using Anaconda# as your Python instance.  

    MRSDeploy Python API with parameters will not generate in Visual Studio on the Python instance loaded with ML Services.  

    This sounds crazy but hey, "if it works, it's true."

    Thursday, April 5, 2018 7:49 PM
  • Hello,

    The issue is that underlining library will inspect your `code_fn` reference for the function's argument signature to publish. This meta-inspection is really only available in ipython|jupyter and not the basic python REPL as ipython is how [most] will interact with the API in this fashion. With that said, if your are attempting to use python's REPL in this fashion I would highly suggest you use ipython|Jupyter interpreter as it is fare more useful for this type of line-by-line interaction. It feels more like R for example.

    To get around this, either:

    1. publish via ipython|Jupyter [or]

    2. If from python REPL and not  ipython|Jupyter put your `init` and `run` functions in a separate file and simple import them.

    Example:

    Rather than doing this:

    def init():
    pass

    def my_run_fn(x):
    pass

    api = client.service('service-name')
    .version('v1.0.0')
    .code_fn(my_run_fn, init)
    .inputs(x=int)
    .deploy()

    result = api.my_run_fn(5)

    Do This:


    import my_init_and_run_functions

    api = client.service('service-name')
    .version('v1.0.0')
    .code_fn(my_run_fn, init)
    .inputs(x=int)
    .deploy()

    result = api.my_run_fn(5)


    3. OR Publish via a string literal.


    init = "def init():" \
    "pass"

    my_run_fn = "def my_run_fn(x):" \
    "pass"
    api = client.service('service-name')
    .version('v1.0.0')
    .code_str(my_run_fn, init)
    .inputs(x=int)
    .deploy()

    result = api.my_run_fn(5)

    The reason for this is that you have many use-case options to publis (e.g) Line by Line via the ipython or from a large application that might import the module.

    Let me know if you run into any problems.

    Regards,

    Sean


    Wednesday, April 18, 2018 8:32 PM