Skip to content

Train, select and serve a model

The title of this example already summarizes where this example is all about. We will show how you can:

  • train multiple models
  • read run metrics to select the best model
  • use the best model to serve a prediction
  • work with input data for jobs

For training, we use an Elastic Net model. The dataset used is Wine Quality. Inspiration for this example came from MLFlow and UbiOps. With the trained AI model, you can predict the quality of wine. Besides training the model, we will also add a job to show how you can use AskAnna to serve a prediction.

If you want to run the models yourself, you need:

First, we will give you a quick tour of running this example. After the quick tour, we will tell more about what happened and share more details regarding this project.

Quick tour

In your terminal, run the following to create a new project using the Demo train, select and serve model template:

askanna create --template --push

You only have to specify the project name. We named it Train, select and serve. If you confirm, the AskAnna CLI will set up a new project.

Now you can run the job that will start different train runs and select the best run:

askanna run select-best-model --data-file input/dummy-select-model.json

Go to and select the project you just created. In this project, open the jobs tab. Click on of the job select-best-model. Next, click on the run.

If the run finished, you can find the selected best model on the METRICS tab, including some statistics.

Next, you can run the serve job. In your terminal, enter the command:

askanna run serve-model --data-file input/dummy-serve-single.json

Go back to the project in the browser, and open the run of the job serve-model. When the run finished, you can find the prediction for the quality of the wine in the RESULT tab.

The above is an abridged version of how you can train, select and serve models in AskAnna. Next, we will deep dive into the different steps to share more background about what just happened.

What happened in the quick tour?

Create the new project

In your terminal, you run the AskAnna command to create a new project using one of our demo projects:

askanna create --template --push

After you confirmed that you want to create the project, the next happened:

  1. a new project in AskAnna was created
  2. a new local directory for the project was created based on the project name
  3. the project files were copied from the project template in the local directory
  4. updated the askanna.yml config with the newly created project URL
  5. pushed a version of the project files to AskAnna

Read more about creating projects and project templates

Run the jobs

In this project, you can run three different jobs. Here we show per job more about running them in AskAnna. In the section About the project code we tell more about the code and the askanna.yml configuration.

Select best model

In the quick tour, we showed how you run the job using the command line:


Running this command will start a run with the JSON file as the data payload. If you want to try other parameters, refer to another JSON file using the same parameters structure.

You can also start the run directly from the web interface. When you created the project and pushed the code, you can go to the project page, open the JOBS tab and click on the job select-best-model.

On the job page, you can start a new run with an optional payload. In the demo below, we used the payload:

    "parameters": [
        {"alpha": 0.3, "l1_ratio": 0.1},
        {"alpha": 0.2, "l1_ratio": 0.7},
        {"alpha": 0.7, "l1_ratio": 0.4}

Run 'select-best-model'

When you start a new run, AskAnna will spin up an environment that also starts the configured train jobs. It can take a couple of minutes before the run finishes. While the run to select the best model is busy, you can already check the status of the individual train jobs. When finished, you can review the result.

Train model

If you want to train a model with parameters alpha and l1_ratio you can use the next command as an example:

askanna run train-model --data '{"alpha": 0.7, "l1_ratio": 0.4}'

The JSON data is optional. You can use the JSON data also to start the job via the web interface.

    "alpha": 0.7,
    "l1_ratio": 0.4

Serve model

When at least one select-best-model is finished, you can also serve a result using the CLI:


Also, you can run the job via the web interface. Additionally, in the web interface, you can find information on how to start this job using the AskAnna API to integrate it with an existing application that wants to consume the result of the served model.

Review the run

Train run

You can find the trained model on the RESULT tab in the web interface for each train run. Here you can also download the trained model. Also, you can review the metrics to see the performance of the model:

Review run metrics

Or variables to check the parameters:

Review run variables

Select best model run

On the run page of this job, you can find information about the selected best model in the metrics:

Review metrics select best model

If you want to see the train jobs used to select the best model, you can find this information in the VARIABLES tab:

Review metrics select best model

Serve model run

The most important information for the job serve-model is the result that contains per wine a prediction of the quality. You can review these predictions on the tab RESULT. Here you can also download the CSV file.

Review run serve model

If you want to check the input used to make the prediction, you can find the payload on the INPUT tab. And to make it possible to reproduce results, you also need to know the model used. The SUUID of the run containing the trained model is tracked as a variable of the run.

About the project code

In the project code, you see two files and three directories. Let's go through it directory by directory, file by file.


This directory contains the Wine Quality dataset from P. Cortez, A. Cerdeira, F. Almeida, T. Matos and J.Reis. We use the data to train the model.


The input directory has some dummy files that can be used to run the jobs select-best-model and serve-model. More about these jobs in the description of the file askanna.yml.


This directory contains the Python functions used to train, select and serve the model.

Train the model that can predict the quality of wine. For the model, we use ElasticNet from scikit-learn. The model will be trained on the wine-quality.csv from the data directory.

In askanna.yml we show how you can configure as a job in AskAnna. You can also run locally from the command line:

python src/ ${alpha} ${l1_ratio}

The arguments alpha and l1_ratio are optional. If you don't provide these arguments, the script will use default values 0.5.

The script's output is a model.pkl file containing the trained model to predict the wine quality.

The script also tracks some additional meta-information:


You also track the meta-information when you run the script locally. You get a link to the JSON files containing the tracked metadata. Read more about tracking metrics.

This Python script starts multiple train jobs. You can run the Python script via the command line, but you need a job train-model in the project in AskAnna to run the script.

First, the script checks if a payload is available. If not, it will use the dummy payload dummy-select-model.json from the input directory. For loading the payload data we use:

input_path = os.getenv("AA_PAYLOAD_PATH", "input/dummy-select-model.json")
with open(input_path, "r") as f:
    input_data = json.load(f)

parameters = input_data.get("parameters", None)

The first line checks if an environment variable AA_PAYLOAD_PATH is available. When you run a job with a payload, AskAnna creates this variable. If there is no payload, we use the default dummy dataset. The following lines load the JSON data from the file and extract the parameters from it.

Next, the script starts multiple train jobs using the parameters alpha and l1_ratio from the input data. These train jobs run parallel. All SUUIDs of the train runs are stored in a list. Starting a run via a Python script is done using:

for param in parameters:
    run ="train-model", data=param)

When the train jobs are finished, the script will get the metadata, including metrics and variables from these train runs. The script finds the run with the lowest rmse and selects that run as the best model.

To get the meta data of multiple runs in Python, use:

runs = askanna.runs.get(runs=train_runs, include_metrics=True, include_variables=True)

The above code will retrun a list with runs. Per run you can get the metric value for the RMSE via:

for run in runs:
    run_rmse = run.metrics.get("rmse")["metric"]["value"]

After selecting the best model, the script tracks the SUUID and metrics of this train run.

The Python script does the following. First, from the last run of the job select-best-model it gets the SUUID of the run that is selected as the best model:

run = askanna.runs.get(job_name="select-best-model", limit=1, offset=0, include_metrics = True)[0]
if run.status == "COMPLETED":
    model_suuid = run.metrics.get("run_suuid")["metric"]["value"]

Then it loads the result object of the seleced train run using pickle:

model_data = askanna.result.get(model_suuid)
model = pickle.loads(model_data)

If a payload is available, the script loads the input data. If not, it will load the dummy payload dummy-serve-single.json from the input directory:

input_path = os.getenv("AA_PAYLOAD_PATH", "input/dummy-serve-single.json")
input_data = pd.read_json(input_path)

And it predicts the wine quality using the loaded model and data:

prediction = model.predict(input_data)

Then it saves the prediction to a CSV file:

pd.DataFrame(prediction).to_csv("prediction.csv", header=["MPG"], index_label="index")

This script also tracks:

  • Identifier of the used trained model
  • Count for input rows
  • Count for output rows


The Python packages that are needed to run the project. If you install the packages in a virtual environment, you can also run the scripts locally.


In the file askanna.yml we define the jobs to train, select and serve models.

job: train-model

The job train-model is defined as follow:

    - pip install -r requirements.txt
    - python src/ ${alpha} ${l1_ratio}
    result: model.pkl

The job installs the requirements.txt and then runs the script Input for this job is optional. When running the job, you can add a JSON payload with the alpha and l1_ratio parameters:

  "alpha": 0.7,
  "l1_ratio": 0.4

If you provide the above JSON payload, AskAnna will make alpha and l1_ratio available as a payload variable.

The created model.pkl file is saved as the run's result.

job: select-best-model

This job runs the script that starts multiple train jobs, compares the metrics and selects the best model. The job tracks the best model as a metric. This metric is used by the script.

The job definition to run the Python script is:

    - python src/
job: serve-model

The job serve-model first installs the requirements.txt and then runs the script As a result, it saves the prediction.csv that is created by the script. The definition for this job:

    - pip install -r requirements.txt
    - python src/
    result: prediction.csv

If you want to predict the wine quality, you can provide this job with a JSON payload using the template below. This template contains input for two wines, but you are flexible in the number of wines you want to predict at the same time.

    "fixed acidity": {
        "0": 7.0,
        "1": 6.3
    "volatile acidity": {
        "0": 0.27,
        "1": 0.3
    "citric acid": {
        "0": 0.36,
        "1": 0.34
    "residual sugar": {
        "0": 20.7,
        "1": 1.6
    "chlorides": {
        "0": 0.045,
        "1": 0.049
    "free sulfur dioxide": {
        "0": 45.0,
        "1": 14.0
    "total sulfur dioxide": {
        "0": 170.0,
        "1": 132.0
    "density": {
        "0": 1.001,
        "1": 0.994
    "pH": {
        "0": 3.0,
        "1": 3.3
    "sulphates": {
        "0": 0.45,
        "1": 0.49
    "alcohol": {
        "0": 8.8,
        "1": 9.5

You can also find an example file dummy-serve-multiple.json in the input directory. This dataset contains 347 different wines.


With this example, you have seen how you can use a project template to set up a project in AskAnna. In this project, three jobs are configured. With these jobs, you could:

  1. Train a model with different parameters
  2. Select the best model by comparing a metric from several train runs
  3. Serve a prediction using the best model
  4. Optionally use JSON data as input for a run

You have seen how you can review each job and get the results of a trained model. And how to get the predictions of the wine quality. Also, you have seen how to track metadata.

We hope that you get an idea about the power of AskAnna via this demo and how this platform can help you and your team collaborate on data science projects. If you have questions or need help starting your first project, don't hesitate to contact us. We love to support you!