-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusionbrain-ai-api.js
137 lines (118 loc) · 3.14 KB
/
fusionbrain-ai-api.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
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
133
134
135
136
137
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: brain;
/*
File: fusionbrain-ai-api.js
Desc: Basic API wrapper for fusionbrain.ai
image generator for Scriptable.app.
Generate API keys on
https://fusionbrain.ai/en/keys/
Author: rvelasq (https://github.com/rvelasq)
Website: https://github.com/rvelasq/scriptable-fusionbrain-api
*/
class FusionBrain {
#API_URL;
#API_KEY;
#API_SECRET;
constructor({
key,
secret,
endpoint = 'https://api-key.fusionbrain.ai'
}) {
this.#API_KEY = key
this.#API_SECRET = secret
this.#API_URL = endpoint
}
#newFusionBrainRequest(url) {
const req = new Request(url)
req.headers = {
"X-Key": `Key ${this.#API_KEY}`,
"X-Secret": `Secret ${this.#API_SECRET}`
}
return req
}
async getModels() {
console.log('getting models')
const url = `${this.#API_URL}/key/api/v1/models`
const req = this.#newFusionBrainRequest(url)
return (await req.loadJSON())
}
async generateImage({
prompt,
model,
images = 1,
width = 512,
height = 512,
pollInterval = 3, pollAttempts = 10
} = {}) {
if (typeof model == 'undefined') {
throw 'generateImage: parameter `model` is required.'
}
if (typeof prompt == 'undefined') {
throw 'generateImage: parameter `prompt` is required.'
}
console.log(`sending prompt - ${prompt}`)
const url = `${this.#API_URL}/key/api/v1/text2image/run`
const req = this.#newFusionBrainRequest(url)
req.method = "POST"
Object.apply(req.headers, {
"Content-Type": "multipart/form-data",
})
const params = JSON.stringify({
type: "GENERATE",
numImages: images,
width,
height,
generateParams: {
query: prompt
}
})
console.log(params)
req.addFileDataToMultipart(
Data.fromString(params),
"application/json",
"params",
"params.json")
req.addParameterToMultipart("model_id", `${model}`)
const genReq = await req.loadJSON()
if (!genReq?.uuid) {
console.log(`failed - ${genReq.message}`)
return
}
const sleep = function (ms) {
return new Promise((resolve, reject) => {
const t = new Timer()
t.timeInterval = ms
t.schedule(() => {
t.invalidate()
resolve()
})
})
}
console.log(`submit and poll for response`)
const statusUrl = `${this.#API_URL}/key/api/v1/text2image/status/${genReq.uuid}`
const statusReq = this.#newFusionBrainRequest(statusUrl)
let resp;
while (true) {
if (pollAttempts > 0) {
resp = (await statusReq.loadJSON())
console.log(`progress = ${resp?.status ?? 'unknown'}`)
if (["DONE", "FAIL"].includes(resp['status'])) {
break
}
pollAttempts = pollAttempts - 1
await sleep(pollInterval * 1000)
} else {
break
}
}
if (pollAttempts == 0) {
console.warn('poll attempts exhausted')
}
if (resp?.images?.length) {
return resp.images
}
return
}
}
module.exports = { FusionBrain }