diff options
author | eleanor-park <eleanor_park@brown.edu> | 2024-12-05 00:22:08 -0500 |
---|---|---|
committer | eleanor-park <eleanor_park@brown.edu> | 2024-12-05 00:22:08 -0500 |
commit | 0066635b2a9ae5fde15defd9dd76c6d7dc1d8959 (patch) | |
tree | 8700f7ca5b2691b3a29f1add84e21d5cf5ca936a /src/server/ApiManagers/FireflyManager.ts | |
parent | b33ffac90703834a4d0c9255aa25702aafe3dfda (diff) |
added image to blob logic and firefly api call in drawingFillHandler
Diffstat (limited to 'src/server/ApiManagers/FireflyManager.ts')
-rw-r--r-- | src/server/ApiManagers/FireflyManager.ts | 90 |
1 files changed, 48 insertions, 42 deletions
diff --git a/src/server/ApiManagers/FireflyManager.ts b/src/server/ApiManagers/FireflyManager.ts index 29aed2c54..55fa1e461 100644 --- a/src/server/ApiManagers/FireflyManager.ts +++ b/src/server/ApiManagers/FireflyManager.ts @@ -14,43 +14,43 @@ export default class FireflyManager extends ApiManager { console.error('Error:', error); return ''; }); - askFirefly = (prompt: string = 'a realistic illustration of a cat coding') => { - const fetched = this.getBearerToken().then(response => - response.json().then((data: { access_token: string }) => - 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: `{ "prompt": "${prompt}" }`, - }) - .then(response => response.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image))) - .catch(error => { - console.error('Error:', error); - return ''; - }) - ) - ); - return fetched; - }; + // askFirefly = (prompt: string = 'a realistic illustration of a cat coding') => { + // const fetched = this.getBearerToken().then(response => + // response.json().then((data: { access_token: string }) => + // 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: `{ "prompt": "${prompt}" }`, + // }) + // .then(response => response.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image))) + // .catch(error => { + // console.error('Error:', error); + // return ''; + // }) + // ) + // ); + // return fetched; + // }; - askFireflyStructure = (prompt: string = 'a realistic illustration of a cat coding', strength: number, imageReference: ) => { + askFirefly = (prompt: string = 'a realistic illustration of a cat coding', uploadId: string, strength: number) => { const fetched = this.getBearerToken().then(response => response.json().then((data: { access_token: string }) => { - const body = { + const body: any = { prompt: prompt, structure: { strength: strength, imageReference: { source: { - uploadId: - } - } - } - } + uploadId: uploadId, + }, + }, + }, + }; return fetch('https://firefly-api.adobe.io/v3/images/generate', { method: 'POST', headers: [ @@ -65,13 +65,13 @@ export default class FireflyManager extends ApiManager { .catch(error => { console.error('Error:', error); return ''; - }) - }) + }); + }) ); return fetched; }; - uploadImageToFirefly = (image: File | Blob) => { + uploadImageToFirefly = (image: Blob) => { const fetched = this.getBearerToken().then(response => response.json().then((data: { access_token: string }) => fetch('https://firefly-api.adobe.io/v3/uploads', { @@ -79,29 +79,35 @@ export default class FireflyManager extends ApiManager { headers: [ ['Content-Type', image.type], ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''], - ['Authorization', `Bearer ${data.access_token}`], // You can replace this with a dynamic token if needed + ['Authorization', `Bearer ${data.access_token}`], ], body: image, }) - .then(response => response.json()) - .then(data => data.uploadId) // Extract the uploadId from the response - .catch(error => { - console.error('Error uploading image:', error); - return ''; - }); + .then(response => response.json()) + .then(data => data.uploadId) // Extract the uploadId from the response + .catch(error => { + console.error('Error:', error); + return ''; + }) + ) + ); + return fetched; }; protected initialize(register: Registration): void { register({ method: Method.POST, subscription: '/queryFireflyImage', - secureHandler: ({ req, res }) => - this.askFirefly(req.body.prompt).then(fire => + secureHandler: async ({ req, res }) => { + const { prompt, imageBlob, strength = 0.5 } = req.body; + const uploadId = imageBlob ? await this.uploadImageToFirefly(imageBlob) : null; + this.askFirefly(prompt, uploadId, strength).then(fire => DashUploadUtils.UploadImage(JSON.parse(fire).url).then(info => { if (info instanceof Error) _invalid(res, info.message); else _success(res, info.accessPaths.agnostic.client); }) - ), + ); + }, }); } } |