/
index.js
115 lines (100 loc) · 3.86 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
110
111
112
113
114
115
const { Configuration, OpenAIApi } = require("openai");
configuration = null
openai = null
module.exports = class ThemeGenerator {
defaultOptions = {
imageGenerationPrompt: 'Human: Write me a prompt for An AI art generator. Generate a prompt that is based on the 1980s in the United States',
colorGenerationPrompt: 'Human: Give me a color scheme representing the 1980s in the United States. give me the colors in Hex Values and give me 8 colors, the first 3 I can use for a background and the other 5 for styling'
}
constructor(options) {
// Initialize properties and state
this.openAIKey = options.openAIKey;
this.imageGenerationPrompt = options.imageGenerationPrompt || this.defaultOptions.imageGenerationPrompt;
this.colorGenerationPrompt = options.colorGenerationPrompt || this.defaultOptions.colorGenerationPrompt;
this.imagePrompt = ''
this.generatedImageUrl = ''
}
async start() {
configuration = new Configuration({
apiKey: this.openAIKey,
});
openai = new OpenAIApi(configuration)
const prompt = await this.createImagePrompt()
const imageUrl = await this.createImage()
const hexValues = await this.generateColors()
const colors = this.createColorDictionary(hexValues)
const themeData = {
colors: colors,
imageUrl: imageUrl,
prompt: prompt
}
return themeData
}
async createImagePrompt() {
return new Promise( async (resolve, reject) => {
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: this.imageGenerationPrompt,
temperature: 1,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
stop: [" Human:", " AI:"],
});
this.imagePrompt = response.data.choices[0].text.replace(/^[\n\s\t]*|[\n\s\t]*$/g, '');
resolve(this.imagePrompt)
} catch(err) {
reject(err)
}
})
}
async createImage() {
return new Promise( async (resolve, reject) => {
try {
const response = await openai.createImage({
prompt: this.imagePrompt,
n: 1,
size: "1024x1024", //Possible options 256x256, 512x512, or 1024x1024
});
this.generatedImageUrl = response.data.data[0].url;
resolve(this.generatedImageUrl)
} catch(err) {
reject(err)
}
})
}
async generateColors() {
return new Promise( async (resolve, reject) => {
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: this.colorGenerationPrompt,
temperature: 0.9,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
stop: [" Human:", " AI:"],
});
let hexValues = response.data.choices[0].text.match(/\#[0-9A-Fa-f]{6}/g);
resolve(hexValues)
} catch(err) {
reject(err)
}
})
}
createColorDictionary(colorArray) {
var colorDictionary = {}
colorDictionary.primary = colorArray[0]
colorDictionary.secondary = colorArray[1]
colorDictionary.tertiary = colorArray[2]
colorDictionary.one = colorArray[3]
colorDictionary.two = colorArray[4]
colorDictionary.three = colorArray[5]
colorDictionary.four = colorArray[6]
colorDictionary.five = colorArray[7]
return colorDictionary
}
}