[upvote]
import random
from dataclasses import dataclass, field
from typing import List, Dict
@dataclass
class PromptConfig:
theme: str # 必填:主题/主体
seed: int | None = None # 复现实验用
count: int = 5 # 生成多少条
aspect_ratio: str = "16:9" # 画幅
stylize: int = 250 # 风格强度(示意)
min_tokens: Dict[str, int] = field(default_factory=lambda: {
"style": 1, "artist": 1, "lighting": 1, "lens": 1, "detail": 1, "extras": 1
})
LIB = {
"style": [
"cinematic", "illustrative", "photorealistic", "low-poly", "isometric",
"watercolor", "oil painting", "flat design", "cyberpunk", "baroque"
],
"artist": [
"Hayao Miyazaki", "Studio Ghibli", "Greg Rutkowski", "Beeple",
"Makoto Shinkai", "Gustav Klimt", "H.R. Giger", "Magritte"
],
"lighting": [
"rim light", "softbox lighting", "global illumination", "volumetric light",
"golden hour", "neon glow", "moonlit", "backlit", "chiaroscuro"
],
"lens": [
"35mm lens", "85mm portrait", "ultra wide", "macro", "tilt-shift", "f1.4 bokeh"
],
"detail": [
"ultradetail", "8k texture", "highly detailed", "sharp focus",
"film grain", "subsurface scattering"
],
"extras": [
"trending on artstation", "depth of field", "rule of thirds",
"symmetry", "dramatic composition", "warm color grading", "cool color grading"
],
}
def weighted_sample(pool: List[str], k: int, weights: List[float] | None = None):
# 从列表中无放回抽样,支持权重
if k <= 0: return []
if weights is None:
return random.sample(pool, k=min(k, len(pool)))
items = list(zip(pool, weights))
chosen = []
for _ in range(min(k, len(pool))):
total = sum(w for _, w in items)
r = random.random() * total
acc = 0.0
for i, (item, w) in enumerate(items):
acc += w
if r <= acc:
chosen.append(item)
items.pop(i)
break
return chosen
def build_prompt(theme: str, take: Dict[str, int], seed: int | None,
ar: str, stylize: int) -> str:
parts = [theme]
for key, n in take.items():
parts += weighted_sample(LIB[key], n)
# 去重并保持顺序
seen, uniq = set(), []
for t in parts:
if t not in seen:
uniq.append(t); seen.add(t)
core = ", ".join(uniq)
tail = f"--ar {ar} --stylize {stylize}" + (f" --seed {seed}" if seed is not None else "")
return f"{core}, {tail}"
def generate(cfg: PromptConfig,
weights: Dict[str, List[float]] | None = None) -> List[str]:
if cfg.seed is not None:
random.seed(cfg.seed)
prompts = []
for _ in range(cfg.count):
take = {}
for key, minimum in cfg.min_tokens.items():
take[key] = minimum + random.randint(0, max(0, min(2, len(LIB[key]) - minimum)))
prompt = build_prompt(
theme=cfg.theme,
take=take,
seed=cfg.seed,
ar=cfg.aspect_ratio,
stylize=cfg.stylize
)
prompts.append(prompt)
return prompts
if __name__ == "__main__":
cfg = PromptConfig(
theme="a fox reading under a street lamp at midnight",
seed=1234, count=3, aspect_ratio="16:9", stylize=250
)
for p in generate(cfg):
print(p)
[/upvote]
9
25


快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。
25 条回复
排序