LoRA (Low-Rank Adaptation) allows you to generate images in a consistent style. For example, you can create multiple images with similar characters in the same setting. To do this, specify a LoRA adapter in the playground or in the API request sent to the selected text-to-image model. You can use a LoRA adapter available at Civitai or deploy your own adapter to Civitai.

Costs

Using LoRA adapters increases the price of images if you generate them by using LoRA adapters. See the price list for text-to-image models.

Prerequisites

If you want to use a Python script or a cURL command, meet the requirements below. If you want to work in the web interface, no prerequisites are needed.
  1. Create an API key for authentication.
  2. Save the API key to an environment variable:
    export NEBIUS_API_KEY=<API_key>
    
    
  3. Install the requests and ipyplot packages:
    pip3 install requests ipyplot
    

Steps

  1. On Civitai, go to the Models section.
  2. Open the page of the required LoRA adapter. If you do not deploy your own adapter and want to select it on Civitai, set the following filters to find appropriate variants:
    • Model types: LoRA.
    • Base model: select one of the text-to-image models that Nebius AI Studio supports.
  3. On the LoRA adapter page, copy the download link:
    1. Right-click the  button.
    2. In the context menu, select Copy link.
Save this link; you will use it in the playground or your API request. The size of the file provided from the link should not exceed 1 GiB. You can check the file size on the LoRA adapter page at Civitai (see the File field).

Generate an image

Run the Python script below. It generates the image of a cat in the style of Studio Ghibli and dark fairytales. The script stores the image file with the .webp extension in the local images directory.
from typing import List, Optional
import random
import time
import os

import requests
import ipyplot

api_key=os.environ.get('NEBIUS_API_KEY')

def gen_image(
        model: str, loras: Optional[List[dict]], prompt: str,
        width: int, height: int, num_inference_steps: int):
    # Generate an image
    resp = requests.post(
        url="https://api.studio.testing.nebius.cloud/v1/images/generations",
        headers={"Authorization": f"Bearer {api_key}"},
        json={
            "model": model,
            "loras": loras,
            "prompt": prompt,
            "width": width,
            "height": height,
            "num_inference_steps": num_inference_steps,
        },
    )
    # Check the status of the API request
    if resp.status_code != 200:
        print(resp.text)
        resp.raise_for_status()

    # Get the image
    image_url = resp.json()['data'][0]['url']
    content = None
    for i in range(3):
        resp = requests.get(image_url)
        if resp.status_code == 200:
            content = resp.content
            break
        if resp.status_code != 200:
            print("No photo:", resp.text)
            time.sleep(1)
            continue

    if content is None:
        resp.raise_for_status()

    # Save the file with the image to the "images" directory
    os.makedirs("images", exist_ok=True)
    name = f"images/{''.join([random.choice('abcde') for _ in range(10)])}.webp"
    with open(name, "wb") as f:
        f.write(content)
    return name

# Call the function to generate the image
completion = gen_image(
    model="black-forest-labs/flux-dev",
    loras=[
        {
            "url": "https://civitai.com/api/download/models/1524423?type=Model&format=SafeTensor",
            "scale": 1
        },
    ],
    prompt="A fancy cat in sunglasses, Studio Ghibli Dark Fairytale, ArsMJStyle, impressionism",
    width=256,
    height=256,
    num_inference_steps=26,
)

print("The image is stored in the file", completion)

To generate the image by using the script above:
  1. Configure the loras parameter. It allows you to build LoRA adapters into the model matrices and to create an image in the required style. Set the following parameters:
    • loras.url: The download link to Civitai that you received earlier.
    • loras.scale (optional): The LoRA adapter scale. This parameter influences how strongly the image style is applied. For example, the style is more pronounced in images if the scale is 3 and less pronounced if the scale is 0.5. The default value is 1.
  2. (Optional) To apply several LoRA adapters, add them to the loras list:
    loras=[
      {"url": "<value>", "scale": <value>},
      {"url": "<value>", "scale": <value>},
      {"url": "<value>", "scale": <value>},
      {"url": "<value>", "scale": <value>},
    ],
    
    
    You can add no more than four LoRA adapters. They are combined together.
  3. In the prompt parameter, specify a prompt with a trigger word. This is the word that triggers the LoRA adapter and helps apply the image style. You can find the trigger word on the LoRA adapter page at Civitai. For example, the Hand Detail FLUX & XL LoRA adapter has the trigger words “detailed hands” so your prompt can be “Generate an image with detailed hands.”