-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from amplience/feature/TAR-1150-merge-ai
feat: TAR-1150 merge changes from shoppable-image-ai repo
- Loading branch information
Showing
33 changed files
with
5,454 additions
and
3,536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"objects": [ | ||
{ | ||
"id": "1d5c7d20-b226-46ac-9e24-8ac237ae405a", | ||
"target": "chair-1", | ||
"selector": ".chair-1", | ||
"points": [ | ||
{ | ||
"x": 0.36824877250409166, | ||
"y": 0.22707423580786026 | ||
}, | ||
{ | ||
"x": 0.613747954173486, | ||
"y": 0.22707423580786026 | ||
}, | ||
{ | ||
"x": 0.613747954173486, | ||
"y": 0.7248908296943232 | ||
}, | ||
{ | ||
"x": 0.36824877250409166, | ||
"y": 0.7248908296943232 | ||
} | ||
], | ||
"center": { | ||
"x": 0.36824877250409166, | ||
"y": 0.7248908296943232 | ||
}, | ||
"width": "0.25", | ||
"height": "0.5" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Bounds, Coordinate } from "./dal/shoppable-image/types/GqlOutput"; | ||
|
||
export interface ContentItemPointOfInterest { | ||
id: string; | ||
target: string; | ||
center: Coordinate; | ||
bounds: Bounds | undefined; | ||
outline: Coordinate[] | undefined; | ||
} | ||
|
||
export type ObjectData = ContentItemPointOfInterest & { | ||
selector: string; | ||
}; | ||
|
||
export enum AIState { | ||
Stale, | ||
Loading, | ||
Loaded, | ||
Error, | ||
InsufficientCredits, | ||
} | ||
|
||
export interface AIImageData { | ||
image?: string; | ||
objects: ObjectData[]; | ||
state: AIState; | ||
drawerOpen: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { ContentFieldExtension } from "dc-extensions-sdk"; | ||
import { ObjectData } from "./AIImageData"; | ||
import { TempFileServiceDal } from "./dal/temp-file-service/TempFileServiceDal"; | ||
import { AiShoppableImageDal } from "./dal/shoppable-image/AiShoppableImageDal"; | ||
import { PointOfInterest } from "./dal/shoppable-image/types/GqlOutput"; | ||
|
||
interface cache { | ||
[url: string]: ObjectData[]; | ||
} | ||
|
||
class AIRequestService { | ||
private basePath: string; | ||
private cache: cache; | ||
private currentPromise: null | ((e: Error) => void); | ||
private readonly tempFileServiceDal: TempFileServiceDal; | ||
private readonly aiPoiServiceDal: AiShoppableImageDal; | ||
|
||
constructor(private readonly sdk: ContentFieldExtension, base: string) { | ||
this.basePath = base; | ||
this.cache = {} as cache; | ||
this.currentPromise = null; | ||
this.tempFileServiceDal = new TempFileServiceDal(sdk); | ||
this.aiPoiServiceDal = new AiShoppableImageDal(sdk); | ||
} | ||
|
||
private cancel() { | ||
if (this.currentPromise) { | ||
this.currentPromise(new Error("Request cancelled")); | ||
} | ||
} | ||
|
||
async get(imageUrl: string, useCache: boolean = true) { | ||
if (useCache && this.cache[imageUrl]) { | ||
return this.cache[imageUrl]; | ||
} | ||
this.cancel(); | ||
return await this.retrieve(imageUrl); | ||
} | ||
|
||
private retrieve(imageUrl: string): Promise<ObjectData[]> { | ||
return new Promise(async (resolve, reject) => { | ||
this.currentPromise = reject; | ||
|
||
try { | ||
const { downloadUrl } = await this.tempFileServiceDal.uploadImage(imageUrl); | ||
const organizationId = Buffer.from(`Organization:${this.sdk!.hub.organizationId}`, "utf-8").toString("base64"); | ||
const detectResponse = await this.aiPoiServiceDal.detectPointsOfInterestInImage({ | ||
organizationId, | ||
imageUrl: downloadUrl, | ||
}); | ||
|
||
const pointsOfInterest = detectResponse.output?.objects ?? []; | ||
const objects = this.generateSelectors(pointsOfInterest); | ||
|
||
this.cache[imageUrl] = objects; | ||
|
||
resolve(objects); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
} | ||
|
||
private generateSelectors(objects: PointOfInterest[]): ObjectData[] { | ||
const stringCount: { [key: string]: number } = {}; | ||
|
||
return objects.map(obj => { | ||
const { label } = obj; | ||
const count = (stringCount[label] || 0); | ||
stringCount[label] = count + 1; | ||
|
||
const selector = count > 0 ? `.${label}-${count}` : `.${label}`; | ||
return { | ||
...obj, | ||
id: obj.id, | ||
target: obj.label, | ||
center: obj.center, | ||
bounds: obj.bounds, | ||
outline: obj.outline, | ||
selector, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
export default AIRequestService; |
Oops, something went wrong.