Spaces:
Runtime error
Runtime error
Delete app_backup.py
Browse files- app_backup.py +0 -71
app_backup.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
# filename: prompt_builder.py
|
| 2 |
-
import os
|
| 3 |
-
from collections import defaultdict
|
| 4 |
-
|
| 5 |
-
class PromptBuilder:
|
| 6 |
-
def __init__(self, options_dir="prompt_options"):
|
| 7 |
-
self.options_dir = options_dir
|
| 8 |
-
self.options = self._load_options()
|
| 9 |
-
|
| 10 |
-
def _load_options(self):
|
| 11 |
-
"""
|
| 12 |
-
Dynamically load all .txt files in the options directory into a dictionary.
|
| 13 |
-
Group fields by prefix (e.g., 'character.gender' or 'background.sky').
|
| 14 |
-
"""
|
| 15 |
-
options = defaultdict(dict)
|
| 16 |
-
if not os.path.exists(self.options_dir):
|
| 17 |
-
return options
|
| 18 |
-
|
| 19 |
-
for filename in os.listdir(self.options_dir):
|
| 20 |
-
if filename.endswith(".txt"):
|
| 21 |
-
path = os.path.join(self.options_dir, filename)
|
| 22 |
-
key = filename.replace(".txt", "")
|
| 23 |
-
if '.' in key:
|
| 24 |
-
group, field = key.split('.', 1)
|
| 25 |
-
with open(path, "r", encoding="utf-8") as f:
|
| 26 |
-
options[group][field] = [line.strip() for line in f if line.strip()]
|
| 27 |
-
return options
|
| 28 |
-
|
| 29 |
-
def get_choices(self, group, field):
|
| 30 |
-
"""Retrieve available choices for a given group/field."""
|
| 31 |
-
return self.options.get(group, {}).get(field, [])
|
| 32 |
-
|
| 33 |
-
def build_prompt(self, base_prompt="", custom_tags=None, **field_values):
|
| 34 |
-
"""
|
| 35 |
-
Build prompt using:
|
| 36 |
-
- base_prompt: user base
|
| 37 |
-
- custom_tags: freeform additional prompt tags
|
| 38 |
-
- field_values: key-value pairs like gender="female", lighting="soft", etc.
|
| 39 |
-
"""
|
| 40 |
-
parts = [base_prompt]
|
| 41 |
-
|
| 42 |
-
for key, value in field_values.items():
|
| 43 |
-
if not value:
|
| 44 |
-
continue
|
| 45 |
-
if key in ("styles", "lighting", "mood"):
|
| 46 |
-
# general tags with descriptors
|
| 47 |
-
if key == "styles":
|
| 48 |
-
parts.append(f"in {value} style")
|
| 49 |
-
elif key == "lighting":
|
| 50 |
-
parts.append(f"with {value} lighting")
|
| 51 |
-
elif key == "mood":
|
| 52 |
-
parts.append(f"evoking a {value} mood")
|
| 53 |
-
else:
|
| 54 |
-
# other tags are descriptive or noun-like
|
| 55 |
-
parts.append(value)
|
| 56 |
-
|
| 57 |
-
if custom_tags:
|
| 58 |
-
parts.append(custom_tags)
|
| 59 |
-
|
| 60 |
-
return ", ".join(filter(None, parts))
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
builder = PromptBuilder()
|
| 64 |
-
builder.get_choices("character", "gender") # → ['male', 'female', 'nonbinary']
|
| 65 |
-
prompt = builder.build_prompt(
|
| 66 |
-
base_prompt="a portrait",
|
| 67 |
-
gender="female", body="slim", clothing="gothic dress",
|
| 68 |
-
land_type="desert", sky="stormy", styles="anime"
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
print(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|