From 283fa671531255176c832c7be132648b05e6d6b2 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Wed, 3 Sep 2025 15:43:10 +0800 Subject: [PATCH] 'commit' --- dsLightRag/Liblib/ControlNetGenerator.py | 76 +++++++++++++++++++ dsLightRag/Liblib/LiblibUtil.py | 5 +- dsLightRag/Liblib/{T1_VersionGet.py => T1.py} | 0 dsLightRag/Liblib/T2.py | 5 +- dsLightRag/Liblib/T3.py | 70 ++++------------- 5 files changed, 94 insertions(+), 62 deletions(-) create mode 100644 dsLightRag/Liblib/ControlNetGenerator.py rename dsLightRag/Liblib/{T1_VersionGet.py => T1.py} (100%) diff --git a/dsLightRag/Liblib/ControlNetGenerator.py b/dsLightRag/Liblib/ControlNetGenerator.py new file mode 100644 index 00000000..fa4d9315 --- /dev/null +++ b/dsLightRag/Liblib/ControlNetGenerator.py @@ -0,0 +1,76 @@ +import json +from LiblibUtil import LiblibUtil + +class ControlNetImageGenerator: + def __init__(self): + self.liblib_util = LiblibUtil() + # PuLID人像换脸参数示例 + self.template_uuid = "6f7c4652458d4802969f8d089cf5b91f" + self.default_params = { + "steps": 20, + "width": 768, + "height": 1024, + "imgCount": 1 + } + + def set_template_uuid(self, template_uuid): + """设置模板UUID""" + self.template_uuid = template_uuid + return self + + def set_default_params(self, **kwargs): + """设置默认生成参数""" + self.default_params.update(kwargs) + return self + + def build_control_net_params(self, reference_image_url, control_weight=1.0): + """构建控制网络参数""" + return [{ + "unitOrder": 0, + "sourceImage": reference_image_url, + "width": self.default_params["width"], + "height": self.default_params["height"], + "preprocessor": 0, + "annotationParameters": {"none": {}}, + "model": "405836d1ae2646b4ba2716ed6bd5453a", + "controlWeight": control_weight, + "startingControlStep": 0, + "endingControlStep": 1, + "pixelPerfect": 1, + "controlMode": 0, + "resizeMode": 1 + }] + + def generate_image(self, prompt, reference_image_url, control_weight=1.0): + """根据提示词和参考图片生成图像""" + # 构建生成参数 + generate_params = { + "templateUuid": self.template_uuid, + "generateParams": { + "prompt": prompt, + **self.default_params, + "controlNet": self.build_control_net_params(reference_image_url, control_weight) + } + } + + # 调用生成接口 + try: + response = self.liblib_util.post_request( + "/api/generate/webui/text2img", + generate_params + ) + + print(f"API响应: {json.dumps(response, ensure_ascii=False, indent=2)}") + + if response and "generateUuid" in response: + print("✅ 图像生成任务已成功提交!") + print(f"生成UUID: {response['generateUuid']}") + print("提示: 可使用T2.py中的get_generation_status方法查询生成进度") + return response + else: + error_msg = response.get('message', '未知错误') if response else 'API无响应' + print(f"❌ 图像生成失败: {error_msg}") + return None + except Exception as e: + print(f"请求发生异常: {str(e)}") + return None \ No newline at end of file diff --git a/dsLightRag/Liblib/LiblibUtil.py b/dsLightRag/Liblib/LiblibUtil.py index 764efcc7..34c5aea1 100644 --- a/dsLightRag/Liblib/LiblibUtil.py +++ b/dsLightRag/Liblib/LiblibUtil.py @@ -1,10 +1,13 @@ import hmac +import os +import tempfile from hashlib import sha1 import base64 import time import uuid import requests import Config.Config +from Util.ObsUtil import ObsUploader class LiblibUtil: @@ -79,7 +82,7 @@ class LiblibUtil: payload = {"generateUuid": generate_uuid} return self.post_request(uri, payload) - def wait_for_generation_completion(self, generate_uuid, interval=5): + def wait_for_generation_completion(self, generate_uuid, interval=5, max_wait_time=100): """等待生成完成并返回最终状态""" while True: status_data = self.get_generation_status(generate_uuid) diff --git a/dsLightRag/Liblib/T1_VersionGet.py b/dsLightRag/Liblib/T1.py similarity index 100% rename from dsLightRag/Liblib/T1_VersionGet.py rename to dsLightRag/Liblib/T1.py diff --git a/dsLightRag/Liblib/T2.py b/dsLightRag/Liblib/T2.py index 655307d9..9803ed41 100644 --- a/dsLightRag/Liblib/T2.py +++ b/dsLightRag/Liblib/T2.py @@ -1,9 +1,6 @@ import json -import requests -import os -import tempfile + from LiblibUtil import LiblibUtil -from Util.ObsUtil import ObsUploader # 导入OBS上传工具 if __name__ == '__main__': liblib = LiblibUtil() diff --git a/dsLightRag/Liblib/T3.py b/dsLightRag/Liblib/T3.py index e36580ad..f7ba6bda 100644 --- a/dsLightRag/Liblib/T3.py +++ b/dsLightRag/Liblib/T3.py @@ -1,61 +1,17 @@ import json from LiblibUtil import LiblibUtil - -def generate_image_with_controlnet(): - # 初始化Liblib工具类 - liblib_util = LiblibUtil() - - # 构建生成参数 - generate_params = { - "templateUuid": "6f7c4652458d4802969f8d089cf5b91f", - "generateParams": { - "prompt": "filmfotos, Asian portrait,A young woman wearing a green baseball cap,covering one eye with her hand", - "steps": 20, - "width": 768, - "height": 1024, - "imgCount": 1, - "controlNet": [ - { - "unitOrder": 0, - "sourceImage": "https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/HuangWanQiao.jpg", - "width": 768, - "height": 1024, - "preprocessor": 0, - "annotationParameters": {"none": {}}, - "model": "405836d1ae2646b4ba2716ed6bd5453a", - "controlWeight": 1, - "startingControlStep": 0, - "endingControlStep": 1, - "pixelPerfect": 1, - "controlMode": 0, - "resizeMode": 1 - } - ] - } - } - - # 调用生成接口 - try: - response = liblib_util.post_request( - endpoint="/api/generate/webui/text2img", - params=generate_params - ) - - print(f"API响应: {json.dumps(response, ensure_ascii=False, indent=2)}") - - # 修复成功条件判断 - 检查是否返回generateUuid - if response and "generateUuid" in response: - print("✅ 图像生成任务已成功提交!") - print(f"生成UUID: {response['generateUuid']}") - print("提示: 可使用T2.py中的get_generation_status方法查询生成进度") - return response - else: - error_msg = response.get('message', '未知错误') if response else 'API无响应' - print(f"❌ 图像生成失败: {error_msg}") - return None - except Exception as e: - print(f"请求发生异常: {str(e)}") - return None +from ControlNetGenerator import ControlNetImageGenerator if __name__ == "__main__": - generate_image_with_controlnet() \ No newline at end of file + # 创建生成器实例 + generator = ControlNetImageGenerator() + + # 可选:修改默认参数 + generator.set_default_params(steps=25, width=800, height=1000) + + # 生成图像 + generator.generate_image( + prompt="filmfotos, Asian portrait,A young woman wearing a green baseball cap,covering one eye with her hand", + reference_image_url="https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/HuangWanQiao.jpg", + control_weight=0.8 + ) \ No newline at end of file