Skip to content

AI

Installing marvin and pydantic is required to use this module.

suggest_tmap(description, num_colors=5, retries=3, verbose=True)

Suggest a TastyMap based on a description of the image.

Parameters:

Name Type Description Default
description str

A description of the image.

required
num_colors int

Number of colors in the colormap. Defaults to 5.

5
retries int

Number of retries to suggest a TastyMap. Defaults to 3.

3
verbose bool

Whether to print the AI description. Defaults to True.

True

Returns:

Name Type Description
TastyMap TastyMap

A new TastyMap instance with the new colormap.

Source code in tastymap/ai.py
def suggest_tmap(
    description: str, num_colors: int = 5, retries: int = 3, verbose: bool = True
) -> TastyMap:
    """
    Suggest a TastyMap based on a description of the image.

    Args:
        description: A description of the image.
        num_colors: Number of colors in the colormap. Defaults to 5.
        retries: Number of retries to suggest a TastyMap. Defaults to 3.
        verbose: Whether to print the AI description. Defaults to True.

    Returns:
        TastyMap: A new TastyMap instance with the new colormap.
    """
    exceptions = []
    for _ in range(retries):
        try:
            ai_description = _refine_description(description, num_colors)
            if verbose:
                print(ai_description)
            ai_palette = AIPalette(ai_description)
            return cook_tmap(
                ["".join(color.split()) for color in ai_palette.colors],
                name=ai_palette.name,
            )
        except Exception as exception:
            exceptions.append(exception)
    else:
        raise ValueError(
            f"Attempted to suggest a TastyMap {retries} times, "
            f"but failed due to {exceptions}"
        )