UNPKG

node-dall-ai-2

Version:

A type safe library for interacting with OpenAI's Dall-E 2 AI.

111 lines (110 loc) 5.05 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dalle = void 0; const axios_1 = __importDefault(require("axios")); const DalleError_1 = require("./DalleError"); const error = new DalleError_1.DalleError(`> Dall AI (Coded By Kabir Singh)`); class Dalle { constructor({ apiKey }) { this.apiKey = apiKey; this.url = `https://labs.openai.com/api/labs`; } generate(query, amount = 4) { return __awaiter(this, void 0, void 0, function* () { // code const body = { task_type: "text2im", prompt: { caption: query, batch_size: amount }, }; return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { const response = yield axios_1.default.post(`${this.url}/tasks`, body, { headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, }); if (response.statusText !== "OK") { error.send(response); return reject("Unauthorised. Invalid unique session ID."); } const { data } = response; const taskId = data.id; const refreshIntervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () { const response = yield axios_1.default.get(`${this.url}/${taskId}`, { headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", "Accept-Encoding": "gzip,deflate,compress", }, }); if (response.statusText !== "OK") { error.send(response); return reject("Dall-e 2 couldn't generate images based upon your caption."); } const { data } = response; if (data.status === "rejected") { clearInterval(refreshIntervalId); resolve(data.status_information); } else if (data.status === "succeeded") { const generations = data.generations.data; clearInterval(refreshIntervalId); resolve(generations); } }), 3000); })); }); } getTask(taskId) { return __awaiter(this, void 0, void 0, function* () { const res = yield axios_1.default .get(`${this.url}/tasks/${taskId}`, { headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, }) .catch((e) => error.send(e)); return res === null || res === void 0 ? void 0 : res.data; }); } getList(limit) { return __awaiter(this, void 0, void 0, function* () { const res = yield axios_1.default .get(`${this.url}/tasks?limit=${limit}`, { headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, }) .catch((e) => error.send(e)); return res === null || res === void 0 ? void 0 : res.data; }); } getCredits() { return __awaiter(this, void 0, void 0, function* () { const res = yield axios_1.default .get(`${this.url}/billing/credit_summary`, { headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, }) .catch((e) => error.send(e)); return res === null || res === void 0 ? void 0 : res.data; }); } } exports.Dalle = Dalle;