forked from TypingMind/plugin-stable-diffusion-v3-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementation.js
63 lines (54 loc) · 1.6 KB
/
implementation.js
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
async function image_generation_via_stable_diffusion_3(params, userSettings) {
const { prompt } = params;
const { stabilityAPIKey } = userSettings;
validateAPIKey(stabilityAPIKey);
try {
const imageData = await generateImageFromStabilityAPI(
stabilityAPIKey,
prompt,
userSettings
);
return imageData;
} catch (error) {
console.error('Error generating image:', error);
throw new Error('Error: ' + error.message);
}
}
function validateAPIKey(apiKey) {
if (!apiKey) {
throw new Error(
'Please set a Stable Diffusion API Key in the plugin settings.'
);
}
}
async function generateImageFromStabilityAPI(
apiKey,
prompt,
{ output_format, aspect_ratio, model, negative_prompt } = {}
) {
const apiUrl = 'https://api.stability.ai/v2beta/stable-image/generate/sd3';
const body = new FormData();
body.append('prompt', prompt);
output_format && body.append('output_format', output_format);
aspect_ratio && body.append('aspect_ratio', aspect_ratio);
model && body.append('model', model);
negative_prompt && body.append("negative_prompt", negative_prompt);
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
Authorization: 'Bearer ' + apiKey,
Accept: 'application/json; type=image/*',
},
body: body,
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Stability API error: ${response.status}, Message: ${errorText}`
);
}
const data = await response.json();
return ``;
}