| const axios = require('axios') | |
| api_base = process.env.OPENAI_API_BASE; | |
| api_key = process.env.OPENAI_API_KEY; | |
| api_version = '2023-06-01-preview' | |
| url = api_base + "openai/images/generations:submit?api-version=" + api_version | |
| headers = { "api-key": api_key, "Content-Type": "application/json" } | |
| function sleep(ms) { | |
| return new Promise((resolve) => setTimeout(resolve, ms)); | |
| } | |
| async function txt2images(prompt,size) { | |
| body = { | |
| "prompt": prompt, | |
| "size": size, | |
| "n": 1 | |
| } | |
| submission = await axios.post(url, body, { headers }); | |
| operation_location = submission.headers['operation-location']; | |
| status = ""; | |
| while (status != "succeeded") { | |
| await sleep(1000); | |
| res = await axios.get(operation_location, { headers }); | |
| status = res.data.status; | |
| console.log(status) | |
| if (status == "succeeded") { | |
| return res.data.result.data[0].url; | |
| } | |
| } | |
| } | |
| exports.txt2img = txt2images; | |