How to Make the Model DAFNI Ready
In the previous section, we discussed how to containerise a model using Docker. In this section, we will demonstrate how to prepare these containerised models for execution on the DAFNI platform by:
Defining model inputs and outputs using a model definition file (
model_definition.yaml
). DAFNI enables the execution and connection of a wide range of models written in different programming languages. To achieve interoperability, models on the DAFNI platform must follow a standardised approach for handling inputs and outputs.Saving the containerised model to a tar archive for upload to the DAFNI platform.
The model definition file defines key details about the model, such as executable commands, input parameters, and output files. When uploaded to the DAFNI platform, these definitions are used to create a user interface, allowing users to interact with the model seamlessly through the platform's GUI (we will see in the next section). For a more detailed explanation of model definitions, see our How to Write a Model Definition guide.
Example 1: Building Python Model DAFNI Ready
Below is an example of a model_definition.yaml
file for the Python Fibonacci model:
kind: M
api_version: v1beta3
metadata:
display_name: Fibonacci Python Model
name: fibonacci
publisher: DAFNI Example Models
contact_point_name: DAFNI
contact_point_email: info@dafni.ac.uk
summary: Generates a Fibonacci sequence
description: >
An example Model designed for use with DAFNI.
This generates a Fibonacci sequence and saves it to a json file.
licence: https://creativecommons.org/licenses/by/4.0/
rights: open
spec:
command: ["python", "/src/fibonacci.py"]
inputs:
parameters:
- name: SEQUENCE_LENGTH
title: Sequence length
description: The number of items in the sequence.
type: integer
default: 20
min: 2
required: true
- name: SEQUENCE_F0
title: F0 start number
description: The initial start value for the sequence.
type: integer
default: 0
required: true
- name: SEQUENCE_F1
title: F1 start number
description: The second start value for the sequence.
type: integer
default: 1
required: true
outputs:
datasets:
- name: sequence.json
type: json
description: A Fibonacci sequence output from the Fibonacci Example Model.
Save this model_definition.yaml
in the same directory as fibonacci.py
. The content should look like:
.
├── Dockerfile
├── fibonacci.py
└── model_definition.yaml
Run the following commands to create a tar archive:
docker build -t fibonacci-py:to-upload .
docker save -o fibonacci-py.tar fibonacci-py:to-upload
gzip fibonacci-py.tar
The compressed tar file fibonacci-py.tar.gz
is now ready for upload to DAFNI, which we will demonstrate in the next section.
Example 2: Building the C++ Model DAFNI Ready
Below is an example of a model_definition.yaml
file for the C++ Fibonacci model:
kind: M
api_version: v1beta3
metadata:
display_name: Fibonacci C++
name: fibonacci
publisher: DAFNI Example Models
contact_point_name: DAFNI
contact_point_email: info@dafni.ac.uk
summary: Generates a Fibonacci sequence
description: >
An example Model designed for use with DAFNI.
This generates a Fibonacci sequence and saves it to a json file.
licence: https://creativecommons.org/licenses/by/4.0/
rights: open
spec:
command: ["/app/fibonacci"]
inputs:
parameters:
- name: SEQUENCE_LENGTH
title: Sequence length
description: The number of items in the sequence.
type: integer
default: 20
min: 2
required: true
- name: SEQUENCE_F0
title: F0 start number
description: The initial start value for the sequence.
type: integer
default: 0
required: true
- name: SEQUENCE_F1
title: F1 start number
description: The second start value for the sequence.
type: integer
default: 1
required: true
outputs:
datasets:
- name: sequence.json
type: json
description: A Fibonacci sequence output from the Fibonacci Example Model.
Save this model_definition.yaml
in the same directory as fibonacci.cpp
. The content should look like:
.
├── Dockerfile
├── fibonacci.cpp
└── model_definition.yaml
Run the following commands to create a tar archive:
docker build -t fibonacci-cpp:to-upload .
docker save -o fibonacci-cpp.tar fibonacci-cpp:to-upload
gzip fibonacci-cpp.tar
The compressed tar file fibonacci-cpp.tar.gz
is now ready for upload to DAFNI.
Now that we've prepared two DAFNI-ready models along with their definition files, the next step is to upload these models to the DAFNI platform and execute them. In the following section, we will walk through the upload and execution process on DAFNI.