File size: 3,979 Bytes
a91de1e
 
 
 
 
 
5c5a1ea
a91de1e
 
 
 
 
 
 
f9e7707
a91de1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849f164
 
f9e7707
a91de1e
 
849f164
f9e7707
a91de1e
 
 
 
849f164
 
a91de1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849f164
a91de1e
 
 
 
 
 
 
 
 
 
 
 
 
849f164
a91de1e
f9e7707
a91de1e
 
 
 
 
849f164
a91de1e
 
849f164
a91de1e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
_id: replicate
author: Anton Breslavskii | https://github.com/breslavsky
description: Provides features to use AI models from Replicate AI
readme: ""
title: Replicate AI
url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/replicate.yaml
version: 6
nodes:
  generate_on_flux_replicate:
    _id: generate_on_flux_replicate
    arrange:
      x: 300
      y: 120
    category:
      _id: generate_images
      title: en=Generate images;ru=Генерация изображений
    environment:
      REPLICATE_TOKEN:
        title: Replicate token
        description: Go to [Replicate](https://replicate.com/account/api-tokens) to take a keys
        type: string
        scope: global
    inputs:
      prompt:
        order: 1
        title: en=Prompt;ru=Подсказка
        type: string
        required: true
        multiline: true
        default: walking cat at the moon
      imagesCount:
        order: 2
        title: en=Images count;ru=Кол-во
        type: integer
        required: true
        min: 1
        max: 4
        step: 1
        default: 1
      aspectRatio:
        order: 3
        title: en=Aspect ratio;ru=Размер
        type: string
        default: 1:1
        enum:
          - 1:1
          - 21:9
          - 16:9
          - 3:2
          - 2:3
          - 4:5
          - 5:4
          - 3:4
          - 4:3
          - 9:21
          - 9:16
    outputs:
      images:
        title: Images
        type: image[]
      image1:
        title: Image 1
        type: image
      image2:
        title: Image 2
        type: image
      image3:
        title: Image 3
        type: image
      image4:
        title: Image 4
        type: image
    package: replicate
    script: |
      export async function run({ inputs, state }) {

          const { RepeatNode, NextNode, FatalError } = DEFINITIONS;

          const REPLICATE_TOKEN = env?.variables?.get('REPLICATE_TOKEN');
          if (!REPLICATE_TOKEN) {
              throw new FatalError('Please, set your API token for Replicate AI');
          }

          const { prompt, imagesCount, aspectRatio } = inputs;

          if (!state) {
              const { data: { id: task } } = await httpClient({
                  method: 'post',
                  url: 'https://api.replicate.com/v1/models/black-forest-labs/flux-schnell/predictions',
                  data: {
                      input: {
                          prompt,
                          num_outputs: imagesCount,
                          aspect_ratio: aspectRatio
                      }
                  },
                  headers: {
                      'Authorization': `Bearer ${REPLICATE_TOKEN}`,
                      'Content-Type': 'application/json',
                      'Prefer': 'wait',
                  }
              });
              return RepeatNode.from({ state: { task }, delay: 2000 });
          } else {
              const { task } = state;

              const { data } = await httpClient({
                  method: 'get',
                  url: `https://api.replicate.com/v1/predictions/${task}`,
                  headers: {
                      'Authorization': `Bearer ${REPLICATE_TOKEN}`,
                      'Content-Type': 'application/json'
                  }
              });

              const { status, error } = data;
              switch (status) {
                  case 'processing':
                      return RepeatNode.from({ state: { task }, delay: 3000 });
                  case 'failed':
                      error.fatal(error);
                  case 'succeeded':
                      const { output: images } = data;
                      const [image1, image2, image3, image4] = images;
                      return NextNode.from({ outputs: { images, image1, image2, image3, image4 } });
              }
          }
      }
    source: catalog
    title: en=Generate on Flux;ru=Генерация Flux
    version: 5