summaryrefslogtreecommitdiff
path: root/internal/image/prompt.go
blob: 7e69a8c80e1c2f8e04436ae7719c0bd07120f704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package image

import "strings"

const maxImagePromptChars = 1000

func promptSubject(englishTranslation, fallback string) string {
	subject := normalizePromptText(englishTranslation)
	if subject != "" {
		return subject
	}

	subject = normalizePromptText(fallback)
	if subject != "" {
		return subject
	}

	return "the requested term"
}

func normalizePromptText(text string) string {
	text = trimMarkdownFence(text)
	text = strings.TrimSpace(text)
	text = strings.Trim(text, "`\"'")
	text = strings.Join(strings.Fields(text), " ")
	return strings.TrimSpace(text)
}

func sanitizeSceneDescription(scene string) string {
	scene = normalizePromptText(scene)
	lower := strings.ToLower(scene)

	for _, prefix := range []string{"scene description:", "scene:", "description:", "image prompt:", "prompt:"} {
		if strings.HasPrefix(lower, prefix) {
			scene = strings.TrimSpace(scene[len(prefix):])
			break
		}
	}

	return strings.TrimSpace(strings.Trim(scene, "."))
}

func usableSceneDescription(scene string) bool {
	scene = sanitizeSceneDescription(scene)
	if scene == "" {
		return false
	}
	if len(scene) < 24 {
		return false
	}
	if len(strings.Fields(scene)) < 3 {
		return false
	}
	return true
}

func trimMarkdownFence(text string) string {
	text = strings.TrimSpace(text)
	if !strings.HasPrefix(text, "```") {
		return text
	}

	lines := strings.Split(text, "\n")
	if len(lines) < 3 {
		return text
	}
	if !strings.HasPrefix(strings.TrimSpace(lines[0]), "```") {
		return text
	}
	if strings.TrimSpace(lines[len(lines)-1]) != "```" {
		return text
	}

	return strings.Join(lines[1:len(lines)-1], "\n")
}

func withTerminalPunctuation(text string) string {
	text = strings.TrimSpace(text)
	if text == "" {
		return ""
	}

	switch {
	case strings.HasSuffix(text, "."):
		return text
	case strings.HasSuffix(text, "!"):
		return text
	case strings.HasSuffix(text, "?"):
		return text
	default:
		return text + "."
	}
}

func fallbackVisualDirection(subject string) string {
	subject = normalizePromptText(subject)
	lower := strings.ToLower(subject)

	if strings.HasPrefix(lower, "to ") || len(strings.Fields(subject)) > 1 {
		return "Show a realistic everyday scene with people, actions, facial expressions, and surrounding objects that make the meaning of \"" + subject + "\" obvious without any text."
	}

	return "Show a single " + subject + " as the clear focal point, prominently centered and immediately recognizable."
}