Skip to content

Input and Output

You can define input and output for image classification like this:

from tungstenkit import BaseIO, Image

class Input(BaseIO):
    image: Image

class Output(BaseIO):
    score: float
    label: str

Here you see:

  • Input and output classes are inherited from tungstenkit.BaseIO.
  • Input has a field named 'image', and its type is tungstenkit.Image.
  • Output has two fields named 'score' and 'label', and their types are float and str.

The BaseIO class

The BaseIO class is the base class for all inputs and outputs in Tungstenkit.

It is a simple wrapper of the BaseModel class in Pydantic, so you can use all useful methods and attributes it provides.

Supported field types

Tungstenkit currently supports the following input/output field types:

Type Input Output
tungstenkit.Image
tungstenkit.Video
tungstenkit.Audio
tungstenkit.Binary
str
float
int
bool
dict or typing.Dict
list or typing.List
tuple or typing.Tuple
A subclass of tungstenkit.BaseIO

For dict, list, tuple, typing.Dict, typing.List and typing.Tuple, type arguments are required. For example, you should use dict[str, str] instead of dict.

Files

Tungstenkit provides four primitives for files: Image, Video, Audio, and Binary. They possess the following property and method:

  • path : a string of the file path.
  • from_path(path: StrPath): a class method for creating file objects from a filepath.
    >>> from tungstenkit import Video
    >>> video_path = "video.mp4"
    >>> video = Video.from_path(video_path)
    >>> video.path
    '/home/tungsten/working_dir/video.mp4'
    

The Image object has more methods:

  • from_pil_image(pil_image: PIL.Image.Image): a class method for creating the object from a PIL.Image.Image object.
  • to_pil_image(mode: str = "RGB"): returns a PIL.Image.Image object.

Input field descriptors

Tungstenkit contains two input field descriptors: Field and Option functions:

  • Field: For setting properties of a required field.
  • Option: For declaring a field as optional and setting its properties. Optional fields will be same in an input batch and hidden in the model demo page by default.

Using them, you can:

  • Distinguish between required and optional fields.
  • Restrict input field values.
  • Set input field descriptions shown in the model demo page.

For example, you can define an input class for text-to-image generation as follows:

from tungstenkit import BaseIO, Field, Option

class Input(BaseIO):
    prompt: str = Field(
        description="Input prompt", 
        min_length=1, 
        max_length=200
    )
    width: int = Option(
        description="Width of output image",
        choices=[128, 256, 512],
        default=512,
    )
    height: int = Option(
        description="Height of output image",
        choices=[128, 256, 512],
        default=512,
    )

Both field descriptors take the following keyword-only arguments:

  • description (str, optional): Human-readable description.
  • ge (float, optional): Greater than or equal. If set, value must be greater than or equal to this. Only applicable to int and float.
  • le (float, optional): Less than or equal. If set, value must be less than or equal to this. Only applicable to int and float.
  • min_length (int, optional): Minimum length for strings.
  • max_length (int, optional): Maximum length for strings.
  • choices (list, optional): List of possible values. If set, value must be among a value in this.

The Option function additionally takes a positional argument:

  • default (Any, required): Default value.