Files
dsProject/dsLightRag/Test/TestQWen3ImageEdit.py
2025-08-27 11:50:34 +08:00

77 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"
def test_edit_image(prompt):
try:
"""测试编辑图片接口"""
# 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"
logger.info(f"调用编辑图片接口: {api_url}")
logger.info(f"请求参数: prompt={prompt[:50]}..., image_url={test_image_url[:50]}...")
# 构造请求数据
payload = {
"prompt": prompt,
"image_url": test_image_url # 正确传递图片URL参数
}
# 记录开始时间
success_count=0
# 发送请求
# 发送请求到正确的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
logger.info(f"编辑图片成功: {json.dumps(result, ensure_ascii=False, indent=2)}")
else:
logger.error(f"编辑图片失败: {json.dumps(result, ensure_ascii=False)}")
# 检查返回的数据
if result.get("code") == 200 and "data" in result:
images = result["data"].get("images", [])
logger.info(f"成功编辑{success_count}张图片")
return True, result
except Exception as e:
logger.exception(f"编辑图片时发生异常: {str(e)}")
return False, None
def main():
"""主函数,运行单元测试"""
logger.info("===== 开始测试QWenImage编辑接口 ====")
# 2. 测试编辑图片接口 - 基本测试
logger.info("\n2. 测试编辑图片接口 - 基本测试")
basic_prompt = "把“霓裳汉服社”改成“通义实验室”"
test_edit_image(
prompt=basic_prompt
)
if __name__ == "__main__":
main()