Setup Code
The sample model will have the following structure:
AI-node-public
|--- aioz_ainode_adapter
|--- ...
|--- my_ai_lib
|--- run.py
|--- models
|--- wiki
|--- aioz.png
|--- setup.py
|--- demo.py
|--- compress.pyIn our project structure, the run.py file plays a crucial role in algorithm development. This is where you can focus on writing code to handle data processing, implement algorithms, and interact with the AI model.
Additionally, within our project structure, we provide a models directory to store the weights related to the algorithm. This directory contains files relevant to the state and parameters of the model, making it convenient for you to store and manage the weights of the AI model.
Input and Output
You also need to specify the necessary variables to ensure that the algorithm functions correctly and meets the requirements of the problem. Properly defining variables is crucial because they determine the input data and output results. You need to specify the data type (e.g., integer, float, text string, list...) and provide clear, understandable, and purposeful variable names that are appropriate for their intended use.
from pathlib import Path
from typing import Any, Union, Literal, Optional
from aioz_ainode_adapter.schemas import InputObject, OutputObject, FileObject
# Define lib
# ...
class MyInput(InputObject):
input_image: str
example_param: Optional[Any] = ""
class MyOutput(OutputObject):
text: str
output_image: FileObjectAlgorithm development
In the process of developing complex algorithms, you may create multiple sub-functions or separate files. However, the final code processing should primarily focus on the do_ai_task function. In this function, you can handle tasks related to the model, utilize the model_storage_directory variable to access the model's weights, and use the device variable to execute the algorithm on the corresponding environment.
When processing the model, remember to leverage the model_storage_directory variable to fetch the model's weights. This ensures flexible access and usage of weights from the storage directory. Additionally, the device variable will provide the execution environment to ensure the algorithm runs correctly on the respective device.
An important note is that since your model may run on different operating systems, be sure to use the os library to read the path of the model's weights. This ensures the portability and reliability of the source code when accessing weight files on different environments.
def do_ai_task(
input_image: Union[str, Path],
example_param: Any,
model_storage_directory: Union[str, Path],
device: Literal["cpu", "cuda", "gpu"] = "cpu",
*args, **kwargs) -> Any:
"""Define AI task: load model, pre-process, post-process, etc ..."""
# Define AI task workflow. Below is an example
text = "This is the AI task result"
output_image = open("wiki/aioz.png", "rb") # io.BufferedReader
return text, output_image