This commit is contained in:
2025-09-04 11:27:59 +08:00
parent 016a7a0b5f
commit b917ab6bf8
4 changed files with 204 additions and 105 deletions

View File

@@ -150,4 +150,155 @@ class LibLibGenerator:
except Exception as e:
print(f"处理图片时发生错误: {str(e)}")
return None
def generate_default_text_to_image(self, prompt, steps=20, width=768, height=1024,
img_count=1, seed=-1, restore_faces=0,
additional_network=None):
"""
使用默认官方模型进行文生图
- Checkpoint默认为官方模型
- 可用模型范围基础算法F.1
- 支持additional network
"""
generate_params = {
"templateUuid": "6f7c4652458d4802969f8d089cf5b91f", # 参数模板ID F.1文生图
"generateParams": {
"prompt": prompt,
"steps": steps,
"width": width,
"height": height,
"imgCount": img_count,
"seed": seed,
"restoreFaces": restore_faces
}
}
# 添加additional network参数如果有
if additional_network:
generate_params["generateParams"]["additionalNetwork"] = additional_network
# 调用生成接口
response = self.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:
generate_uuid = response["generateUuid"]
print("✅ 图像生成任务已成功提交!")
print(f"生成UUID: {generate_uuid}")
print("开始轮询生成状态...")
# 每2秒探测一次生成状态直到完成
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_url = self.download_and_upload_to_obs(image_url, generate_uuid)
if obs_url:
print(f"✅ 文件处理完成OBS地址: {obs_url}")
file_url = f"https://{Config.Config.OBS_BUCKET}.{Config.Config.OBS_SERVER}/{obs_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
def generate_custom_checkpoint_text_to_image(self, template_uuid, checkpoint_id,
prompt="", negative_prompt="", steps=20,
sampler=15, cfg_scale=7, width=768,
height=1024, img_count=1, randn_source=0,
seed=-1, restore_faces=0, hi_res_fix_info=None):
"""
使用自定义Checkpoint模型进行文生图
"""
generate_params = {
"templateUuid": template_uuid,
"generateParams": {
"checkPointId": checkpoint_id,
"prompt": prompt,
"negativePrompt": negative_prompt,
"sampler": sampler,
"steps": steps,
"cfgScale": cfg_scale,
"width": width,
"height": height,
"imgCount": img_count,
"randnSource": randn_source,
"seed": seed,
"restoreFaces": restore_faces
}
}
# 添加高分辨率修复参数(如果有)
if hi_res_fix_info:
generate_params["generateParams"]["hiResFixInfo"] = hi_res_fix_info
# 调用生成接口
response = self.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:
generate_uuid = response["generateUuid"]
print("✅ 图像生成任务已成功提交!")
print(f"生成UUID: {generate_uuid}")
print("开始轮询生成状态...")
# 每2秒探测一次生成状态直到完成
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_url = self.download_and_upload_to_obs(image_url, generate_uuid)
if obs_url:
print(f"✅ 文件处理完成OBS地址: {obs_url}")
file_url = f"https://{Config.Config.OBS_BUCKET}.{Config.Config.OBS_SERVER}/{obs_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