157 lines
5.3 KiB
Python
157 lines
5.3 KiB
Python
"""
|
||
EPEEKit 集中配置。
|
||
所有可调参数从环境变量读取,每次调用实时读取(不缓存),确保 load_dotenv() 后生效。
|
||
"""
|
||
|
||
import os
|
||
from typing import Any
|
||
|
||
|
||
def get_llm_model() -> str:
|
||
return os.getenv("LLM_MODEL", "gpt-4o-mini")
|
||
|
||
|
||
def get_llm_max_iterations() -> int:
|
||
return int(os.getenv("LLM_MAX_ITERATIONS", "5"))
|
||
|
||
|
||
# ─── 图像模型注册表 ─────────────────────────────────────
|
||
#
|
||
# 每个模型的配置说明:
|
||
# id — 前端/API 使用的短 ID
|
||
# name — 显示名称
|
||
# provider — 生成服务提供者(对应 image_gen.py 中的 Provider)
|
||
# model_id — Replicate 上的完整模型 ID
|
||
# description — 前端下拉列表中的说明文字
|
||
# supports_ref_image — 是否原生支持参考图输入(IP-Adapter 等)
|
||
# ref_image_param — 传给 Replicate 的参考图参数名(模型间可能不同)
|
||
# num_images_param — 批量生成参数名(Flux 用 num_outputs,Kolors 用 number_of_images)
|
||
# default_params — 默认推理参数
|
||
|
||
IMAGE_MODELS: dict[str, dict[str, Any]] = {
|
||
"flux-schnell": {
|
||
"id": "flux-schnell",
|
||
"name": "Flux Schnell",
|
||
"provider": "replicate",
|
||
"model_id": "black-forest-labs/flux-schnell",
|
||
"description": "快速生成,适合快速迭代",
|
||
"supports_ref_image": False,
|
||
"num_images_param": "num_outputs",
|
||
"default_params": {
|
||
"aspect_ratio": "1:1",
|
||
"output_format": "png",
|
||
},
|
||
},
|
||
"flux-dev": {
|
||
"id": "flux-dev",
|
||
"name": "Flux Dev",
|
||
"provider": "replicate",
|
||
"model_id": "black-forest-labs/flux-dev",
|
||
"description": "高质量生成,细节更好",
|
||
"supports_ref_image": False,
|
||
"num_images_param": "num_outputs",
|
||
"default_params": {
|
||
"aspect_ratio": "1:1",
|
||
"output_format": "png",
|
||
},
|
||
},
|
||
"sdxl": {
|
||
"id": "sdxl",
|
||
"name": "Stable Diffusion XL",
|
||
"provider": "replicate",
|
||
"model_id": "stability-ai/sdxl",
|
||
"description": "经典 SDXL,支持 negative prompt",
|
||
"supports_ref_image": False,
|
||
"num_images_param": "num_outputs",
|
||
"default_params": {
|
||
"width": 1024,
|
||
"height": 1024,
|
||
"num_inference_steps": 50,
|
||
"guidance_scale": 7.5,
|
||
},
|
||
},
|
||
"instant-style": {
|
||
"id": "instant-style",
|
||
"name": "InstantStyle",
|
||
"provider": "replicate",
|
||
"model_id": "jyoung105/instant-style:c6f01e12f31cb99f9ee774a78992a71294f630a6f433d9aecfdc33b816fc4baa",
|
||
"description": "强风格迁移,画风还原度高(较慢)",
|
||
"supports_ref_image": True,
|
||
"ref_image_param": "style_image",
|
||
"num_images_param": "num_outputs",
|
||
"default_params": {
|
||
"width": 1024,
|
||
"height": 1024,
|
||
"num_inference_steps": 30,
|
||
"guidance_scale": 5,
|
||
"style_strength": 1.0,
|
||
"block_mode": "style-only",
|
||
"adapter_mode": "original",
|
||
},
|
||
},
|
||
"kolors-ipadapter": {
|
||
"id": "kolors-ipadapter",
|
||
"name": "Kolors IP-Adapter",
|
||
"provider": "replicate",
|
||
"model_id": "fofr/kolors-with-ipadapter:5a1a92b2c0f81813225d48ed8e411813da41aa84e7582fb705d1af46eea36eed",
|
||
"description": "风格参考生成,上传参考图效果最佳",
|
||
"supports_ref_image": True,
|
||
"ref_image_param": "image",
|
||
"num_images_param": "number_of_images",
|
||
"default_params": {
|
||
"width": 1024,
|
||
"height": 1024,
|
||
"steps": 25,
|
||
"cfg": 4,
|
||
"ip_adapter_weight": 0.8,
|
||
"ip_adapter_weight_type": "style transfer precise",
|
||
"output_format": "png",
|
||
},
|
||
},
|
||
}
|
||
|
||
|
||
def get_default_image_model_id() -> str:
|
||
"""返回 .env 中配置的默认模型短 ID,若不在注册表中则回退到 flux-schnell。"""
|
||
env_model = os.getenv("IMAGE_MODEL", "flux-schnell")
|
||
for mid, cfg in IMAGE_MODELS.items():
|
||
if cfg["model_id"] == env_model or mid == env_model:
|
||
return mid
|
||
return "flux-schnell"
|
||
|
||
|
||
def get_image_model_config(model_id: str | None = None) -> dict[str, Any]:
|
||
"""根据短 ID 获取模型配置,未指定或不存在则使用默认模型。"""
|
||
if model_id and model_id in IMAGE_MODELS:
|
||
return IMAGE_MODELS[model_id]
|
||
return IMAGE_MODELS[get_default_image_model_id()]
|
||
|
||
|
||
def get_ref_image_model_id() -> str | None:
|
||
"""返回有参考图时推荐使用的模型 ID(第一个 supports_ref_image=True 的模型)。"""
|
||
for mid, cfg in IMAGE_MODELS.items():
|
||
if cfg.get("supports_ref_image"):
|
||
return mid
|
||
return None
|
||
|
||
|
||
def get_image_models_list() -> list[dict]:
|
||
"""返回前端下拉列表所需的模型摘要信息。"""
|
||
return [
|
||
{
|
||
"id": cfg["id"],
|
||
"name": cfg["name"],
|
||
"description": cfg["description"],
|
||
"supports_ref_image": cfg.get("supports_ref_image", False),
|
||
}
|
||
for cfg in IMAGE_MODELS.values()
|
||
]
|
||
|
||
|
||
def get_image_aspect_ratio() -> str:
|
||
return os.getenv("IMAGE_ASPECT_RATIO", "1:1")
|
||
|
||
|
||
def get_image_output_format() -> str:
|
||
return os.getenv("IMAGE_OUTPUT_FORMAT", "png")
|