-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
59 lines (51 loc) · 1.68 KB
/
index.ts
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
import fetch from 'node-fetch'
import FormData from 'form-data'
import fs from 'node:fs'
const engineId = 'stable-diffusion-512-v2-1'
const apiHost = process.env.API_HOST ?? 'https://api.stability.ai'
const apiKey = "sk-TGsTOx6oaNFY95INbcDbr166veumaOjC60T73FtpJwhUbRP6"
if (!apiKey) throw new Error('Missing Stability API key.')
// NOTE: This example is using a NodeJS FormData library. Browser
// implementations should use their native FormData class. React Native
// implementations should also use their native FormData class.
const formData = new FormData()
formData.append('init_image', fs.readFileSync('./in/imagen.png'))
formData.append('mask_image', fs.readFileSync('./in/mask.png'))
formData.append('mask_source', 'MASK_IMAGE_BLACK')
formData.append(
'text_prompts[0][text]',
'A realistic image of a model wearing a gucci shirt and blazer'
)
formData.append('cfg_scale', '7')
formData.append('clip_guidance_preset', 'FAST_BLUE')
formData.append('samples', 1)
formData.append('steps', 30)
const response = await fetch(
`${apiHost}/v1/generation/${engineId}/image-to-image/masking`,
{
method: 'POST',
headers: {
...formData.getHeaders(),
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: formData,
}
)
if (!response.ok) {
throw new Error(`Non-200 response: ${await response.text()}`)
}
interface GenerationResponse {
artifacts: Array<{
base64: string
seed: number
finishReason: string
}>
}
const responseJSON = (await response.json()) as GenerationResponse
responseJSON.artifacts.forEach((image, index) => {
fs.writeFileSync(
`out/v1_img2img_masking_${index}.png`,
Buffer.from(image.base64, 'base64')
)
})