diff --git a/package-lock.json b/package-lock.json index a508acb..ae2ea1a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ai_bot", - "version": "1.2.5", + "version": "1.2.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ai_bot", - "version": "1.2.5", + "version": "1.2.6", "dependencies": { "@iamtraction/google-translate": "^2.0.1", "@xenova/transformers": "^2.15.1", diff --git a/package.json b/package.json index f05ab48..035d94a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ai_bot", - "version": "1.2.5", + "version": "1.2.6", "private": true, "main": "src/app.ts", "type": "module", diff --git a/src/bot/commands/mediaTracker.command.ts b/src/bot/commands/mediaTracker.command.ts index b431681..0ac88b1 100644 --- a/src/bot/commands/mediaTracker.command.ts +++ b/src/bot/commands/mediaTracker.command.ts @@ -52,6 +52,7 @@ export class MediaTrackerCommand extends Command { similarity: number; }; const chatPhotoMessageRepository = this.dataSource.getRepository(ChatPhotoMessage); + const t1 = performance.now(); const messages = await chatPhotoMessageRepository .createQueryBuilder('msg') .select('msg.messageId', 'messageId') @@ -66,6 +67,8 @@ export class MediaTrackerCommand extends Command { matchImageThreshold: this.configService.get('MATCH_IMAGE_THRESHOLD'), }) .getRawMany(); + const t2 = performance.now(); + console.log(`DB query time: ${Math.round(t2 - t1)} ms`); // When similar if (messages.length > 0) { await ctx.reply('🕵️‍♀️ Здається, я це вже десь бачив...', { @@ -110,6 +113,7 @@ export class MediaTrackerCommand extends Command { similarity: number; }; const chatPhotoMessageRepository = this.dataSource.getRepository(ChatPhotoMessage); + const t1 = performance.now(); const messages = await chatPhotoMessageRepository .createQueryBuilder('msg') .select('msg.messageId', 'messageId') @@ -124,6 +128,8 @@ export class MediaTrackerCommand extends Command { matchImageThreshold: this.configService.get('MATCH_TEXT_THRESHOLD'), }) .getRawMany(); + const t2 = performance.now(); + console.log(`DB query time: ${Math.round(t2 - t1)} ms`); // When similar if (messages.length > 0) { await ctx.reply('🔎 Ось, що мені вдалось знайти:', { diff --git a/src/services/ai.service.ts b/src/services/ai.service.ts index 9a3fed8..7cf98ba 100644 --- a/src/services/ai.service.ts +++ b/src/services/ai.service.ts @@ -118,18 +118,26 @@ export class AIService { return new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels); } - async getTextClipEmbedding(text: string): Promise { - const tokenizer = await this.getClipTokenizer(); - const text_model = await this.getClipTextModel(); + async getEnglishTranslation(text: string) { + const isEnglish = /^[a-zA-Z\s\d!"#№$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]+$/.test(text); + if (isEnglish) return text; const t1 = performance.now(); const { text: engText } = await googleTranslate(text); const t2 = performance.now(); console.log(`googleTranslate(${Math.round(t2 - t1)} ms)`, '|', text, '|', engText); + return engText; + } + + async getTextClipEmbedding(text: string): Promise { + const tokenizer = await this.getClipTokenizer(); + const text_model = await this.getClipTextModel(); + const engText = await this.getEnglishTranslation(text); + const t1 = performance.now(); const textInputs = tokenizer(engText, { padding: true, truncation: true }); const { text_embeds } = await text_model(textInputs); const textEmbedding = text_embeds.tolist()[0] as number[]; - const t3 = performance.now(); - console.log(`textEmbedding(${Math.round(t3 - t2)} ms)`); + const t2 = performance.now(); + console.log(`textEmbedding(${Math.round(t2 - t1)} ms)`); return textEmbedding; } @@ -174,17 +182,13 @@ export class AIService { async isTextToxic(text: string): Promise { const toxicThreshold = 0.7; - const { text: engText } = await googleTranslate(text); - console.log('googleTranslate', '|', text, '|', engText); + const engText = await this.getEnglishTranslation(text); const toxicResult = await this.toxicAnalysis(engText); return !!toxicResult.find(({ score }) => score > toxicThreshold); } async getMaxToxicScore(text: string): Promise { - const t1 = performance.now(); - const { text: engText } = await googleTranslate(text); - const t2 = performance.now(); - console.log(`googleTranslate(${Math.round(t2 - t1)} ms)`, '|', text, '|', engText); + const engText = await this.getEnglishTranslation(text); const [{ score }] = await this.toxicAnalysis(engText); return score; }