修改 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:
            ......
相关推荐
kobe_OKOK_4 小时前
__getattr__和__getattribute__如何用
python
全栈弄潮儿5 小时前
Python实战第2期: 变量与数据类型
后端·python·agent
长沙三为智能科技5 小时前
家政小程序门店运营模块设计:会员、下单、派单、结算四段链路的落地拆解
python·小程序·内容运营
Cc.Y5 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
启观川5 小时前
数据结构与算法 -第 3 章 常用算法-动态规划
数据结构·笔记·python·算法
码爸5 小时前
排序算法介绍
python·算法·排序算法
笔墨登场说说5 小时前
flink bin/start-cluster.sh 帮我做成开机启动
开发语言·python
MoRanzhi12036 小时前
第 3 篇 · 数据清洗与特征工程:从问题判定到特征构建
python·pandas·数据清洗·特征工程·缺失值处理·异常值检测·特征编码
刘科领7 小时前
使用ollama & openweb-ui 本地搭建自己的AI
人工智能·python·ui·ai
专业程序开发源7 小时前
flask家电故障预测系统92491-计算机课程设计/毕业设计
vscode·python·sql·算法·flask·课程设计