Search code examples
nucliomlrun

What is the difference between mlrun.code_to_function and mlrun.new_project?


What is the difference between mlrun.code_to_function and mlrun.new_project? For example, we can deploy a function with code_to_function:

a_fn = mlrun.code_to_function(name='my_function', 
                              handler='handler', 
                              kind='nuclio', 
                              image='mlrun/mlrun')
a_fn.deploy()

or we can deploy a function with mlrun.project.set_function

project = mlrun.new_project(project_name_base,
                            context=project_path,                           
                            user_project=True)                                                    
fn = project.set_function("my_function.ipynb", 
                           name='my_function')

In which case I should use code_to_function or set_function?


Solution

  • One of the core concepts of MLRun is creating a serverless function out of a piece of code. You can specify a Python file, entrypoint function, Docker image, K8s resources, and more. code_to_function is how this is accomplished. See this page in the docs for more info.

    # file.py
    def handler(context):
        context.logger.info("Hello World")
    
    # deploy.py
    from mlrun import code_to_function, new_project
    
    project = new_project(name="my-project")
    
    fn = code_to_function(
        name="my-function",
        project="my-project",
        filename="file.py",
        kind="job",
        handler="handler",
        image="mlrun/mlrun"
    )
    fn.run()
    

    You can create and run these functions on their own, or use something like KubeFlow to orchestrate a pipeline with multiple functions. set_project is part of that workflow. You can use the function created via code_to_function or just specify some of the parameters within set_project. Then, you will be able to use this function as a part of a larger KubeFlow pipeline. See this page in the docs for more info.

    project.set_function(fn)
    
    project.set_function(
        name="my-function",
        func="file.py",
        kind="job",
        image="mlrun/mlrun"
    )