diff options
Diffstat (limited to 'src/server/ApiManagers')
| -rw-r--r-- | src/server/ApiManagers/FireflyManager.ts | 128 | 
1 files changed, 91 insertions, 37 deletions
| diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts index d757a23fe..6daa5840e 100644 --- a/src/server/ApiManagers/FireflyManager.ts +++ b/src/server/ApiManagers/FireflyManager.ts @@ -1,4 +1,4 @@ -import { Dropbox, files } from 'dropbox'; +import { Dropbox } from 'dropbox';  import * as fs from 'fs';  import * as multipart from 'parse-multipart-data';  import * as path from 'path'; @@ -19,7 +19,62 @@ export default class FireflyManager extends ApiManager {              console.error('Error:', error);              return undefined;          }); -    generateImage = (prompt: string = 'a realistic illustration of a cat coding') => { + +    generateImageFromStructure = (prompt: string = 'a realistic illustration of a cat coding', width: number = 2048, height: number = 2048, structureUrl: string, strength: number = 50, styles: string[]) => +        this.getBearerToken().then(response => +            response?.json().then((data: { access_token: string }) => +                //prettier-ignore +                fetch('https://firefly-api.adobe.io/v3/images/generate', { +                    method: 'POST', +                    headers: [ +                        ['Content-Type', 'application/json'], +                        ['Accept', 'application/json'], +                        ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''], +                        ['Authorization', `Bearer ${data.access_token}`], +                    ], +                    body: JSON.stringify({ +                        prompt: prompt, +                        size: { width: width, height: height }, +                        structure: !structureUrl +                            ? undefined +                            : { +                                  strength, +                                  imageReference: { +                                      source: { url: structureUrl }, +                                  }, +                              }, +                        //prettier-ignore +                        style: { presets: styles } +                    }), +                }) +                    .then(response2 => response2.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image))) +                    .catch(error => { +                        console.error('Error:', error); +                        return ''; +                    }) +            ) +        ); + +    uploadImageToDropbox = (fileUrl: string, dbx = new Dropbox({ accessToken: process.env.DROPBOX_TOKEN })) => +        new Promise<string>((res, rej) => +            fs.readFile(path.join(filesDirectory, `${Directory.images}/${path.basename(fileUrl)}`), undefined, (err, contents) => { +                if (err) { +                    console.log('Error: ', err); +                    rej(); +                } else { +                    dbx.filesUpload({ path: `/Apps/browndash/${path.basename(fileUrl)}`, contents }).then(response => { +                        dbx.filesGetTemporaryLink({ path: response.result.path_display ?? '' }).then(link => res(link.result.link)); +                    }); +                } +            }) +        ); + +    generateImage = (prompt: string = 'a realistic illustration of a cat coding', width: number = 2048, height: number = 2048, seed?: number) => { +        let body = `{  "prompt": "${prompt}", "size": { "width": ${width}, "height": ${height}} }`; +        if (seed) { +            console.log('RECEIVED SEED', seed); +            body = `{  "prompt": "${prompt}", "size": { "width": ${width}, "height": ${height}}, "seeds": [${seed}]}`; +        }          const fetched = this.getBearerToken().then(response =>              response?.json().then((data: { access_token: string }) =>                  fetch('https://firefly-api.adobe.io/v3/images/generate', { @@ -30,9 +85,15 @@ export default class FireflyManager extends ApiManager {                          ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],                          ['Authorization', `Bearer ${data.access_token}`],                      ], -                    body: `{  "prompt": "${prompt}" }`, +                    body: body,                  }) -                    .then(response2 => response2.json().then(json => (json.outputs?.[0] as { image: { url: string } })?.image.url)) +                    .then(response2 => +                        response2.json().then(json => { +                            const seed = json.outputs?.[0]?.seed; +                            const url = json.outputs?.[0]?.image?.url; +                            return { seed, url }; +                        }) +                    )                      .catch(error => {                          console.error('Error:', error);                          return undefined; @@ -165,10 +226,23 @@ export default class FireflyManager extends ApiManager {      protected initialize(register: Registration): void {          register({              method: Method.POST, +            subscription: '/queryFireflyImageFromStructure', +            secureHandler: async ({ req, res }) => +                this.uploadImageToDropbox(req.body.structureUrl).then(uploadUrl => +                    this.generateImageFromStructure(req.body.prompt, req.body.width, req.body.height, uploadUrl, req.body.strength, req.body.styles).then(fire => +                        DashUploadUtils.UploadImage(JSON.parse(fire ?? '').url).then(info => { +                            if (info instanceof Error) _invalid(res, info.message); +                            else _success(res, info); +                        }) +                    ) +                ), +        }); +        register({ +            method: Method.POST,              subscription: '/queryFireflyImage',              secureHandler: ({ req, res }) => -                this.generateImage(req.body.prompt).then(url => -                    DashUploadUtils.UploadImage(url ?? '').then(info => { +                this.generateImage(req.body.prompt, req.body.width, req.body.height, req.body.seed).then(img => +                    DashUploadUtils.UploadImage(img?.url ?? '', undefined, img?.seed).then(info => {                          if (info instanceof Error) _invalid(res, info.message);                          else _success(res, info);                      }) @@ -180,7 +254,7 @@ export default class FireflyManager extends ApiManager {              subscription: '/queryFireflyImageText',              // eslint-disable-next-line @typescript-eslint/no-unused-vars              secureHandler: ({ req, res }) => -                fetch('http://localhost:1050/files/images/testshot.png').then(json => +                fetch(req.body.file).then(json =>                      json.blob().then(file =>                          this.getImageText(file).then(text => {                              _success(res, text); @@ -192,36 +266,16 @@ export default class FireflyManager extends ApiManager {              method: Method.POST,              subscription: '/expandImage',              secureHandler: ({ req, res }) => -                new Promise<void>((resolve, reject) => { -                    const dbx = new Dropbox({ accessToken: process.env.DROPBOX_TOKEN }); -                    fs.readFile(path.join(filesDirectory, `${Directory.images}/${path.basename(req.body.file)}`), undefined, (err, contents) => { -                        if (err) { -                            console.log('Error: ', err); -                            reject(); -                        } else { -                            dbx.filesUpload({ path: `/Apps/browndash/${path.basename(req.body.file)}`, contents }) -                                .then(response => { -                                    dbx.filesGetTemporaryLink({ path: response.result.path_display ?? '' }).then(link => { -                                        console.log(link.result); -                                        this.expandImage(link.result.link, req.body.prompt).then(text => { -                                            if (text.error_code) _error(res, text.message); -                                            else -                                                DashUploadUtils.UploadImage(text.outputs[0].image.url).then(info => { -                                                    if (info instanceof Error) _invalid(res, info.message); -                                                    else _success(res, info); -                                                    resolve(); -                                                }); -                                        }); -                                    }); -                                }) -                                .catch(uploadErr => { -                                    console.log(uploadErr); -                                    _error(res, 'upload to dropbox failed'); -                                    reject(); -                                }); -                        } -                    }); -                }), +                this.uploadImageToDropbox(req.body.file).then(uploadUrl => +                    this.expandImage(uploadUrl, req.body.prompt).then(text => { +                        if (text.error_code) _error(res, text.message); +                        else +                            DashUploadUtils.UploadImage(text.outputs[0].image.url).then(info => { +                                if (info instanceof Error) _invalid(res, info.message); +                                else _success(res, info); +                            }); +                    }) +                ),          });      }  } | 
