修改 Stable Diffusion 使 api 接口增加模型参数

参考:https://zhuanlan.zhihu.com/p/644545784

1、修改 modules/api/models.py 中的 StableDiffusionTxt2ImgProcessingAPI 增加模型名称

python 复制代码
StableDiffusionTxt2ImgProcessingAPI = PydanticModelGenerator(
    "StableDiffusionProcessingTxt2Img",
    StableDiffusionProcessingTxt2Img,
    [
        {"key": "sampler_index", "type": str, "default": "Euler"},
        {"key": "script_name", "type": str, "default": None},
        {"key": "script_args", "type": list, "default": []},
        {"key": "send_images", "type": bool, "default": True},
        {"key": "save_images", "type": bool, "default": False},
        {"key": "alwayson_scripts", "type": dict, "default": {}},
        {"key": "model_name", "type": str, "default": None},
    ]
).generate_model()

2、修改 modules/processing.py 中的 StableDiffusionProcessingTxt2Img,增加模型名称接收

python 复制代码
@dataclass(repr=False)
class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):
    enable_hr: bool = False
    denoising_strength: float = 0.75
    firstphase_width: int = 0
    firstphase_height: int = 0
    hr_scale: float = 2.0
    hr_upscaler: str = None
    hr_second_pass_steps: int = 0
    hr_resize_x: int = 0
    hr_resize_y: int = 0
    hr_checkpoint_name: str = None
    hr_sampler_name: str = None
    hr_prompt: str = ''
    hr_negative_prompt: str = ''
    model_name: str = None

3.修改 modules/api/api.py 中 text2imgapi 代码:

python 复制代码
......

from modules import sd_samplers, deepbooru, sd_hijack, images, scripts, ui, \
    postprocessing, errors, restart, shared_items, sd_models
from modules.api import models
from modules.shared import opts, models_path

......

def text2imgapi(self, txt2imgreq: models.StableDiffusionTxt2ImgProcessingAPI):
        ......
        model_name = txt2imgreq.model_name
        if model_name is None:
            raise HTTPException(status_code=404, detail="model_name not found")
        ......
        with self.queue_lock:
            checkpoint_info = sd_models.CheckpointInfo(os.path.join(models_path, 'Stable-diffusion', model_name))
            sd_models.reload_model_weights(info=checkpoint_info)
            with closing(StableDiffusionProcessingTxt2Img(sd_model=shared.sd_model, **args)) as p:
            ......
相关推荐
合作小小程序员小小店1 小时前
桌面预测类开发,桌面%性别,姓名预测%系统开发,基于python,scikit-learn机器学习算法(sklearn)实现,分类算法,CSV无数据库
python·算法·机器学习·scikit-learn·sklearn
Q26433650231 小时前
【有源码】基于Hadoop+Spark的豆瓣电影数据分析与可视化系统-基于大数据的电影评分趋势分析与可视化系统
大数据·hadoop·python·数据分析·spark·毕业设计·课程设计
天特肿瘤电场研究所2 小时前
靠谱的肿瘤电场疗法公司
人工智能·python
闲人编程2 小时前
2025年,如何选择Python Web框架:Django, Flask还是FastAPI?
前端·后端·python·django·flask·fastapi·web
python开发笔记3 小时前
python(77) python脚本与jenkins pipeline交互的5种方式
python·jenkins·交互
vx_dmxq2113 小时前
免费领源码-Spring boot的物流管理系统 |可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·大数据·python·jupyter·课程设计
飞翔的佩奇3 小时前
【完整源码+数据集+部署教程】鸡只与养殖场环境物品图像分割: yolov8-seg等50+全套改进创新点发刊_一键训练教程_Web前端展示
python·yolo·计算机视觉·数据集·yolov8·yolo11·鸡只与养殖场环境物品图像分割
dreams_dream3 小时前
Django 数据库迁移命令
数据库·python·django
两只程序猿4 小时前
数据可视化 | 热力图Heatmap绘制Python代码 相关性矩阵学术可视化
python·信息可视化·矩阵
倔强青铜三4 小时前
苦练Python第58天:filecmp模块——文件和目录“找不同”的利器
人工智能·python·面试