This commit is contained in:
2025-08-20 10:35:52 +08:00
parent 3be54a9dfb
commit afa29b93c6
2 changed files with 8 additions and 46 deletions

View File

@@ -1,4 +1,3 @@
import os
import json import json
import requests import requests
@@ -37,25 +36,18 @@ payload = {
# 发送POST请求 # 发送POST请求
try: try:
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=30) response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=60)
# 检查响应状态码 # 检查响应状态码
response.raise_for_status() response.raise_for_status()
# 解析响应数据 # 解析响应数据
result = response.json() result = response.json()
print('请求成功:')
# 提取choices[0].message.content的内容 # 提取choices[0].message.content的内容
if 'choices' in result and len(result['choices']) > 0: if 'choices' in result and len(result['choices']) > 0:
choice = result['choices'][0] choice = result['choices'][0]
if 'message' in choice and 'content' in choice['message']: if 'message' in choice and 'content' in choice['message']:
content = choice['message']['content'] content = choice['message']['content']
print('提取的内容:')
print(content) print(content)
# 这里可以添加对content的进一步处理逻辑
# 例如保存到文件、解析表格内容等
else: else:
print('响应中未找到message或content字段') print('响应中未找到message或content字段')
else: else:

View File

@@ -40,7 +40,7 @@ class JmImg2Video(JmCommon):
def main(): def main():
try: try:
# 玩法参考: https://www.volcengine.com/docs/85621/1544774 # 参考: https://www.volcengine.com/docs/85621/1544774
image_urls = [ image_urls = [
"https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/Text2Img.jpg" "https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/Text2Img.jpg"
] ]
@@ -55,41 +55,11 @@ def main():
""" """
mp4_file_name = "image2video.mp4" mp4_file_name = "image2video.mp4"
submit_result = JmImg2Video.submit_image_to_video_task(image_urls, prompt)
# 添加重试逻辑处理API并发限制错误 logger.info(f"提交结果: {submit_result}")
submit_result = None code = submit_result.get("code")
submit_retry_count = 0 if not JmErrorCode.is_success(code):
max_submit_retries = 1000 # 最大重试次数 logger.error(f"提交任务失败: 错误码={code}, 错误信息={JmErrorCode.get_message_by_code(code)}")
submit_retry_interval = 3000 # 重试间隔(毫秒)
while submit_retry_count < max_submit_retries:
try:
submit_result = JmImg2Video.submit_image_to_video_task(image_urls, prompt)
logger.info(f"提交结果: {submit_result}")
code = submit_result.get("code")
if code == JmErrorCode.API_CONCURRENT_LIMIT[0]:
logger.warning(f"API并发限制等待{submit_retry_interval}毫秒后重试...")
time.sleep(submit_retry_interval / 1000)
submit_retry_count += 1
continue
elif not JmErrorCode.is_success(code):
logger.error(f"提交任务失败: 错误码={code}, 错误信息={JmErrorCode.get_message_by_code(code)}")
return
# 成功获取结果,跳出循环
break
except Exception as e:
logger.error(f"提交任务异常: {str(e)}", exc_info=True)
submit_retry_count += 1
if submit_retry_count < max_submit_retries:
logger.warning(f"等待{submit_retry_interval}毫秒后重试...")
time.sleep(submit_retry_interval / 1000)
else:
raise # 达到最大重试次数,抛出异常
if submit_retry_count >= max_submit_retries:
logger.error(f"提交任务失败,已达到最大重试次数: {max_submit_retries}")
return return
# 获取任务ID # 获取任务ID