Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ ModelArts/ Best Practices/ Model Inference/ Creating a Custom Image and Using It to Create an AI Application

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

Updated on 2024-03-05 GMT+08:00

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.
    Figure 4 Uploaded images

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 5 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.
    Figure 6 Usage Guides
  3. Access the real-time service on the Prediction tab page.
    Figure 7 Accessing a real-time service

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback