File size: 2,548 Bytes
ad52e57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dee1bf0
 
 
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
# filename: prompt_builder.py
import os
from collections import defaultdict

class PromptBuilder:
    def __init__(self, options_dir="prompt_options"):
        self.options_dir = options_dir
        self.options = self._load_options()

    def _load_options(self):
        """
        Dynamically load all .txt files in the options directory into a dictionary.
        Group fields by prefix (e.g., 'character.gender' or 'background.sky').
        """
        options = defaultdict(dict)
        if not os.path.exists(self.options_dir):
            return options

        for filename in os.listdir(self.options_dir):
            if filename.endswith(".txt"):
                path = os.path.join(self.options_dir, filename)
                key = filename.replace(".txt", "")
                if '.' in key:
                    group, field = key.split('.', 1)
                    with open(path, "r", encoding="utf-8") as f:
                        options[group][field] = [line.strip() for line in f if line.strip()]
        return options

    def get_choices(self, group, field):
        """Retrieve available choices for a given group/field."""
        return self.options.get(group, {}).get(field, [])

    def build_prompt(self, base_prompt="", custom_tags=None, **field_values):
        """
        Build prompt using:
        - base_prompt: user base
        - custom_tags: freeform additional prompt tags
        - field_values: key-value pairs like gender="female", lighting="soft", etc.
        """
        parts = [base_prompt]

        for key, value in field_values.items():
            if not value:
                continue
            if key in ("styles", "lighting", "mood"):
                # general tags with descriptors
                if key == "styles":
                    parts.append(f"in {value} style")
                elif key == "lighting":
                    parts.append(f"with {value} lighting")
                elif key == "mood":
                    parts.append(f"evoking a {value} mood")
            else:
                # other tags are descriptive or noun-like
                parts.append(value)

        if custom_tags:
            parts.append(custom_tags)

        return ", ".join(filter(None, parts))


builder = PromptBuilder()
builder.get_choices("character", "gender")  # → ['male', 'female', 'nonbinary']
prompt = builder.build_prompt(
    base_prompt="a portrait",
    gender="female", body="slim", clothing="gothic dress",
    land_type="desert", sky="stormy", styles="anime"
)

print(prompt)