'commit'
This commit is contained in:
30
dsLightRag/JiMeng/Async_SubmitTask.py
Normal file
30
dsLightRag/JiMeng/Async_SubmitTask.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from __future__ import print_function
|
||||||
|
from JiMeng.Kit.JmCommon import JmCommon
|
||||||
|
from volcengine.visual.VisualService import VisualService
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
visual_service = VisualService()
|
||||||
|
|
||||||
|
visual_service.set_ak(JmCommon.ak)
|
||||||
|
visual_service.set_sk(JmCommon.sk)
|
||||||
|
|
||||||
|
# 请求Body(查看接口文档请求参数-请求示例,将请求参数内容复制到此)
|
||||||
|
form = {
|
||||||
|
"req_key": "jimeng_high_aes_general_v21_L",
|
||||||
|
# "prompt": "少女,光影,瘦,白皙,干净,美丽",
|
||||||
|
"prompt": "雨天街头,一位头戴黄色安全帽、身穿荧光黄安全背心与棕色外套、搭配蓝色牛仔裤和棕色工装靴的工人,背着一个小女孩。工人脖子上侧挂一个小熊玩偶包,拖住小女孩臀部的手同时挂着一袋小熊饼干。小女孩双手搂着工人的脖子。小女孩穿红色雨衣,戴红色雨衣帽。两个人露出微笑。背景是居民楼和树木,地面潮湿,氛围温馨。",
|
||||||
|
"width": 1024,
|
||||||
|
"height": 1024,
|
||||||
|
"seed": -1,
|
||||||
|
"ddim_steps": 20,
|
||||||
|
"scale": 7.0,
|
||||||
|
"return_url": True,
|
||||||
|
"logo_info": {
|
||||||
|
"add_logo": True,
|
||||||
|
"position": 2,
|
||||||
|
"language": 0,
|
||||||
|
"opacity": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resp = visual_service.cv_submit_task(form)
|
||||||
|
print(resp)
|
@@ -1,16 +0,0 @@
|
|||||||
# uv pip install volcengine
|
|
||||||
from volcengine.visual.VisualService import VisualService
|
|
||||||
|
|
||||||
from JiMeng.JmTxt2Img import JmTxt2Img
|
|
||||||
from JiMeng.Kit.JmCommon import JmCommon
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
visual_service = VisualService()
|
|
||||||
|
|
||||||
visual_service.set_ak(JmCommon.ak)
|
|
||||||
visual_service.set_sk(JmCommon.sk)
|
|
||||||
|
|
||||||
prompt = "鸿门宴,室内场景图..."
|
|
||||||
req = {"req_key": JmTxt2Img.req_key, "prompt": prompt}
|
|
||||||
resp = visual_service.cv_submit_task(req)
|
|
||||||
print(resp)
|
|
30
dsLightRag/JiMeng/Async_text2img_xl_sft.py
Normal file
30
dsLightRag/JiMeng/Async_text2img_xl_sft.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# uv pip install volcengine
|
||||||
|
from volcengine.visual.VisualService import VisualService
|
||||||
|
from JiMeng.Kit.JmCommon import JmCommon
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
visual_service = VisualService()
|
||||||
|
|
||||||
|
visual_service.set_ak(JmCommon.ak)
|
||||||
|
visual_service.set_sk(JmCommon.sk)
|
||||||
|
|
||||||
|
# https://www.volcengine.com/docs/6791/1330203
|
||||||
|
form = {
|
||||||
|
"req_key": "t2i_xl_sft",
|
||||||
|
# "prompt": "少女,光影,瘦,白皙,干净,美丽",
|
||||||
|
"prompt": "雨天街头,一位头戴黄色安全帽、身穿荧光黄安全背心与棕色外套、搭配蓝色牛仔裤和棕色工装靴的工人,背着一个小女孩。工人脖子上侧挂一个小熊玩偶包,拖住小女孩臀部的手同时挂着一袋小熊饼干。小女孩双手搂着工人的脖子。小女孩穿红色雨衣,戴红色雨衣帽。两个人露出微笑。背景是居民楼和树木,地面潮湿,氛围温馨。",
|
||||||
|
"width": 1024,
|
||||||
|
"height": 1024,
|
||||||
|
"seed": -1,
|
||||||
|
"ddim_steps": 20,
|
||||||
|
"scale": 7.0,
|
||||||
|
"return_url": True,
|
||||||
|
"logo_info": {
|
||||||
|
"add_logo": True,
|
||||||
|
"position": 2,
|
||||||
|
"language": 0,
|
||||||
|
"opacity": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resp = visual_service.text2img_xl_sft(form)
|
||||||
|
print(resp)
|
BIN
dsLightRag/JiMeng/Example/消防员与小女孩【错误案例】.jpg
Normal file
BIN
dsLightRag/JiMeng/Example/消防员与小女孩【错误案例】.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 457 KiB |
@@ -52,101 +52,16 @@ class JmTxt2Img:
|
|||||||
print(f"保存图片失败: {str(e)}")
|
print(f"保存图片失败: {str(e)}")
|
||||||
raise Exception(f"保存图片失败: {str(e)}")
|
raise Exception(f"保存图片失败: {str(e)}")
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
# 新增一个方法用于查询任务状态
|
|
||||||
@staticmethod
|
|
||||||
def query_task_status(task_id):
|
|
||||||
"""查询任务状态"""
|
|
||||||
req = {"task_id": task_id}
|
|
||||||
response_body = JmCommon.do_request("POST", {}, json.dumps(req).encode('utf-8'), "CVProcessStatus")
|
|
||||||
jo = json.loads(response_body)
|
|
||||||
|
|
||||||
code = jo.get("code")
|
|
||||||
if not JmErrorCode.is_success(code):
|
|
||||||
error_msg = JmErrorCode.get_message_by_code(code)
|
|
||||||
print(f"查询任务状态失败: 错误码={code}, 错误信息={error_msg}")
|
|
||||||
raise Exception(f"查询任务状态失败: {error_msg}")
|
|
||||||
|
|
||||||
return jo.get("data", {})
|
|
||||||
|
|
||||||
# 修改generate_image方法,使其支持异步模式
|
|
||||||
@staticmethod
|
|
||||||
def generate_image_async(prompt):
|
|
||||||
"""异步生成图片,返回任务ID"""
|
|
||||||
req = {"req_key": JmTxt2Img.req_key, "prompt": prompt}
|
|
||||||
response_body = JmCommon.do_request("POST", {}, json.dumps(req).encode('utf-8'), JmTxt2Img.action)
|
|
||||||
jo = json.loads(response_body)
|
|
||||||
|
|
||||||
code = jo.get("code")
|
|
||||||
if not JmErrorCode.is_success(code):
|
|
||||||
error_msg = JmErrorCode.get_message_by_code(code)
|
|
||||||
print(f"提交生成图片任务失败: 错误码={code}, 错误信息={error_msg}")
|
|
||||||
raise Exception(f"提交生成图片任务失败: {error_msg}")
|
|
||||||
|
|
||||||
return jo.get("data", {}).get("task_id")
|
|
||||||
|
|
||||||
# 修改main方法,实现异步轮询
|
|
||||||
@staticmethod
|
|
||||||
def main():
|
|
||||||
prompt = "鸿门宴,室内场景图..." # 详细提示词略
|
|
||||||
save_image_path = os.path.abspath("../../../../../Text2Img.jpg")
|
|
||||||
print(f"保存图片路径:{save_image_path}")
|
|
||||||
|
|
||||||
# 1. 提交生成任务
|
|
||||||
task_id = JmTxt2Img.generate_image_async(prompt)
|
|
||||||
print(f"图片生成任务已提交,任务ID: {task_id}")
|
|
||||||
|
|
||||||
# 2. 轮询任务状态
|
|
||||||
max_polling_times = 60 # 最多轮询60次
|
|
||||||
polling_interval = 5000 # 每5秒查询一次
|
|
||||||
polling_count = 0
|
|
||||||
|
|
||||||
while polling_count < max_polling_times:
|
|
||||||
try:
|
|
||||||
polling_count += 1
|
|
||||||
print(f"第{polling_count}次查询任务状态...")
|
|
||||||
task_status = JmTxt2Img.query_task_status(task_id)
|
|
||||||
|
|
||||||
# 检查任务是否完成
|
|
||||||
if task_status.get("status") == "completed":
|
|
||||||
print("图片生成完成!")
|
|
||||||
# 获取并保存图片
|
|
||||||
img_base64 = task_status.get("binary_data_base64", [""])[0]
|
|
||||||
os.makedirs(os.path.dirname(save_image_path), exist_ok=True)
|
|
||||||
|
|
||||||
if img_base64.startswith('data:image'):
|
|
||||||
img_base64 = img_base64.split(',')[1]
|
|
||||||
bytes_data = base64.b64decode(img_base64)
|
|
||||||
with open(save_image_path, 'wb') as f:
|
|
||||||
f.write(bytes_data)
|
|
||||||
print(f"文件保存成功!文件位置: {save_image_path}")
|
|
||||||
return
|
|
||||||
elif task_status.get("status") == "failed":
|
|
||||||
print(f"图片生成失败: {task_status.get('error_msg', '未知错误')}")
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
print(f"任务进行中,当前状态: {task_status.get('status')}")
|
|
||||||
time.sleep(polling_interval / 1000)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"查询任务状态异常: {str(e)}")
|
|
||||||
time.sleep(polling_interval / 1000)
|
|
||||||
|
|
||||||
print(f"已达到最大轮询次数: {max_polling_times}")
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def main():
|
def main():
|
||||||
"""主函数,用于测试"""
|
"""主函数,用于测试"""
|
||||||
# 示例提示词
|
# 示例提示词
|
||||||
prompt = "鸿门宴,室内场景图。非真人,是一幅画。远景镜头,让人一看就知道是在讲鸿门宴,"
|
prompt = "雨天街头,一位头戴黄色安全帽、身穿荧光黄安全背心与棕色外套、搭配蓝色牛仔裤和棕色工装靴的中国工人,背着一个中国小女孩。工人脖子上侧挂一个小熊玩偶包,拖住小女孩臀部的手同时挂着一袋小熊饼干。小女孩双手搂着工人的脖子。小女孩穿红色雨衣,戴红色雨衣帽。两个人露出微笑。背景是居民楼和树木,地面潮湿,氛围温馨。"
|
||||||
prompt += "1、要亮!"
|
|
||||||
prompt += "2、不要有人出现"
|
|
||||||
prompt += "3、有大厅、墙面,桌子,不要有台阶,否则项庄没机会刺杀刘邦"
|
|
||||||
prompt += "4、西周时期特点"
|
|
||||||
prompt += "5、宏大,雄伟,颜色鲜明,不要灰色调的,不要有雾霾之类的"
|
|
||||||
prompt += "6、超高清画质"
|
|
||||||
|
|
||||||
# 保存图片路径 - 使用绝对路径避免相对路径问题
|
# 保存图片路径 - 使用绝对路径避免相对路径问题
|
||||||
save_image_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "..", "..", "Text2Img.jpg"))
|
save_image_path = r"D:\Text2Img.jpg"
|
||||||
print(f"保存图片路径:{save_image_path}")
|
print(f"保存图片路径:{save_image_path}")
|
||||||
|
|
||||||
# 添加重试逻辑,处理API并发限制错误
|
# 添加重试逻辑,处理API并发限制错误
|
||||||
|
@@ -100,8 +100,8 @@ class JmCommon:
|
|||||||
|
|
||||||
# 发送请求
|
# 发送请求
|
||||||
try:
|
try:
|
||||||
print(f"请求URL: {url}")
|
#print(f"请求URL: {url}")
|
||||||
print(f"请求头: {headers}")
|
#print(f"请求头: {headers}")
|
||||||
response = requests.request(
|
response = requests.request(
|
||||||
method=method,
|
method=method,
|
||||||
url=url,
|
url=url,
|
||||||
@@ -109,8 +109,8 @@ class JmCommon:
|
|||||||
data=body,
|
data=body,
|
||||||
timeout=(30, 30) # 连接超时和读取超时
|
timeout=(30, 30) # 连接超时和读取超时
|
||||||
)
|
)
|
||||||
print(f"响应状态码: {response.status_code}")
|
#print(f"响应状态码: {response.status_code}")
|
||||||
print(f"响应内容: {response.text}")
|
#print(f"响应内容: {response.text}")
|
||||||
response.raise_for_status() # 如果状态码不是200,抛出异常
|
response.raise_for_status() # 如果状态码不是200,抛出异常
|
||||||
return response.text
|
return response.text
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
|
@@ -1,2 +1,5 @@
|
|||||||
即梦AI视频教程:(二)结合AI写提示词
|
即梦AI视频教程:(二)结合AI写提示词
|
||||||
https://baijiahao.baidu.com/s?id=1832704271457492977&wfr=spider&for=pc
|
https://baijiahao.baidu.com/s?id=1832704271457492977&wfr=spider&for=pc
|
||||||
|
|
||||||
|
# 官方例子
|
||||||
|
https://github.com/volcengine/volc-sdk-python/blob/main/volcengine/example/visual/cv_submit_task.py
|
Reference in New Issue
Block a user