-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
109 lines (91 loc) · 2.81 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
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
import { spawn } from "child_process";
import stripAnsi from "strip-ansi";
// import Replicate from "replicate";
import dotenv from "dotenv";
// import download from 'download'
import path from "path";
import WebSocket from "ws";
import fs from "fs";
dotenv.config();
const ws = new WebSocket("ws://100.76.41.44:5000");
// const replicate = new Replicate();
const outputDirectory = "./output";
let frameNumber = 0;
// clean up output dir
fs.readdir(outputDirectory, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(outputDirectory, file), (err) => {
if (err) throw err;
});
}
});
ws.on("open", () => {
console.log("Connected to WebSocket server");
});
ws.on("message", (data) => {
const uniqueFileName = `coreweave_output_${frameNumber}.jpg`;
fs.writeFileSync(path.join(outputDirectory, uniqueFileName), data);
fs.writeFileSync(path.join(outputDirectory, "_latest.jpg"), data);
console.log(`Image saved as ${uniqueFileName}`);
frameNumber += 1;
});
async function makeImage(prompt) {
const lastImagePath = path.join(
outputDirectory,
`coreweave_output_${frameNumber - 1}.jpg`
);
let image;
if (frameNumber === 0) {
// image = fs.readFileSync("./circle.jpg").toString("base64");
image = undefined;
} else if (!fs.existsSync(lastImagePath)) {
console.log("CANT FIND IMAGE!!!!!!!!!!", lastImagePath);
} else {
image = fs.readFileSync(lastImagePath).toString("base64");
}
ws.send(JSON.stringify({ prompt, image }));
}
// async function makeReplicateImage(prompt) {
// console.log("Running on replicate");
// const input = {
// prompt,
// };
// const output = await replicate.run(
// "fofr/latent-consistency-model:683d19dc312f7a9f0428b04429a9ccefd28dbf7785fef083ad5cf991b65f406f",
// { input }
// );
// console.log(output);
// for (const url of output) {
// const filename = "replicate_output.png";
// await download(url, "./output/", { filename });
// }
// }
async function generateImage(speechData) {
const crazyWhisperPrompt = stripAnsi(speechData)
.replace(/[\r\n]+/g, " ")
.replace(/\[.*?\]/g, "")
.split(".")
.map((sentence) => sentence.trim())
.filter((sentence, index, self) => self.indexOf(sentence) === index)
.join(". ")
.trim();
if (crazyWhisperPrompt.toLowerCase().includes("humans")) {
HUMANS = true;
console.log("HUMANS HAS BEEN SAID!");
} else if (crazyWhisperPrompt.toLowerCase().includes("void")) {
HUMANS = false;
frameNumber = 0;
}
if (!crazyWhisperPrompt.length) {
console.log("no prompt yet!");
} else {
await makeImage(crazyWhisperPrompt);
}
}
// Whisper.cpp stuff
const listen = spawn("sh", ["./listen.sh"]);
listen.stdout.on("data", async (chunk) => {
generateImage(String(chunk));
});
listen.stdout.on("end", () => {});