178 lines
5.7 KiB
Python
178 lines
5.7 KiB
Python
"""User-facing helpers for cmhub hosted model aliases."""
|
||
|
||
from __future__ import annotations
|
||
|
||
|
||
TIER_DEFAULT = "default"
|
||
TIER_HIGH_QUALITY = "high_quality"
|
||
TIER_ECONOMICAL = "economical"
|
||
|
||
TIER_LABELS = {
|
||
TIER_DEFAULT: "默认档",
|
||
TIER_HIGH_QUALITY: "高质量档",
|
||
TIER_ECONOMICAL: "省点档",
|
||
}
|
||
|
||
TIER_DESCRIPTIONS = {
|
||
TIER_DEFAULT: "日常批量,质量、速度和成本平衡",
|
||
TIER_HIGH_QUALITY: "重点商品或失败重试,质量优先,成本可能更高",
|
||
TIER_ECONOMICAL: "低价值 SKU 或初稿,省点优先,上线前需人工复核",
|
||
}
|
||
|
||
|
||
def normalize_tier(value):
|
||
text = str(value or "").strip().lower().replace("-", "_").replace(" ", "_")
|
||
if text in {"high", "quality", "high_quality", "premium", "pro", "sol"}:
|
||
return TIER_HIGH_QUALITY
|
||
if text in {"low", "cheap", "economy", "economical", "saving", "lite", "luna"}:
|
||
return TIER_ECONOMICAL
|
||
if text in {"default", "standard", "balanced", "balance", "terra"}:
|
||
return TIER_DEFAULT
|
||
return ""
|
||
|
||
|
||
def model_tier(model):
|
||
model = model if isinstance(model, dict) else {}
|
||
for key in ("tier", "model_tier", "quality_tier", "cmhub_tier", "recommended_tier"):
|
||
tier = normalize_tier(model.get(key))
|
||
if tier:
|
||
return tier
|
||
haystack = " ".join(_model_text_values(model)).lower()
|
||
if any(token in haystack for token in ("省点", "低成本", "高频", "high-volume", "luna", "economy", "cheap", "lite")):
|
||
return TIER_ECONOMICAL
|
||
if any(token in haystack for token in ("高质量", "旗舰", "重点", "失败重试", "premium", "high-quality", "sol")):
|
||
return TIER_HIGH_QUALITY
|
||
if any(token in haystack for token in ("平衡", "默认", "日常", "standard", "balanced", "terra")):
|
||
return TIER_DEFAULT
|
||
alias = str(model.get("alias") or "").lower()
|
||
if alias.endswith("-hd") or alias.endswith("_hd") or alias.endswith(".hd") or alias == "hd":
|
||
return TIER_HIGH_QUALITY
|
||
return TIER_DEFAULT
|
||
|
||
|
||
def tier_label(tier):
|
||
return TIER_LABELS.get(normalize_tier(tier) or tier, TIER_LABELS[TIER_DEFAULT])
|
||
|
||
|
||
def tier_description(tier):
|
||
return TIER_DESCRIPTIONS.get(normalize_tier(tier) or tier, TIER_DESCRIPTIONS[TIER_DEFAULT])
|
||
|
||
|
||
def model_display_name(model):
|
||
model = model if isinstance(model, dict) else {}
|
||
for key in ("display_name", "name", "label"):
|
||
value = str(model.get(key) or "").strip()
|
||
if value:
|
||
return value
|
||
return str(model.get("alias") or "").strip()
|
||
|
||
|
||
def price_text(prices):
|
||
if not isinstance(prices, list):
|
||
return ""
|
||
parts = []
|
||
for item in prices[:3]:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
cost = item.get("points_cost")
|
||
if cost is None:
|
||
cost = item.get("cost")
|
||
if cost is None:
|
||
continue
|
||
resolution = item.get("resolution") or item.get("name") or ""
|
||
parts.append(f"{resolution}:{cost}点" if resolution else f"{cost}点")
|
||
return "/".join(parts)
|
||
|
||
|
||
def alias_label(model):
|
||
model = model if isinstance(model, dict) else {}
|
||
alias = str(model.get("alias") or "").strip()
|
||
display_name = model_display_name(model)
|
||
tier = model_tier(model)
|
||
parts = [tier_label(tier)]
|
||
if display_name and display_name != alias:
|
||
parts.append(display_name)
|
||
elif alias:
|
||
parts.append(alias)
|
||
prices = price_text(model.get("prices"))
|
||
if prices:
|
||
parts.append(prices)
|
||
if model.get("requires_image"):
|
||
parts.append("需参考图")
|
||
return " · ".join(parts)
|
||
|
||
|
||
def alias_tooltip(model):
|
||
model = model if isinstance(model, dict) else {}
|
||
alias = str(model.get("alias") or "").strip()
|
||
tier = model_tier(model)
|
||
usage = _recommended_for_text(model)
|
||
lines = [f"{tier_label(tier)}:{usage or tier_description(tier)}"]
|
||
if alias:
|
||
lines.append(f"cmhub 别名:{alias}")
|
||
prices = price_text(model.get("prices"))
|
||
if prices:
|
||
lines.append(f"扣点:{prices}")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def configured_alias_summary(alias, *, model=None):
|
||
alias = str(alias or "").strip()
|
||
if model is None:
|
||
model = {"alias": alias}
|
||
else:
|
||
model = dict(model)
|
||
model.setdefault("alias", alias)
|
||
tier = model_tier(model)
|
||
if alias:
|
||
return f"cmhub 托管{tier_label(tier)},生图别名 {alias},扣点以返回结果为准"
|
||
return "cmhub 托管默认档,未配置生图别名;请先到⑤设置选择生图别名"
|
||
|
||
|
||
def _recommended_for_text(model):
|
||
for key in ("recommended_for", "usage", "description"):
|
||
value = model.get(key)
|
||
if isinstance(value, (list, tuple)):
|
||
text = "、".join(str(item).strip() for item in value if str(item).strip())
|
||
else:
|
||
text = str(value or "").strip()
|
||
if text:
|
||
return text
|
||
return ""
|
||
|
||
|
||
def _model_text_values(model):
|
||
for key in (
|
||
"alias",
|
||
"display_name",
|
||
"name",
|
||
"label",
|
||
"tier",
|
||
"model_tier",
|
||
"quality_tier",
|
||
"recommended_tier",
|
||
"recommended_for",
|
||
"description",
|
||
):
|
||
value = model.get(key)
|
||
if isinstance(value, (list, tuple, set)):
|
||
for item in value:
|
||
text = str(item or "").strip()
|
||
if text:
|
||
yield text
|
||
elif isinstance(value, dict):
|
||
for item in value.values():
|
||
text = str(item or "").strip()
|
||
if text:
|
||
yield text
|
||
else:
|
||
text = str(value or "").strip()
|
||
if text:
|
||
yield text
|
||
tags = model.get("tags")
|
||
if isinstance(tags, (list, tuple, set)):
|
||
for tag in tags:
|
||
text = str(tag or "").strip()
|
||
if text:
|
||
yield text
|