|
| 1 | +import { createAction, Property } from '@activepieces/pieces-framework'; |
| 2 | +import { randomBytes } from 'node:crypto'; |
| 3 | +import { kebabCase } from 'lodash'; |
| 4 | + |
| 5 | +import { |
| 6 | + httpClient, |
| 7 | + HttpMethod, |
| 8 | + HttpRequest, |
| 9 | +} from '@activepieces/pieces-common'; |
| 10 | +import { stableDiffusionAuth, StableDiffusionAuthType } from '../../index'; |
| 11 | + |
| 12 | +export const textToImage = createAction({ |
| 13 | + name: 'textToImage', |
| 14 | + displayName: 'Text to Image', |
| 15 | + description: '', |
| 16 | + auth: stableDiffusionAuth, |
| 17 | + props: { |
| 18 | + prompt: Property.LongText({ |
| 19 | + displayName: 'Prompt', |
| 20 | + required: true, |
| 21 | + }), |
| 22 | + model: Property.Dropdown({ |
| 23 | + displayName: 'Model', |
| 24 | + required: true, |
| 25 | + refreshers: ['auth'], |
| 26 | + options: async ({ auth }) => { |
| 27 | + if (!auth) { |
| 28 | + return { |
| 29 | + disabled: true, |
| 30 | + options: [], |
| 31 | + placeholder: 'Please authenticate first', |
| 32 | + }; |
| 33 | + } |
| 34 | + const { baseUrl } = auth as StableDiffusionAuthType; |
| 35 | + const request: HttpRequest = { |
| 36 | + method: HttpMethod.GET, |
| 37 | + url: `${baseUrl}/sdapi/v1/sd-models`, |
| 38 | + headers: { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + }, |
| 41 | + }; |
| 42 | + const response = await httpClient.sendRequest(request); |
| 43 | + const options = response.body |
| 44 | + ?.map((model: { model_name: string }) => { |
| 45 | + return { |
| 46 | + label: model.model_name, |
| 47 | + value: model.model_name, |
| 48 | + }; |
| 49 | + }) |
| 50 | + ?.sort((a: { label: string }, b: { label: string }) => |
| 51 | + a['label'].localeCompare(b['label']) |
| 52 | + ); |
| 53 | + return { |
| 54 | + options: options, |
| 55 | + }; |
| 56 | + }, |
| 57 | + }), |
| 58 | + advancedParameters: Property.Object({ |
| 59 | + displayName: 'Advanced parameters (key/value)', |
| 60 | + required: false, |
| 61 | + description: 'Refer to API documentation', |
| 62 | + }), |
| 63 | + }, |
| 64 | + async run({ auth, propsValue, files }) { |
| 65 | + const request: HttpRequest = { |
| 66 | + method: HttpMethod.POST, |
| 67 | + url: `${auth.baseUrl}/sdapi/v1/txt2img`, |
| 68 | + headers: { |
| 69 | + 'Content-Type': 'application/json', |
| 70 | + }, |
| 71 | + body: JSON.stringify({ |
| 72 | + ...propsValue.advancedParameters, |
| 73 | + prompt: propsValue.prompt, |
| 74 | + override_settings: { |
| 75 | + sd_model_checkpoint: propsValue.model, |
| 76 | + }, |
| 77 | + override_settings_restore_afterwards: true, |
| 78 | + }), |
| 79 | + }; |
| 80 | + const response = await httpClient.sendRequest(request); |
| 81 | + const images = await Promise.all( |
| 82 | + response.body['images']?.map(async (imageBase64: string) => { |
| 83 | + const fileName = `${randomBytes(16).toString('hex')}-${kebabCase( |
| 84 | + propsValue.prompt |
| 85 | + ).slice(0, 42)}.png`; |
| 86 | + const imageUrl = await files.write({ |
| 87 | + fileName, |
| 88 | + data: Buffer.from(imageBase64, 'base64'), |
| 89 | + }); |
| 90 | + return { |
| 91 | + fileName, |
| 92 | + url: imageUrl, |
| 93 | + }; |
| 94 | + }) |
| 95 | + ); |
| 96 | + return { |
| 97 | + images, |
| 98 | + }; |
| 99 | + }, |
| 100 | +}); |
0 commit comments