-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
74 lines (64 loc) · 1.77 KB
/
index.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
const WebSocket = require('ws');
const API_URL = "wss://runwayml-stable-diffusion-v1-5.hf.space/queue/join"
function generateHash() {
const chars = "qwertyuopasdfghjklizxcvbnm0123456789"
let hash = ""
for (let i = 0; i < 11; i++) {
hash += chars[Math.floor(Math.random() * chars.length)]
}
return {
session_hash: hash,
fn_index: 2
}
}
let enhancements = " realistic, smoothening, epic cinematic lighting, dark villanous looking background."
function generate(prompta, cb) {
const client = new WebSocket(API_URL);
const hash = generateHash()
let prompt = prompta
if (prompt.endsWith('{enhanced}')) {
prompt = prompt.replace('{enhanced}', enhancements)
}
let tmr = setTimeout(() => {
client.close()
cb({
error: true
})
}, 120000);
client.on("open", () => {
// console.log("Connected to Websocket!")
})
client.on("error",()=>{
cb({
error:true,
})
})
client.on("message", (message) => {
let msg = JSON.parse("" + message)
if (msg.msg == "send_hash") {
client.send(JSON.stringify(hash))
} else if (msg.msg == "send_data") {
let data = {
data: [prompt],
...hash
}
client.send(JSON.stringify(data))
} else if (msg.msg == "process_completed") {
clearTimeout(tmr)
try{
const results = msg.output.data[0]
cb({
error:false,
results
})
}catch(e){
cb({
error:true,
})
}
}
})
}
module.exports = {
generate
}