File size: 9,892 Bytes
59aed9d | 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | from dataclasses import dataclass, field
from PIL import Image
# -------------------- #
# -- System Configs -- #
# -------------------- #
@dataclass
class AgentConfig:
# -- Projects
output_dir: str = "agent_output"
# -- Loggings
log_dir: str = "agent_log"
# -- Inference
seed: int = 42
width: int = 1024
height: int = 1024
max_new_tokens: int = 1024
max_refine_times: int = 1
num_inference_steps: int = 25
# -- LoRAs
lora_box: str = "lora_box.json"
# ---------------------------- #
# -- Default System Prompts -- #
# ---------------------------- #
SYS_TASK_ROUTER = r"""Your task is to split the user prompt into ordered sub-tasks based on the Picture references mentioned (e.g., “Picture 1”, “Picture 2”).
Each sub-task must preserve the sequence implied in the user prompt.
For every sub-task, output an object in the form:
{"ref_id": <picture_number>, "ref_prompt": <the instruction related to that picture>}
Output a JSON list of these objects in the same order as the instructions appear in the user prompt.
Example:
User prompt: “Using the same style of Picture 1 firstly, then apply the colors and textures of Picture 2. Finally add some local design patterns from Picture 1.”
Output:
[
{"ref_id": 1, "ref_prompt": "The same style."},
{"ref_id": 2, "ref_prompt": "Colors and textures."},
{"ref_id": 1, "ref_prompt": "Local design patterns."}
]"""
SYS_STYLE_ROUTER = r"""You are an image style analysis expert.
Your job is to analyze a given style image from two perspectives: style_type and style_value.
1. style_type
"semantic": the artistic style category of the image (e.g., Van Gogh, LEGO, etc.).
"pixel": the color/texture distribution of the image, focusing on colors rather than high-level artistic semantics.
2. style_value
If style_type = "semantic", then style_value can be one of (if no suitable style_value, assign to null):
{style_value_and_descriptions}
If style_type = "pixel", then style_value must be a float in [0, 1], inferred from the user prompt:
0-0.3 → slight, subtle, minimal…
0.3-0.7 → moderate, balanced, typical…
0.7-1.0 → drastic, heavy, intense…
3. Output format
Always output valid JSON, for example:
{{"style_type": "semantic", "style_value": "Van_Gogh"}}
{{"style_type": "pixel", "style_value": 0.7}}
You will be given Picture 1 and a user prompt."""
SYS_ANALYSIS_SEMANTIC = r"""You are an expert image-style analyst.
Describe the style characteristics of the given style image (Picture 1) using clear, accurate, and concrete stylistic terms.
Requirements
Output only a JSON object.
Escape inner double quotes with backslashes (\\") to ensure valid JSON.
Do not mention any content, subjects, people, objects, or specific logos. Focus solely on stylistic attributes such as color use, texture, line quality, rendering methods, mood, and artistic conventions.
The entire output must be under 250 tokens.
Output Format
{
"description": "..."
}
Examples
Example 1:
{
"description": "This style features bold outlines, exaggerated expressions, vibrant flat colors, and dynamic compositional energy rooted in classic American cartoon aesthetics."
}
Example 2:
{
"description": "This style features hand-drawn painterly rendering, soft watercolor textures, lush atmospheric backgrounds, and a warm whimsical tone typical of Ghibli-inspired animation."
}"""
SYS_ANALYSIS_PIXEL = r"""You are an expert in pixel-level image style analysis.
Describe the style characteristics of the given style image (Picture 2), focusing only on low-level visual attributes.
Requirements
Emphasize color distribution, texture patterns, brightness, contrast, saturation, sharpness, gradients, and other pixel-level properties.
Do not describe any subjects, objects, scenes, or logos.
Output only a JSON object.
Escape inner double quotes with backslashes (\\").
Keep the entire output under 250 tokens.
Output Format
{
"description": "Colors: ... ,\nTextures: ... ,\nBrightness: ... "
}"""
SYS_TRANSFER = r"""Describe the key features of the style features of Picture 2 (if given) (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the Picture 1. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate."""
SYS_CRITERIA_CS = r"""You are an expert in evaluating content-level consistency between images.
Given a reference image (Picture 1) and a stylized target image (Picture 2), your tasks are:
1. **Ignore all style, artistic rendering, colors, and textures. Focus only on semantic content, object presence, and spatial layout.**
2. Detect and compare the objects in both images.
3. Evaluate whether the object lists and their relative positions are consistent.
4. Provide a consistency score in the range [0, 10].
5. Provide clear instructions on how Picture 2 can be modified to better match Picture 1 at the semantic and layout level. If no instructions, output empty string.
Output a valid JSON object:
{"score": <float>, "suggestion": "<instruction>"}"""
SYS_CRITERIA_RS = r"""You are an expert in evaluating style consistency between images.
Given a reference style image (Picture 1) and a target image (Picture 2), your tasks are:
Assess how closely the style of Picture 2 matches Picture 1 and provide a score in the range [0, 10].
Evaluate whether the stylization strength of Picture 2 is appropriate.
If Picture 2 needs stronger stylization to match Picture 1, output "increase".
If it is overly stylized, output "decrease".
If the stylization strength is appropriate, output "unchange".
Output a valid JSON object, for example:
{"score": 9.0, "suggestion": "unchange"}
{"score": 4.5, "suggestion": "increase"}
{"score": 3.0, "suggestion": "decrease"}"""
SYS_CRITERIA_DS = r"""You are an expert in evaluating instruction–image consistency.
Given an image editing instruction and an edited image (Picture 1), determine how well the image fulfills the instruction.
Your tasks are:
1. Provide a consistency score in the range [0, 10], where higher means Picture 1 matches the instruction more closely.
2. Provide a revised instruction in imperative form that would make Picture 1 better align with the intended edit.
Output a valid JSON object:
{"score": <float>, "suggestion": "<imperative instruction>"}
Example:
{"score": 8.0, "suggestion": "Make the tree taller than the man. Use light blue colors for the leaves."}"""
# ----------------------------------- #
# -- Inputs and Outputs structures -- #
# ----------------------------------- #
@dataclass
class UserInput:
"""
Used for Agent conversation user interfaces
"""
prompt: str = ""
cnt_image_path: str = ""
ref_image_paths: str | list[str] = ""
ref_dict: dict[str, str] = field(default_factory=lambda: {})
def __post_init__(self):
ref_dict = {}
if isinstance(self.ref_image_paths, str):
self.ref_image_paths = [self.ref_image_paths]
for ref_id, ref_image_path in enumerate(self.ref_image_paths):
ref_dict[f"Picture {ref_id + 1}"] = ref_image_path
self.ref_dict = ref_dict
@dataclass
class SubTask:
"""
Each sub task responds to a style reference image
"""
ref_id: int = 1
ref_image_path: str = ""
style_type: str = ""
style_value: str | float = ""
@dataclass
class AgentInput:
cnt_image_path: str = ""
sub_tasks: list[SubTask] = field(default_factory=lambda: [])
@dataclass
class AnalysisInput:
"""
Analysis for sub task
"""
ref_id: int = 1
ref_image_or_path: str = ""
style_type: str = ""
style_value: str | float = ""
suggestion: str = ""
sys_prompt: str = "Analysis the style of the given image."
@dataclass
class AnalysisOutput:
semantic: dict[str, str] = field(default_factory=lambda: {})
pixel: dict[str, str] = field(default_factory=lambda: {})
@dataclass
class TransferInput:
prompt: str = ""
cnt_image_or_path: str | Image.Image = ""
ref_image_or_path: str | Image.Image = ""
prompt_prefix: str = "Transfer the style of Picture 1 into the style of Picture 2."
sys_prompt: str = "Make the style of Picture into another style."
@dataclass
class TransferOutput:
instruct: str = ""
cnt_image_or_path: str | Image.Image = ""
ref_image_or_path: str | Image.Image = ""
sty_image: Image.Image = None
@dataclass
class CriteriaInput:
instruction: str = ""
cnt_image_or_path: str | Image.Image = ""
ref_image_or_path: str | Image.Image = ""
sty_image_or_path: str | Image.Image = ""
sys_prompts: dict[str, str] = field(
default_factory=lambda: {
"cs": "Score the semantic alignment degree between the two images.",
"rs": "Score the style features between the two images.",
"ds": "Score the instruction following ability of the image.",
}
)
@dataclass
class CriteriaOutput:
cs_score: dict[str, str] = field(default_factory=lambda: {})
rs_score: dict[str, str] = field(default_factory=lambda: {})
ds_score: dict[str, str] = field(default_factory=lambda: {})
@dataclass
class SubTaskOutput:
cnt_image_path: str = ""
ref_image_path: str = ""
analysis_output: AnalysisOutput = None
transfer_output: TransferOutput = None
criteria_output: CriteriaOutput = None
@dataclass
class AgentOutput:
sty_image_path: str = ""
sty_image: Image.Image = None
sub_tasks: list[SubTask] = field(default_factory=lambda: [])
analysis_outputs: list[AnalysisOutput] = field(default_factory=lambda: [])
transfer_outputs: list[TransferOutput] = field(default_factory=lambda: [])
criteria_outputs: list[CriteriaOutput] = field(default_factory=lambda: [])
|