diff --git a/dsLightRag/Liblib/LibLibUtil.py b/dsLightRag/Liblib/LibLibUtil.py index fdbe6431..e7ee6b49 100644 --- a/dsLightRag/Liblib/LibLibUtil.py +++ b/dsLightRag/Liblib/LibLibUtil.py @@ -42,6 +42,8 @@ class LibLibUtil: if response_data.get('code') == 0: return response_data.get('data') else: + print(f"url={url}") + print(f"response_data={response_data}") print(f"API错误: {response_data.get('msg')}") # print(response_data) return None @@ -71,6 +73,12 @@ class LibLibUtil: return None def generate_text_to_image(self, template_uuid, generate_params): + # "templateUuid": self.default_params["templateUuid"], + # "generateParams": { + # "prompt": prompt, + # **self.default_params, + # "controlNet": self.build_control_net_params(reference_image_url, control_weight) + # } """调用text2img接口生成图片""" uri = "/api/generate/webui/text2img" payload = { @@ -190,6 +198,62 @@ class LibLibUtil: print(f"处理生成任务时发生异常: {str(e)}") return None + def generate_image_with_obs(self, template_uuid, generate_params): + """ + 完整生图流程:生成图片 → 等待完成 → 上传OBS → 返回URL + :param template_uuid: 模板UUID + :param generate_params: 生成参数字典 + :return: OBS文件URL或None + """ + try: + # 调用生成接口 + + response = self.generate_text_to_image(template_uuid, generate_params) + + if response and "generateUuid" in response: + generate_uuid = response["generateUuid"] + print("✅ 图像生成任务已成功提交!") + print(f"生成UUID: {generate_uuid}") + print("开始轮询生成状态...") + + # 等待生成完成 + status_data = self.wait_for_generation_completion(generate_uuid, interval=2) + + # 检查生成状态 + if status_data and status_data.get("generateStatus") == 5: + print("🎉 图像生成完成!开始处理文件...") + + # 提取图片URL + if status_data.get("images") and len(status_data["images"]) > 0: + image_url = status_data["images"][0]["imageUrl"] + + # 下载并上传到OBS + obs_object_key = self.download_and_upload_to_obs(image_url, generate_uuid) + + if obs_object_key: + # 构建完整的OBS URL + import Config.Config + file_url = f"https://{Config.Config.OBS_BUCKET}.{Config.Config.OBS_SERVER}/{obs_object_key}" + print(f"✅ 文件处理完成,OBS地址: {file_url}") + return file_url + else: + print("❌ 文件上传OBS失败") + return None + else: + print("❌ 未找到生成的图片数据") + return None + else: + error_msg = status_data.get('message', '未知错误') if status_data else '生成状态查询失败' + print(f"❌ 图像生成失败: {error_msg}") + return None + 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 + # 采样方法字典 - 包含28种采样方法及其枚举值和推荐度 SAMPLING_METHODS = { diff --git a/dsLightRag/Liblib/T2.py b/dsLightRag/Liblib/T2.py index 6f79ef39..26d043a4 100644 --- a/dsLightRag/Liblib/T2.py +++ b/dsLightRag/Liblib/T2.py @@ -1,18 +1,45 @@ -from Config.Config import OBS_BUCKET, OBS_SERVER -from LibLibUtil import LibLibUtil +import json +from LibLibUtil import LibLibUtil, SAMPLING_METHODS + +def test_generate_image_with_obs(): + """测试generate_image_with_obs方法""" + liblib = LibLibUtil() + print("===== 测试 generate_image_with_obs 方法 =====") + + # 加载配置文件 + with open('config.json', 'r', encoding='utf-8') as f: + config = json.load(f) + + # 使用第一个模型的配置 + first_model = config['models'][0] + + # 设置生图参数 + template_uuid = first_model["template_uuid"] + generate_params = { + "prompt": first_model["prompt"], + "negative_prompt": first_model["negative_prompt"], + "steps": first_model["steps"], + "width": first_model["width"], + "height": first_model["height"], + "cfg_scale": first_model["cfgScale"], + "sampler_name": first_model["sampler"], + "sampler_index": SAMPLING_METHODS[first_model["sampler"]], + "seed": first_model["seed"] + } + + print(f"使用模型: {first_model['name']}") + print(f"模板UUID: {template_uuid}") + + # 使用新的generate_image_with_obs方法 + file_url = liblib.generate_image_with_obs(template_uuid, generate_params) + + if file_url: + print(f"✅ 生图任务完成,OBS地址: {file_url}") + return file_url + else: + print("❌ 生图任务失败") + return None if __name__ == '__main__': - liblib = LibLibUtil() - print("===== 测试生成任务处理流程 =====") - test_generate_uuid = "df9bd6e03a204542b1daaba2f17c42e6" - - if test_generate_uuid: - # 调用封装好的完整处理方法 - obs_url = liblib.process_generation_task(test_generate_uuid, interval=2) - if obs_url: - file_url = f"https://{OBS_BUCKET}.{OBS_SERVER}/{obs_url}" - print(f"\n✅ 处理完成,OBS地址: {file_url}") - else: - print("\n❌ 任务处理失败") - else: - print("请先设置有效的生图任务UUID") + # 测试新的方法 + test_generate_image_with_obs() diff --git a/dsLightRag/Liblib/__pycache__/LibLibUtil.cpython-310.pyc b/dsLightRag/Liblib/__pycache__/LibLibUtil.cpython-310.pyc index 7f64bb9a..92560557 100644 Binary files a/dsLightRag/Liblib/__pycache__/LibLibUtil.cpython-310.pyc and b/dsLightRag/Liblib/__pycache__/LibLibUtil.cpython-310.pyc differ