From b268b5ef2fe007d325ee105b431a9299450b20e8 Mon Sep 17 00:00:00 2001 From: metal079 Date: Sun, 29 Sep 2024 19:46:26 -0500 Subject: [PATCH] Detect if trigger words are already in prompt to not add duplicates --- src/app/home/options/options.component.ts | 46 ++++++++++++++++------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/app/home/options/options.component.ts b/src/app/home/options/options.component.ts index f50d2c6..a20a9d5 100644 --- a/src/app/home/options/options.component.ts +++ b/src/app/home/options/options.component.ts @@ -244,10 +244,10 @@ export class OptionsComponent implements OnInit { async migrateBase64ToBlobStore() { const db = await this.getDatabase(); const batchSize = 100; // Adjust batch size as needed - + let cursorPosition: IDBValidKey | undefined = undefined; let hasMore = true; - + while (hasMore) { const batch = await this.readBatch(db, cursorPosition, batchSize); if (batch.length > 0) { @@ -257,10 +257,10 @@ export class OptionsComponent implements OnInit { hasMore = false; // No more records to process } } - + console.log('Migration completed.'); } - + async readBatch(db: IDBDatabase, startAfter: IDBValidKey | undefined, batchSize: number): Promise { return new Promise((resolve, reject) => { const transaction = db.transaction('base64Store', 'readonly'); @@ -268,9 +268,9 @@ export class OptionsComponent implements OnInit { const request = startAfter ? base64Store.openCursor(IDBKeyRange.lowerBound(startAfter, true)) // Exclude startAfter : base64Store.openCursor(); - + const batch: any[] = []; - + request.onsuccess = (event) => { const cursor = (event.target as IDBRequest).result; if (cursor && batch.length < batchSize) { @@ -280,29 +280,29 @@ export class OptionsComponent implements OnInit { resolve(batch); } }; - + request.onerror = (event) => { console.error('Error reading batch:', event); reject(event); }; }); } - + async processBatch(batch: any[], db: IDBDatabase) { for (const data of batch) { const uuid = data.UUID; const base64Data = data.base64; - + try { let blob = this.blobMigrationService.base64ToBlob(base64Data); blob = await this.blobMigrationService.convertToWebP(blob); - + // Open a new transaction for each write operation await new Promise((resolve, reject) => { const transaction = db.transaction(['blobStore', 'base64Store'], 'readwrite'); const base64Store = transaction.objectStore('base64Store'); const blobStore = transaction.objectStore('blobStore'); - + const blobRequest = blobStore.put({ UUID: uuid, blob: blob }); blobRequest.onsuccess = () => { // Delete the base64 data @@ -325,7 +325,7 @@ export class OptionsComponent implements OnInit { } } } - + async ngOnInit() { this.subscription = this.sharedService.getPrompt().subscribe(value => { @@ -1405,9 +1405,27 @@ export class OptionsComponent implements OnInit { this.selectedLoras.push(lora); this.generationRequest.loras = this.selectedLoras; - // Add the trigger prompt, if it exists to the prompt input + // Add the trigger prompt, if it exists to the prompt input (only if it's not already there) if (lora.trigger_words) { - lora.trigger_words.forEach((element: String) => { + // Check if the prompt already contains the trigger words and single out the missing ones + const missing_trigger_words: string[] = []; + + // seperate this.generationRequest.prompt into an array of words and remove any whitespace and sanitize + let prompt_words = this.generationRequest.prompt.split(','); + prompt_words = prompt_words.map((word: string) => word.trim().toLowerCase()); + + // sanitize trigger words and check if they are in the prompt + let trigger_words = lora.trigger_words; + trigger_words = trigger_words.map((word: string) => word.trim().toLowerCase()); + + trigger_words.forEach((element: string) => { + if (!prompt_words.includes(element)) { + missing_trigger_words.push(element); + } + }); + + // Add missing trigger words if they exists + missing_trigger_words.forEach((element: String) => { this.generationRequest.prompt += ', ' + element; }); this.sharedService.setPrompt(this.generationRequest.prompt);