zh21608 commited on
Commit
e1999d7
1 Parent(s): 62af9da
Files changed (1) hide show
  1. txt2images.js +30 -0
txt2images.js ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios')
2
+ api_base = process.env.OPENAI_API_BASE;
3
+ api_key = process.env.OPENAI_API_KEY;
4
+ api_version = '2023-06-01-preview'
5
+ url = api_base + "openai/images/generations:submit?api-version=" + api_version
6
+ headers = { "api-key": api_key, "Content-Type": "application/json" }
7
+
8
+ function sleep(ms) {
9
+ return new Promise((resolve) => setTimeout(resolve, ms));
10
+ }
11
+ async function txt2images(prompt,size) {
12
+ body = {
13
+ "prompt": prompt,
14
+ "size": size,
15
+ "n": 1
16
+ }
17
+ submission = await axios.post(url, body, { headers });
18
+ operation_location = submission.headers['operation-location'];
19
+ status = "";
20
+ while (status != "succeeded") {
21
+ await sleep(1000);
22
+ res = await axios.get(operation_location, { headers });
23
+ status = res.data.status;
24
+ console.log(status)
25
+ if (status == "succeeded") {
26
+ return res.data.result.data[0].url;
27
+ }
28
+ }
29
+ }
30
+ exports.txt2img = txt2images;