-
-
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.
* Added vision feature * Refactor background index.ts * Fixed error handling * Add dependencies for Firebase functions, core common, and core service
- Loading branch information
1 parent
84d95e4
commit 6679a7d
Showing
22 changed files
with
268 additions
and
63 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,21 +1,42 @@ | ||
import { ActionSettings } from '@dhruv-techapps/acf-common'; | ||
import { Value } from '@dhruv-techapps/acf-util'; | ||
import { GoogleSheetsValue } from '@dhruv-techapps/google-sheets'; | ||
import { SandboxValue } from '@dhruv-techapps/sandbox'; | ||
import { VisionService, VisionValue } from '@dhruv-techapps/vision'; | ||
import Common from '../common'; | ||
|
||
export const VALUE_MATCHER = { | ||
GOOGLE_SHEETS: /^GoogleSheets::/i, | ||
FUNC: /^Func::/i, | ||
IMAGE: /^Image::/i, | ||
}; | ||
|
||
export class ACFValue { | ||
static async getValue(value: string) { | ||
static async getValue(value: string, settings?: ActionSettings): Promise<string> { | ||
value = await Value.getValue(value); | ||
if (VALUE_MATCHER.GOOGLE_SHEETS.test(value)) { | ||
value = GoogleSheetsValue.getSheetValue(value); | ||
} | ||
if (VALUE_MATCHER.FUNC.test(value)) { | ||
value = await SandboxValue.getFuncValue(value); | ||
} | ||
if (VALUE_MATCHER.IMAGE.test(value)) { | ||
try { | ||
const elementFinder = value.replace(/image::/i, ''); | ||
const elements = await Common.start(elementFinder, settings); | ||
if (elements === undefined || typeof elements === 'number' || elements.length === 0) { | ||
throw new Error('No element found with the given selector'); | ||
} | ||
const data = VisionValue.getImageSrc(elements); | ||
value = await VisionService.imagesAnnotate(data); | ||
return value; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
return error.message; | ||
} | ||
} | ||
return value; | ||
} | ||
return value; | ||
} | ||
} |
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
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,25 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": "error" | ||
} | ||
} | ||
] | ||
} |
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,10 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'vision', | ||
preset: '../../../jest.preset.js', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/shared/vision', | ||
}; |
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,13 @@ | ||
{ | ||
"name": "@dhruv-techapps/vision", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"tslib": "^2.3.0", | ||
"@dhruv-techapps/firebase-functions": "*", | ||
"@dhruv-techapps/core-common": "*", | ||
"@dhruv-techapps/core-service": "*" | ||
}, | ||
"type": "commonjs", | ||
"main": "./src/index.js", | ||
"typings": "./src/index.d.ts" | ||
} |
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,42 @@ | ||
{ | ||
"name": "vision", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/shared/vision/src", | ||
"projectType": "library", | ||
"release": { | ||
"version": { | ||
"generatorOptions": { | ||
"packageRoot": "dist/{projectRoot}", | ||
"currentVersionResolver": "git-tag" | ||
} | ||
} | ||
}, | ||
"tags": [], | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/js:tsc", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/libs/shared/vision", | ||
"main": "libs/shared/vision/src/index.ts", | ||
"tsConfig": "libs/shared/vision/tsconfig.lib.json", | ||
"assets": [] | ||
} | ||
}, | ||
"nx-release-publish": { | ||
"options": { | ||
"packageRoot": "dist/{projectRoot}" | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint" | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/shared/vision/jest.config.ts" | ||
} | ||
} | ||
} | ||
} |
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,5 @@ | ||
export * from './lib/vision-background'; | ||
export * from './lib/vision-types'; | ||
export * from './lib/vision-value'; | ||
export * from './lib/vision.constant'; | ||
export * from './lib/vision.service'; |
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,13 @@ | ||
import { FirebaseFunctionsBackground } from '@dhruv-techapps/firebase-functions'; | ||
import { VisionImageRequest, VisionImageResponse } from './vision-types'; | ||
|
||
export class VisionBackground extends FirebaseFunctionsBackground { | ||
async imagesAnnotate({ content, imageUrl }: VisionImageRequest): Promise<string> { | ||
if (!content && !imageUrl) { | ||
throw new Error('No image data found'); | ||
} | ||
const data: VisionImageRequest = { content, imageUrl }; | ||
const result = await this.visionImagesAnnotate<VisionImageRequest, VisionImageResponse>(data); | ||
return result.responses[0].fullTextAnnotation.text; | ||
} | ||
} |
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,8 @@ | ||
export type VisionImageRequest = { | ||
content: string; | ||
imageUrl: string; | ||
}; | ||
|
||
export type VisionImageResponse = { | ||
responses: Array<{ fullTextAnnotation: { text: string } }>; | ||
}; |
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,36 @@ | ||
import { VisionImageRequest } from './vision-types'; | ||
|
||
export class VisionValue { | ||
static isBase64Image(dataURL: string) { | ||
const regex = /^data:image\/(png|jpeg|jpg|gif|webp|bmp|svg\+xml|x-icon);base64,/; | ||
return regex.test(dataURL); | ||
} | ||
|
||
static extractBase64Data(dataURL: string) { | ||
// Split the data URL at the comma | ||
const base64Data = dataURL.split(',')[1]; | ||
return base64Data; | ||
} | ||
|
||
static getImageSrc(elements: Array<HTMLElement>): VisionImageRequest { | ||
const element = elements[0]; | ||
if (!element) { | ||
throw new Error('No element found'); | ||
} | ||
if (element.tagName !== 'IMG') { | ||
throw new Error('Element is not an image'); | ||
} | ||
const src = element.getAttribute('src'); | ||
if (!src) { | ||
throw new Error('No image found'); | ||
} | ||
let content = ''; | ||
let imageUrl = ''; | ||
if (this.isBase64Image(src)) { | ||
content = this.extractBase64Data(src); | ||
} else { | ||
imageUrl = src; | ||
} | ||
return { content, imageUrl }; | ||
} | ||
} |
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 @@ | ||
export const RUNTIME_MESSAGE_VISION = 'vision'; |
Oops, something went wrong.