-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.js
424 lines (380 loc) · 12.4 KB
/
generators.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import axios from 'axios';
import EventSource from 'eventsource';
import WebSocket from 'ws';
import fs from 'fs';
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
const { bannerMusicGen, nevPrompt } = config;
function getEventId() {
const bytes = new Uint8Array(16);
for (let i = 0; i < 16; i++) {
bytes[i] = Math.floor(Math.random() * 256);
}
const hexString = Array.from(bytes).map(byte => byte.toString(16).padStart(2, '0')).join('');
return hexString;
}
async function fetchAndExtractRootUrl(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const htmlContent = await response.text();
const rootMatch = htmlContent.match(/window\.gradio_config = (.*?});/s);
if (rootMatch) {
const gradioConfig = JSON.parse(rootMatch[1]);
return gradioConfig.root;
} else {
throw new Error("Could not extract root value.");
}
} catch (error) {
console.error('Failed to fetch:', error);
return null;
}
}
function generateSessionHash() {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = '';
for (let i = 0; i < 5; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
function generateRandomDigits() {
return Math.floor(Math.random() * (999999999 - 100000000 + 1) + 100000000);
}
function speechGen(prompt, language) {
let x, y;
if (language == 'English') {
x = 'EN';
y = 'EN-Default';
} else {
switch (language) {
case 'Spanish':
x = y = 'ES';
break;
case 'French':
x = y = 'FR';
break;
case 'Chinese':
x = y = 'ZH';
break;
case 'Korean':
x = y = 'KR';
break;
case 'Japanese':
x = y = 'JP';
break;
default:
x = 'EN';
y = 'EN-Default';
}
}
return new Promise((resolve, reject) => {
try {
const url = "https://mrfakename-melotts.hf.space";
const session_hash = generateSessionHash();
const urlFirstRequest = `${url}/queue/join?`;
const dataFirstRequest = {
"data": [prompt, y, 1, x],
"event_data": null,
"fn_index": 1,
"trigger_id": 8,
"session_hash": session_hash
};
axios.post(urlFirstRequest, dataFirstRequest).then(responseFirst => {
const urlSecondRequest = `${url}/queue/data?session_hash=${session_hash}`;
const eventSource = new EventSource(urlSecondRequest);
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.msg === "process_completed") {
eventSource.close();
const full_url = data?.output?.data?.[0]?.url;
if (full_url) {
resolve(full_url);
} else {
reject(new Error("Invalid path: URL does not exist."));
console.log(data);
}
}
};
eventSource.onerror = (error) => {
eventSource.close();
reject(error);
};
}).catch(error => {
reject(error);
});
} catch (error) {
reject(error);
}
});
}
async function musicGen(prompt) {
let socket;
try {
const sessionHash = generateSessionHash();
const banner = bannerMusicGen;
// WebSocket connection promise
const socketPromise = new Promise((resolve, reject) => {
socket = new WebSocket('wss://surn-unlimitedmusicgen.hf.space/queue/join');
socket.onopen = () => {
resolve();
};
socket.onerror = (error) => {
console.error('WebSocket error.');
reject(new Error('WebSocket connection error'));
};
});
// Wait for socket to be ready
await socketPromise;
// Send and process messages
const url = await new Promise((resolve, reject) => {
socket.onmessage = (message) => {
const data = JSON.parse(message.data);
if (data.msg === 'send_hash') {
socket.send(JSON.stringify({
fn_index: 0,
session_hash: sessionHash,
}));
} else if (data.msg === 'send_data') {
socket.send(JSON.stringify({
data: ['large', prompt, null, 30, 2, 280, 1150, 0.7, 8.5, banner, 'MusicGen', './assets/arial.ttf', '#fff', -1, 2, 0, true, false, 'No'],
event_data: null,
fn_index: 5,
session_hash: sessionHash,
}));
} else if (data.msg === 'process_completed') {
const name = data?.output?.data?.[0]?.[0]?.name;
if (name) {
const url = `https://surn-unlimitedmusicgen.hf.space/file=${name}`;
resolve(url);
} else {
reject(new Error("Output URL is missing"));
console.log(data);
}
}
};
socket.onerror = () => {
reject(new Error('WebSocket encountered an error during message handling.'));
};
socket.onclose = () => {
reject(new Error('WebSocket connection was closed unexpectedly.'));
};
});
return url;
} catch (error) {
if (socket && socket.readyState !== WebSocket.CLOSED) {
socket.close();
}
throw error;
}
}
async function generateWithPlayground(prompt, resolution) {
let width, height;
if (resolution == 'Square') {
width = 1024;
height = 1024;
} else if (resolution == 'Wide') {
width = 1280;
height = 768;
} else if (resolution == 'Portrait') {
width = 768;
height = 1280;
}
return new Promise(async (resolve, reject) => {
try {
const session_hash = generateSessionHash();
const event_id = getEventId();
const randomDigit = generateRandomDigits();
const rootUrl = await fetchAndExtractRootUrl("https://playgroundai-playground-v2-5.hf.space/");
const urlJoinQueue = `https://playgroundai-playground-v2-5.hf.space/queue/join?fn_index=3&session_hash=${session_hash}`;
const eventSource = new EventSource(urlJoinQueue);
eventSource.onmessage = async (event) => {
const data = JSON.parse(event.data);
if (data.msg === "send_data") {
const eventId = data?.event_id;
fetch("https://playgroundai-playground-v2-5.hf.space/queue/data", {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: [prompt, nevPrompt, true, randomDigit, width, height, 3, true],
event_data: null,
fn_index: 3,
trigger_id: 6,
session_hash: session_hash,
event_id: eventId
})
});
} else if (data.msg === "process_completed") {
eventSource.close();
const path = data?.output?.data?.[0]?.[0]?.image?.path;
if (path) {
const fullUrl = `${rootUrl}/file=${path}`;
resolve({ images: [{ url: fullUrl }], modelUsed: "Playground" });
} else {
reject(new Error('No image path found in the process_completed message.'));
console.log(data);
}
}
};
eventSource.onerror = (error) => {
eventSource.close();
reject(error);
};
} catch (error) {
reject(error);
}
});
}
const modelsData = {
"DallE-XL": {
url: "https://ehristoforu-dalle-3-xl-lora-v2.hf.space",
fnIndex: 3,
triggerId: 6,
useSize: false,
dataPattern: (prompt, randomDigit, width, height) => [prompt, nevPrompt, true, randomDigit, width, height, 6, true]
},
"Anime": {
url: "https://cagliostrolab-animagine-xl-3-1.hf.space",
fnIndex: 5,
triggerId: 49,
useSize: true,
dataPattern: (prompt, randomDigit, size) => [prompt, nevPrompt, randomDigit, 1024, 1024, 7, 35, "DPM++ SDE Karras", size, "(None)", "Standard v3.1", false, 0.55, 1.5, true]
},
"SD-XL": {
url: "https://kingnish-sdxl-flash.hf.space",
fnIndex: 2,
triggerId: 5,
useSize: false,
dataPattern: (prompt, randomDigit, width, height) => [prompt, nevPrompt, true, randomDigit, width, height, 3, 12, true, 1]
},
"PixArt-Sigma": {
url: "https://dataautogpt3-pixart-sigma-900m.hf.space",
fnIndex: 2,
triggerId: 5,
useSize: false,
dataPattern: (prompt, randomDigit, width, height) => [prompt, nevPrompt, randomDigit, true, width, height, 5, 35]
},
"Kolors": {
url: "https://gokaygokay-kolors.hf.space",
fnIndex: 0,
triggerId: 23,
useSize: false,
dataPattern: (prompt, randomDigit, width, height) => [prompt, nevPrompt, height, width, 35, 5, 1, true, randomDigit]
},
"FLUX.1 [dev]": {
url: "https://black-forest-labs-flux-1-dev.hf.space",
fnIndex: 2,
triggerId: 5,
useSize: false,
dataPattern: (prompt, randomDigit, width, height) => [prompt, randomDigit, true, width, height, 4, 35]
},
"FLUX.1 [schnell]": {
url: "https://multimodalart-flux-1-merged.hf.space",
fnIndex: 2,
triggerId: 5,
useSize: false,
dataPattern: (prompt, randomDigit, width, height) => [prompt, randomDigit, true, width, height, 3.5, 16]
}
};
function getResolution(resolution) {
switch (resolution) {
case 'Square': return { width: 1024, height: 1024, size: '1024 x 1024' };
case 'Wide': return { width: 1280, height: 768, size: '1344 x 768' };
case 'Portrait': return { width: 768, height: 1280, size: '832 x 1216' };
default: throw new Error("Invalid resolution");
}
}
function generateImage(prompt, resolution, model) {
return new Promise((resolve, reject) => {
try {
const { width, height, size } = getResolution(resolution);
const { url, fnIndex, triggerId, useSize, dataPattern } = modelsData[model];
const randomDigit = generateRandomDigits();
const session_hash = generateSessionHash();
const data = useSize ? dataPattern(prompt, randomDigit, size) : dataPattern(prompt, randomDigit, width, height);
const urlFirstRequest = `${url}/queue/join?`;
const dataFirstRequest = {
"data": data,
"event_data": null,
"fn_index": fnIndex,
"trigger_id": triggerId,
"session_hash": session_hash
};
axios.post(urlFirstRequest, dataFirstRequest).then(responseFirst => {
const urlSecondRequest = `${url}/queue/data?session_hash=${session_hash}`;
const eventSource = new EventSource(urlSecondRequest);
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.msg === "process_completed") {
eventSource.close();
const full_url = data?.output?.data?.[0]?.[0]?.image?.url || data?.output?.data?.[0]?.url || data?.output?.data?.[1]?.url;
if (full_url) {
resolve({ images: [{ url: full_url }], modelUsed: model });
} else {
reject(new Error("Invalid path: URL does not exist."));
console.error(data);
}
}
};
eventSource.onerror = (error) => {
eventSource.close();
reject(error);
};
}).catch(reject);
} catch (error) {
reject(error);
}
});
}
async function generateWithDalle3(prompt) {
try {
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
};
const body = JSON.stringify({
model: "dall-e-3",
prompt: prompt,
n: 1
});
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Request timed out (30 seconds limit)'));
}, 30000);
});
const fetchPromise = fetch(`${process.env.OPENAI_BASE_URL}/images/generations` || 'https://api.openai.com/v1/images/generations', {
method: 'POST', headers, body
});
const response = await Promise.race([fetchPromise, timeoutPromise]);
const data = await response.json();
if (!response.ok) {
throw new Error(`${response.status} ${data?.message || ''}`);
}
return { images: data.data, modelUsed: "DallE-3" };
} catch (error) {
console.log(error);
throw error;
}
}
const imgModels = [
...Object.keys(modelsData),
'Playground',
'DallE-3'
];
const imageModelFunctions = {
...Object.fromEntries(Object.keys(modelsData).map(model => [model, generateImage])),
'Playground': generateWithPlayground,
'DallE-3': generateWithDalle3
};
export {
speechGen,
musicGen,
generateWithPlayground,
generateImage,
generateWithDalle3,
imgModels,
imageModelFunctions
};