Skip to content

Commit 268691b

Browse files
authored
Merge pull request activepieces#4058 from alan-eu/olivier-sambourg/feat-add-stable-diffusion-web-UI-piece
feat(stable-diffusion-web-ui): text to image action
2 parents 4cfc515 + 4ebf9fa commit 268691b

File tree

9 files changed

+256
-0
lines changed

9 files changed

+256
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"extends": [
3+
"../../../../.eslintrc.json"
4+
],
5+
"ignorePatterns": [
6+
"!**/*"
7+
],
8+
"overrides": [
9+
{
10+
"files": [
11+
"*.ts",
12+
"*.tsx",
13+
"*.js",
14+
"*.jsx"
15+
],
16+
"rules": {},
17+
"extends": [
18+
"plugin:prettier/recommended"
19+
],
20+
"plugins": ["prettier"]
21+
},
22+
{
23+
"files": [
24+
"*.ts",
25+
"*.tsx"
26+
],
27+
"rules": {}
28+
},
29+
{
30+
"files": [
31+
"*.js",
32+
"*.jsx"
33+
],
34+
"rules": {}
35+
}
36+
]
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# pieces-stable-diffusion-webui
2+
3+
This library was generated with [Nx](https://nx.dev).
4+
5+
Integration with [Stable Diffusion web UI](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
6+
7+
## Building
8+
9+
Run `nx build pieces-stable-diffusion-webui` to build the library.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@activepieces/piece-stable-diffusion-webui",
3+
"version": "0.0.2"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "pieces-stable-diffusion-webui",
3+
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "packages/pieces/community/stable-diffusion-webui/src",
5+
"projectType": "library",
6+
"targets": {
7+
"build": {
8+
"executor": "@nx/js:tsc",
9+
"outputs": [
10+
"{options.outputPath}"
11+
],
12+
"options": {
13+
"outputPath": "dist/packages/pieces/community/stable-diffusion-webui",
14+
"tsConfig": "packages/pieces/community/stable-diffusion-webui/tsconfig.lib.json",
15+
"packageJson": "packages/pieces/community/stable-diffusion-webui/package.json",
16+
"main": "packages/pieces/community/stable-diffusion-webui/src/index.ts",
17+
"assets": [
18+
"packages/pieces/community/stable-diffusion-webui/*.md"
19+
],
20+
"buildableProjectDepsInPackageJsonType": "dependencies",
21+
"updateBuildableProjectDepsInPackageJson": true
22+
}
23+
},
24+
"publish": {
25+
"command": "node tools/scripts/publish.mjs pieces-stable-diffusion-webui {args.ver} {args.tag}",
26+
"dependsOn": [
27+
"build"
28+
]
29+
},
30+
"lint": {
31+
"executor": "@nx/eslint:lint",
32+
"outputs": [
33+
"{options.outputFile}"
34+
],
35+
"options": {
36+
"lintFilePatterns": [
37+
"packages/pieces/community/stable-diffusion-webui/**/*.ts"
38+
]
39+
}
40+
}
41+
},
42+
"tags": []
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
createPiece,
3+
PieceAuth,
4+
Property,
5+
} from '@activepieces/pieces-framework';
6+
import { textToImage } from './lib/actions/text-to-image';
7+
8+
export const stableDiffusionAuth = PieceAuth.CustomAuth({
9+
required: true,
10+
props: {
11+
baseUrl: Property.ShortText({
12+
displayName: 'Stable Diffusion web UI API base URL',
13+
required: true,
14+
}),
15+
},
16+
});
17+
18+
export type StableDiffusionAuthType = {
19+
baseUrl: string;
20+
};
21+
22+
export const stableDiffusion = createPiece({
23+
displayName: 'Stable Dffusion web UI',
24+
auth: stableDiffusionAuth,
25+
minimumSupportedRelease: '0.20.0',
26+
logoUrl: 'https://cdn.activepieces.com/pieces/stable-diffusion-webui.png',
27+
authors: ['AdamSelene'],
28+
actions: [textToImage],
29+
triggers: [],
30+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "../../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"forceConsistentCasingInFileNames": true,
6+
"strict": true,
7+
"noImplicitOverride": true,
8+
"noPropertyAccessFromIndexSignature": true,
9+
"noImplicitReturns": true,
10+
"noFallthroughCasesInSwitch": true
11+
},
12+
"files": [],
13+
"include": [],
14+
"references": [
15+
{
16+
"path": "./tsconfig.lib.json"
17+
}
18+
]
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "../../../../dist/out-tsc",
6+
"declaration": true,
7+
"types": ["node"]
8+
},
9+
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
10+
"include": ["src/**/*.ts"]
11+
}

tsconfig.base.json

+3
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@
453453
"@activepieces/piece-stability-ai": [
454454
"packages/pieces/community/stability-ai/src/index.ts"
455455
],
456+
"@activepieces/piece-stable-diffusion-webui": [
457+
"packages/pieces/community/stable-diffusion-webui/src/index.ts"
458+
],
456459
"@activepieces/piece-store": [
457460
"packages/pieces/community/store/src/index.ts"
458461
],

0 commit comments

Comments
 (0)