-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
669 lines (543 loc) · 22.5 KB
/
index.js
File metadata and controls
669 lines (543 loc) · 22.5 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//No More ChatGPT: Build Your Own AI With OpenAI
// https://levelup.gitconnected.com/no-more-chatgpt-build-your-own-ai-with-openai-73a5c087b660
import dotenv from 'dotenv';
dotenv.config();
import { Configuration, OpenAIApi } from "openai";
import readline from 'readline';
import https from 'https';
import fs from 'fs';
import { default as Replicate } from 'replicate';
const replicate = new Replicate({ auth: process.env.REPLICATE_API_TOKEN });
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openAi = new OpenAIApi(configuration);
const banner = `
\x1b[1m\x1b[34m+----------------------------------------------------------------------------+
| \x1b[36mConsole ChatGPT 1.0\x1b[34m |
+----------------------------------------------------------------------------+\x1b[0m
Commands:
history: Show the last 10 messages in the conversation.
save: Save the conversation history to a file.
image:
<prompt>: Enter a prompt to generate an image.
<generator>: Choose an image generator at the prompt.
video:
<prompt>: Enter a prompt to generate a video.
<generator>: Choose a video generator at the prompt.
new topic: Start a new conversation topic.
exit: Exit the program.
quit: Exit the program.
Send a message at the prompt to ChatGPT and it will respond.
`;
console.log(banner);
console.log('\n');
// Create a User Interface
const userInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Image directory
const imageDirectory = 'images';
if (!fs.existsSync(imageDirectory)) {
fs.mkdirSync(imageDirectory);
}
// Video directory
const videoDirectory = 'videos';
if (!fs.existsSync(videoDirectory)) {
fs.mkdirSync(videoDirectory);
}
// history directory
const historyDirectory = 'history';
if (!fs.existsSync(historyDirectory)) {
fs.mkdirSync(historyDirectory);
}
// conversation history
const conversationHistory = [];
// Sanitize a Filename
const sanitizeFilename = (text, maxLength = 50) => {
const excerpt = text.split(/\s+/).slice(0, 9).join(' ').trim();
const sanitized = excerpt.replace(/[^a-zA-Z0-9 ]/g, '').replace(/\s+/g, '_');
return sanitized.substring(0, maxLength);
};
// Download a File
const MAX_REDIRECTS = 5; // Maximum number of redirects to follow
const downloadFile = (url, dest, redirectCount = 0) => new Promise((resolve, reject) => {
if (redirectCount > MAX_REDIRECTS) {
reject(new Error(`Too many redirects when trying to download '${url}'`));
return;
}
const file = fs.createWriteStream(dest);
https.get(url, (response) => {
if (response.statusCode === 307 || response.statusCode === 302) { // Handle redirects
file.close();
fs.unlink(dest, () => {}); // Delete the file since we won't be writing to it
return downloadFile(response.headers.location, dest, redirectCount + 1)
.then(resolve)
.catch(reject);
}
// Check if the request was successful
if (response.statusCode !== 200) {
reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
return;
}
response.pipe(file);
file.on('finish', () => {
file.close(resolve); // Close the write stream
});
}).on('error', (err) => {
fs.unlink(dest, () => { // Delete the file if there was an error
reject(err);
});
});
file.on('error', (err) => { // Handle errors
fs.unlink(dest, () => {}); // Delete the file
reject(err);
});
});
// Generate an Image
const generateImage = async (prompt, generator) => {
let imageUrl;
if (generator === 'dalle2') {
const response = await openAi.createImage({
prompt: prompt,
n: 1,
size: "1024x1024",
});
imageUrl = response.data.data[0].url;
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `dl2_${excerpt}_${timestamp}.png`;
await downloadFile(imageUrl, imageDirectory + '/' + filename);
console.log(`Image saved as ${filename}`);
} else if (generator === 'replicate') {
try{
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
width: 1024,
height: 1024,
num_outputs: 1,
num_iterations: 50,
guidance_scale: 10.5,
num_inference_steps: 70,
negative_prompt: 'duplicate heads bad anatomy blurry fog soft blur haze painted illustration duplicate animals',
scheduler: 'K_EULER_ANCESTRAL',
});
if (output === null) {
console.log('Waiting for output...');
await sleep(1000); // Sleep for 1 second
}
}
imageUrl = output[0];
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `rp_${excerpt}_${timestamp}.png`;
await downloadFile(imageUrl, imageDirectory + '/' + filename);
console.log(`Image saved as ${filename}`);
}catch (error) {
console.error('Error calling Replicate API:', error.message);
return;
}
} else if (generator === 'future-diffusion') {
try{
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "cjwbw/future-diffusion:b5c46a3b3f0db2a154d4be534ba7758caded970b748a2e26e6d02e9b3bd7da2a";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
width: 512,
height: 704,
num_outputs: 1,
num_iterations: 50,
guidance_scale: 7,
num_inference_steps: 20,
negative_prompt: 'duplicate heads bad anatomy blurry fog soft blur haze painted illustration',
scheduler: 'K_EULER_ANCESTRAL',
});
if (output === null) {
console.log('Waiting for output...');
await sleep(1000); // Sleep for 1 second
}
}
imageUrl = output[0];
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `fd_${excerpt}_${timestamp}.png`;
await downloadFile(imageUrl, imageDirectory + '/' + filename);
console.log(`Image saved as ${filename}`);
}catch (error) {
console.error('Error calling Replicate API:', error.message);
return;
}
} else if(generator === 'midjourney') {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "tstramer/midjourney-diffusion:436b051ebd8f68d23e83d22de5e198e0995357afef113768c20f0b6fcef23c8b";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
width: 1024,
height: 1024,
num_outputs: 1,
num_iterations: 90,
guidance_scale: 9,
num_inference_steps: 52,
negative_prompt: 'duplicate heads bad anatomy blurry fog soft painted illustration',
scheduler: 'K-K_EULER_ANCESTRAL',
});
if (output === null) {
console.log('Waiting for output...');
await sleep(1000); // Sleep for 1 second
}
}
console.log(output[0]);
imageUrl = output[0];
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `mj_${excerpt}_${timestamp}.png`;
await downloadFile(imageUrl, imageDirectory + '/' + filename);
console.log(`Image saved as ${filename}`);
} else if(generator === 'ai-forever') {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "ai-forever/kandinsky-2:65a15f6e3c538ee4adf5142411455308926714f7d3f5c940d9f7bc519e0e5c1a";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
width: 1024,
height: 1024,
num_outputs: 1,
num_iterations: 90,
guidance_scale: 4,
prior_cf_scale: 4,
prior_steps: 5,
num_inference_steps: 101,
negative_prompt: 'duplicate heads bad anatomy blurry fog soft painted illustration duplicate animals',
scheduler: 'p_sampler',
});
if (output === null) {
console.log('Waiting for output...');
await sleep(1000); // Sleep for 1 second
}
}
console.log(output);
imageUrl = output;
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `af_${excerpt}_${timestamp}.png`;
await downloadFile(imageUrl, imageDirectory + '/' + filename);
console.log(`Image saved as ${filename}`);
} else {
console.log("Invalid generator choice. Please enter 'dalle2' or 'midjourney' or 'replicate' or 'future-diffusion or 'ai-forever'.", error.message);
return;
}
};
// Generate video from prompt
const generateVideo = async (prompt, generator) => {
let videoUrl;
let finished = false;
if (generator === 'openjourney') {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "wcarle/text2video-zero-openjourney:2bf28cacd1f02765bd557294ec53f743b42be123675773c810bb3e0f8e3ce6f6";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
chunk_size: 8,
motion_field_strength_x: 12,
motion_field_strength_y: 12,
t0: 44,
t1: 45,
resolution: 512,
video_length: 30,
fps: 8,
negative_prompt: 'duplicate heads bad anatomy blurry fog soft painted illustration'
});
if (output === null) {
console.log('Waiting for output...');
await sleep(1000); // Sleep for 1 second
}
}
//console.log(output);
videoUrl = output;
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `oj_${excerpt}_${timestamp}.mp4`;
await downloadFile(videoUrl, videoDirectory + '/' + filename);
//console.log(`Video saved as ${filename}`);
finished = true;
}
else if(generator === 'damo'){
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "cjwbw/damo-text-to-video:1e205ea73084bd17a0a3b43396e49ba0d6bc2e754e9283b2df49fad2dcf95755";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
num_frames: 45,
num_inference_steps: 47,
fps: 8
});
if (output === null) {
console.log('Waiting for output...');
await sleep(2000); // Sleep for 2 second
}
}
console.log(output);
videoUrl = output;
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `damo_${excerpt}_${timestamp}.mp4`;
await downloadFile(videoUrl, videoDirectory + '/' + filename);
//console.log(`Video saved as ${filename}`);
finished = true;
}
else if(generator === 'stable-diffusion'){
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "wcarle/stable-diffusion-videos-openjourney:bd5fd4290fc2ab4b6931c90aee17581a62047470422737e035f34badb8af4132";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
num_steps: 100,
guidance_scale: 7.5,
num_inference_steps: 50,
seeds: 42 | 1337,
fps: 15
});
if (output === null) {
console.log('Waiting for output...');
await sleep(1000); // Sleep for 1 second
}
}
//console.log(output);
videoUrl = output;
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `sd_${excerpt}_${timestamp}.mp4`;
await downloadFile(videoUrl, videoDirectory + '/' + filename);
//console.log(`Video saved as ${filename}`);
finished = true;
}
else if(generator === 'mo-di'){
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const model = "wcarle/stable-diffusion-videos-mo-di:5c3d74439416de290f1d1c9b379c5364c131e69a2823c26a63f2a88fa95f3204";
const input = { prompt: prompt };
let output = null;
while (output === null) {
output = await replicate.run(
model,
{
input,
num_steps: 100,
guidance_scale: 7.5,
num_inference_steps: 50,
seeds: 1 | 2,
fps: 15,
scheduler: 'klms'
});
if (output === null) {
console.log('Waiting for output...');
await sleep(1000); // Sleep for 1 second
}
}
//console.log(output);
videoUrl = output;
const excerpt = sanitizeFilename(prompt);
const timestamp = Date.now();
const filename = `modi_${excerpt}_${timestamp}.mp4`;
await downloadFile(videoUrl, videoDirectory + '/' + filename);
//console.log(`Video saved as ${filename}`);
finished = true;
}
return finished;
}
// Show conversation history
const displayLast10Array = () => {
const lastIndex = conversationHistory.length - 1;
const startIndex = Math.max(0, lastIndex - 9);
const last10Array = conversationHistory.slice(startIndex);
console.log('\n');
console.log('Conversation History');
console.log('----------------------------------------------------------------------------');
last10Array.forEach((message, index) => {
console.log(`${index + 1}. ${message.role}: ${message.content}`);
});
console.log('\n');
};
// Get a Timestamp
const getTimestamp = () => {
const now = new Date();
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}T${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`;
};
// Save conversation history to file
const saveHistoryToFile = async () => {
const timestamp = getTimestamp();
const filename = historyDirectory + '/' + `conversation_history_${timestamp}.txt`;
const content = conversationHistory.map(message => `[${message.role}]: ${message.content}`).join('\n');
// Write the conversation history to a file
try{
fs.writeFileSync(filename, content);
console.log(`Conversation history saved to ${filename}`);
console.log('\n');
}
catch (err) {
if (err) {
console.error('Error writing conversation history to file:', err);
console.log('\n');
}
return false;
}
return true;
};
// Prompt the User
userInterface.prompt();
// Listen for User Input
userInterface.on('line', async (input) => {
if(input.toLowerCase() === 'exit' || input.toLowerCase() === 'quit') {
userInterface.close();
process.exit();
}
if (input.trim().toLowerCase() === 'history') {
displayLast10Array();
userInterface.prompt();
return;
}
if (input.trim().toLowerCase() === 'save') {
const result = saveHistoryToFile();
if (result) {
userInterface.prompt();
}
return;
}
if(input.trim().toLowerCase() === 'video') {
userInterface.question('Enter your video prompt: ', async (prompt) => {
userInterface.question("Choose the generator ('openjourney' or 'damo' or 'stable-diffusion' or 'mo-di'): ", async (generator) => {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
console.log('\n');
const ora = (await import('ora')).default;
const spinner = ora().start();
spinner.text = null;
spinner.color = 'yellow';
spinner.spinner = 'sand';
spinner.text = 'Retrieving your video\n';
await sleep(8000); // Sleep for 8 seconds
spinner.text = null;
spinner.color = 'red';
spinner.spinner = 'arc';
spinner.text = 'Spinning up cloud server.\n';
await sleep(9000); // Sleep for 9 seconds
spinner.text = null;
spinner.color = 'green';
spinner.spinner = 'growHorizontal';
spinner.text = 'Generating video on AI server. This could take 3 to 5 minutes to process.\n';
await sleep(3000); // Sleep for 3 seconds
spinner.text = null;
let output = null;
while (output === null) {
output = await generateVideo(prompt, generator.toLowerCase());
if (output === null) {
await sleep(1000); // Sleep for 1 second
}
else{
spinner.text = null;
await sleep(3000); // Sleep for 3 seconds
}
}
spinner.stop();
console.log('\n');
console.log('Your video was generated successfully!');
console.log('\n');
// Prompt the user for the next question
userInterface.prompt();
});
});
}
else if (input.trim().toLowerCase() === 'image') {
userInterface.question('Enter your image prompt: ', async (prompt) => {
userInterface.question("Choose the generator ('dalle2' or 'midjourney or replicate or future-diffusion or ai-forever'): ", async (generator) => {
console.log('\n');
const ora = (await import('ora')).default;
const spinner = ora().start();
spinner.color = 'yellow';
spinner.spinner = 'sand';
spinner.text = 'Retrieving your image\n';
await generateImage(prompt, generator.toLowerCase());
spinner.stop();
console.log('\n');
console.log('Your image was generated successfully!');
console.log('\n');
// Prompt the user for the next question
userInterface.prompt();
});
});
} else {
console.log('\n');
const ora = (await import('ora')).default;
const spinner = ora('Processing your prompt...\n').start();
spinner.color = 'yellow';
spinner.spinner = 'dots';
// Add the user's input to the conversation history
conversationHistory.push({ role: 'user', content: input.trim() });
try{
const response = await openAi.createChatCompletion({
// gpt-3.5-turbo is the model used by ChatGPT
// gpt-3.5-turbo-0301 is the model used by ChatGPT
model: 'gpt-3.5-turbo-0301',
messages: conversationHistory,
});
// Stop the spinner
spinner.stop();
spinner.clear();
console.log('\n');
// Display ChatGPT's response
const { default: chalk } = await import('chalk');
console.log(chalk.blackBright(response.data.choices[0].message.content));
console.log('\n');
// Add ChatGPT's response to the conversation history
conversationHistory.push({ role: 'assistant', content: response.data.choices[0].message.content });
}catch(error){
// Stop the spinner
spinner.stop();
spinner.clear();
console.log('\n');
if (error.response && error.response.data && error.response.data.error) {
console.error(chalk.red(`Error: ${error.response.data.error.message}`));
} else {
console.error(chalk.red(`Error: ${error.message}`));
}
console.log('\n');
}finally{
// Prompt the user for the next question
userInterface.prompt();
}
}
// Prompt the user for the next question
userInterface.prompt();
});