Skip to main content
Version: 2024.1

Hugging Face Text to Image (One Shot)

This Action allows generating images using AI models, based on specified fields within a Data Object, and saves these generated images to an Image field in the same Data Object. It's possible to repeatedly perform this action to produce images until you achieve the desired outcome.

Configuration Options

# Specifies the model's endpoint. A full URL overrides the default base URL.
model_endpoint: 'https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1'

# Specifies the folder for storing generated images.
asset_folder: '/AI Generated'

# This field within the Data Object is where the generated image will be stored.
output_field: 'AIGeneratedImage'

# Twig template that constructs the prompt sent to the image generation model.
prompt_template: "Realistic picture of {{ subject.name }}, production year: {{ subject.productionYear }}, {{ subject.color|join(',') }}, detailed, 8k"

# Twig template to generate the filename based on Data Object or Asset fields.
filename_template: "{{ subject.name|replace({' ': '-'}) }}-{{ random() }}"

Detailed Configuration Options

model_endpoint: (Required) Specifies the endpoint for the AI model to use. The default base URL is https://api-inference.huggingface.co. If only the model name is provided (e.g., /models/CompVis/stable-diffusion-v1-4), this default base URL is used. However, if a full URL is specified, it will override the default base URL.

asset_folder: (Required) Designates the Assets folder where generated images will be stored.

output_field: (Required) Identifies the Image field within the Data Object where the generated image will be saved. This field should be of type Image in the Data Object -> Classes configuration.

prompt_template: (Required) Twig template that constructs the prompt sent to the image generation model. The selected data object can be accessed using the subject variable.

filename_template: (Required) Twig template to generate the filename dynamically. The selected data object can be accessed using the subject variable.

parameters: (Optional) Contains additional parameters for the image generation process:

  • height: (Optional) Specifies the height of the generated image in pixels.
  • width: (Optional) Specifies the width of the generated image in pixels.
  • negative_prompt: (Optional) A Twig template that outlines descriptions to avoid in the generated images. The selected data object can be accessed using the subject variable.
  • guidance_scale: (Optional) Determines how closely the generated image should adhere to the prompt as opposed to the model's own creativity.
  • num_inference_steps: (Optional) Sets the number of steps the model undergoes to refine the generated image. Higher values can lead to more detailed images.

options: (Optional) Contains additional options for the image generation process: use_cache: (Optional, default: true) Utilizes previously generated images for similar requests to accelerate response times. Setting this to false ensures a new image is generated for each request, enhancing uniqueness but potentially increasing wait times.

Examples

The example below illustrates how to generate an image using the AI model "stabilityai/stable-diffusion-2-1". It takes values from the "name" and "description" fields to generate the image, saves the image in the "AI Generated" folder within Assets, and assigns the generated image to the "AIGeneratedImage" field in the Data Object.

# Specifies the model's endpoint. A full URL overrides the default base URL.
model_endpoint: 'https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1'

# Specifies the folder for storing generated images.
asset_folder: '/AI Generated'

# This field within the Data Object is where the generated image will be stored.
output_field: 'AIGeneratedImage'

# Twig template that constructs the prompt sent to the image generation model.
prompt_template: "Realistic picture of {{ subject.name }}, production year: {{ subject.productionYear }}, {{ subject.color|join(',') }}, detailed, 8k"

# Twig template to generate the filename based on Data Object or Asset fields.
filename_template: "{{ subject.name|replace({' ': '-'}) }}-{{ random() }}"

parameters:
height: 600
width: 800
negative_prompt: "ugly, deformed, disfigured, poor details, bad anatomy"
guidance_scale: 8.5
num_inference_steps: 80

options:
# Uses cached images for similar requests to speed up response times. Set to false for new images per request.
use_cache: true

Additional Information

To explore different visual outcomes, it's possible to switch to other AI models by changing the model_endpoint value. Below are examples of models that can be utilized for image generation:

https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4
https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0
https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1
https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5
https://api-inference.huggingface.co/models/kandinsky-community/kandinsky-2-2-decoder
https://api-inference.huggingface.co/models/lllyasviel/control_v11p_sd15_openpose