from flushai import Chain

Text-to-Image Models

At the very basic level, we can use chains to utilize prompt templates with text-to-iamge models. An example of this is shown below. First, let’s create a prompt template below:

from flushai.prompts import PromptTemplate

prompt = '''
photo of a {animal} dressed suit and tie sitting at a table in a bar with a 
bar stools, award winning photography, Elke vogelsang
'''

prompt_template = PromptTemplate(prompt)

Next, let’s get our model. Let’s use Stable Diffusion XL.

from flushai.models.diffusion.text2img import StableDiffusionXL

diffusion = StableDiffusionXL(api_key="YOUR_API_KEY")

Finally, let’s create the chain:

params = {
    "num_images": 1
}

chain = Chain(
    output = (diffusion, prompt_template, params)
)

Let’s dive a bit deeper into the formatting of chains. Each part of the chain for text-to-text or text-to-image models can be simplified into the following format:

MODEL_OUTPUT = (MODEL, PROMPT, PARAMETERS)

where MODEL_OUTPUT is the output of each model, MODEL is the model object that we are running, PROMPT is the prompt we pass into each model, and PARAMETERS is an optional parameters for any other parameters we want to specify. In this case, PARAMETERS is equivalent to a dictionary that represents the optional parameters of text-to-image models. Additionally, as there is only one model in this chain, MODEL_OUTPUT is the final output of the chain.

We can run the chain as below:

chain.run(animal="dinosaur")

Note how we can specify the animal parameter within this statement due to the prompt template we passed in. This will give us the following output:

Text-to-Text Models

Text-to-Text models have the same format as text-to-image models, except that their output is strictly text and they do not have any additional parameters.

MODEL_OUTPUT = (MODEL, PROMPT)

An simple example of this is given below:

from flushai.prompts import PromptTemplate
from flushai.models.llms import OpenAI

llm = OpenAI(api_key="YOUR_OPENAI_API_KEY")

prompt = "Return only the number {num}"
prompt_template = PromptTemplate(prompt)

chain = Chain(
    output = (llm, prompt_template)
)

Now, if we call this chain, we’ll simply get:

chain.run(num="11")
# Output: 11

Image-to-Image Models

Unlike text-to-image models, image-to-image models take in a prompt and an image as input. We’ll look at both situations below.

Text as Input

If we are taking text as input, the format is the same as text-to-image models. The only difference is that we include an image in our PARAMETERS dictionary:

from flushai.models.diffusion.img2img import StableDiffusionXL

diffusion = StableDiffusionXL(api_key="YOUR_API_KEY")

params = {
    "image": "https://flush-user-images.s3.amazonaws.com/generated_images/8fe20804-c677-491c-be3f-262fe0c3653a/image_168.jpg"
    "num_images": 1
}

chain = Chain(
    output = (diffusion, prompt_template, params)
)

chain.run()

Image as Input

If we are taking text as input, the format is a bit different. Rather than passing in a string or PromptTemplate into the PROMPT section, we pass in an image url. Therefore, each part of the chain for text-to-image models can be simplified into the following format:

MODEL_OUTPUT = (MODEL, IMAGE, PARAMETERS)

where IMAGE is the image that we pass into the image-to-image model. Therefore, now our PARAMETERS dictionary contains a prompt rather than an image. An example of this is shown below with the PromptTemplate from the first example.

from flushai.models.diffusion.img2img import StableDiffusionXL

diffusion = StableDiffusionXL(api_key="YOUR_API_KEY")

image = "https://flush-user-images.s3.amazonaws.com/generated_images/8fe20804-c677-491c-be3f-262fe0c3653a/image_168.jpg"
params = {
    "prompt": '''photo of a rhino dressed suit and tie sitting at a table in a 
bar with a bar stools, award winning photography, Elke vogelsang'''
    "num_images": 1
}

chain = Chain(
    output = (diffusion, image, params)
)

chain.run(animal="dinosaur")

Upscalers

We put upscalers in a separate section from image-to-image models because, although they do take in images as input and return images as output, they do not take in text. They additionally do not have any other customizable parameters after their initialization. Therefore, their format is:

MODEL_OUTPUT = (MODEL, IMAGE)

An example of this is given below:

from flushai.models.diffusion.img2img.upscalers import RealESRGAN

upscaler = RealESRGAN(api_key="YOUR_API_KEY", scale=4)
image = "https://flush-user-images.s3.amazonaws.com/generated_images/8fe20804-c677-491c-be3f-262fe0c3653a/image_168.jpg"

chain = Chain(
    _ = (upscaler, image)
)

chain.run()