0

Train a Custom Model

by
Published Apr 8, 2025

This endpoint will train a new custom model

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 Model
7
 * This endpoint will train a new custom model
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
    modelType?:
17
      | "GENERAL"
18
      | "BUILDINGS"
19
      | "CHARACTERS"
20
      | "ENVIRONMENTS"
21
      | "FASHION"
22
      | "ILLUSTRATIONS"
23
      | "GAME_ITEMS"
24
      | "GRAPHICAL_ELEMENTS"
25
      | "PHOTOGRAPHY"
26
      | "PIXEL_ART"
27
      | "PRODUCT_DESIGN"
28
      | "TEXTURES"
29
      | "UI_ELEMENTS"
30
      | "VECTOR";
31
    nsfw?: false | true;
32
    resolution?: number;
33
    sd_version?: "v1_5" | "v2";
34
    strength?: "VERY_LOW" | "LOW" | "MEDIUM" | "HIGH";
35
  },
36
) {
37
  const url = new URL(`https://cloud.leonardo.ai/api/rest/v1/models`);
38

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