修改 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:
            ......
相关推荐
weixin_307779135 分钟前
Python获取能唯一确定一棵给定的树的最少数量的拓扑序列
数据结构·python
A.sir啊25 分钟前
爬虫基础(六)代理简述
爬虫·python·网络协议
weixin_3077791328 分钟前
PySPARK带多组参数和标签的SparkSQL批量数据导出到S3的程序
大数据·数据仓库·python·sql·spark
Hi Man2 小时前
Python之如何在Visual Studio Code 中写的python程序打包成可以在Windows系统下运行的.exe程序
开发语言·vscode·python
Return-Log2 小时前
Matplotlab显示OpenCV读取到的图像
python·opencv
程序趣谈2 小时前
算法随笔_36: 复写零
数据结构·python·算法
九亿AI算法优化工作室&2 小时前
GWO优化LSBooST回归预测matlab
人工智能·python·算法·机器学习·matlab·数据挖掘·回归
weixin_307779133 小时前
在AWS上使用Flume搜集分布在不同EC2实例上的应用程序日志具体流程和代码
python·flask·云计算·flume·aws
sirius123451234 小时前
自定义数据集 ,使用朴素贝叶斯对其进行分类
python·分类·numpy
shanks664 小时前
【PyQt】学习PyQt进行GUI开发从基础到进阶逐步掌握详细路线图和关键知识点
python·pyqt