-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (74 loc) · 2.67 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
/**
*
* dall-e-intl
*
* See https://github.com/asirihewage/dall-e-intl
*/
import { Translator } from './translator.js';
import { isSupported } from './languages.js';
import axios from "axios";
const DallEIntl = async (text, DALL_E_API_KEY, opts, lang) => {
opts = JSON.parse(JSON.stringify(opts));
let errors = [
'The language «[lang]» is not supported',
'One or more options missing',
'Something went wrong',
];
const generateImage = async (translateIn, opts, DALL_E_API_KEY) => {
try {
const model = "image-alpha-001";
const response = await axios.post("https://api.openai.com/v1/images/generations", {
model,
prompt: translateIn,
num_images: opts.num_images,
size: opts.size,
response_format: opts.response_format,
}, {
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${DALL_E_API_KEY}`,
},
});
return response.data.data[0].url;
} catch (error) {
console.error(error);
}
};
const ChatGPT_Intl = (translateIn, opts, DALL_E_API_KEY) => {
return new Promise(async (resolve, reject) => {
return await Translator(text, { to: "en" }).then( async (translateIn) => {
await generateImage(translateIn, opts, DALL_E_API_KEY);
}).then( async (response) => {
return await Translator(response.data.choices[0].text, {to: lang})
}).then((translateOut) => {
return resolve({response: translateOut.text, query: text, lang: lang});
}).catch( er => {
return reject({error: er})
});
}
);
};
if (text && DALL_E_API_KEY && lang && opts && opts.model && opts.response_format && opts.num_images && opts.size) {
if(!isSupported(lang)){
return Promise.reject({error: errors[0].replace('[lang]', lang)});
}else {
return ChatGPT_Intl(text, opts, DALL_E_API_KEY);
}
}else {
return Promise.reject({
error: errors[1],
data: {
opts: !!opts ? {
model: !!opts.model,
response_format: !!opts.response_format,
num_images: !!opts.num_images,
size: !!opts.size
} : false ,
text: !!text ,
DALL_E_API_KEY : !!DALL_E_API_KEY ,
lang: !!lang
}
});
}
}
export default DallEIntl;