0

Train a Custom Element

by
Published Apr 8, 2025

This endpoint will train a new custom element.

Script leonardoai Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Leonardoai = {
3
  apiKey: string;
4
};
5
/**
6
 * Train a Custom Element
7
 * This endpoint will train a new custom element.
8
 */
9
export async function main(
10
  auth: Leonardoai,
11
  body: {
12
    name: string;
13
    description?: string;
14
    datasetId: string;
15
    instance_prompt: string;
16
    lora_focus: string;
17
    train_text_encoder: false | true;
18
    resolution?: number;
19
    sd_version:
20
      | "SDXL_0_9"
21
      | "SDXL_1_0"
22
      | "LEONARDO_DIFFUSION_XL"
23
      | "LEONARDO_LIGHTNING_XL"
24
      | "VISION_XL"
25
      | "KINO_XL"
26
      | "ALBEDO_XL";
27
    num_train_epochs: number;
28
    learning_rate: number;
29
  },
30
) {
31
  const url = new URL(`https://cloud.leonardo.ai/api/rest/v1/elements`);
32

33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      "Content-Type": "application/json",
37
      Authorization: "Bearer " + auth.apiKey,
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47