Files
dsProject/dsLightRag/Test/TestQWen3ImageEdit.py

77 lines
2.5 KiB
Python
Raw Normal View History

2025-08-27 11:35:01 +08:00
import requests
import json
import logging
import time
import os
import base64
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("TestQWenImageEdit")
# API基础URL
base_url = "http://localhost:8200/api/qwenImage"
2025-08-27 11:50:34 +08:00
def test_edit_image(prompt):
2025-08-27 11:35:01 +08:00
try:
2025-08-27 11:50:34 +08:00
"""测试编辑图片接口"""
# 1. 定义测试图片URL
test_image_url = "https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/7219965571/p1000025.png"
# 2. 定义API端点URL修复核心问题
api_url = f"{base_url}/edit"
2025-08-27 11:35:01 +08:00
2025-08-27 11:50:34 +08:00
logger.info(f"调用编辑图片接口: {api_url}")
logger.info(f"请求参数: prompt={prompt[:50]}..., image_url={test_image_url[:50]}...")
2025-08-27 11:35:01 +08:00
2025-08-27 11:50:34 +08:00
# 构造请求数据
payload = {
2025-08-27 11:35:01 +08:00
"prompt": prompt,
2025-08-27 11:50:34 +08:00
"image_url": test_image_url # 正确传递图片URL参数
2025-08-27 11:35:01 +08:00
}
# 记录开始时间
2025-08-27 11:50:34 +08:00
success_count=0
2025-08-27 11:35:01 +08:00
# 发送请求
2025-08-27 11:50:34 +08:00
# 发送请求到正确的API端点
headers = {"Content-Type": "application/json"}
response = requests.post(api_url, headers=headers, data=json.dumps(payload, ensure_ascii=False))
# 解析响应结果
result = response.json()
if result.get("code") == 200:
# 修复计数逻辑 - 从响应数据判断成功状态
success_count += 1
2025-08-27 11:35:01 +08:00
logger.info(f"编辑图片成功: {json.dumps(result, ensure_ascii=False, indent=2)}")
2025-08-27 11:50:34 +08:00
else:
logger.error(f"编辑图片失败: {json.dumps(result, ensure_ascii=False)}")
2025-08-27 11:35:01 +08:00
# 检查返回的数据
if result.get("code") == 200 and "data" in result:
images = result["data"].get("images", [])
2025-08-27 11:50:34 +08:00
logger.info(f"成功编辑{success_count}张图片")
2025-08-27 11:35:01 +08:00
return True, result
2025-08-27 11:50:34 +08:00
2025-08-27 11:35:01 +08:00
except Exception as e:
logger.exception(f"编辑图片时发生异常: {str(e)}")
return False, None
def main():
"""主函数,运行单元测试"""
logger.info("===== 开始测试QWenImage编辑接口 ====")
# 2. 测试编辑图片接口 - 基本测试
logger.info("\n2. 测试编辑图片接口 - 基本测试")
2025-08-27 11:50:34 +08:00
basic_prompt = "把“霓裳汉服社”改成“通义实验室”"
test_edit_image(
prompt=basic_prompt
2025-08-27 11:35:01 +08:00
)
if __name__ == "__main__":
main()