From e670494adfe00a1fa55c7e8ffcbf0172ff662cce Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 20 Apr 2026 09:25:00 +0300 Subject: Fix task 25 comic generation leak handling --- internal/comic/image_validation.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'internal/comic/image_validation.go') diff --git a/internal/comic/image_validation.go b/internal/comic/image_validation.go index 475cb96..8148c15 100644 --- a/internal/comic/image_validation.go +++ b/internal/comic/image_validation.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "os/exec" + "regexp" "strings" ) @@ -31,9 +32,16 @@ var imageLeakMarkers = []string{ "no text of any kind", } +var englishLeakWords = []string{ + "page", "comic", "photograph", "photography", "panel", "panels", + "cover", "title", "subtitle", "blurb", "story", "gallery", + "rendering", "language", "mandatory", "speech", "bubble", "caption", + "close-up", "medium-long", "ultra-realistic", "illustration", "text", +} + var validateImagePromptLeakageFn = validateImagePromptLeakage -func validateImagePromptLeakage(ctx context.Context, outputFile, label string) error { +func validateImagePromptLeakage(ctx context.Context, outputFile, label, script string) error { if ctx == nil { ctx = context.Background() } @@ -53,7 +61,12 @@ func validateImagePromptLeakage(ctx context.Context, outputFile, label string) e ocr := strings.ToLower(out.String()) if marker, ok := findImageLeakMarker(ocr); ok { - return fmt.Errorf("%s contains prompt leakage marker %q", label, marker) + return fmt.Errorf("%s contains prompt leakage marker %q (ocr=%q)", label, marker, compactOCRSnippet(ocr)) + } + if strings.EqualFold(script, "Cyrillic") { + if word, ok := findEnglishLeakWord(ocr); ok { + return fmt.Errorf("%s contains English leakage word %q despite %s script (ocr=%q)", label, word, script, compactOCRSnippet(ocr)) + } } return nil } @@ -66,3 +79,22 @@ func findImageLeakMarker(text string) (string, bool) { } return "", false } + +func findEnglishLeakWord(text string) (string, bool) { + for _, word := range englishLeakWords { + pattern := regexp.MustCompile(`\b` + regexp.QuoteMeta(word) + `\b`) + if pattern.MatchString(text) { + return word, true + } + } + return "", false +} + +func compactOCRSnippet(text string) string { + text = strings.TrimSpace(text) + text = strings.Join(strings.Fields(text), " ") + if len(text) > 240 { + text = text[:240] + "..." + } + return text +} -- cgit v1.2.3