|
| 1 | +import axios from 'axios'; |
| 2 | + |
| 3 | +import AI, { LogError } from './ai'; |
| 4 | +import config from '../config'; |
| 5 | + |
| 6 | +const { |
| 7 | + LEONARDOAI: { KEY, IMAGE }, |
| 8 | +} = config(); |
| 9 | + |
| 10 | +class DeepAI extends AI { |
| 11 | + generationId?: string; |
| 12 | + constructor(meta?: any) { |
| 13 | + super('leonardoai', meta); |
| 14 | + this.generationId = undefined; |
| 15 | + this.styles = IMAGE.STYLE; |
| 16 | + } |
| 17 | + |
| 18 | + async generateImageId() { |
| 19 | + const { data } = await axios({ |
| 20 | + method: 'post', |
| 21 | + url: 'https://cloud.leonardo.ai/api/rest/v1/generations', |
| 22 | + headers: { |
| 23 | + Authorization: `Bearer ${KEY}`, |
| 24 | + }, |
| 25 | + data: { |
| 26 | + prompt: `${this.meta.summary.summary}, ${this.style}`, |
| 27 | + negative_prompt: IMAGE.NEGATIVE_PROMPT, |
| 28 | + modelId: IMAGE.MODEL_ID, |
| 29 | + sd_version: IMAGE.SD_VERSION, |
| 30 | + num_images: IMAGE.NUM_IMAGES, |
| 31 | + width: IMAGE.WIDTH, |
| 32 | + height: IMAGE.HEIGHT, |
| 33 | + num_inference_steps: IMAGE.NUM_INFERENCE_STEPS, |
| 34 | + guidance_scale: IMAGE.GUIDANCE_SCALE, |
| 35 | + scheduler: IMAGE.SCHEDULER, |
| 36 | + presetStyle: IMAGE.PRESET_STYLE, |
| 37 | + tiling: IMAGE.TILING, |
| 38 | + public: IMAGE.PUBLIC, |
| 39 | + promptMagic: IMAGE.PROMPT_MAGIC, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + if (data?.sdGenerationJob?.generationId) this.generationId = data.sdGenerationJob.generationId; |
| 44 | + } |
| 45 | + |
| 46 | + async generateImage() { |
| 47 | + await this.createAttemptHandler('create generation', this.generateImageId.bind(this))(); |
| 48 | + if (!this.generationId) { |
| 49 | + this.log.error('generation id not found'); |
| 50 | + return; |
| 51 | + } |
| 52 | + const images = await this.waitForImage(); |
| 53 | + if (images?.length) await this.downloadImage(images); |
| 54 | + } |
| 55 | + |
| 56 | + async waitForImage() { |
| 57 | + try { |
| 58 | + this.log.info('wait for image'); |
| 59 | + for (let i = 0; i < IMAGE.TIMEOUT; i++) { |
| 60 | + const { data } = await axios({ |
| 61 | + method: 'get', |
| 62 | + url: `https://cloud.leonardo.ai/api/rest/v1/generations/${this.generationId}`, |
| 63 | + headers: { |
| 64 | + Authorization: `Bearer ${KEY}`, |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + if (i === IMAGE.TIMEOUT / 2) { |
| 69 | + this.log.info('still waiting'); |
| 70 | + } |
| 71 | + |
| 72 | + const { status, generated_images } = data.generations_by_pk; |
| 73 | + if (status == 'FAILED') { |
| 74 | + this.log.error('failed to generate image'); |
| 75 | + break; |
| 76 | + } else if (status == 'COMPLETE') { |
| 77 | + return generated_images.map(({ url }: { url: string }) => ({ |
| 78 | + url, |
| 79 | + })); |
| 80 | + } |
| 81 | + await this.sleep(1); |
| 82 | + } |
| 83 | + this.log.warn(`image was not generated after ${IMAGE.TIMEOUT} seconds`); |
| 84 | + } catch (error: any) { |
| 85 | + this.logError({ type: 'wait for image', error }); |
| 86 | + throw new Error(error); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + async test() { |
| 91 | + try { |
| 92 | + const { data } = await axios({ |
| 93 | + method: 'get', |
| 94 | + url: 'https://cloud.leonardo.ai/api/rest/v1/me', |
| 95 | + headers: { Authorization: `Bearer ${KEY}` }, |
| 96 | + timeout: 10000, |
| 97 | + }); |
| 98 | + return true; |
| 99 | + } catch (error: any) { |
| 100 | + return error?.response?.data || error; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + logError({ type, error }: LogError) { |
| 105 | + this.log.error(`${type}: ${error?.response?.data.error || error}`); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export default DeepAI; |
0 commit comments