Connecting to ModelArts to Recognize Images and Text
ModelArts is a one-stop AI development platform geared toward developers and data scientists of all skill levels. It enables you to rapidly build, train, and deploy models anywhere (from the cloud to the edge), and manage full-lifecycle AI workflows. ModelArts accelerates AI development and fosters AI innovation with key capabilities, including data preprocessing and auto labeling, distributed training, automated model building, and one-click workflow executing.
You can connect your applications to ModelArts to enable image and text recognition in AI scenarios, such as identifying flower varieties.
Prerequisites
- You have obtained the access key ID (AK) and secret access key (SK). For details, see Obtaining an AK/SK.
- You have created and deployed a flower recognition model in ModelArts.
Creating a ModelArts Connector
- Log in to the application designer by referring to Logging In to the Application Designer.
- In the navigation pane, choose Integrations.
- Click Connector Instance under Connector.
- In the navigation pane on the left, choose AI > ModelArts.
- In the right pane, click + and set the parameters.
Figure 1 Creating ModelArts
Table 1 Parameters for creating a ModelArts connector Parameter
Description
Name
Name of the connector to be created. The naming requirements are as follows:
- The value cannot exceed 64 characters, including the prefix namespace.
The content before the name is the namespace. To avoid duplicate data names among different tenants in Astro Zero, a tenant needs to define a namespace when creating an application for the first time. A tenant can create only one namespace. After being created, the namespace cannot be modified.
- Start with a letter and can contain only letters, digits, and underscores (_). Do not end with an underscore (_).
Access Key ID
The AK and the secret access key (SK) are used together to encrypt and sign a request. Set this parameter to the value of AK in Prerequisites.
Secret Access Key
Secret access key is used together with the access key ID to encrypt the request, identify the sender, and prevent the request from being tempered. Set this parameter to the value of AK in Prerequisites.
Region
Select the region of ModelArts.
Project ID
Project ID corresponding to the region where ModelArts is located.
Description
Enter the description of the connector.
Value: 1–255 characters.
Package Current Configuration
If this parameter is selected, the connector (including the access key and key) will be packed together with applications. For example, if an application package is released to the runtime environment, the current configuration is synchronized to the runtime environment by default. If this option is not selected, the connector will not be packaged and released to the runtime environment. In this case, you need to create the corresponding connector by referring to Managing Connectors in Applications.
This parameter is selected by default. You are advised not to select this option in information-sensitive scenarios.
- The value cannot exceed 64 characters, including the prefix namespace.
- Click Save.
- (Optional) Test whether flower images can be recognized.
- Click Test on the connector details page.
- After setting the parameters, click Test.
Choose Type: The object to be recognized can be an image or text. In this example, select Image File and upload the image.
- Image URL: If the object to be recognized is an image, select Image URL and set the details.
- Image File: If the object to be recognized is an image, select Image File and drag the image to the target area.
- Text: If the object to be recognized is text, select Text and enter the specific text.
- Is Api g Code: Used by Astro Zero to call services that have been brought online in ModelArts.
- The operations in Prerequisites have been performed.
- On the ModelArts management console and choose Service Deployment > Real-Time Services.
- Click the name of the target service. The service details page is displayed.
- On the Usage Guides tab of the service details page, check the API URL.
- Model Id: ID of the service that has been brought online on the ModelArts console.
- The operations in Prerequisites have been performed.
- On the ModelArts management console and choose Service Deployment > Real-Time Services.
- Click the name of the target service. The service details page is displayed.
- Check the service ID.
Calling a Connector in a Script
Call the connector in the script to connect to ModelArts.
- Create an empty script by referring to Creating a Blank Script.
- In the script editor, enter the following code.
import * as modelarts from 'modelarts'; export class Input { @action.param({ type: "String", required: false, description: "the image url" }) url: string; @action.param({ type: "String", required: false, description: "the base64 coded image" }) image: string; @action.param({ type: "String", required: false, description: "" }) apigCode: string @action.param({ type: "String", required: false, description: "" }) modelId: string } export class Output { @action.param({ type: "string" }) result: string; } export class testModelarts { @action.method({ input: "Input", output: "Output", description: "modelartspro test case" }) run(input: Input): void { // Enter the name of the instantiated ModelArts Pro connector. let client = modelarts.newClient("testArts") let result = null result = client.modelArtsWithURL(input.url, input.apigCode, input.modelId); console.log(result) return result } }
In modelarts.newClient("testArts"), "testArts" is the connector name. The code must include the namespace prefix if it is present in the connector name.
- Click
in the upper part of the script editor to save the script.
- Click
to execute the script.
- On the input parameter tab page at the bottom of the page, enter request parameters and click
in the upper right corner of the test window.
{ "url": "https://XXXXXXXXX/testmodelarts.jpg", "apigCode": "bec274062225485b95fbcd4d6e8f128a", "modelId": "5ecac550-eefe-4437-9767-57eca07fa91f" }
In the code, set url to the image URL.
- On the output parameter tab page, you can check the recognition result.
{ "predicted_label": "roses", "scores": [ [ "roses", "0.947" ], [ "daisy", "0.050" ], [ "tulips", "0.002" ], [ "dandelion", "0.000" ], [ "sunflowers", "0.000" ] ] }
- Click
in the upper part of the script editor to activate the script.
Calling a Connector in a Flow
Call the connector in a flow to connect to ModelArts.
- Create a blank flow by referring to Creating a Blank Flow.
- Create the input and output parameters of the flow.
The flow calls the ModelArts API. Therefore, the flow's input and output parameters must be defined and match the API's parameter types.
Table 2 Flow variables Variable Type
Parameter
Parameter Type
Input/Output Type
Description
Common
image
Text
Input Only
Base64-encoded image data.
Common
apigCode
Text
Input Only
Model APIG code.
Common
modelID
Text
Input Only
Model ID.
Common
result
Any
Output Only
Output query result.
Common
error
Text
Output Only
Error information.
- On the right of the flow designer, click
.
- On the Context panel, click the plus sign (+) next to Variable.
Figure 2 Creating a variable
- Change the variable name to image.
Figure 3 Setting a variable
- Add other variables in Table 2, click the start diagram element
, and set the input and output parameters of the flow on the right.
Figure 4 Setting input and output parameters
- On the right of the flow designer, click
- On the left of the flow design page, choose Connector > AI and drag the ModelArts diagram element to the canvas.
The ModelArts diagram element is preset in the system for connecting to ModelArts.
Figure 5 Dragging ModelArts diagram element to the canvas - Select the ModelArts diagram element, click
on the right, and select the testArts connector created in Creating a ModelArts Connector.
- Click
and set action parameters.
Figure 6 Configuring action parameters- Action: ModelArts API to be called. In this example, select modelarts_with_image.
- modelarts_with_url: Identifies images based on image URLs.
- modelarts_with_image: Identifies images based on image files.
- modelarts_with_text: Recognizes text based on text information.
- Input Parameters: Input parameters of the API. Select a parameter from Target and drag the corresponding variable in Context to Source.
- Output Parameters: Output parameters of the API.
- Select result from Source and drag result in Context to Target to receive the calling result.
- Select error from Source and drag error in Context to Target to receive the error information.
- Action: ModelArts API to be called. In this example, select modelarts_with_image.
- Connect the Start and ModelArts diagram elements.
Figure 7 Connecting diagram elements
- Click
to save the flow.
- Click
to activate the flow.
- Set input parameters and click the run button.
{ "image": "https://XXXXXXXX/testmodelarts.jpg", "apigCode": "bec274062225485b95fbcd4d6e8f128a", "modelID": "5ecac550-eefe-4437-9767-57eca07fa91f" }
In the code, image is the image URL. If the recognition result is displayed, the connector is successfully called.
Figure 8 Output result - Click
in the upper part of the editor to activate the flow.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot