Help Center> ModelArts> Image Management> Using a Custom Image to Create AI applications for Inference Deployment> Creating a Custom Image and Using It to Create an AI Application
Updated on 2024-04-30 GMT+08:00

Creating a Custom Image and Using It to Create an AI Application

If you want to use an AI engine that is not supported by ModelArts, create a custom image for the engine, import the image to ModelArts, and use the image to create AI applications. This section describes how to use a custom image to create an AI application and deploy the application as a real-time service.

The process is as follows:

  1. Building an Image Locally: Create a custom image package locally. For details, see Custom Image Specifications for Creating AI Applications.
  2. Verifying the Image Locally and Uploading It to SWR: Verify the APIs of the custom image and upload the custom image to SWR.
  3. Using the Custom Image to Create an AI Application: Import the image to ModelArts AI application management.
  4. Deploying the AI Application as a Real-Time Service: Deploy the model as a real-time service.

Building an Image Locally

This section uses a Linux x86_x64 host as an example. You can purchase an ECS of the same specifications or use an existing local host to create a custom image.

For details about how to purchase an ECS, see Purchasing and Logging In to a Linux ECS. When creating the ECS, select an Ubuntu 18.04 public image.
Figure 1 Creating an ECS using an x86 public image
  1. After logging in to the host, install Docker. For details, see Docker official documents. Alternatively, run the following commands to install Docker:
    curl -fsSL get.docker.com -o get-docker.sh
    sh get-docker.sh
  2. Obtain the base image. Ubuntu 18.04 is used in this example.
    docker pull ubuntu:18.04
  3. Create the self-define-images folder, and edit Dockerfile and test_app.py in the folder for the custom image. In the sample code, the application code runs on the Flask framework.
    The file structure is as follows:
    self-define-images/
        --Dockerfile
        --test_app.py
    • Dockerfile
      From ubuntu:18.04
      # Configure the HUAWEI CLOUD source and install Python, Python3-PIP, and Flask.
      RUN cp -a /etc/apt/sources.list /etc/apt/sources.list.bak && \
        sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list && \
        sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list && \
        apt-get update && \
        apt-get install -y python3 python3-pip && \
        pip3 install  --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple  Flask
      
      # Copy the application code to the image.
      COPY test_app.py /opt/test_app.py
      
      # Specify the boot command of the image.
      CMD python3  /opt/test_app.py
    • test_app.py
      from flask import Flask, request
      import json 
      app = Flask(__name__)
      
      @app.route('/greet', methods=['POST'])
      def say_hello_func():
          print("----------- in hello func ----------")
          data = json.loads(request.get_data(as_text=True))
          print(data)
          username = data['name']
          rsp_msg = 'Hello, {}!'.format(username)
          return json.dumps({"response":rsp_msg}, indent=4)
      
      @app.route('/goodbye', methods=['GET'])
      def say_goodbye_func():
          print("----------- in goodbye func ----------")
          return '\nGoodbye!\n'
      
      
      @app.route('/', methods=['POST'])
      def default_func():
          print("----------- in default func ----------")
          data = json.loads(request.get_data(as_text=True))
          return '\n called default func !\n {} \n'.format(str(data))
      
      # host must be "0.0.0.0", port must be 8080
      if __name__ == '__main__':
          app.run(host="0.0.0.0", port=8080)
  4. Switch to the self-define-images folder and run the following command to create custom image test:v1:
    docker build -t test:v1 .
  5. Run docker images to view the custom image you have created.

Verifying the Image Locally and Uploading It to SWR

  1. Run the following command in the local environment to start the custom image:
    docker run -it -p 8080:8080 test:v1
    Figure 2 Starting a custom image
  2. Open another terminal and run the following commands to test the functions of the three APIs of the custom image:
    curl -X POST -H "Content-Type: application/json" --data '{"name":"Tom"}'  127.0.0.1:8080/
    curl -X POST -H "Content-Type: application/json" --data '{"name":"Tom"}' 127.0.0.1:8080/greet
    curl -X GET 127.0.0.1:8080/goodbye

    If information similar to the following is displayed, the function verification is successful.

    Figure 3 Testing API functions
  1. Upload the custom image to SWR. For details, see How Can I Upload Images to SWR?
  2. View the uploaded image on the My Images > Private Images page of the SWR console.

Using the Custom Image to Create an AI Application

Import a meta model. For details, see Creating and Importing a Model Image. Key parameters are as follows:
  • Meta Model Source: Select Container image.
    • Container Image Path: Select the created private image.
      Figure 4 Created private image
    • Container API: Protocol and port number for starting a model. Ensure that the protocol and port number are the same as those provided in the custom image.
    • Image Replication: indicates whether to copy the model image in the container image to ModelArts. This parameter is optional.
    • Health Check: checks health status of a model. This parameter is optional. This parameter is configurable only when the health check API is configured in the custom image. Otherwise, creating the AI application will fail.
  • APIs: APIs of a custom image. This parameter is optional. The model APIs must comply with ModelArts specifications. For details, see Specifications for Editing a Model Configuration File.
    The configuration file is as follows:
    [{
            "url": "/",
            "method": "post",
            "request": {
                "Content-type": "application/json"
            },
            "response": {
                "Content-type": "application/json"
            }
        },
    {
            "url": "/greet",
            "method": "post",
            "request": {
                "Content-type": "application/json"
            },
            "response": {
                "Content-type": "application/json"
            }
        },
    {
            "url": "/goodbye",
            "method": "get",
            "request": {
                "Content-type": "application/json"
            },
            "response": {
                "Content-type": "application/json"
            }
        }
    ]

Deploying the AI Application as a Real-Time Service

  1. Deploy the AI application as a real-time service. For details, see Deploying as a Real-Time Service.
  2. View the details about the real-time service.
  3. Access the real-time service on the Prediction tab page.
    Figure 5 Accessing a real-time service