'commit'
This commit is contained in:
@@ -79,33 +79,76 @@ class LiblibUtil:
|
||||
payload = {"generateUuid": generate_uuid}
|
||||
return self.post_request(uri, payload)
|
||||
|
||||
def wait_for_generation_completion(self, generate_uuid, check_interval=5, max_wait_time=300):
|
||||
"""等待生图任务完成并获取最终结果"""
|
||||
start_time = time.time()
|
||||
|
||||
while time.time() - start_time < max_wait_time:
|
||||
def wait_for_generation_completion(self, generate_uuid, interval=5):
|
||||
"""等待生成完成并返回最终状态"""
|
||||
while True:
|
||||
status_data = self.get_generation_status(generate_uuid)
|
||||
|
||||
if status_data:
|
||||
status = status_data.get('status')
|
||||
progress = status_data.get('percentCompleted', 0) * 100
|
||||
print(f"生成进度:{progress:.2f}%")
|
||||
|
||||
if status == 'completed':
|
||||
print("生图任务已完成")
|
||||
images = status_data.get('images', [])
|
||||
if images:
|
||||
print("生成的最终图片:")
|
||||
for img in images:
|
||||
print(f"- {img.get('imageUrl')}")
|
||||
return status_data
|
||||
elif status == 'failed':
|
||||
print(f"生图任务失败: {status_data.get('errorMessage', '未知错误')}")
|
||||
return None
|
||||
else:
|
||||
print(f"生图任务处理中,当前状态: {status}")
|
||||
|
||||
time.sleep(check_interval)
|
||||
if not status_data:
|
||||
print("获取状态失败,重试中...")
|
||||
time.sleep(interval)
|
||||
continue
|
||||
|
||||
# 获取当前状态和进度
|
||||
generate_status = status_data.get('generateStatus')
|
||||
percent = status_data.get('percentCompleted', 0) * 100
|
||||
status_text = {
|
||||
0: "初始化",
|
||||
1: "处理中",
|
||||
2: "排队中",
|
||||
3: "已取消",
|
||||
4: "失败",
|
||||
5: "完成"
|
||||
}.get(generate_status, f"未知状态({generate_status})")
|
||||
|
||||
# 显示进度和状态
|
||||
print(f"生成进度:{percent:.2f}% | 状态:{status_text}")
|
||||
|
||||
# 检查是否完成
|
||||
if generate_status in [3, 4, 5]: # 取消、失败或完成
|
||||
return status_data
|
||||
|
||||
time.sleep(interval)
|
||||
|
||||
print(f"生图任务超时({max_wait_time}秒)")
|
||||
return None
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def download_and_upload_to_obs(image_url, generate_uuid):
|
||||
"""下载图片并上传到OBS"""
|
||||
try:
|
||||
# 1. 清理URL(去除可能的引号和空格)
|
||||
clean_url = image_url.strip('` ')
|
||||
print(f"清理后的图片URL: {clean_url}")
|
||||
|
||||
# 2. 下载图片到临时文件
|
||||
response = requests.get(clean_url, stream=True)
|
||||
response.raise_for_status() # 检查HTTP错误
|
||||
|
||||
# 创建临时文件
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
temp_file.write(chunk)
|
||||
temp_file_path = temp_file.name
|
||||
print(f"图片已下载至临时文件: {temp_file_path}")
|
||||
|
||||
# 3. 上传到OBS
|
||||
obs_uploader = ObsUploader()
|
||||
obs_object_key = f"HuangHai/Liblib/{generate_uuid}.jpg"
|
||||
success, result = obs_uploader.upload_file(
|
||||
object_key=obs_object_key,
|
||||
file_path=temp_file_path
|
||||
)
|
||||
|
||||
# 4. 清理临时文件
|
||||
os.unlink(temp_file_path)
|
||||
|
||||
if success:
|
||||
print(f"✅ 图片成功上传至OBS")
|
||||
return obs_object_key
|
||||
else:
|
||||
print(f"❌ OBS上传失败: {result}")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
print(f"处理图片时发生错误: {str(e)}")
|
||||
return None
|
||||
|
@@ -1,20 +1,33 @@
|
||||
from .LiblibUtil import LiblibUtil
|
||||
|
||||
import json
|
||||
import requests
|
||||
import os
|
||||
import tempfile
|
||||
from LiblibUtil import LiblibUtil
|
||||
from Util.ObsUtil import ObsUploader # 导入OBS上传工具
|
||||
|
||||
if __name__ == '__main__':
|
||||
liblib = LiblibUtil()
|
||||
print("===== 测试查询生图结果 =====")
|
||||
test_generate_uuid = "061d402be5af492d9949d53719912737"
|
||||
|
||||
print("===== 测试查询生图结果并上传OBS =====")
|
||||
test_generate_uuid = "df9bd6e03a204542b1daaba2f17c42e6"
|
||||
|
||||
if test_generate_uuid:
|
||||
# 直接查询状态
|
||||
status_data = liblib.get_generation_status(test_generate_uuid)
|
||||
if status_data:
|
||||
print(f"生图任务状态:{status_data}")
|
||||
# 等待任务完成
|
||||
status_data = liblib.wait_for_generation_completion(test_generate_uuid)
|
||||
print(f"生图状态: {json.dumps(status_data, ensure_ascii=False, indent=2)}")
|
||||
|
||||
# 检查生成状态是否为5(完成)
|
||||
if status_data and status_data.get('generateStatus') == 5:
|
||||
# 提取图片URL
|
||||
if status_data.get('images') and len(status_data['images']) > 0:
|
||||
image_url = status_data['images'][0]['imageUrl']
|
||||
# 下载并上传到OBS
|
||||
# 使用实例调用而非类调用
|
||||
obs_path = liblib.download_and_upload_to_obs(image_url, test_generate_uuid)
|
||||
if obs_path:
|
||||
print(f"\n最终OBS路径: {obs_path}")
|
||||
else:
|
||||
print("❌ 未找到图片数据")
|
||||
else:
|
||||
print("查询生图任务状态失败")
|
||||
|
||||
# 等待任务完成(如需测试请取消注释)
|
||||
# liblib.wait_for_generation_completion(test_generate_uuid)
|
||||
print(f"❌ 生图未完成或失败,状态码: {status_data.get('generateStatus')}")
|
||||
else:
|
||||
print("请先设置有效的生图任务UUID")
|
61
dsLightRag/Liblib/T3.py
Normal file
61
dsLightRag/Liblib/T3.py
Normal file
@@ -0,0 +1,61 @@
|
||||
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
|
||||
|
||||
if __name__ == "__main__":
|
||||
generate_image_with_controlnet()
|
BIN
dsLightRag/Liblib/__pycache__/LiblibUtil.cpython-310.pyc
Normal file
BIN
dsLightRag/Liblib/__pycache__/LiblibUtil.cpython-310.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user